---
title: "WindowsAPI 窗口"
author: "smallearth"
date: "2012-05-15T23:25:00Z"
tags: []
slug: "windowsapi"
draft: false
lastmod: "2013-11-30T20:10:47Z"
id: 1983335
url: "https://smallearth.iteye.com/blog/1983335"
---

好吧 好忙 还得整windows编程 好不容易整出的窗口

#include "StdAfx.h"  
#include &lt;windows.h&gt;  
TCHAR Title\[32]=TEXT("迷宫"); //窗口的标题  
TCHAR WinName\[32]=TEXT("Simple"); //窗口的名称  
ATOM RegisterWin(HINSTANCE);  
BOOL CreatWin(HINSTANCE, int);  
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,  
HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)  
{  
MSG msg;

RegisterWin(hInstance);

if (!CreatWin(hInstance, nCmdShow))  
{  
return FALSE;  
}  
while (GetMessage(&msg, NULL, 0, 0))  
{  
TranslateMessage(&msg);  
DispatchMessage(&msg);  
}

return (int)msg.wParam;  
}

ATOM RegisterWin(HINSTANCE hInstance)  
{

WNDCLASSEX wc;

wc.cbSize= sizeof(WNDCLASSEX);  
wc.style= CS\_HREDRAW | CS\_VREDRAW;  
wc.lpfnWndProc= (WNDPROC)WndProc;  
wc.cbClsExtra= 0;  
wc.cbWndExtra= 0;  
wc.hInstance= hInstance;  
wc.hIcon= LoadIcon(hInstance, (LPCTSTR)IDI\_APPLICATION);  
wc.hCursor = LoadCursor(NULL, IDC\_ARROW);  
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE\_BRUSH);  
wc.lpszMenuName = NULL;  
wc.lpszClassName = WinName;  
wc.hIconSm = LoadIcon(wc.hInstance, (LPCTSTR)IDI\_APPLICATION);

return RegisterClassEx(&wc);  
}

//初始化窗口  
BOOL CreatWin(HINSTANCE hInstance, int nCmdShow)  
{  
HWND hWnd;

//创建窗口

hWnd = CreateWindow( WinName,  
Title,

WS\_OVERLAPPEDWINDOW, //窗口式样

360,  
100,  
600,  
480,  
NULL,  
NULL,  
hInstance,  
NULL);  
if (!hWnd)  
{  
return FALSE;  
}  
ShowWindow(hWnd, nCmdShow); //显示窗口

UpdateWindow(hWnd);  
//立即显示![](http://my.csdn.net/uploads/201205/15/1337095725_6110.jpg)  
return TRUE;  
}

//窗口消息处理  
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
{  
switch (message)  
{  
case WM\_DESTROY:  
//关闭窗口  
PostQuitMessage(0);  
break;  
default:

//发送关闭消息  
return DefWindowProc(hWnd, message, wParam, lParam);

}  
return 0;  
}

#include "StdAfx.h"  
#include &lt;windows.h&gt;  
TCHAR Title\[32]=TEXT("迷宫"); //窗口的标题  
TCHAR WinName\[32]=TEXT("Simple"); //窗口的名称  
ATOM RegisterWin(HINSTANCE);  
BOOL CreatWin(HINSTANCE, int);  
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,  
HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)  
{  
MSG msg;

RegisterWin(hInstance);

if (!CreatWin(hInstance, nCmdShow))  
{  
return FALSE;  
}  
while (GetMessage(&msg, NULL, 0, 0))  
{  
TranslateMessage(&msg);  
DispatchMessage(&msg);  
}

return (int)msg.wParam;  
}

ATOM RegisterWin(HINSTANCE hInstance)  
{

WNDCLASSEX wc;

wc.cbSize= sizeof(WNDCLASSEX);  
wc.style= CS\_HREDRAW | CS\_VREDRAW;  
wc.lpfnWndProc= (WNDPROC)WndProc;  
wc.cbClsExtra= 0;  
wc.cbWndExtra= 0;  
wc.hInstance= hInstance;  
wc.hIcon= LoadIcon(hInstance, (LPCTSTR)IDI\_APPLICATION);  
wc.hCursor = LoadCursor(NULL, IDC\_ARROW);  
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE\_BRUSH);  
wc.lpszMenuName = NULL;  
wc.lpszClassName = WinName;  
wc.hIconSm = LoadIcon(wc.hInstance, (LPCTSTR)IDI\_APPLICATION);

return RegisterClassEx(&wc);  
}

//初始化窗口  
BOOL CreatWin(HINSTANCE hInstance, int nCmdShow)  
{  
HWND hWnd;

//创建窗口

hWnd = CreateWindow( WinName,  
Title,

WS\_OVERLAPPEDWINDOW, //窗口式样

360,  
100,  
600,  
480,  
NULL,  
NULL,  
hInstance,  
NULL);  
if (!hWnd)  
{  
return FALSE;  
}  
ShowWindow(hWnd, nCmdShow); //显示窗口

UpdateWindow(hWnd);  
//立即显示  
return TRUE;  
}

//窗口消息处理  
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)  
{  
switch (message)  
{  
case WM\_DESTROY:  
//关闭窗口  
PostQuitMessage(0);  
break;  
default:

//发送关闭消息  
return DefWindowProc(hWnd, message, wParam, lParam);

}  
return 0;  
}