有关SetDlgItemInt函数在WM_COMMAND消息下导致程序无响应的问题
源程序:
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =DLGWINDOWEXTRA;
wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor =LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance =hInstance;
wndclass.lpfnWndProc =WndProc;
wndclass.lpszClassName=TEXT("MyWindow");
wndclass.lpszMenuName =NULL;
wndclass.style =CS_VREDRAW|CS_HREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"注册失败!","警告",MB_ICONERROR|MB_OK);
return 0;
}
hwnd=CreateDialog(hInstance,wndclass.lpszClassName ,0,NULL);
ShowWindow(hwnd,nShowCmd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CHAR:
SendMessage(GetDlgItem(hwnd,(int)wParam),BM_SETSTATE,1,0);
Sleep(100);
SendMessage(GetDlgItem(hwnd,(int)wParam),BM_SETSTATE,0,0);
SetDlgItemInt(hwnd,IDC_EDIT,(int)wParam-48,TRUE);
return 0;
case WM_COMMAND:
SetFocus(hwnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
当在WM_COMMAND消息下添加 SetDlgItemInt(hwnd,IDC_EDIT,(int)LOWORD(wParam)-48,TRUE);后运行程序,会出现在点击了按钮以后程序无响应的现象,求解释。
resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by 计算器.rc
//
#define _DC_NUM1 1
#define IDC_NUM1 1
#define IDC_EDIT1 1000
#define IDC_EDIT 1000
#define IDC_NUM2 1002
#define IDC_NUM3 1003
#define IDC_NUM4 1004
#define IDC_NUM5 1005
#define IDC_NUM6 1006
#define IDC_NUM7 1007
#define IDC_NUM8 1008
#define IDC_NUM9 1009
#define IDC_NUM0 1010
#define IDC_PLUS 1011
#define IDC_EQUAL 1012
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1002
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
rc文件里的部分代码:
MYWINDOW DIALOG DISCARDABLE 0, 0, 208, 193
STYLE WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
CLASS "MyWindow"
FONT 10, "System"
BEGIN
EDITTEXT IDC_EDIT,36,24,102,18,ES_AUTOHSCROLL
PUSHBUTTON "1",49,27,52,35,18
PUSHBUTTON "2",50,85,51,35,18
PUSHBUTTON "3",51,145,51,35,18
PUSHBUTTON "4",52,27,83,35,18
PUSHBUTTON "5",53,85,83,35,18
PUSHBUTTON "6",54,145,83,35,18
PUSHBUTTON "7",55,27,119,35,18
PUSHBUTTON "8",56,84,119,35,18
PUSHBUTTON "9",57,144,119,35,18
PUSHBUTTON "0",48,27,151,35,18
PUSHBUTTON "+",IDC_PLUS,84,151,35,18
PUSHBUTTON "=",IDC_EQUAL,145,150,35,18
END
[解决办法]
改成PostMessage(hWnd, WM_SETTEXT, ...)试试?
[解决办法]
我也在学习sdk
case WM_CHAR:
SendMessage(GetDlgItem(hwnd,(int)wParam),BM_SETSTATE,1,0);
Sleep(100);
GetDlgItem第二个参数似乎是dlgitem的id吧,和wparam有关系?
WM_CHAR消息的wparam是输入消息的字符码吧
[解决办法]
在WM_CHAR里
TCHAR acInput[4] = {0};
sprintf(acInput, "%c", (TCHAR)wParam);
你就可以看到wm_char的建入字符,然后你再
SetDlgItemInt(hwnd,IDC_EDIT,atoi(acInput),TRUE);