امتیاز موضوع:
  • 0 رأی - میانگین امتیازات: 0
  • 1
  • 2
  • 3
  • 4
  • 5
اموزش ساخت منو
#1
Rainbow 
ساخت منو يكي از سرفصل هاي مهم اموزشي در درس برنامه سازي پيشرفته 2ميباشد

كه در اين پست قصد داريم با ارائه چند سورس از منو هاي ساخته شده

. . . دانشجويان عزيز را بيشتر با اين مبحث اشنا كنيم



[تصویر:  doubly-linkedlist-menu.png]


کد:
Introduction

This example show how to create an
application with a main menu, how to add an item to the system menu, and how tocreate a context-sensitive menu activated when the user right-clickssomewhere.
Resource Header

#define IDS_APP_NAME 1
#defineIDR_MAIN_MENU 101
#define IDR_POPUP 102
#define IDM_FILE_EXIT 40001
#define IDM_SMALL 40002
#define IDM_MEDIUM 40003
#defineIDM_LARGE 40004
#define IDM_JUMBO 40005
#define IDM_HELP_ABOUT 40006
Resource Script

#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MAIN_MENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_FILE_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About...\tF1", IDM_HELP_ABOUT
END
END

IDR_POPUP MENU
BEGIN
POPUP "_POPUP_"
BEGIN
MENUITEM "&Small", IDM_SMALL
MENUITEM "&Medium", IDM_MEDIUM
MENUITEM "&Large", IDM_LARGE
MENUITEMSEPARATOR
MENUITEM "&Jumbo", IDM_JUMBO
END
END


/////////////////////////////////////////////////////////////////////////////
//
// String Table
//

STRINGTABLE
BEGIN
IDM_FILE_EXIT "Closes theapplication\nClose"
IDM_SMALL "Selects a small pizza"
IDM_MEDIUM "Selectsa medium pizza"
IDM_LARGE "Makes up a large pizza"
IDM_JUMBO "Builds anextra-large pizza"
IDM_HELP_ABOUT "About thisapplication"
END

STRINGTABLE
BEGIN
IDS_APP_NAME "MenuApplied"
END

Source Code


#include <windows.h>
#include "resource.h"

HINSTANCE hInst;
LRESULTCALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAMlParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCEhPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWNDhWnd;
WNDCLAS*** WndCl***;

hInst = hInstance;

const char *ClsName = "MenuApplied";
const char *WndName = "Techniques of UsingMenus";

// Create the application window
WndCl***.cbSize = sizeof(WNDCLAS***);
WndCl***.style = CS_HREDRAW | CS_VREDRAW;
WndCl***.lpfnWndProc = WndProcedure;
WndCl***.cbCl***tra = 0;
WndCl***.cbWndExtra = 0;
WndCl***.hIcon = LoadIcon(NULL, IDI_WARNING);
WndCl***.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCl***.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
WndCl***.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU);
WndCl***.lpszClassName = ClsName;
WndCl***.hInstance = hInstance;
WndCl***.hIconSm = LoadIcon(NULL, IDI_WARNING);

RegisterClas***(&WndCl***);

hWnd = CreateWindow(ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
200,
160,
460,
320,
NULL,
NULL,
hInstance,
NULL);

if( !hWnd )
return 0;

ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);

while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return 0;
}

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAMwParam, LPARAM lParam)
{
// Handle to a menu. This will be used with thecontext-sensitive menu
HMENU hMenu;
// Handle to the system menu
HMENUhSysMenu;
// Handle to the context menu that will be created
HMENUhMenuTrackPopup;

switch(Msg)
{
case WM_CREATE:
// To modify thesystem menu, first get a handle to it
hSysMenu = GetSystemMenu(hWnd, FALSE);
// This is how to add a separator to a menu
InsertMenu(hSysMenu, 2, MF_SEPARATOR, 0, "-");
// This is how to add a menu item using astring
AppendMenu(hSysMenu, MF_STRING, 1, "Practical Techniques");
// Thisis how to add a menu item using a defined identifier
AppendMenu(hSysMenu, MF_STRING, IDM_HELP_ABOUT, "About...");
return 0;

caseWM_COMMAND:
switch(LOWORD(wParam))
{
caseIDM_LARGE:
MessageBox(hWnd, "Menu Item Selected = Large", "Message", MB_OK);
break;

caseIDM_FILE_EXIT:
PostQuitMessage(WM_QUIT);
break;
}
return 0;

case WM_CONTEXTMENU:
// Get a handle to the popup menu using itsresource
if( (hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_POPUP))) == NULL )
return 0;

// Get a handle to the first shortcutmenu
hMenuTrackPopup = GetSubMenu(hMenu, 0);

// Display the popup menuwhen the user right-clicks
TrackPopupMenu(hMenuTrackPopup,
TPM_LEFTALIGN | TPM_RIGHTBUTTON,
LOWORD(lParam),
HIWORD(lParam),
0,
hWnd,
NULL);
break;

caseWM_DESTROY:
PostQuitMessage(WM_QUIT);
break;

default:
returnDefWindowProc(hWnd, Msg, wParam, lParam);
}

return 0;
}


کد:
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>

