各位大虾好。我是第一次编程。编译器告诉我有一个 error! 为什么?代码如下:
(在消息处理中,EnumWindowsProc的调用被注释掉了。如果不注释掉,我的这种写法对吗?
/*-----------------------------------------
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998 -----------------------------------------*/#include <windows.h>#define ID_TIMER 1LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Beeper1") ;
static TCHAR MsgShown[] = TEXT ("OK")
static TCHAR MsgPop[] = TEXT ("NT Needed!")
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 = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, MsgPop,
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
SetTimer (hwnd, ID_TIMER, 3000, NULL) ;
// 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)
{
LPARAM NoValue;
switch (message)
{
case WM_TIMER :
/* EnumWindows(EnumWindowsProc, // callback function
NoValue // application-defined value
); */
MessageBox (NULL, MsgShown, szAppName, MB_ICONERROR); return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8); switch (theCaption)
{
case *theCaption == TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
// break; default:
return true;
}
(在消息处理中,EnumWindowsProc的调用被注释掉了。如果不注释掉,我的这种写法对吗?
/*-----------------------------------------
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998 -----------------------------------------*/#include <windows.h>#define ID_TIMER 1LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Beeper1") ;
static TCHAR MsgShown[] = TEXT ("OK")
static TCHAR MsgPop[] = TEXT ("NT Needed!")
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 = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, MsgPop,
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
SetTimer (hwnd, ID_TIMER, 3000, NULL) ;
// 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)
{
LPARAM NoValue;
switch (message)
{
case WM_TIMER :
/* EnumWindows(EnumWindowsProc, // callback function
NoValue // application-defined value
); */
MessageBox (NULL, MsgShown, szAppName, MB_ICONERROR); return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8); switch (theCaption)
{
case *theCaption == TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
// break; default:
return true;
}
#define ID_TIMER不好的,
ID_TIMER应该是个无符号整形变量。
我知道了,ID_TIMER的定义怎么没有内容呢?另外,使用
#define ID_TIMER不好的,
ID_TIMER应该是个无符号整形变量。
======怎么没有呢? 是 1 嘛
// UpdateWindow (hwnd) ;
问什么不要这两句?
// UpdateWindow (hwnd) ;
问什么不要这两句?
static TCHAR MsgShown[] = TEXT ("OK")
static TCHAR MsgPop[] = TEXT ("NT Needed!")
掉了分号static TCHAR szAppName[] = TEXT ("Beeper1") ;
static TCHAR MsgShown[] = TEXT ("OK");
static TCHAR MsgPop[] = TEXT ("NT Needed!");
是三个局部变量怎么可以在别的函数中使用 LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8);switch (theCaption)
{
case *theCaption == TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
// break;default:
return true;这里掉了一个"}"号,而且switch怎么可以这样用????不懂,实在不懂
还有如果你把
// ShowWindow (hwnd, iCmdShow) ;
// UpdateWindow (hwnd) ;
屏蔽了,你的程序只要在98下运行,就会一秒钟弹一个消息框,关都不知道
在哪里关!!
天哪!!
我的机器已经被消息框堆满了,给分!
不然跟你没完!!!!!
如同前面朋友说的一样,少分号,
case语句用法错误
case *theCaption == TEXT("myWindow"):
这一语句还是有两个错误:(编译器说:)error C2046: illegal case
error C2446: '==' : no conversion from 'char *' to 'int'我想表达的是,看 theCaption(指针) 所指向的字符串是否等于 'MyWindow",这样写法对吗?另外,金黄兄说三个是局部变量,是否是由于他们是在 winMain 函数体中定义。我现在把他们放在所有函数的外面来定义,可以了吗?
/*-----------------------------------------
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998 -----------------------------------------*/#include <windows.h>#define ID_TIMER 1LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
static TCHAR szAppName[] = TEXT ("Beeper1") ;
static TCHAR MsgShown[] = TEXT ("OK");
static TCHAR MsgPop[] = TEXT ("NT Needed!");int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR 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 = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, MsgPop,
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
SetTimer (hwnd, ID_TIMER, 3000, NULL) ;
// 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)
{
//LPARAM NoValue;
switch (message)
{
case WM_TIMER :
/* EnumWindows(EnumWindowsProc, // callback function
NoValue // application-defined value
); */
MessageBox (NULL, MsgShown, szAppName, MB_ICONERROR); return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8); switch (*theCaption)
{
case *theCaption == TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
// break; default:
return true;
}
}
操作所以就干脆屏蔽调了!/*-----------------------------------------
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998 -----------------------------------------
#include <windows.h>#define ID_TIMER 1static TCHAR szAppName[] = TEXT ("Beeper1") ;
static TCHAR MsgShown[] = TEXT ("OK");
static TCHAR MsgPop[] = TEXT ("NT Needed!");LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR 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 = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, MsgPop,
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
SetTimer (hwnd, ID_TIMER, 3000, NULL) ;
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)
{
// LPARAM NoValue;
switch (message)
{
case WM_TIMER :
/* EnumWindows(EnumWindowsProc, // callback function
NoValue // application-defined value
); */
MessageBox (NULL, MsgShown, szAppName, MB_ICONERROR); return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
// LPTSTR theCaption;
// GetWindowText(hwnd, theCaption,8);// switch (theCaption)
// {
// case *theCaption == TEXT("myWindow"):
// SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
// return false;
// break;// default:
// return true;
// }
return true;
}
strcmp(const char* str1,const char* str2);
函数,如果两个函数相等就返回0,如果第一个字串大于第二个字串
就返回>0,如果第一个字串小于第二个字串就返回<0。 char theCaption[64] = "";
GetWindowText(hwnd, theCaption,sizeof(theCaption)); if(strcmp(theCaption,"myWindow")==0)
{
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
}
return true;你的代码还有一个错误
LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8);这样做编译最多只会给你一个编译警告,但是如果执行程序就会出现错误!!
你只定义了一个指针,这个指针指向任意地方,如果想把把窗口标题放到这个指针
指向的地方就会引发错误。
“已知该窗口 的 Caption 的开头八个字符是固定的”
是不是说这个窗口标题除了开头的“myWindow”还有别的内容,
如果是这样就不能用字符串比较的方法,而只能用内存比较函数内存比较函数原型是
memcmp(const void *p1,const void *p2,size_t s);具体用法如下
if(memcmp(theCaption,"myWindow",8)==0)
{
…………
}
…………
theCaption= 0 //初始化指针
GetWindowText(hwnd, theCaption,8); switch (theCaption)
{
case *theCaption == TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
break; default:
// 省略
}
一个逻辑表达式是不是不行.
{
case TEXT("myWindow"):
/// 对吗?
case中只能够用整形常量呵呵,还有你的指针初始化没有用,还是要出错,
指针初始化应该是分配一块内存然后把指针指向
这块内存,比如
theCaption = new char[32];
……
……
在使用完后必须要释放申请的内存
delete theCaption;
如果不释放就会出现内存泄漏。
///......
///在 EnumWindowsProc 函数中加入:LPTSTR theCaption = 0 //初始化指针
GetWindowText(hwnd, theCaption,8); switch (*theCaption)
{
case TEXT("myWindow"):
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return false;
break; default:
// 省略
}
////....
(先不管 Case 后面是否应用 常整的问题,我去看看书先)
Compiling...
mysdk.cpp
D:\MYVC\mysdk\mysdk.cpp(223) : error C2065: 'MsgShown' : undeclared identifier
D:\MYVC\mysdk\mysdk.cpp(223) : error C2065: 'szAppName' : undeclared identifier
D:\MYVC\mysdk\mysdk.cpp(244) : error C2450: switch expression of type 'char *' is illegal
Integral expression required
D:\MYVC\mysdk\mysdk.cpp(245) : error C2446: '==' : no conversion from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\MYVC\mysdk\mysdk.cpp(245) : error C2040: '==' : 'int' differs in levels of indirection from 'char [9]'
D:\MYVC\mysdk\mysdk.cpp(245) : error C2051: case expression not constant
D:\MYVC\mysdk\mysdk.cpp(252) : warning C4065: switch statement contains 'default' but no 'case' labels
Error executing cl.exe.mysdk.exe - 6 error(s), 1 warning(s)
调试信息如上
BEEPER1.C -- Timer Demo Program No. 1
(c) Charles Petzold, 1998-----------------------------------------*/#include <windows.h>
#include "string.h"
#define ID_TIMER 1LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
TCHAR szAppName[] = TEXT ("Beeper1") ;
TCHAR MsgShown[] = TEXT ("OK");
TCHAR MsgPop[] = TEXT ("NT Needed!");int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR 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 = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, MsgPop,
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
SetTimer (hwnd, ID_TIMER, 3000, NULL) ;
// 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)
{
// LPARAM NoValue;
switch (message)
{
case WM_TIMER :
/* EnumWindows(EnumWindowsProc, // callback function
NoValue // application-defined value
); */
MessageBox (NULL, MsgShown, szAppName, MB_ICONERROR);
return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}BOOL CALLBACK EnumWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
LPTSTR theCaption;
GetWindowText(hwnd, theCaption,8);
if(strcmp(theCaption,"myWindow")==1)
{
SendNotifyMessage(hwnd, WM_KEYDOWN, VK_F5, 0x1);
return FALSE;
// break;
}
else
return TRUE;
}