user-0 提问: 消息处理  key……事件

回复:
user: user-1
content: VC6：  Setting a Breakpoint on a Message Received by a Windows Function You can set message breakpoints to stop the debugger when a particular WndProc function message is received.  Note   Message breakpoints work only on x86- or Pentium-based systems.  To set a breakpoint on a message   From the Edit menu, click Breakpoints.  The Breakpoints dialog box appears.  Click the Messages tab.   In the Break At WndProc text box, type the name of the Windows function.  If you are setting a breakpoint during a debug session, the list contains the exported functions in your project.   In the Set One Breakpoint For Each Message To Watch list box, select the message.   To set another breakpoint, press ENTER, and then repeat steps 3 and 4.  The Breakpoints list displays the currently active breakpoints.  Click OK to set the breakpoints.  Note If you interrupt execution while Windows or other system code is running, the results can be unpredictable.

user: user-0
content: 是我太贪心了，我想几天搞完api就学mfc，所以没看书只看视频，以前学习也从来没看过书，看来我得好好改正了

user: user-2
content: 这是七个数组：     char key[][30]={         "WM_KEYUP",         "WM_CHAR",         "WM_DEADCHAR",         "WM_SYSKEYDOWN",         "WM_SYSKEYUP",         "WM_SYSCHAR",         "WM_SYSDEADCHAR",     }; 这是八个下标      case WM_KEYDOWN:     case WM_KEYUP:     case WM_CHAR:     case WM_DEADCHAR:     case WM_SYSKEYDOWN:     case WM_SYSKEYUP:     case WM_SYSCHAR:     case WM_SYSDEADCHAR:  key[uMsg-WM_KEYFIRST] 这个下标计算把WM_KEYDOWN去掉了。自己改下这个逻辑

user: user-2
content: 真心的，楼主去看一下《windows 程序设计》这本书再来提问好吗？你问的基础知识书本都有写的，为什么不去看呢？

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:建议楼回族学习使用Spy++跟踪消息。 看到了很多窗口，怎么跟踪消息呢？

user: user-0
content: 引用 6 楼 user-3 的回复:sprintf(temp,"LCONTROL+%c",wParam); 改成 sprintf(temp,"LCONTROL+%c",wParam+('a'-1));  为什么呢

user: user-3
content: sprintf(temp,"LCONTROL+%c",wParam); 改成 sprintf(temp,"LCONTROL+%c",wParam+('a'-1));

user: user-1
content: 建议楼回族学习使用Spy++跟踪消息。

