Win32编写窗口程序的步骤

窗口程序的创建步骤:
   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;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值