找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3169|回复: 3

关于在DialogBox 创建的窗口的处理函数中使用MesageBox的问题

[复制链接]

17

主题

28

回帖

590

积分

用户组: 大·技术宅

UID
140
精华
5
威望
30 点
宅币
440 个
贡献
26 次
宅之契约
0 份
在线时间
54 小时
注册时间
2014-3-22
发表于 2014-11-14 13:03:47 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×


QQ截图20141114130131.png

MessageBox 弄出来的 提示框根本没有反应,关都关不了,好像这个提示框永远无法获取焦点似的

  1. // ghhg.cpp : Defines the entry point for the application.
  2. //

  3. #include "stdafx.h"
  4. #include "resource.h"

  5. #define MAX_LOADSTRING 100

  6. // Global Variables:
  7. HINSTANCE hInst;                                                                // current instance
  8. TCHAR szTitle[MAX_LOADSTRING];                                                                // The title bar text
  9. TCHAR szWindowClass[MAX_LOADSTRING];                                                                // The title bar text

  10. // Foward declarations of functions included in this code module:
  11. ATOM                                MyRegisterClass(HINSTANCE hInstance);
  12. BOOL                                InitInstance(HINSTANCE, int);
  13. LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
  14. LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
  15. BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
  16. int APIENTRY WinMain(HINSTANCE hInstance,
  17.                      HINSTANCE hPrevInstance,
  18.                      LPSTR     lpCmdLine,
  19.                      int       nCmdShow)
  20. {
  21.         // TODO: Place code here.
  22.         MSG msg;
  23.         HACCEL hAccelTable;

  24.         // Initialize global strings
  25.         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  26.         LoadString(hInstance, IDC_GHHG, szWindowClass, MAX_LOADSTRING);
  27.         MyRegisterClass(hInstance);

  28.         // Perform application initialization:
  29.         if (!InitInstance (hInstance, nCmdShow))
  30.         {
  31.                 return FALSE;
  32.         }

  33.         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_GHHG);

  34.         // Main message loop:
  35.         while (GetMessage(&msg, NULL, 0, 0))
  36.         {
  37.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  38.                 {
  39.                         TranslateMessage(&msg);
  40.                         DispatchMessage(&msg);
  41.                 }
  42.         }

  43.         return msg.wParam;
  44. }



  45. //
  46. //  FUNCTION: MyRegisterClass()
  47. //
  48. //  PURPOSE: Registers the window class.
  49. //
  50. //  COMMENTS:
  51. //
  52. //    This function and its usage is only necessary if you want this code
  53. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  54. //    function that was added to Windows 95. It is important to call this function
  55. //    so that the application will get 'well formed' small icons associated
  56. //    with it.
  57. //
  58. ATOM MyRegisterClass(HINSTANCE hInstance)
  59. {
  60.         WNDCLASSEX wcex;

  61.         wcex.cbSize = sizeof(WNDCLASSEX);

  62.         wcex.style                        = CS_HREDRAW | CS_VREDRAW;
  63.         wcex.lpfnWndProc        = (WNDPROC)WndProc;
  64.         wcex.cbClsExtra                = 0;
  65.         wcex.cbWndExtra                = 0;
  66.         wcex.hInstance                = hInstance;
  67.         wcex.hIcon                        = LoadIcon(hInstance, (LPCTSTR)IDI_GHHG);
  68.         wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
  69.         wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
  70.         wcex.lpszMenuName        = (LPCSTR)IDC_GHHG;
  71.         wcex.lpszClassName        = szWindowClass;
  72.         wcex.hIconSm                = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

  73.         return RegisterClassEx(&wcex);
  74. }

  75. //
  76. //   FUNCTION: InitInstance(HANDLE, int)
  77. //
  78. //   PURPOSE: Saves instance handle and creates main window
  79. //
  80. //   COMMENTS:
  81. //
  82. //        In this function, we save the instance handle in a global variable and
  83. //        create and display the main program window.
  84. //
  85. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  86. {
  87.    HWND hWnd;

  88.    hInst = hInstance; // Store instance handle in our global variable

  89.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  90.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  91.    if (!hWnd)
  92.    {
  93.       return FALSE;
  94.    }

  95.    ShowWindow(hWnd, nCmdShow);
  96.    UpdateWindow(hWnd);

  97.    return TRUE;
  98. }

  99. //
  100. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  101. //
  102. //  PURPOSE:  Processes messages for the main window.
  103. //
  104. //  WM_COMMAND        - process the application menu
  105. //  WM_PAINT        - Paint the main window
  106. //  WM_DESTROY        - post a quit message and return
  107. //
  108. //
  109. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  110. {
  111.         int wmId, wmEvent;
  112.         PAINTSTRUCT ps;
  113.         HDC hdc;
  114.         TCHAR szHello[MAX_LOADSTRING];
  115.         LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

  116.         switch (message)
  117.         {
  118.         case WM_CREATE:
  119.                 {
  120.                         CreateWindow(_T("BUTTON"),_T("1"),WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,0,0,100,100,hWnd,(HMENU)0xFFFF,NULL,NULL);

  121.                         break;
  122.                 }
  123.                 case WM_COMMAND:
  124.                         wmId    = LOWORD(wParam);
  125.                         wmEvent = HIWORD(wParam);
  126.                         // Parse the menu selections:
  127.                         switch (wmId)
  128.                         {
  129.                                 case IDM_ABOUT:
  130.                                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  131.                                    break;
  132.                                 case IDM_EXIT:
  133.                                    DestroyWindow(hWnd);
  134.                                    break;
  135.                                 case 0xFFFF:
  136.                                         {
  137.                                                 //MessageBox(hWnd,_T("1"),_T("10"),MB_OK);
  138.                                                 DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DialogProc);
  139.                                                 break;
  140.                                         }
  141.                                 default:
  142.                                    return DefWindowProc(hWnd, message, wParam, lParam);
  143.                         }
  144.                         break;
  145.                 case WM_PAINT:
  146.                         hdc = BeginPaint(hWnd, &ps);
  147.                         // TODO: Add any drawing code here...
  148.                         RECT rt;
  149.                         GetClientRect(hWnd, &rt);
  150.                         DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  151.                         EndPaint(hWnd, &ps);
  152.                         break;
  153.                 case WM_DESTROY:
  154.                         PostQuitMessage(0);
  155.                         break;
  156.                 default:
  157.                         return DefWindowProc(hWnd, message, wParam, lParam);
  158.    }
  159.    return 0;
  160. }

  161. // Mesage handler for about box.
  162. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  163. {
  164.         switch (message)
  165.         {
  166.                 case WM_INITDIALOG:
  167.                                 return TRUE;

  168.                 case WM_COMMAND:
  169.                         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  170.                         {
  171.                                 EndDialog(hDlg, LOWORD(wParam));
  172.                                 return TRUE;
  173.                         }
  174.                         break;
  175.         }
  176.     return FALSE;
  177. }

  178. BOOL CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  179. {
  180.         switch(message)
  181.         {
  182.         case WM_COMMAND:
  183.                 {
  184.                         int Id = LOWORD(wParam);

  185.                         int Event = HIWORD(wParam);

  186.                         switch(Id)
  187.                         {
  188.                         case IDOK:
  189.                                 {
  190.                                         MessageBox(NULL,_T("1"),_T("111"),0);
  191.                                         break;
  192.                                 }
  193.                         }

  194.                         break;
  195.                 }
  196.         default:return DefWindowProc(hDlg,message,wParam,lParam);
  197.                

  198.         }
  199. }
