懂windowsAPI的都可以进~~程序出错了,不知道哪里错了找了二天了也没有找到,请高手看看~~

@THINK
企业官方账号
2008-01-14 03:19:47
代码如下:

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

#define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{

HWND hwnd ;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName =szAppName ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("MenuDemonstration"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int idColor[5]={WHITE_BRUSH,LTGRAY_BRUSH,GRAY_BRUSH,DKGRAY_BRUSH,BLACK_BRUSH};
static int iSelection=IDM_BKGND_WHITE;
HMENU hMenu;

switch (message)
{
case WM_COMMAND:
hMenu=GetMenu(hwnd);
switch(LOWORD(wParam))
{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep(0);
return 0;
case IDM_APP_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
return 0;

case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
MessageBeep(0);
return 0;

case IDM_BKGND_WHITE:
case IDM_BKGND_LTGRAY:
case IDM_BKGND_GRAY:
case IDM_BKGND_DKGRAY:
case IDM_BKGND_BLACK:

CheckMenuItem(hMenu,iSelectiom,MF_UNCHECKED);
iSelection=LOWORD(wParam);
CheckMenuItem(hMenu,iSelectiom,MF_CHECKED);

SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(idColor[LOWORD(wParam)-IDM_BKGND_WHITE]));

InvalidateRect(hwnd,NULL,TRUE);
return 0;

case IDM_TIMER_START:
if(SetTimer(hwnd,ID_TIMER,1000,NULL))
{
EnableMenuItem(hMenu,IDM_TIMER_START,MF_GRAYED);
EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_ENABLED);
}
return 0;

case IDM_TIMER_STOP:
KillTimer(hwnd,ID_TIMER);
EnableMenuItem(hMenu,IDM_TIMER_START,MF_ENABLED);
EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_GRAYED);
return 0;

case IDM_APP_HELP:
MessageBox(hwnd,TEXT("Help not yet implemented!"),szAppName,MB_ICONEXCLAMATION|MB_OK);
return 0;
case IDM_APP_ABOUT:
MessageBox(hwnd,TEXT("MenuDemonstration Program\n")
TEXT("(C)Charles Petzold,1998"),
szAppName,MB_ICONINFORMATION|MB_OK);
return 0;
}
break;

case WM_TIMER:
MessageBeep(0);
return 0;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
...全文
209 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
tang21814 2010-11-29
  • 打赏
  • 举报
回复
呵呵 感谢14楼! 郁闷了我一下午
学习了!
zyb_debug 2008-02-14
  • 打赏
  • 举报
回复
首先,在文件里面建立一个 Resource Script ,然后插入-> 资源->插入menu
在看看书中的资源描述档案
该添加什么就添加什么
paerxiushi 2008-02-14
  • 打赏
  • 举报
回复
wndclass.lpszMenuName =szAppName ; 中的szAppName是菜单的资源名称,一个资源名称必须使用MAKEINTRESOURCE(xxx)来表示,其中XXX表示菜单的资源标识符
你的szAppName是MenuDemo,当然显示不了,还有CreateWindow方法中也必须指定菜单
sandy_zc_1 2008-02-11
  • 打赏
  • 举报
回复


编译器找不到'IDM_BKGND_WHITE' 这个菜单,说明你的resourse.h有问题啊 ,你的窗口资源上并没有这个菜单,而代码中出现了这个,当然会报错了。

要么在代码中把使用这个菜单相关的代码删掉,要么在你的窗口中加入这个菜单。

发现你对Win32编程的概念还了解得不是很清楚,不然这个错误很好解决的。
WZ19860913 2008-02-11
  • 打赏
  • 举报
回复
这个是《Windows程序设计》上的一个程序吧,好好看看书!
winnerkun 2008-01-30
  • 打赏
  • 举报
回复
注释掉:“case IDM_BKGND_WHITE:”!
@THINK 2008-01-19
  • 打赏
  • 举报
回复
resource.h文件我添加了的啊 可还是有问题的啊
long_xing 2008-01-18
  • 打赏
  • 举报
回复
你的资源文件中没有添加这些菜单项。
resource.h文件中没有对应的宏定义,所以编译出错。

这估计是你在其它地方复制过来的代码吧,没有复制相应的resource.h和.rc文件。
jwybobo2007 2008-01-16
  • 打赏
  • 举报
回复
到底什么错,看你的提示应该是缺少定义,很有可能是少头文件
sxcong 2008-01-16
  • 打赏
  • 举报
回复
对了,就是什么都没有。想要的话,自己在资源里面加,或程序生成。比如toolbar,menu,statusbar
@THINK 2008-01-14
  • 打赏
  • 举报
