PDA

توجه ! این یک نسخه آرشیو شده می باشد و در این حالت شما عکسی را مشاهده نمی کنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : كدسورس روش ساخت منو



TAHA
01-03-2010, 03:02 PM
Introduction

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


#define IDS_APP_NAME 1
#define IDR_MAIN_MENU 101
#define IDR_POPUP 102
#define IDM_FILE_EXIT 40001
#define IDM_SMALL 40002
#define IDM_MEDIUM 40003
#define IDM_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
MENUITEM SEPARATOR
MENUITEM "&Jumbo", IDM_JUMBO
END
END


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

STRINGTABLE
BEGIN
IDM_FILE_EXIT "Closes the application\nClose"
IDM_SMALL "Selects a small pizza"
IDM_MEDIUM "Selects a medium pizza"
IDM_LARGE "Makes up a large pizza"
IDM_JUMBO "Builds an extra-large pizza"
IDM_HELP_ABOUT "About this application"
END

STRINGTABLE
BEGIN
IDS_APP_NAME "MenuApplied"
END

Source Code


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

HINSTANCE hInst;
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
WNDCLAS*** WndCl***;

hInst = hInstance;

const char *ClsName = "MenuApplied";
const char *WndName = "Techniques of Using Menus";

// 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,
WPARAM wParam, LPARAM lParam)
{
// Handle to a menu. This will be used with the context-sensitive menu
HMENU hMenu;
// Handle to the system menu
HMENU hSysMenu;
// Handle to the context menu that will be created
HMENU hMenuTrackPopup;

switch(Msg)
{
case WM_CREATE:
// To modify the system 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 a string
AppendMenu(hSysMenu, MF_STRING, 1, "Practical Techniques");
// This is how to add a menu item using a defined identifier
AppendMenu(hSysMenu, MF_STRING, IDM_HELP_ABOUT, "About...");
return 0;

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

case IDM_FILE_EXIT:
PostQuitMessage(WM_QUIT);
break;
}
return 0;

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

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

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

case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;

default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

return 0;
}