--
以下是一个窗口类的定义以及使用方法,分3个文件,App.h,App.cpp为窗口类文件,main.cpp为主函数文件,可以正常使用,编译环境为VS 2008,具体的函数意义请参考任意一本windows游戏编程书,基本书里都有详细的介绍。
App.h

Code
/***************
file:App.h
****************/
#ifndef _APP_H_
#define _APP_H_

#include <windows.h>
class cApplication

{
private:
HINSTANCE m_hInst; //实例句柄
HWND m_hWnd; //窗口句柄

protected:
char m_Class[MAX_PATH];
char m_Caption[MAX_PATH];

WNDCLASSEX m_wcex; //窗口类

DWORD m_Style; //窗口风格
DWORD m_XPos; //窗口位置坐标X
DWORD m_YPos; //窗口位置坐标Y
DWORD m_Width; //窗口的横宽
DWORD m_Height; //窗口的纵高

public:
cApplication();

HWND GethWnd();
HINSTANCE GethInst();

BOOL Run(); //
BOOL Error(BOOL Fatal, char *Text,...);

BOOL Move(long XPos, long YPos); //移动窗口
BOOL Resize(long Width, long Height); //重定义窗口大小

BOOL ShowMouse(BOOL Show = TRUE); //是否显示鼠标指针
//消息处理函数
virtual LRESULT WINAPI MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hWnd, uMsg, wParam, lParam); }
virtual BOOL Init() { return TRUE; }//初始化函数
virtual BOOL Shutdown() { return TRUE; }//释放资源
virtual BOOL Frame() { return TRUE; }//绘制函数
};
static cApplication *g_pApplication = NULL;
static LRESULT WINAPI AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
#endif

App.cpp

Code
#include "App.h"

cApplication::cApplication()

{
// Save instance handle
g_pApplication = this;

// Get the instance handle
m_hInst = GetModuleHandle(NULL);

// Set a default window class and caption
strcpy(m_Class, "AppClass");
strcpy(m_Caption, "Application Caption");

// Set default window style, position, width, height
m_Style = WS_OVERLAPPEDWINDOW;
m_XPos = 0;
m_YPos = 0;
m_Width = 256;
m_Height = 256;

// Set default WNDCLASSEX structure
m_wcex.cbSize = sizeof(WNDCLASSEX);
m_wcex.style = CS_CLASSDC;
m_wcex.lpfnWndProc = AppWindowProc;
m_wcex.cbClsExtra = 0;
m_wcex.cbWndExtra = 0;
m_wcex.hInstance = m_hInst;
m_wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wcex.hbrBackground = (HBRUSH)GetStockObject(0);
m_wcex.lpszMenuName = NULL;
m_wcex.lpszClassName = m_Class;
m_wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
}

HWND cApplication::GethWnd()

{
return m_hWnd;
}

HINSTANCE cApplication::GethInst()

{
return m_hInst;
}

BOOL cApplication::Run()

{
MSG Msg;

// Register window class
if(!RegisterClassEx(&m_wcex))
return FALSE;

// Create the Main Window
m_hWnd = CreateWindow(m_Class, m_Caption,
m_Style,
m_XPos, m_YPos,
m_Width, m_Height,
NULL, NULL, m_hInst, NULL);
if(!m_hWnd)
return FALSE;

// Show and update the window
ShowWindow(m_hWnd, SW_NORMAL);
UpdateWindow(m_hWnd);

// Make sure client area is correct size
Resize(m_Width, m_Height);

// Initialize COM
CoInitialize(NULL);

if(Init() == TRUE) {
// Enter the message pump
ZeroMemory(&Msg, sizeof(MSG));

while(Msg.message != WM_QUIT) {

// Handle Windows messages (if any)

if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);

} else {
//Do per-frame processing, break on FALSE return value
if(Frame() == FALSE)
break;
}
}
}
Shutdown();

// Shutdown COM
CoUninitialize();

// Unregister the window class
UnregisterClass(m_Class, m_hInst);

return TRUE;
}

BOOL cApplication::Error(BOOL Fatal, char *Text,
)

{
char CaptionText[12];
char ErrorText[2048];
va_list valist;

// Build the message box caption based on fatal flag
if(Fatal == FALSE)
strcpy(CaptionText, "Error");
else
strcpy(CaptionText, "Fatal Error");

// Build variable text buffer
va_start(valist, Text);
vsprintf(ErrorText, Text, valist);
va_end(valist);

// Display the message box
MessageBox(NULL, ErrorText, CaptionText, MB_OK | MB_ICONEXCLAMATION);

// Post a quit message if error was fatal
if(Fatal == TRUE)
PostQuitMessage(0);

return TRUE;
}

BOOL cApplication::Move(long XPos, long YPos)

{
RECT ClientRect;

GetClientRect(m_hWnd, &ClientRect);
MoveWindow(m_hWnd, XPos, YPos, ClientRect.right, ClientRect.bottom, TRUE);

return TRUE;
}

BOOL cApplication::Resize(long Width, long Height)

{
RECT WndRect, ClientRect;
long WndWidth, WndHeight;

GetWindowRect(m_hWnd, &WndRect);
GetClientRect(m_hWnd, &ClientRect);

WndWidth = (WndRect.right - (ClientRect.right - Width)) - WndRect.left;
WndHeight = (WndRect.bottom - (ClientRect.bottom - Height)) - WndRect.top;

MoveWindow(m_hWnd, WndRect.left, WndRect.top, WndWidth, WndHeight, TRUE);

return TRUE;
}

BOOL cApplication::ShowMouse(BOOL Show)

{
ShowCursor(Show);
return TRUE;
}

// The message procedure - empty except for destroy message
LRESULT WINAPI AppWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch(uMsg) {
case WM_DESTROY:

{
PostQuitMessage(0);
}
return 0;

default: return g_pApplication->MsgProc(hWnd, uMsg, wParam, lParam);
}
}



main.cpp:

Code
#include "App.h"
class cApp:public cApplication

{
private:
public:

cApp();
BOOL Init();
BOOL Shutdown();
BOOL Frame();
};

cApp::cApp()

{
m_Style = WS_OVERLAPPEDWINDOW;
m_XPos = 100;
m_YPos = 20;
m_Height =600;
m_Width = 800;
strcpy_s(m_Caption,"My Name Example");
strcpy_s(m_Class,"NameClass");
}

BOOL cApp::Init()

{
return TRUE;
}

BOOL cApp::Shutdown()

{

return TRUE;
}


BOOL WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine,int nShowCmd)

{
cApp app;
return app.Run();
}
