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

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

 

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

对话框例子

 

 


一些说明
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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值