#2
天使梦魔2015-04-08 15:32
|
程序代码:
#include <windows.h>
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK BoxDlgProc(HWND, UINT, WPARAM, LPARAM) ;
HWND hDlgModeless;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
LPCWSTR szAppName = TEXT("Russian_Box");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style= CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc= WndProc;
wndclass.cbClsExtra= 0 ;
wndclass.cbWndExtra= 0 ;
wndclass.hInstance= hInstance;
wndclass.hIcon= LoadIcon(hInstance, szAppName) ;
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, TEXT("RUSSIAN_BOX"),
WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, szCmdLine) ;
ShowWindow(hwnd, iCmdShow) ;
UpdateWindow(hwnd) ;
hDlgModeless= CreateDialog(hInstance, TEXT("Russian_Box"), hwnd, BoxDlgProc) ;
while (GetMessage(&msg, NULL, 0, 0))
{
if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
SetFocus (hDlgModeless);
return 0;
case WM_DESTROY:
DestroyWindow(hDlgModeless);
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK BoxDlgProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
return TRUE;
case WM_KILLFOCUS:
SetFocus (hDlg);
return TRUE;
case WM_KEYDOWN:
return TRUE;
case WM_CHAR:
return TRUE;
}
return FALSE;
}
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK BoxDlgProc(HWND, UINT, WPARAM, LPARAM) ;
HWND hDlgModeless;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
LPCWSTR szAppName = TEXT("Russian_Box");
MSG msg;
HWND hwnd;
WNDCLASS wndclass;
wndclass.style= CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc= WndProc;
wndclass.cbClsExtra= 0 ;
wndclass.cbWndExtra= 0 ;
wndclass.hInstance= hInstance;
wndclass.hIcon= LoadIcon(hInstance, szAppName) ;
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, TEXT("RUSSIAN_BOX"),
WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, szCmdLine) ;
ShowWindow(hwnd, iCmdShow) ;
UpdateWindow(hwnd) ;
hDlgModeless= CreateDialog(hInstance, TEXT("Russian_Box"), hwnd, BoxDlgProc) ;
while (GetMessage(&msg, NULL, 0, 0))
{
if (hDlgModeless == 0 || !IsDialogMessage (hDlgModeless, &msg))
{
TranslateMessage(&msg) ;
DispatchMessage(&msg) ;
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
SetFocus (hDlgModeless);
return 0;
case WM_DESTROY:
DestroyWindow(hDlgModeless);
PostQuitMessage(0) ;
return 0 ;
}
return DefWindowProc(hwnd, message, wParam, lParam) ;
}
BOOL CALLBACK BoxDlgProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_SETFOCUS:
return TRUE;
case WM_KILLFOCUS:
SetFocus (hDlg);
return TRUE;
case WM_KEYDOWN:
return TRUE;
case WM_CHAR:
return TRUE;
}
return FALSE;
}
问题1:这个非模态对话框不能对 VK_LEFT、VK_UP、VK_DOWN....等几个键盘按键产生WM_KEYDOWN消息,应该是 IsDialogMessage() 导致的,那么问题来了,
我怎么样才能在按下键盘 方向键 的时候,使对话框处理程序收到 WM_KEYDOWN 消息????
问题2:我们知道TranslateMessage ()处理 WM_KEYDOWN 产生相应的 WM_CHAR 消息,从而在 WM_CHAR 消息中可以得到按下的是哪一个键,但是非模态对话框
的消息传递 通过 IsDialogMesage () , 并不经过 TranslateMessage() 处理,经实践,非模态对话框消息处理程序也不处理 WM_CHAR 消息(应该是收不到
这个消息),那么问题又来了, 非模态对话框怎么才能收到 WM_CHAR 消息????
问题3:这个是对模态对话框的困惑,创建模态对话框资源时,若加上 WS_CHILD 类型, 程序就卡死,我想问模态对话框是不是不可以带 WS_CHILD 类型???
(全是API调用的疑问, 不涉及MFC, 请尽量用 API 解答,谢谢各位大神了, 本人分不多,这点要sorry了)