user: user-0
content: 为了验证char消息中wParam代表字符的ASC码，我改了程序，但按下左ctrl再按其他字母显示的都是ctrl+l,求正解 #include <Windows.h> #include <stdio.h>  int i=0; LRESULT CALLBACK WinSunProc( 	HWND hwnd,      // handle to window 	UINT uMsg,      // message identifier 	WPARAM wParam,  // first message parameter 	LPARAM lParam   // second message parameter 	); #define WM_MYMESSAGE WM_USER+1    int WINAPI WinMain( 	HINSTANCE hInstance,  // handle to current instance 	HINSTANCE hPrevInstance,  // handle to previous instance 	LPSTR lpCmdLine,      // pointer to command line 	int nCmdShow          // show state of window 	) {	 	//创建一个窗口类 	WNDCLASS wndcls; 	 	 	//额外参数 	wndcls.cbClsExtra=0; 	wndcls.cbWndExtra=0; 	//背景色 	wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); 	//光标 	wndcls.hCursor=LoadCursor(NULL,IDC_ARROW); 	//图标 	wndcls.hIcon=LoadIcon(NULL,IDI_SHIELD); 	//应用程序实例号 	wndcls.hInstance=hInstance; 	//窗口过程函数 	wndcls.lpfnWndProc=WinSunProc; 	//类名 	//#include <tchar.h>	更多信息： http://bbs.csdn.net/topics/390631479?page=1 	wndcls.lpszClassName="weixin"; 	//菜单名 	wndcls.lpszMenuName=NULL; 	//风格 	wndcls.style=CS_HREDRAW|CS_VREDRAW;//水平重画 或上 垂直重画  	//注册 	RegisterClass(&wndcls);  	//创建窗口 	/*参考信息： 	CreateWindow 	该函数创建一个重叠式窗口、弹出式窗口或子窗口。它指定窗口类 	，窗口标题，窗口风格，以及窗口的初始位置及大小（可选的）。 	函数也指该窗口的父窗口或所属窗口（如果存在的话），及窗口的 	菜单。若要使用除CreateWindow函数支持的风格外的扩展风格，则 	使用CreateWindowEx函数代替CreateWindow函数。*/ 	HWND hwnd; 	hwnd=CreateWindow("weixin","北京维新科学技术中心",WS_OVERLAPPEDWINDOW,100, 		100,600,400,NULL,NULL,hInstance,NULL);  	//显示窗口 	ShowWindow(hwnd,SW_NORMAL);  	//显示欢迎信息 	HDC hdc; 	hdc=GetDC(hwnd); 	TextOut(hdc,200,0,"欢迎使用本程序！",strlen("欢迎使用本程序！")); 	ReleaseDC(hwnd,hdc);  	//更新窗口 	UpdateWindow(hwnd);   	  	//消息循环 	MSG msg; 	while(GetMessage(&msg,NULL,0,0)) 	{ 		TranslateMessage(&msg); 		DispatchMessage(&msg); 	}  	return 0; }  LRESULT CALLBACK WinSunProc( 	HWND hwnd,      // handle to window 	UINT uMsg,      // message identifier 	WPARAM wParam,  // first message parameter 	LPARAM lParam   // second message parameter 	) {	 	HDC hdc; 	//char ch[30]; 	//LOGFONT lf={0}; 	//CHOOSEFONT cf={0}; 	//POINT pt[3]; 	//RECT r={23,56,34,89};//矩形区域 	 	 	 	char temp[30]=""; 	char str[30]=""; 	char key[][30]={ 		"WM_KEYDOWN", 		"WM_KEYUP", 		"WM_CHAR", 		"WM_DEADCHAR", 		"WM_SYSKEYDOWN", 		"WM_SYSKEYUP", 		"WM_SYSCHAR", 		"WM_SYSDEADCHAR", 	};  	switch(uMsg) 	{	 	case WM_CHAR: 		if(GetKeyState(VK_LCONTROL)<0)//判断左ctrl是否按下 		{	 			hdc=GetDC(hwnd); 			sprintf_s(temp,30,"LCONTROL+%c",wParam); 			TextOut(hdc,0,(i++)*20,temp,strlen(temp)); 			ReleaseDC(hwnd,hdc); 		} 		break; 		//与字符有关的消息 	//case WM_KEYDOWN: 	//	/*if(wParam==VK_LEFT) 	//	sprintf_s(temp,10,"left key"); 	//	else if(wParam==VK_RIGHT) 	//	sprintf_s(temp,10,"right key"); 	//	else if(wParam==VK_UP) 	//	sprintf_s(temp,10,"up key"); 	//	else if(wParam==VK_DOWN) 	//	sprintf_s(temp,10,"down key"); 	//	hdc=GetDC(hwnd); 	//	TextOut(hdc,0,0,temp,10); 	//	ReleaseDC(hwnd,hdc); 	//	break;*/ 	//case WM_KEYUP: 	//case WM_CHAR: 	//case WM_DEADCHAR: 	//case WM_SYSKEYDOWN: 	//case WM_SYSKEYUP: 	//case WM_SYSCHAR: 	//case WM_SYSDEADCHAR: 	//	GetKeyNameText(lParam,temp,10); 	//	sprintf_s(str,30,"%s********%d:%s", 	//		key[uMsg-WM_KEYFIRST],wParam,temp);  	//	hdc=GetDC(hwnd);  	//	TextOut(hdc,0,20*(i++),str,strlen(str));  	//	ReleaseDC(hwnd,hdc); 	//	break; 	//case WM_RBUTTONDOWN: 	//	//字体与颜色，windows api,参考：WINDOWS API 刘思成 in E盘 	//	cf.lStructSize=sizeof(CHOOSEFONT); 	//	cf.lpLogFont=&lf; 	//	cf.Flags=CF_SCREENFONTS|CF_EFFECTS;//|CF_INITTOLOGFONTSTRUCT;(用lf的信息初始化) 	//	ChooseFont(&cf); 	//	hdc=GetDC(hwnd); 	//	SelectObject(hdc,CreateFontIndirect(cf.lpLogFont)); 	//	TextOut(hdc,50,300,"北京维新科学技术中心",strlen("北京维新科学技术中心")); 	//	 	//	//从(50,50)划线到(30,30) 	//	MoveToEx(hdc,50,50,NULL);//省略此句从(0,0)到(30,30) 	//	LineTo(hdc,30,30); 	//	 	//	pt[0].x=40; 	//	pt[0].y=40; 	//	pt[1].x=90; 	//	pt[1].y=40; 	//	pt[2].x=130; 	//	pt[2].y=20; 	//	Polyline(hdc,pt,3);//polylineto()折线 	//	Arc(hdc,20,100,40,300,40,100,40,100);//弧 	//	Rectangle(hdc,20,100,40,300);//矩形 	//	Ellipse(hdc,20,100,40,150);//椭圆  	//	//使用系统画笔 	//	/*GetStockObject() 	//		SelectObject() 	//		DeleteObject()*/  	//	//创建画笔对象 	//	SelectObject(hdc,CreatePen(PS_DOT,3,RGB(255,255,0))); 	//	SelectObject(hdc,GetStockObject(BLACK_BRUSH));//使用系统画刷 	//	SelectObject(hdc,CreateSolidBrush(RGB(0,255,255)));//创建纯色画刷 	//	Rectangle(hdc,360,130,490,290); 	//	SelectObject(hdc,CreateHatchBrush(HS_DIAGCROSS,RGB(0,255,255)));//创建图案画刷 	//	RoundRect(hdc,30,30,60,60,25,25); 	//	FillRect(hdc,&r,CreateHatchBrush(HS_DIAGCROSS,RGB(0,255,255)));//填充矩形区域 	//	FrameRect(hdc,&r,(HBRUSH)GetStockObject(BLACK_BRUSH));//填充矩形区域边框 	//	InvertRect(hdc,&r);//将矩形区域所有像素（包括边框）翻转 	//	  	//	ReleaseDC(hwnd,hdc); 	//	break; 	/*case WM_MYMESSAGE:		 		sprintf_s(ch,30,"wParam is %d,lParam is %d",wParam,lParam); 		MessageBox(hwnd,ch,"weixin(MOVE的后序触发事件)",0); 		break;*/ 	//case WM_MOVE: 	//	MessageBox(hwnd,"MOVE","weixin",0); 	//	//PostMessage(hwnd,WM_MYMESSAGE,100,200); 	//	break; 		/*case WM_CREATE: 		MessageBox(hwnd,"CREATE","weixin",0); 		break;*/ 	//case WM_SIZE: 	//	 	//	//lParam低16位存放宽度，高16位存放高度，又可表示鼠标坐标 	//	sprintf_s(ch,30,"宽度：%d 高度：%d",LOWORD(lParam),HIWORD(lParam)); 	//	MessageBox(hwnd,ch,"weixin",0); 	//	break; 	/*case WM_CHAR: 		sprintf_s(ch,30,"Char is %d",wParam); 		MessageBox(hwnd,ch,"weixin",0); 		break;*/ 		/*case WM_LBUTTONDOWN: 		MessageBox(hwnd,"左键按下","weixin",0); 		hdc=GetDC(hwnd); 		TextOut(hdc,50,120,"维新培训！",strlen("维新培训！")); 		ReleaseDC(hwnd,hdc); 		break;*/ 	//case WM_PAINT: 	//	PAINTSTRUCT ps; 	//	hdc=BeginPaint(hwnd,&ps);  	//	SelectObject(hdc,GetStockObject(ANSI_VAR_FONT));//选择启用新字体 	//	TextOut(hdc,50,50,"北京维新科学技术中心",strlen("北京维新科学技术中心")); 	//	 	//	TEXTMETRIC tm;//与文字信息相关的一个结构体 	//	GetTextMetrics(hdc,&tm);  	//	//LOGFONT lf={0};写在这里的话初始化由case跳过，error 	//	lf.lfHeight=45; 	//	lf.lfUnderline=1; 	//	SelectObject(hdc,CreateFontIndirect(&lf));  	//	TextOut(hdc,50,50+tm.tmHeight,"北京维新科学技术中心",strlen("北京维新科学技术中心")); 	//	 	//	lf.lfHeight=10; 	//	lf.lfUnderline=1; 	//	SelectObject(hdc,CreateFontIndirect(&lf));  	//	TextOut(hdc,50,50+(-1)*tm.tmHeight,"北京维新科学技术中心",strlen("北京维新科学技术中心"));  	//	SelectObject(hdc,GetStockObject(ANSI_FIXED_FONT));//选择启用新字体 	//	for(int i=0;i<10;i++) 	//	{	 	//		sprintf_s(ch,30,"%d is %c",i+1,'A'+i); 	//		TextOut(hdc,300,150+i*tm.tmHeight/*字体高度*/,ch,strlen((ch))); 	//	} 	//	EndPaint(hwnd,&ps); 	//	break; 	case WM_CLOSE: 		if(IDYES==MessageBox(hwnd,"真的要退出吗？","weixin",MB_YESNO)) 		{ 			DestroyWindow(hwnd); 		} 		break; 	case WM_DESTROY: 		PostQuitMessage(0); 		break; 	default: 		return DefWindowProc(hwnd,uMsg,wParam,lParam); 	} 	return 0; }

user: user-2
content: 你按下一个键"A"会产生三条消息：  WM_KEYDOWN wParam= VK_A 这是键盘虚拟码的a WM_CHAR  wParam = 'a' 这是ascii码的a WM_KEYUP  wParam= VK_A  这就是它们的参数为什么不一样。  在<windows程序设计>这本书里键盘的这一章有详细说明

user: user-0
content: 额，好明显的错误……  谢谢 另外wParam参数表示什么意思喔，为什么这个参数对于keydown和char消息不一样？