复制代码



回复

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24241 个
贡献
46222 次
宅之契约
0 份
在线时间
2297 小时
注册时间
2014-1-26
发表于 2014-11-14 14:03:59 | 显示全部楼层
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是“灰色”的,因此也不能处理消息。
网上流传的解决方式是借助多线程。我感觉那种方法还可以。
回复 赞! 靠!

使用道具 举报

0

主题

31

回帖

109

积分

用户组: 小·技术宅

UID
141
精华
0
威望
2 点
宅币
74 个
贡献
0 次
宅之契约
0 份
在线时间
10 小时
注册时间
2014-3-23
发表于 2014-11-14 15:48:49 | 显示全部楼层
哥们 回去先多看看书吧 初学者也不该犯这错误 然后来坛子发问
DialogBox宏展开是DialogBoxParam 也就是创建模态对话框 窗口过程你写的是这个DialogProc
哪本书告诉你 对话框过程的default 写成这样的 return DefWindowProc(hDlg,message,wParam,lParam);

正确应该是处理过的消息返回TRUE 没处理过的default返回FALSE...
我没看懂问题 =。= 不过貌似应该说得是这个...
回复 赞! 靠!

使用道具 举报

1111

主题

1651

回帖

7万

积分

用户组: 管理员

一只技术宅

UID
1
精华
244
威望
743 点
宅币
24241 个
贡献
46222 次
宅之契约
0 份
在线时间
2297 小时
注册时间
2014-1-26
发表于 2014-11-14 16:06:12 | 显示全部楼层
我晕 发表于 2014-11-14 15:48
哥们 回去先多看看书吧 初学者也不该犯这错误 然后来坛子发问
DialogBox宏展开是DialogBoxParam 也就是创建 ...

是的,这也是一个问题。不过有人能在论坛提问还是很不错的,至少能活跃论坛的气氛。
你说的也是对的,而且我估计要是照你所说修改代码的话,也许!也许能解决问题。因为我从来就没有遇到过这种问题。我觉得很可能是DefWindowProc引起的问题。
回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-25 01:43 , Processed in 0.037445 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表