回复
这个跑是跑起来了的啊 可是没有菜单的啊~~~~~什么也没有的啊
rand0941 2008-01-14
  • 打赏
  • 举报
回复
你先跑起来吧,link模式改成windows
[code=C/C++]
#include <windows.h>
//#include "resource.h"
#define ID_TIMER 1
#define IDM_FILE_NEW 103
#define IDM_FILE_OPEN 104
#define IDM_FILE_SAVE 105
#define IDM_FILE_SAVE_AS 106

#define IDM_EDIT_UNDO 107
#define IDM_EDIT_CLEAR 108
#define IDM_EDIT_CUT 201
#define IDM_EDIT_COPY 202
#define IDM_EDIT_PASTE 203

#define IDM_BKGND_WHITE 204
#define IDM_BKGND_LTGRAY 205
#define IDM_BKGND_GRAY 206
#define IDM_BKGND_DKGRAY 207
#define IDM_BKGND_BLACK 208

#define IDM_APP_EXIT 209

#define IDM_TIMER_START 210
#define IDM_TIMER_STOP 211
#define IDM_APP_HELP 212
#define IDM_APP_ABOUT 213

#define ID_TIMER 1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

TCHAR szAppName[] = TEXT ("MenuDemo") ;

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{

HWND hwnd ;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc= WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName =szAppName ;
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("MenuDemonstration"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int idColor[5]={WHITE_BRUSH,LTGRAY_BRUSH,GRAY_BRUSH,DKGRAY_BRUSH,BLACK_BRUSH};
static int iSelection=IDM_BKGND_WHITE;
HMENU hMenu;

switch (message)
{
case WM_COMMAND:
hMenu=GetMenu(hwnd);
switch(LOWORD(wParam))
{
case IDM_FILE_NEW:
case IDM_FILE_OPEN:
case IDM_FILE_SAVE:
case IDM_FILE_SAVE_AS:
MessageBeep(0);
return 0;
case IDM_APP_EXIT:
SendMessage(hwnd,WM_CLOSE,0,0);
return 0;

case IDM_EDIT_UNDO:
case IDM_EDIT_CUT:
case IDM_EDIT_COPY:
case IDM_EDIT_PASTE:
case IDM_EDIT_CLEAR:
MessageBeep(0);
return 0;

case IDM_BKGND_WHITE:
case IDM_BKGND_LTGRAY:
case IDM_BKGND_GRAY:
case IDM_BKGND_DKGRAY:
case IDM_BKGND_BLACK:

// CheckMenuItem(hMenu,iSelectiom,MF_UNCHECKED);
iSelection=LOWORD(wParam);
// CheckMenuItem(hMenu,iSelectiom,MF_CHECKED);

SetClassLong(hwnd,GCL_HBRBACKGROUND,(LONG)GetStockObject(idColor[LOWORD(wParam)-IDM_BKGND_WHITE]));

InvalidateRect(hwnd,NULL,TRUE);
return 0;

case IDM_TIMER_START:
if(SetTimer(hwnd,ID_TIMER,1000,NULL))
{
EnableMenuItem(hMenu,IDM_TIMER_START,MF_GRAYED);
EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_ENABLED);
}
return 0;

case IDM_TIMER_STOP:
KillTimer(hwnd,ID_TIMER);
EnableMenuItem(hMenu,IDM_TIMER_START,MF_ENABLED);
EnableMenuItem(hMenu,IDM_TIMER_STOP,MF_GRAYED);
return 0;

// case IDM_APP_HELP:
// MessageBox(hwnd,TEXT("Help not yet implemented!"),szAppName,MB_ICONEXCLAMATION ¦MB_OK);
// return 0;
// case IDM_APP_ABOUT:
// MessageBox(hwnd,TEXT("MenuDemonstration Program\n")
// TEXT("(C)Charles Petzold,1998"),
// szAppName,MB_ICONINFORMATION ¦MB_OK);
return 0;
}
break;

case WM_TIMER:
MessageBeep(0);
return 0;

case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
[code=C/C++]
VcGreenhand 2008-01-14
  • 打赏
  • 举报
回复
是不是差头文件需要include啊,你可以去google搜一下需要哪些头文件
@THINK 2008-01-14
  • 打赏
  • 举报
回复
error C2065: 'IDM_BKGND_WHITE' : undeclared identifier

可是我怎么也找不到错的啊 也不知道怎么做了的啊 这只是其中的一个 还有很长一段错误代码 你可以试着调试一下的啊 我实在是不知道怎么搞了的啊 先谢了~~~~~~~~~
rand0941 2008-01-14
  • 打赏
  • 举报
回复
什么错

2,585

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