您的位置:首页 > 编程语言 > C语言/C++

Win32编写窗口程序的步骤

2013-08-06 11:00 232 查看
窗口程序的创建步骤:
   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;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 窗口程序