|
Advertisement |
Creating Windows Style Graphical Menus Using C Programming
Posted On January 9, 2012 by Sneha Latha filed under
1: INTRODUCTION
Now Days, the C programming Language proficiency is considered as an important and default level skill set for the computer professionals and academicians. In today’s scenario where the visual languages like Visual C++ , Visual Basic etc. are quite popular, the C/C++ in MS DOS environment is still important.
2: THE PROGRAM
Turbo C/C++ 3.0 is commonly used C programming IDE in DOS environment. Here the program is developed in Turbo C installed as ‘C:\TC’ directory in MS DOS environment. Hence to run it in Turbo C++ environment some prototype declaration for the function is advised. This program is using the graphics capabilities of the C Languages.
2.1: Mouse Initialization: The initialization of the mouse, showing and hiding the mouse pointer is performed using functionsentitled initmouse(), showmouseptr(), hidemouseptr() respectively as given in Code: 2.1. This Type of code can be found in any intermediate level C Programming Text book.
Code: 2.1 Functions for mouse operations
initmouse(){ i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax);}
showmouseptr(){ i.x.ax=1; int86(0x33,&i,&o); return(o.x.ax);}
hidemouseptr(){ i.x.ax=2; int86(0x33,&i,&o); return(o.x.ax);}
Code: 2.1 Function to Get Mouse Position
getmousepos(int *button,int *x,int *y)
{ i.x.ax=3; int86(0x33,&i,&o);
*button=o.x.bx; *x=o.x.cx; *y=o.x.dx;
}
2.2: Displaying Window: The main window in which menu is to be attached is drawn by using the function main_g_window.as shown in Fig. 2.2. This function also contains the code for getting the mouse response on that window.
The control box buttons like Minimize button, Maximize button and close button are created by using functions minimize_button, maximize_button and close_button respectively. Out of these three buttons only close button has the mouse response; other two are only for display purpose. The code for this function is given in Code 2.2

Code : 2.2.1 Function to Draw main Window
main_g_window(char *str,int x1,int y1,int x2,int y2,int c)
{
int button,x,y;
setcolor(BLACK); rectangle(x1,y1+1,x2+1,y2+1);
setcolor(8); rectangle(x1,y1+1,x2,y2);
setcolor(WHITE); rectangle(x1,y1,x2-1,y2-1);
setfillstyle(SOLID_FILL,7); bar(x1+1,y1+1,x2-1,y2-1);
setfillstyle(SOLID_FILL,c); bar(x1+3,y1+3,x2-3,y1+19);
settextstyle(SMALL_FONT,0,5); setcolor(WHITE); outtextxy(x1+25,y1+2,str); setcolor(YELLOW);
outtextxy(x1+7,y1,"/"); outtextxy(x1+8,y1,"/");
outtextxy(x1+9,y1+2,"/"); outtextxy(x1+10,y1+2,"/");
outtextxy(x1+11,y1+4,"/"); outtextxy(x1+12,y1+4,"/");
minimise_button(x2-48,y1+8); maximize_button(x2-32,y1+8);
close_button(x2-15,y1+8);
setfillstyle(SOLID_FILL,7); bar(x1+5,y1+42,x2-7,y2-23);
setcolor(8);line(x1+4,y1+41,x2-5,y1+41);line(x1+4,y1+41,x1+4,y2-21);
setcolor(BLACK);
line(x1+5,y1+42,x2-6,y1+42); line(x1+5,y1+42,x1+5,y2-22);
setcolor(WHITE);line(x1+4,y2-20,x2-5,y2-20); line(x2-4,y1+41,x2-4,y2-20);
getmousepos(&button,&x,&y);
if(x>=x2-20 && x<=x2-6 && y>=y1+5 && y<=y1+17)
{ if((button & 1)==1)
{ hidemouseptr(); exit(0); }
}
}
Code : 2.2.2 Function to display a Button
corner_button(int x,int y)
{ setcolor(WHITE); setfillstyle(SOLID_FILL,7); settextstyle(SMALL_FONT,0,6);
rectangle(x-5,y-3,x+8,y+8); setcolor(8);
rectangle(x-4,y-2,x+9,y+9); bar(x-4,y-2,x+8,y+8);
}
Code : 2.2.3 Function to display a Button
close_button(int x,int y)
{ corner_button(x,y); setcolor(BLACK);
outtextxy(x-1,y-9,"x"); outtextxy(x-2,y-9,"x");
}
Code : 2.2.4 Function to display a Button
minimise_button(int x,int y)
{ corner_button(x,y); setcolor(WHITE); outtextxy(x-1,y-9,"_");
setcolor(BLACK); outtextxy(x-1,y-10,"_");
}
Code : 2.2.5 Function to display a Button
maximize_button(int x,int y)
{ corner_button(x,y); setcolor(WHITE); line(x-2,y-1,x+6,y-1);
setcolor(BLACK); rectangle(x-2,y,x+6,y+7);
}
2.3: Displaying Horizontal Menus:The horizontal Menu is displayed by using the function displaymenuh() as given in Code 2.3.1.Here the horizontal menu items are taken as ‘File’, ‘Edit’, ‘ Run’, ‘Compile’, and ‘Project’ The number of menu items are automatically counted by the statement count=sizeof(menu)/sizeof(char*); therefore user can easily modify the number of menu items without worrying the number of menu items. The highlighting and de-highlighting of menu item on hovering of mouse over the menu item is done by using the function highlight() and dehighlight() as given in Code 2.3.3, 2.3.4, 2.3.5. Now the click event on the menu items of the left mouse button is retrieved in infinite while loop using function getresponseh() as given in Code 2.3.2 which returns the menu item number which is clicked.
Fig. 2.3: Screen Shot of Horizontal Menu

