SDK各种方式创建的菜单
#include <Windows.h>HINSTANCE g_hInstance;
HMENU MainhMenu;
void OnCreate(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
MainhMenu= CreateMenu(); //为什么在这里创建的窗口句柄,放在窗口CreateWindowEx中显示不出来?
HMENU hEdit = CreatePopupMenu();
AppendMenu(hEdit,MF_STRING,1001,TEXT("新建(&N)"));
AppendMenu(MainhMenu,MF_STRING|MF_POPUP,(UINT)hEdit,TEXT("文件(&F)"));
HMENU hHelp = CreatePopupMenu();
AppendMenu(hHelp,MF_STRING,1002,TEXT("查找(&F)"));
AppendMenu(MainhMenu,MF_STRING|MF_POPUP,(UINT)hHelp,TEXT("关于(&A)"));
//SetMenu(hWnd,MainhMenu);
}
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE:
{
OnCreate(hWnd,uMsg,wParam,lParam);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
void RegisterWnd(LPCTSTR lpClassName)
{
WNDCLASSEX wce;
wce.style = CS_HREDRAW|CS_VREDRAW;
wce.lpfnWndProc = WndProc;
wce.cbSize = sizeof(WNDCLASSEX);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hInstance = g_hInstance;
wce.hIcon= 0;
wce.hCursor = 0;
wce.hIconSm = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE);
wce.lpszClassName = lpClassName;
wce.lpszMenuName = 0;
if(!RegisterClassEx(&wce))
{
MessageBox(NULL,TEXT("注册失败"),TEXT("提示"),MB_OK);
}
}
HWND CreateWnd(LPCTSTR lpClassName)
{
HWND hWnd = CreateWindowEx(0,lpClassName,L"HELLO",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
200,200,NULL,(HMENU)MainhMenu,g_hInstance,NULL);
return hWnd;
}
void DisplayWnd(HWND hWnd)
{
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
}
void Message()
{
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInstance = hInstance;
RegisterWnd(L"hello");
HWND hWnd = CreateWnd(L"hello");
DisplayWnd(hWnd);
Message();
return 0;
}
因为CreateWnd中的“CreateWindowEx”发生在OnCreate中的“MainhMenu”之前,你调试下就知道了,不可能调用未来的东西,因为它还没有发生 感觉好复杂,本来还想学学的 我还是想学学,虽然很复杂
页:
[1]