求助:我用SDK手工定义的菜单怎么不显示出来啊?
我用SDK在devC++4.9.9.2里编一个细胞生命游戏,现在需要加一个菜单,但是我照书上的方法添加了资源文件,编译通过,但出来的程序里面还是没有菜单,现在我把部分程序贴出来,请各位帮忙看看是哪里有问题?谢谢啊!程序框架是IDE生成的,所以有些注释是英文:
资源文件:
//资源文件cell.rc
#include "rc.h "
//菜单标志是不是这里的 "MYMENU?
MYMENU MENU DISCARDABLE
BEGIN
POPUP "&游戏 "
BEGIN
MENUITEM "重新开始 " , IDM_GAME_RESTART
MENUITEM "结束 " , IDM_GAME_END
MENUITEM "退出 " , IDM_GAME_EXIT
END
END
//与资源文件对应的rc.h文件
#define IDM_GAME_RESTART 40001
#define IDM_GAME_END 40002
#define IDM_GAME_EXIT 40003
#define MYMENU 40004
//主程序文件
#include <windows.h>
#include "CCell.h " //这个是一个类的定义,与我的问题无关,为节省篇幅就没贴出来了,其中定义了#define SIZE 200
#include "rc.h "
Ccell mycell; //细胞对象
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[] = TEXT( "WindowsApp ");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = "MYMENU "; //我的疑问是这里,对吗?编译通过了但是菜单还是没有显示出来
//《windows程序设计》上是用szClassName,我试过,一样没有菜单
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows 's default color as the background of the window */
wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let 's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App ", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
CW_USEDEFAULT, /* The programs width */
CW_USEDEFAULT, /* and height in pixels */
NULL, /* The window is a child-window to desktop */
NULL, /* No menu */ //这里是不是也要变啊?
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HBRUSH hbr;
HMENU hMenu;
RECT rc;
static int cxClient,cyClient;
static int bx,by;
static int times = 1000; //时钟周期
static int x,y;
PAINTSTRUCT ps;
switch (message) /* handle the messages */
{
case WM_COMMAND:
hMenu = GetMenu(hwnd);
switch(LOWORD(wParam))
{
case IDM_GAME_RESTART:
mycell.RandCell();
SetTimer(hwnd,1,times,NULL);
break;
case IDM_GAME_END:
KillTimer(hwnd,1);
break;
case IDM_GAME_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
break;
}
break;
case WM_CREATE:
SetTimer(hwnd,1,times,NULL);
GetClientRect(hwnd,&rc);
cxClient = rc.right;
cyClient = rc.bottom;
bx = cxClient/SIZE;
by = cyClient/SIZE;
mycell.SetProb(0.5);
break;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
bx = cxClient/SIZE;
by = cyClient/SIZE;
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_TIMER:
mycell.Run();
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
//SaveDC(hdc);
SelectObject(hdc,GetStockObject(WHITE_BRUSH));
Rectangle(hdc,0,0,cxClient,cyClient);
hbr = CreateSolidBrush(RGB(200,100,240));
SelectObject(hdc,hbr);
for(x = 0;x <SIZE;x++) //“SIZE”是在头文件CCell.h里定义的常量,= 200
for(y = 0;y <SIZE;y++)
{
if(mycell.OutputState[x][y] == 1)
Rectangle(hdc,x*bx,y*by,(x+1)*bx,(y+1)*by);
}
//RestoreDC(hdc,-1);
EndPaint(hwnd,&ps);
DeleteObject(hbr);
break;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don 't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
请大家看看菜单为什么没有显示啊?谢谢!
[解决办法]
wincl.lpszMenuName = "MYMENU "; //我的疑问是这里,对吗?编译通过了但是菜单
这里不对:这样wincl.lpszMenuName= "MYMENU "
你需要强行转换为(char*)MYMENU
NULL, /* No menu */ //这里是不是也要变啊?
------------------------------
CreateWindowEx函数的这个hMenu菜单句柄,也可以为窗口指定一个菜单,它将代替wincl的设置。也赋值为NULL。