在Win32控制台程序中添加Windows窗口

来源:互联网 发布:sql 多个结果 编辑:程序博客网 时间:2024/05/16 17:29

控制台程序和窗口程序的区别就在于窗口程序有一个消息循环, 处理窗口事件。本文给出了在控制台程序中使用窗口的代码例子。

 

******************************************************************************

对话框例子

 

 


一些说明
1. 需要多线程库支持

#define _MT

请看如下MSDN上关于 /MT和_MT的解释
/MT
Causes your application to use the multithread, static version of the run-time library. Defines _MT and causes the compiler to place the library name LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to resolve external symbols.

2. 创建窗口
HWND hWnd = ::CreateDialog(NULL,MAKEINTRESOURCE(IDD_DIALOG1),NULL,NULL);
if ( hWnd!=NULL )
    {
        // show dialog
        ::ShowWindow(hWnd,SW_SHOW);
    }
创建对话框, 使用VS资源编辑器创建一个对话框, ID=IDD_DIALOG1

3. 接收和处理消息
MSG msg;
while(1)  {
::PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)
}
PeekMessage: Dispatches incoming sent messages, checks the thread message queue for a posted message, and retrieves the message (if any exist).

 

******************************************************************************

不用对话框, 用常规方法创建窗口.

1. 定义窗口, 注册窗口, 注意这里在定义窗口时, 使用了默认的消息处理函数  wndclass.lpfnWndProc   = DefWindowProc ;

2. 创建窗口

3. 显示, 更新窗口

4. 消息循环

 

参考: http://www.codeproject.com/KB/winsdk/winconsole.aspx

原创粉丝点击