Code : 2.3.1 Function to Display Horizontal Menu
displaymenuh(char **menu,int count,int x1,int y1)
{
int i,tw,xc;
xc=x1;
setfillstyle(SOLID_FILL,7); bar(x1-3,y1,x1+500,y1+17);
settextstyle(SMALL_FONT,0,4); setcolor(BLACK);
for(i=0;i<count;i++)
{ tw=textwidth(menu[i]); outtextxy(xc,y1+2,menu[i]);
xc=xc+tw+10;
}
}
Code 2.3.2 Function to Get Mouse Response of Horizontal Menu
getresponseh(char **menu,int count,int x1,int y1)
{
int choice=1,prevchoice=0,x,y,x2,y2,button;
int in,i,h,tw,xc;
h=textheight(menu[0]);
y2=y1+h+6; x2=x1;
displaymenuh(menu,count,x1,y1);
for(i=0;i<count;i++){x2=x2+textwidth(menu[i])+10;}
showmouseptr();
while(1)
{
getmousepos(&button,&x,&y);getmousepos(&button,&x,&y);
if(x>=630-20 && x<=630-6 && y>=5+5 && y<=5+17)
{ if((button & 1)==1){ hidemouseptr(); exit(0); }
}
settextstyle(SMALL_FONT,0,4);
if(x>=x1 && x<=x2 && y>=y1 && y<=y2)
{ in=1;xc=x1;
for(i=1;i<=count;i++)
{ settextstyle(SMALL_FONT,0,4);
xc=xc+textwidth(menu[i-1])+10;
if(x<=xc){ choice=i; break; }
}
if(prevchoice!=choice)
{ hidemouseptr();
if(prevchoice)
dehighlight(menu,prevchoice,x1,y1);
highlight(menu,choice,x1,y1);
prevchoice=choice; showmouseptr();
}
if((button & 1)==1)
{ outtextxy(100,300,"YES");
while((button & 1)==1)
getmousepos(&button,&x,&y);
if(x>=x1 && x<=x2 && y>=y1 && y<=y2)
{ outtextxy(100,300,"NO***");
hidemouseptr();
highlight_select(menu,choice,x1,y1);
return(choice);
}
}
}
else
{ if(in==1)
{ in=0; prevchoice=0;hidemouseptr();
dehighlight(menu,choice,x1,y1);
showmouseptr();
}
}
}
}
Code 2.3.3 Function to highlight Menu Item
highlight (char **menu,int ch,int x1,int y1)
{ int xc=x1,tw,tw1,i;
for(i=1;i<=ch;i++) { xc=xc+textwidth(menu[i-1])+10; }
tw=textwidth(menu[ch-1]); setcolor(WHITE);
line(xc-10-tw,y1,xc,y1); line(xc-10-tw,y1,xc-10-tw,y1+17);
setcolor(8); line(xc-10-tw,y1+17,xc,y1+17); line(xc,y1,xc,y1+17);
}
Code 2.3.4 Function to Highlight Selected Menu
highlight_select(char **menu,int ch,int x1,int y1)
{ int xc=x1,tw,tw1,i;
for(i=1;i<=ch;i++) { xc=xc+textwidth(menu[i-1])+10; }
tw=textwidth(menu[ch-1]); setcolor(8);
line(xc-10-tw,y1,xc,y1); line(xc-10-tw,y1,xc-10-tw,y1+17);
setcolor(WHITE); line(xc-10-tw,y1+17,xc,y1+17); line(xc,y1,xc,y1+17);
}
Code 2.3.5 Function to Dehighlight Menu Item
dehighlight(char **menu,int ch,int x1,int y1)
{ int xc=x1,tw,tw1,i;
for(i=1;i<=ch;i++) { xc=xc+textwidth(menu[i-1])+10; }
tw=textwidth(menu[ch-1]); setcolor(7);
line(xc-10-tw,y1,xc,y1); line(xc-10-tw,y1,xc-10-tw,y1+17);
setcolor(7); line(xc-10-tw,y1+17,xc,y1+17); line(xc,y1,xc,y1+17);
}
2.4: Displaying Vertical Menus: The vertical Menu is displayed by using the function displaymenuv()
Here the vertical menu items are taken as ‘File’, ‘Edit’, ‘ Run’, ‘Compile’, and ‘Project’. The click event on the menu items of the left mouse button is retrieved in infinite while loop using function getresponsev() which returns the menu item number which is clicked.