//Menu Global Item
#define pixTOrc(x) (8*(x-1)) //convert pixel into row and col format
#define INC 5 //Increment Distance Between Menu Items
#define ROW 15 //Row Value for Menu Item
#define COL 8 //Column Value for Menu Item
#define MAXITEM 5 //Total menu items

// To display the Inventory Main menu options
typedef char option[15];
option mainMenu[]= {
"NEW",
"OPEN",
"SAVE",
"ABOUT ME",
"CLOSE"
};


// Function to displays all the menu prompt messages from the pointer array of option a[]
void normalvideo(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
outtextxy(x,y,str);
}

// Function to move the cursor on the menu prompt with a reverse video color
void reversevideo(int x,int y,char *str)
{
x=pixTOrc(x);
y=pixTOrc(y);
setcolor(YELLOW); //Selected Item
sound(400);
delay(100);
nosound();
outtextxy(x,y,str);
setcolor(WHITE); //Unselected Item
sound(500);
delay(100);
nosound();
}


//Keep Track of which arrow key is pressed
char menu()
{
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
setcolor(WHITE); //Initial Menu Item Color
int i,done;
for(i = 1; i < MAXITEM; i++)
normalvideo(COL, (i*INC)+ROW, mainMenu[i]);

reversevideo(COL,ROW, mainMenu[0]);
i = done = 0;
do
{
/**Status Bar Logic**/
//Message will be displayed as status bar guide-line
setfillstyle(SOLID_FILL,BLUE);
settextstyle(SMALL_FONT,HORIZ_DIR,5);
bar(pixTOrc(2),pixTOrc(52.5),pixTOrc(75),pixTOrc(5 5));
setcolor(LIGHTCYAN);
switch(i){
case 0 : outtextxy(pixTOrc(5),pixTOrc(52.75),"New --> Create New file");
break;
case 1 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Open --> Open Existing file");
break;
case 2 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Save --> Save file");
break;
case 3 : outtextxy(pixTOrc(5),pixTOrc(52.75),"About Me --> Programmer : Vivek Patel");
break;
case 4 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Close the Program --> BYE C U");
break;
}
/**status Bar ends**/

//Restore Orignal Color and Font Setting
setcolor(WHITE);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);

int key = getch();
switch (key)
{

case 00:
key = getch();
switch (key)
{
case 72:
normalvideo(COL, (i*INC)+ROW, mainMenu[i]);
i--;
if (i == -1)
i = MAXITEM-1;
reversevideo(COL,(i*INC)+ROW,mainMenu[i]);
break;
case 80:
normalvideo(COL, (i*INC)+ROW, mainMenu[i]);
i++;
if (i == MAXITEM)
i = 0;
reversevideo(COL, (i*INC)+ROW, mainMenu[i]);
break;
}
break;
case 13:
done = 1;
}
}
while (!done);
return(i+49);
}


//Advertise Screen will displayed to utilize empty screen area
//It can be utilize for some effective...work
void advertise(){
setcolor(BLUE);
outtextxy(pixTOrc(30),pixTOrc(20),"URL : cpp.blogfa.com");
outtextxy(pixTOrc(30),pixTOrc(26),"Mail : Salman_Mazidi@yahoo.com");
setcolor(YELLOW);
}


/* The function is used to display the main menu*/
//Actual code for all the menu utility resides in this
//Function...
void control_menu()
{
char choice;
do
{
choice = menu();
switch (choice)
{
case '1': //New
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"New");
advertise();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50 ));
advertise();
break;

case '2': //Open
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Open");
advertise();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50 ));
advertise();
break;

case '3': //Save
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"Save");
advertise();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50 ));
advertise();
break;

case '4': //Modify the status of item in inventory
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"About Me");
advertise();
getch();
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50 ));
advertise();
break;

case '5': //Close the program
setcolor(BLUE);
outtextxy(pixTOrc(40),pixTOrc(15),"CLOSE");
advertise();
delay(1000);
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(pixTOrc(28),pixTOrc(14),pixTOrc(75),pixTOrc(50 ));
advertise();
goto out;
}
} while (choice != MAXITEM);
out:
}



void main()
{
int i,j;

int gd=DETECT,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi\\");

///code as space holder\\\\\
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(0,0,640,480);

setcolor(DARKGRAY);
rectangle(0,0,639,480);
rectangle(1,1,638,479);
setcolor(BLACK);
rectangle(2,1,637,478);
rectangle(3,1,636,477);

settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
setcolor(BLUE);
outtextxy(pixTOrc(8),pixTOrc(2)," MENU -->> (Simple Style) ");

setfillstyle(HATCH_FILL,DARKGRAY);
for(i=15,j=70;i<40||j>40;i++,j--){
bar(pixTOrc(8),pixTOrc(7),pixTOrc(i),pixTOrc(7.5)) ;
delay(10);
bar(pixTOrc(j),pixTOrc(7),pixTOrc(70),pixTOrc(7.5) );
delay(20);
}

bar(pixTOrc(7),pixTOrc(14),pixTOrc(25),pixTOrc(50) );

///code as space holder\\\\\

//Calling Menu
control_menu();

closegraph();
}
پاسخ


پرش به انجمن:


کاربرانِ درحال بازدید از این موضوع: 1 مهمان