Leonard

In theory, there is no difference between theory and practice. But, in practice, there is.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

最近项目组让新来的小MM开发一个PC端的串口模拟器,我正好也一起学习一下win32编程。

懒得用CreateWindow建立窗口,所以就用VC自带的资源编辑器画Dialog了。

话说这个Dialog和window的编程思想差不多,都是create,show,update,消息队列循环这样一个流程。所以也就习惯了。

但是今天晚上还是碰到了一些麻烦,我的Dialog程序关闭后窗口消失了,但是进程管理器显示进程并未被杀死。

原来是在消息处理函数里少了如下语句:

case WM_DESTROY:
        PostQuitMessage(
0);

        return TRUE;  

另外还有一个小小的收获:

发生上述错误是因为codeblocks自动生成的代码在创建DialogBox使用的是:

int DialogBox(
  HINSTANCE hInstance,  
// handle to application instance
  LPCTSTR lpTemplate,   // identifies dialog box template
  HWND hWndParent,      // handle to owner window
  DLGPROC lpDialogFunc  // pointer to dialog box procedure

);  

而之后我把代码改成了CreateDialog,ShowDialog,UpdateDialog模式的代码。

而关于DialogBox函数和CreateDialog,ShowDialog,UpdateDialog的区别,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 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消息(如果对话框模板设置字体,则也发送WS_VISIBLE消息),使父窗体失效,启动自身的消息循环获取和处理消息。

当对话框调用 EndDialog 函数,DialogBox 销毁对话框,结束消息循环,使父窗体有效,返回被对话框过程调用EndDialog时指定的nResult参数。

(山寨翻译,敬请批评指正) 

posted on 2011-04-02 23:10  Leonard Tse  阅读(9178)  评论(1编辑  收藏  举报