Code 2.4.1 Funtion to Display Drop Down Menu
drop_down_menu(char **menu,int count,int x1,int y1,int color,int bk_color)
{ int i,width=0;
for(i=0;i<count;i++){
if(textwidth(menu[i])>width){ width=textwidth(menu[i]); }
}
width=width+30; showmouseptr();
getresponsev(menu,count,width,x1,y1,color,bk_color);
}
Code 2.4.2 Function to display Vertical Menu
displaymenuv(char **menu,int count,int width,int x1,int y1,int bk_color)
{ int i,h;
setcolor(0);
h=textheight(menu[0]); rectangle(x1-5,y1-10,x1+width+12,y1+count*(h+10)+10);
setcolor(8); rectangle(x1-5,y1-10,x1+width+11,y1+count*(h+10)+9);
setcolor(WHITE); rectangle(x1-5,y1-10,x1+width+10,y1+count*(h+10)+8);
setfillstyle(SOLID_FILL,bk_color);
bar(x1-4,y1-9,x1+width+10,y1+count*(h+10)+8);
settextstyle(SMALL_FONT,0,4); setcolor(BLACK);
for(i=0;i<count;i++) { outtextxy(x1+10,y1+i*(h+10),menu[i]); }
}
Code 2.4.3 Function to Get Mouse Response of Vertical Menu
getresponsev(char **menu,int count,int width,int x1,int y1,int color,int bk_color)
{ int choice=1,prevchoice=0,x,y,x2,y2,button,in,i,h,tw,xc,area;
long int *buffer;
h=textheight(menu[0]); y2=y1+count*(h+10); x2=x1+width+10;
hidemouseptr(); area=imagesize(x1-6,y1-11,x1+width+13,y1+count*(h+10)+10);
buffer=malloc(area);
getimage(x1-6,y1-11,x1+width+13,y1+count*(h+10)+10,buffer);
displaymenuv(menu,count,width,x1,y1,bk_color); showmouseptr();
settextstyle(SMALL_FONT,0,4);
while(1)
{ getmousepos(&button,&x,&y);
if(x<x1 || x>x2 || y<y1 || y>y2)
{ if((button & 1)==1)
{ hidemouseptr(); putimage(x1-6,y1-11,buffer,COPY_PUT);
free(buffer); return(0);
}
}
if(x>=x1 && x<=x2 && y>=y1 && y<=y2)
{ in=1;
for(i=1;i<=count;i++)
{ if(y<=y1+i*(h+10)){choice=i; break;}
}
if(prevchoice!=choice)
{ hidemouseptr();
if(prevchoice)
dehighlightv(menu,prevchoice,width,x1,y1,bk_color);
highlightv(menu,choice,width,x1,y1,color);
prevchoice=choice; showmouseptr();
}
if((button & 1)==1)
{ while((button & 1)==1)
getmousepos(&button,&x,&y);
if(x>=x1 && x<=x2 && y>=y1 && y<=y2)
{ hidemouseptr();putimage(x1-6,y1-11,buffer,COPY_PUT);
free(buffer); return(choice);
}
}
}
else
{ if(in==1)
{ in=0; prevchoice=0; hidemouseptr();
dehighlightv(menu,choice,width,x1,y1,bk_color);
showmouseptr();
}
}
}
}
Code 2.4.4 Function to highlight vertical Menu
highlightv(char **menu,int ch,int width,int x1,int y1,int color)
{
int h;
h=textheight(menu[0]);
setfillstyle(SOLID_FILL,color);
bar(x1,y1+(ch-1)*(h+10),x1+width+9,y1+(ch-1)*(h+10)+12);
setcolor(WHITE);
outtextxy(x1+10,y1+(ch-1)*(h+10),menu[ch-1]);
}
Code 2.4.5 function to dehighlight vertical menu
dehighlightv(char **menu,int ch,int width,int x1,int y1,int bk_color)
{
int h;
h=textheight(menu[0]);
setfillstyle(SOLID_FILL,bk_color);
bar(x1,y1+(ch-1)*(h+10),x1+width+9,y1+(ch-1)*(h+10)+12);
setcolor(BLACK);
outtextxy(x1+10,y1+(ch-1)*(h+10),menu[ch-1]);
}
2.5: Application to Main Program: Now all the function created for displaying menus, getting mouse response etc. is used in sample application program given in Code 2.5. This program is developed in Turbo C installed as ‘C:\TC’ directory in MS DOS environment. The horizontal menu choice is given by the getresponseh() is assigned to an integer variable choice which is used in switch statement for drop down menus. The dropdown menu choice given by drop_down_menu() is assigned to an integer variable choicev. Now user can use the value of choice and choicev as per their requirement.
Code 2.5 Sample Application Program
#define ON 1
#define OFF 0
#define YES 1
#define NO 2
#define CANCEL 3
#define CLOSE 4
#define OK 5
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<alloc.h>
char *menu[]={" File"," Edit"," Run"," Compile"," Project"};
char *menuvF[]={"New","Open .F3","Save F2","Save As .","Print .","Quit Alt+X",};
char *menuvE[]={"Cut","Copy","Paste","Clear"};
char *menuvR[]={"Run","Go to Cursor","Trace into"};
char *menuvC[]={"Compile","Make","Link","Build All"};
char *menuvP[]={"Open Project","Close Project"};
union REGS i,o;
struct SREGS s;
main()
{ int gd=DETECT,gm,choice,i,count,countvF,countvE,countvR,countvC,countvP;
int choicev,xco=20,yco=27,xco1=22,yco1=56,xco2=60,yco2=56,ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(BLACK);
main_g_window("Sun",5,5,630,470,BLUE);
if(initmouse()==0)
{ printf("\n Unable to initialise Mouse"); exit(0);
}
showmouseptr();
count=sizeof(menu)/sizeof(char*);
countvF=sizeof(menuvF)/sizeof(char*);
countvE=sizeof(menuvE)/sizeof(char*);
countvR=sizeof(menuvR)/sizeof(char*);
countvC=sizeof(menuvC)/sizeof(char*);
countvP=sizeof(menuvP)/sizeof(char*);
settextstyle(SMALL_FONT,0,6); setcolor(BLACK);
displaymenuh(menu,count,xco,yco); showmouseptr();
while(1)
{ choice=getresponseh(menu,count,xco,yco);
switch(choice)
{
case 1: choicev=drop_down_menu(menuvF,countvF,xco1,yco1,BLUE,7);
break;
case 2: choicev=drop_down_menu(menuvE,countvE,xco2,yco2,BLUE,7);
break;
case 3: choicev=drop_down_menu(menuvR,countvR,100,yco2,BLUE,7);
break;
case 4: choicev=drop_down_menu(menuvC,countvC,135,yco2,BLUE,7);
break;
case 5: choicev=drop_down_menu(menuvP,countvP,190,yco2,BLUE,7);
break;
}
}
}
3. CONCLUSION
Although many other languages are there by which we can design windows based application rapidly but many situations demands for customized design hence in that circumstance coding in C is best options as it end with robust development. Main advantage of using C over other languages is its platform independent and it’s least run time.
Author’s Biography:

Mr. S.K.Mahajan is working as Asst.Professor in Department of Civil Engineering of Kavikulguru Institute of Technology and Science, Ramtek (Dist.Nagpur). He had M.Tech in Structural Engineering from VNIT Nagpur. For more information on C programming you can be reached at sunrays_75@rediffmail.com





