关于在DialogBox 创建的窗口的处理函数中使用MesageBox的问题
MessageBox 弄出来的 提示框根本没有反应,关都关不了,好像这个提示框永远无法获取焦点似的
// ghhg.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle; // The title bar text
TCHAR szWindowClass; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_GHHG, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GHHG);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
//FUNCTION: MyRegisterClass()
//
//PURPOSE: Registers the window class.
//
//COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GHHG);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_GHHG;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
//FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//PURPOSE:Processes messages for the main window.
//
//WM_COMMAND - process the application menu
//WM_PAINT - Paint the main window
//WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello;
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
{
CreateWindow(_T("BUTTON"),_T("1"),WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,0,0,100,100,hWnd,(HMENU)0xFFFF,NULL,NULL);
break;
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case 0xFFFF:
{
//MessageBox(hWnd,_T("1"),_T("10"),MB_OK);
DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_COMMAND:
{
int Id = LOWORD(wParam);
int Event = HIWORD(wParam);
switch(Id)
{
case IDOK:
{
MessageBox(NULL,_T("1"),_T("111"),0);
break;
}
}
break;
}
default:return DefWindowProc(hDlg,message,wParam,lParam);
}
}
MSDN的资料:
The DialogBox macro uses the CreateWindowEx function to create the dialog box. DialogBox then sends a WM_INITDIALOG message (and a WM_SETFONT message if the template specifies the DS_SETFONT or DS_SHELLFONT style) to the dialog box procedure. The function displays the dialog box (regardless of whether the template specifies the WS_VISIBLE style), disables the owner window, and starts its own message loop to retrieve and dispatch messages for the dialog box.
When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter specified by the dialog box procedure when it called EndDialog.
翻译过来就是:
DialogBox是个宏,它调用CreateWindowEx来创建对话框。DialogBox发送WM_INITDIALOG消息(如果模板设置了DS_SETFONT或DS_SHELLFONT,它还会发送WM_SETFONT)给对话框消息处理函数。该函数显示对话框(无论你有没有设置WS_VISIBLE),然后把父窗口弄成“灰色”,然后开始它自己的消息循环来处理对话框的消息。
当对话框消息处理程序调用了EndDialog函数,DialogBox销毁对话框,结束消息循环,重新恢复父窗口(如果之前是“灰色”的),然后返回EndDialog的nResult参数。
我感觉MessageBox和DialogBox有类似的机制,估计是消息都让DialogBox处理了,而MessageBox却没有处理消息的机会。而DialogBox是“灰色”的,因此也不能处理消息。
网上流传的解决方式是借助多线程。我感觉那种方法还可以。 哥们 回去先多看看书吧 初学者也不该犯这错误 然后来坛子发问
DialogBox宏展开是DialogBoxParam 也就是创建模态对话框 窗口过程你写的是这个DialogProc
哪本书告诉你 对话框过程的default 写成这样的 return DefWindowProc(hDlg,message,wParam,lParam);
正确应该是处理过的消息返回TRUE 没处理过的default返回FALSE...
我没看懂问题 =。= 不过貌似应该说得是这个... 我晕 发表于 2014-11-14 15:48
哥们 回去先多看看书吧 初学者也不该犯这错误 然后来坛子发问
DialogBox宏展开是DialogBoxParam 也就是创建 ...
是的,这也是一个问题。不过有人能在论坛提问还是很不错的,至少能活跃论坛的气氛。
你说的也是对的,而且我估计要是照你所说修改代码的话,也许!也许能解决问题。因为我从来就没有遇到过这种问题。我觉得很可能是DefWindowProc引起的问题。
页:
[1]