Win32编写窗口程序的步骤
2013-08-06 11:00
232 查看
窗口程序的创建步骤:
1 定义WinMain入口函数
2 定义窗口处理函数 WindowProc
3 注册窗口类 RegisterClass
4 创建窗口 CreateWindow
5 显示窗口
ShowWindow/UpdateWindow
6 消息循环
GetMessage
TranslateMessage
DisptachMessage
7 消息处理
1 定义WinMain入口函数
2 定义窗口处理函数 WindowProc
3 注册窗口类 RegisterClass
4 创建窗口 CreateWindow
5 显示窗口
ShowWindow/UpdateWindow
6 消息循环
GetMessage
TranslateMessage
DisptachMessage
7 消息处理
#include "stdafx.h" #include "stdio.h" HINSTANCE g_hInstance = 0;//接收应用程序实例句柄 //主窗口处理函数 LRESULT CALLBACK WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { switch( nMsg ) { case WM_DESTROY: PostQuitMessage( 0 );//可以是GetMessage返回0?? break; } return DefWindowProc( hWnd, nMsg, wParam, lParam ); } //注册窗口类 BOOL Register( LPSTR lpClassName, WNDPROC wndproc ) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof( wce ); wce.cbClsExtra = 200; wce.cbWndExtra = 200; wce.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInstance; wce.lpfnWndProc = wndproc; wce.lpszClassName = lpClassName; wce.lpszMenuName = NULL; wce.style = CS_HREDRAW | CS_VREDRAW; ATOM nAtom = RegisterClassEx( &wce ); if ( nAtom==0 ) { return FALSE; } return TRUE; } //创建主窗口 HWND CreateMain( LPSTR lpClassName, LPSTR lpWndName ) { HWND hWnd = CreateWindowEx( 0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL ); return hWnd; } //显示窗口 void Display( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } //消息循环 void Message( ) { MSG nMsg = { 0 }; while( GetMessage( &nMsg, NULL, 0, 0 ) ) { TranslateMessage( &nMsg ); DispatchMessage( &nMsg ); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; if( !Register( "Main", WndProc ) ) { MessageBox( NULL, "注册失败", "Error", MB_OK ); return 0; } HWND hWnd = CreateMain( "Main", "WINDOW" ); Display( hWnd ); Message( ); return 0; }
相关文章推荐
- win32窗口程序开发步骤详解
- 用 C 语言编写 Windows 服务程序的五个步骤
- VC++编写交互程序时将打印结果输出到cmd窗口显示
- 使用WIN32 API CreateProcess()以无窗口方式创建DOS程序
- Win32汇编教程四 编写一个简单的窗口
- 第三章 分析jdbc程序的编写步骤和原理
- win32窗口程序建立用户界面线程
- 用Qt Creator编写一个简单的窗口程序
- Win32汇编--开始了解窗口程序
- Win32多窗口程序的创建
- 32位汇编第二讲,编写窗口程序,加载资源,响应消息,以及调用C库函数
- 手动编写最简单win32窗口
- 用VS2012或VS2013在win7下编写的程序在XP下运行就出现“不是有效的win32应用程序
- 用C语言编写Windows服务程序的五个步骤
- 【实验 1-1】编写一个简单的 TCP 服务器和 TCP 客户端程序。程序均为控制台程序窗口。
- win32 窗口程序实现控制台信息跟踪 简单案例
- 用 C 语言编写 Windows 服务程序的五个步骤(附源代码)
- win32下将控制台程序改为窗口程序
- 怎样编写一个类Spy++的搜索窗口程序
- 编写Linux系统下Daemon程序的方法步骤