C Programming Tutorial

 
 
 
 
#include <conio.h>
#include<dos.h>
#include <graphics.h>
#include<stdio.h>
#include<stdlib.h>
#define NO 0
#define YES 1
#define NONE -1
#define MAX_MENU 7//change this according to maximum no. of items in the menu

int X,Y;
int Selection;
int button,x,y;
void *p;
size_t area;
struct MENU
{
      int x;
      int y;
      int x1;
      int y1;
      unsigned char Active[MAX_MENU];// Item Activation flag
      char *head;// name of menu ( can be eliminated)
};

int MouseOn(struct MENU *t);
void Process();
void Xorbar(int sx,int sy,int ex,int ey,int c);
void Show();
void Hide();
void Status();

// To change the position of button just change these values here,no need
// to touch rest of the code.
struct MENU File={200,110,250,130};
struct MENU Edit={295,110,345,130};
struct MENU Help={390,110,440,130};
struct MENU Close={600,10,620,30};
struct MENU Synt={380,300,460,315};

void main()
{
 int gd = DETECT,gm;
 initgraph(&gd, &gm,\"\");
 Selection=NONE;
 for(gd=0;gd<MAX_MENU;gd++)
 {
    File.Active[gd]=YES;
    Edit.Active[gd]=YES;
    Help.Active[gd]=YES;
    Close.Active[gd]=YES;
 }
 File.head=\"[ File Menu ]\";
 Edit.head=\"[ Edit Menu ]\";
 Help.head=\"[ Help Menu ]\";
 Close.head=\"[ Exit ]\";
 area=imagesize(0,0,150,150);
 p=malloc(area);
 if(p==NULL)
 {
    closegraph();
    restorecrtmode();
    printf(\"Not Enough Memory !\\n try to reduce the area used.\\n\");
    exit(1);
 }
 setfillstyle(1,7);
 bar(0,0,640,480);
 X=2;Y=2;
 Process();
 End();
 closegraph();
 printf(\"GUI-MENUS\\n\");
 getch();
}

void Process()
{
    char *fm[]={\"New\",\"Open\",\"Save\",\"Save as\",\"Exit\"};
    char *em[]={\"Undo\",\"Cut\",\"Copy\",\"Paste\",\"Delete\",\"Select All\"};
    char *hm[]={\"Help Topics\",\"About Menus\"};
    // if you chage no. of items here then you need to chage that in
    // function PullMenuDown(char *,int);
    char KEY;
    // draw buttons
    Win(140,100,500,140);
    DrawBtn(&File,\"File\");
    DrawBtn(&Edit,\"Edit\");
    DrawBtn(&Help,\"Help\");
    settextstyle(0,0,0);
    outtextxy(Close.x+7,Close.y+7,\"x\");
    Comments();
    Show();
    // you can set active & disable item in the menu as shown below,
    // here \'save as\' is set to be disable.
    File.Active[3]=NO;// Save as
    Edit.Active[0]=NO;// undo
    while(1)
    {
        if(kbhit())
        {
            KEY=getch();
            if(KEY==27|| KEY==45) break;
        }
        Status();
        if(button==1)
        {
          if(MouseOn(&Close)) break;
          if(MouseOn(&File))
          {
              PullMenuDown(fm,5,&File);// no. of items = 5
              if(Selection==NONE) Msg(\"No Selection !\");
              else Msg(fm[Selection]);
              if(Selection==4) break;
          }
          if(MouseOn(&Edit))
          {
              PullMenuDown(em,6,&Edit);// no. of items = 6
              if(Selection==NONE) Msg(\"No Selection !\");
              else Msg(em[Selection]);
          }
          if(MouseOn(&Help))
          {
              PullMenuDown(hm,2,&Help);// no. of items = 2
              if(Selection==NONE) Msg(\"No Selection !\");
              else Msg(hm[Selection]);
              if(Selection==1) Msg(\"* Syntax-Example *\");
          }
        }
        else
        {
            if(MouseOn(&File)) Hlight(&File);
            if(MouseOn(&Edit)) Hlight(&Edit);
            if(MouseOn(&Help)) Hlight(&Help);
            if(MouseOn(&Close)) Hlight(&Close);
            if(MouseOn(&Syntax-Empl))
            {
                Msg(\"http://www.syntax-example.com\");
                while(MouseOn(&Syntax-Empl))
                {
                    Status();
                    if(button!=0) break;
                    if(kbhit()) break;
                 }
                 Msg(\"\");
            }
        }
    }
    Msg(\":: Bye Bye ::\");
}

DrawBtn(struct MENU *tm,char *txt)
{
    setcolor(0);
    settextstyle(2,0,4);
    outtextxy(tm->x+15,tm->y+4,txt);
}

Hlight(struct MENU *t)
{
 winp(t,0);
 Msg(t->head);
 while(MouseOn(t))
 {
    Status();
    if(button!=0) break;
    if(kbhit()) break;
 }
 setcolor(7);
 Hide();
 rectangle(t->x,t->y,t->x1,t->y1);
 Msg(\"\");
 Show();
}

int MouseOn(struct MENU *t)
{
 int flag;
 flag=0;
 if(x>=t->x && x<=t->x1 && y>=t->y && y<=t->y1) flag=1;
 return flag;
}

void Xorbar(int sx,int sy,int ex,int ey,int c)
{
 int i;
 setwritemode(1);
 setcolor(c);
 Hide();
 for(i=sy;i<=ey;i++)
 line(sx,i,ex,i);
 Show();
 setwritemode(0);
}

Push(struct MENU *tm)
{
    winp(tm,1);
    while(button==1)
    {
        Status();
        if(kbhit()) break;
    }
}

Up(struct MENU *tm)
{
    setcolor(7);
    Hide();
    rectangle(tm->x,tm->y,tm->x1,tm->y1);
    Show();
    while(button==1)
    {
        Status();
        if(kbhit()) break;
    }
}

PullMenuDown(char *H[],int n,struct MENU *tm)
{
    int i;
    i=n*20;
    // save background
    Push(tm);
    Hide();
    getimage(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5,p);
    Win(tm->x+1,tm->y1+2,tm->x+100,tm->y1+i+5);
    setcolor(0);
    settextstyle(2,0,4);
    for(i=0;i<n;i++)
    {
        if(!tm->Active[i])
        {
            setcolor(15);
            outtextxy(tm->x+15,tm->y1+9+i*20,H[i]);
            setcolor(0);
            outtextxy(tm->x+14,tm->y1+8+i*20,H[i]);
        }
        else    outtextxy(tm->x+14,tm->y1+8+i*20,H[i]);
    }
    Show();
    while(button==1)  Status();
    HandleMenu(n,tm);
}

PullMenuUp(int n,struct MENU *tm)
{
    int i;
    //load background
    Hide();
    putimage(tm->x+1,tm->y1+2,p,COPY_PUT);
    Show();
    Up(tm);
}

HandleMenu(int n,struct MENU *tm)
{
    int i;
    char Key;
    setcolor(0);
    Selection=NONE;
    i=0;
    while(1)
    {
        Status();
        if(button==1) break;
        for(i=0;i<n;i++)
        {
            Status();
            if(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20))
            {
                Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14);
                while(MouseOn1(tm->x+4,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20))
                {
                Status();
                if(button==1)
                {
                    if(tm->Active[i]==YES)
                    {
                        Selection=i;
                        break;
                    }
                    else
                    Selection=NONE;
                }
                if(kbhit()) break;
                }
                Xorbar(tm->x+3,tm->y1+6+i*20,tm->x+97,tm->y1+22+i*20,14);
            }
        }
    }
    PullMenuUp(n,tm);
}

MouseOn1(int cx,int cy,int cx1,int cy1)
{
 int flag;
 flag=0;
 if(x>=cx && x<=cx1 && y>=cy && y<=cy1) flag=1;
 return flag;
}

// Mouse & win routines

union REGS i,o;
struct SREGS ss;
void Show()
{
     i.x.ax=1;
     int86(0x33,&i,&o);
}

void Hide()
{
     i.x.ax=2;
     int86(0x33,&i,&o);
}

void Status()
{
     i.x.ax=3;
     int86(0x33,&i,&o);
     button=o.x.bx;
     x=o.x.cx;
     y=o.x.dx;
}

winp(struct MENU *t,int state)
{
   Hide();
   if(state==1)
   {
    setcolor(0);
    line(t->x,t->y,t->x,t->y1);
    line(t->x,t->y,t->x1,t->y);
    setcolor(15);
    line(t->x1,t->y,t->x1,t->y1);
    line(t->x,t->y1,t->x1,t->y1);
  }
  else
  {
    setcolor(15);
    line(t->x,t->y,t->x,t->y1);
    line(t->x,t->y,t->x1,t->y);
    setcolor(0);
    line(t->x1,t->y,t->x1,t->y1);
    line(t->x,t->y1,t->x1,t->y1);
  }
  Show();
}

Win(int sx,int sy,int ex,int ey)
{
   Hide();
   setfillstyle(1,7);
   bar(sx,sy,ex,ey);
   setcolor(15);
   line(sx,sy,sx,ey);
   line(sx,sy,ex,sy);
   setcolor(0);
   line(ex,sy,ex,ey);
   line(sx,ey,ex,ey);
   Show();
}

Msg(char *m)
{
 int l;
 setcolor(14);
 rectangle(18,438,622,477);
 setfillstyle(1,0);
 bar(20,440,620,475);
 if(MouseOn(&Syntax-Empl))
 {
     settextstyle(2,0,7);
     setcolor(11);
     l=textwidth(m)/2;
     outtextxy(320-l,440,m);
 }
 else
 {
     settextstyle(2,0,8);
     setcolor(15);
     l=textwidth(m)/2;
     outtextxy(320-l,440,m);
     outtextxy(321-l,441,m);
 }
}

Comments()
{
   setfillstyle(1,0);
   bar(140,5,500,40);
   setcolor(14);
   rectangle(140,5,500,40);
   settextstyle(2,0,8);
   setcolor(15);
   outtextxy(260,10,\"GUI-MENUS\");
   setfillstyle(1,8);
   bar(140,150,502,402);
   setcolor(14);
   rectangle(138,148,502,402);
   Msg(\"Welcome !\");
   settextstyle(2,0,5);
   setcolor(10);
   outtextxy(158,180,\"Hello Friends !\");
   settextstyle(2,0,4);
   outtextxy(260,200,\"Here i am with my menu program.\");
   outtextxy(158,220,\"As you can see it\'s like windows menu,you can\");
   outtextxy(158,240,\"add more menus to it. It\'s just a concept for\");
   outtextxy(158,260,\"building more menus. I hope you will like my effort,\");
}

End()
{
  int i,j;
  setcolor(0);
  Hide();
  for(j=0;j<40;j++)
  {
    for(i=0;i<481;i+=20)
       line(0,0+i+j,640,j+0+i);
    delay(100);
    if(kbhit()) if(getch()==27) break;
  }
  settextstyle(2,0,5);
  setcolor(15);
  outtextxy(220,180,\"Thanks for being with us !\");
  Show();
  delay(3000);
}

    Related Post:
  1. Program to do Sum of numbers from 1 to 10, by dividing the job into two processes(parent and one child)

  2. Summation of an even numbers

  3. Program to simulate process synchronization, call wait function so that the executing process gets blocked and then print their process ids

  4. Selection sort

  5. Program to display different type of progress bar as displayed in windows system

  6. Program to fork a child and print the process id of parent and child process

  7. Program to read the following numbers,round them off to the nearest integers and print out the results in integer form: 35.7 50.21 -23.73 -46.45

  8. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBOARD SUM =1+4-9+16-25+36-.....

  9. simple queue with array

  10. PROGRAM THAT WILL PRINT YOUR MAILING ADDRESS IN THE FOLLOWING FORM: FIRSTLINE:- NAME, SECOND LINE:-DOOR NO. STREET, THIRD LINE:-PIN CODE

  11. Use of getchar function

  12. Password Creation Program

  13. Program that accepts a string in an array and prints the reverse of the string i.e. if the string is abcd then it will print dcba.

  14. Program to rotate an entered string

  15. Example of passing structure to function

  16. Program to check entered string is palindrome or not

  17. Program to multiply two nXn matrix using indirect scheduling in two dimensional matrix

  18. Program of 2-thread for Linux Using the POSIX threads library. Once the thread spawning is done, the first thread should wait for user input

  19. NEWTONS BACKWARD DIFFERENCE INTERPOLATION

  20. FUNCTION TABULATED AT EQUAL INTERVAL USING BACKWARD DIFFERENCE BETWEEN TWO VALUE