#pragma once
#define STRICT
#include <tchar.h>
#include <windows.h>
#include <assert.h>
class TWindows
...{
public:
TWindows ();
virtual ~TWindows ();
//住执行过程
LONG Run (HINSTANCE hInstance, HINSTANCE hPreInstance
,LPCTSTR lpCmdLine, int nCmdShow);
BOOL UpdateWindow ();
void ShowWindow (int nCmdShow=SW_SHOW);
protected:
void SetWindowHandler (HWND hWnd);
//固定窗口函数
static LRESULT CALLBACK WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//消息循环处理
virtual LONG MessageLoop ();
//可控窗口函数
virtual LRESULT DefaultWindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//注册窗口类前调用
virtual void PreRegisterClass (WNDCLASSEX* wcex);
//创建窗口时前调用
virtual void PreCreateWindow (TCHAR lpWindowName[], DWORD* dwStyle
,DWORD* dwExStyle, int* x, int* y, int* nWidth, int* nHeight, HMENU* hMenu);
//窗口创建时调用
virtual void OnCreate ();
//窗口撤销时调用
virtual void OnDestroy();
//窗口重画时调用
virtual void OnDraw (HDC hDC)=0;
private:
//固定窗口类注册函数
BOOL RegisterClass (LPCTSTR lpClassName, HINSTANCE hInstance);
//固定窗口创建函数
BOOL CreateWindowEx (LPCTSTR lpClassName, HINSTANCE hInstance=NULL);
//窗口句柄
HWND m_wnd;
};
#include ". windows.h"
TWindows::TWindows ()
...{
m_wnd=NULL;
}
TWindows::~TWindows ()
...{
}
BOOL TWindows::RegisterClass (LPCTSTR lpClassName, HINSTANCE hInstance)
...{
WNDCLASSEX wcex; //窗口类
//位清零
memset(&wcex, 0, sizeof(WNDCLASSEX));
wcex.cbSize=sizeof(WNDCLASSEX); //窗口类结构大小
wcex.hIconSm=NULL; //小图标句柄
wcex.style=0; //窗口类类型
wcex.cbWndExtra=0; //窗口类附加类型
wcex.cbClsExtra=0; //与窗口类关联的附加字节
wcex.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); //窗口背景刷句柄
wcex.hCursor=LoadCursor(NULL, IDC_ARROW); //程序光标
wcex.hIcon=LoadIcon(NULL, IDI_APPLICATION); //程序图标
wcex.hInstance=hInstance; //进程句柄
wcex.lpfnWndProc=WindowProc; //窗口过程
wcex.lpszClassName=lpClassName; //窗口类名
wcex.lpszMenuName=NULL; //菜单资源名
this->PreRegisterClass (&wcex);
return ::RegisterClassEx(&wcex); //注册窗口类
};
BOOL TWindows::CreateWindowEx (LPCTSTR lpClassName, HINSTANCE hInstance)
...{
TCHAR lpWindowName[255]; //窗口标题
_tcscpy(lpWindowName, _T("Application Framework"));
DWORD dwStyle=WS_OVERLAPPEDWINDOW; //普通窗口样式
DWORD dwExStyle=0; //附加窗口样式
//窗口大小 位置坐标
int x=CW_USEDEFAULT, y=CW_USEDEFAULT,
nWidth=CW_USEDEFAULT, nHeight=CW_USEDEFAULT;
HMENU hMenu=NULL; //菜单句柄
this->PreCreateWindow(lpWindowName, &dwStyle, &dwExStyle, &x, &y, &nWidth
, &nHeight, &hMenu);
//创建窗口
if ((m_wnd=::CreateWindowEx(dwExStyle, lpClassName, lpWindowName
, dwStyle, x, y, nWidth, nHeight, NULL
, hMenu, hInstance, this))!=NULL)
...{
return TRUE;
}
return FALSE;
}
BOOL TWindows::UpdateWindow ()
...{
//更新窗口
return ::UpdateWindow(m_wnd);
}
void TWindows::ShowWindow (int nCmdShow)
...{
//显示窗口
::ShowWindow(m_wnd, nCmdShow);
}
void TWindows::SetWindowHandler(HWND hWnd)
...{
//设置窗口句柄
m_wnd=hWnd;
}
LONG TWindows::Run (HINSTANCE hInstance, HINSTANCE hPreInstance
,LPCTSTR lpCmdLine, int nCmdShow)
...{
//默认窗口类名
TCHAR szClassName[]=_T("Application Framework");
//注册窗口类
if (!this->RegisterClass(szClassName, hInstance))
...{
OutputDebugString("Error: RegisterClass ");
return 1;
}
//创建窗口类
if (!this->CreateWindowEx(szClassName, hInstance))
...{
OutputDebugString("Error; CreateWindow ");
return 2;
}
this->ShowWindow(nCmdShow);
this->UpdateWindow();
//进入消息循环
return this->MessageLoop();
}
LONG TWindows::MessageLoop ()
...{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
...{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT TWindows::DefaultWindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
...{
PAINTSTRUCT ps;
HDC hDC;
switch (message)
...{
case WM_CREATE:
OnCreate();
break;
case WM_DESTROY:
OnDestroy();
::PostQuitMessage(0);
break;
case WM_PAINT:
hDC=BeginPaint(hWnd, &ps);
OnDraw(hDC);
EndPaint(hWnd, &ps);
break;
default:
return ::DefWindowProc(m_wnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK TWindows::WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
...{
TWindows* pWindows;
if (message==WM_NCCREATE)
...{
//获得创建时传递的参数
CREATESTRUCT* ct=(CREATESTRUCT*)(lParam);
pWindows=(TWindows*)(ct->lpCreateParams);
pWindows->SetWindowHandler(hWnd); //设置窗口句柄
//将对象与窗口类关联
::SetWindowLong(hWnd, GWL_USERDATA, (unsigned long)(pWindows));
}
else
...{
//获得与窗口类关联的对象指针
pWindows=(TWindows*)(::GetWindowLong(hWnd, GWL_USERDATA));
}
if (pWindows!=NULL)
...{
//给可重载窗口过程处理
return pWindows->DefaultWindowProc(hWnd, message, wParam, lParam);
}
else
...{
//交由系统默认处理
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
}
void TWindows::OnCreate ()
...{
}
void TWindows::OnDestroy()
...{
}
void TWindows::OnDraw (HDC hDC)
...{
}
void TWindows::PreRegisterClass (WNDCLASSEX* wcex)
...{
}
void TWindows::PreCreateWindow (TCHAR lpWindowName[], DWORD* dwStyle
,DWORD* dwExStyle, int* x, int* y, int* nWidth, int* nHeight, HMENU* hMenu)
...{
}
//测试代码
#include "TWindows.h"
class MyWindows : public TWindows
...{
public:
virtual ~MyWindows ()
...{
}
MyWindows ()
...{
}
protected:
virtual void OnDraw (HDC hDC);
virtual void PreCreateWindow (TCHAR lpWindowName[], DWORD* dwStyle
,DWORD* dwExStyle, int* x, int* y, int* nWidth, int* nHeight, HMENU* hMenu);
};
void MyWindows::PreCreateWindow (TCHAR lpWindowName[], DWORD* dwStyle
,DWORD* dwExStyle, int* x, int* y, int* nWidth, int* nHeight, HMENU* hMenu)
...{
_tcscpy(lpWindowName, _T("Hello World"));
}
void MyWindows::OnDraw (HDC hDC)
...{
Rectangle(hDC, 0, 0, 100, 100);
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPreInstance
,LPTSTR lpCmdLine, int nCmdShow)
...{
MyWindows theApp;
return theApp.Run(hInstance, hPreInstance, lpCmdLine, nCmdShow);
}发表于 @ 2007年11月17日 18:29:00 | 评论( loading... ) | 举报| 收藏