三国纷争
先定义了一个WNDCLASSEX 结构,然后指定一些窗口特性,调用 RegisterClassEx返回。实际上,要生成一个窗口很简单,这里给你一个我写的CWin类,在winmain中加入以下语句即可。CWin win;win.Create(hInstance, "程序示例");以下是CWin基本框架:win.h// Win.h: interface for the CWin class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_)#define AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CWin{public:void SetBkGnd(LPCTSTR BkGndFile);void OnGetminmaxinfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);virtual int RealWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);void OnDraw(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);CWin(int nWidth, int nHeight);static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);bool Create(HINSTANCE hInstance, TCHAR* WndName);CWin();virtual ~CWin();protected:HBITMAP m_hBkGnd;int m_nBkColor;int m_nHeight;int m_nWidth;HWND m_hWnd;};#endif // !defined(AFX_WIN_H__4782C3BD_80D9_4A13_869D_D80A4A7CAC0D__INCLUDED_)win.cpp// Win.cpp: implementation of the CWin class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "Win.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CWin::CWin(){m_hWnd = NULL;m_nWidth = 640;m_nHeight = 480;m_nBkColor = 0xffffff; // whitem_hBkGnd = NULL;}CWin::CWin(int nWidth, int nHeight){m_nWidth = nWidth>0 ? nWidth:640;m_nHeight = nHeight>0 ? nHeight:480;}CWin::~CWin(){if(m_hBkGnd){DeleteObject(m_hBkGnd);m_hBkGnd = NULL;}}bool CWin::Create(HINSTANCE hInstance, TCHAR *WndName){WNDCLASSEX wcex;int x, y, w, h;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);wcex.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wcex.lpszMenuName = NULL;wcex.lpszClassName = WndName;wcex.hIconSm = LoadIcon(NULL, (LPCTSTR)IDI_WINLOGO);RegisterClassEx(&wcex);m_hWnd = CreateWindow(WndName, WndName, WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);if(!m_hWnd)return NULL;::SetWindowLong(m_hWnd,GWL_USERDATA,(LONG)this);//将this指针传给窗口w = m_nWidth + 2*GetSystemMetrics(SM_CXFRAME);h = m_nHeight +2*GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION);x = GetSystemMetrics(SM_CXSCREEN);y = GetSystemMetrics(SM_CYSCREEN);x = (x - w) / 2;y = (y - h) / 2;SetWindowPos(m_hWnd, HWND_NOTOPMOST, x, y, w, h, SWP_HIDEWINDOW);ShowWindow(m_hWnd, SW_NORMAL);UpdateWindow(m_hWnd);return true;}LRESULT CALLBACK CWin::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){CWin* pWin = NULL;if(message!=WM_NCCREATE && message !=WM_CREATE)pWin = (CWin*)::GetWindowLong(hWnd,GWL_USERDATA);if(pWin)return pWin->RealWinProc(hWnd, message, wParam, lParam);return DefWindowProc(hWnd, message, wParam, lParam);}void CWin::OnDraw(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){HDC hDC, hMemDC;HBITMAP hBitmap = NULL;HBRUSH hBrush = NULL;RECT rt = {0, 0, m_nWidth, m_nHeight};HFONT hFont;TCHAR szWait[20]="等待中...";ValidateRect(hWnd, NULL);hDC = GetDC(hWnd);hMemDC = CreateCompatibleDC(hDC);hBitmap = CreateCompatibleBitmap(hDC, m_nWidth, m_nHeight);hBrush = CreateSolidBrush(m_nBkColor);SelectObject(hMemDC, hBitmap);SelectObject(hMemDC, hBrush);FillRect(hMemDC, &rt, hBrush);hFont = CreateFont(30, // nHeight0, // nWidth0, // nEscapement0, // nOrientationFW_NORMAL, // nWeighttrue, // bItalicFALSE, // bUnderline0, // cStrikeOutANSI_CHARSET, // nCharSetOUT_DEFAULT_PRECIS, // nOutPrecisionCLIP_DEFAULT_PRECIS, // nClipPrecisionDEFAULT_QUALITY, // nQualityDEFAULT_PITCH | FF_SWISS, // nPitchAndFamily"Arial");SelectObject(hMemDC, hFont);if(m_hBkGnd)SelectObject(hMemDC, m_hBkGnd);///////////////////////////////////////////////////////////////SetBkColor(hMemDC, m_nBkColor);SetTextColor(hMemDC, 0x000000);SetBkMode(hMemDC, TRANSPARENT);TextOut(hMemDC, 0, 0, szWait, strlen(szWait));///////////////////////////////////////////////////////////////BitBlt(hDC, 0, 0, m_nWidth, m_nHeight, hMemDC, 0, 0, SRCCOPY);DeleteObject(hFont);DeleteObject(hBrush);DeleteObject(hBitmap);ReleaseDC(hWnd, hMemDC);ReleaseDC(hWnd, hDC);}int CWin::RealWinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){switch(message){case WM_GETMINMAXINFO:OnGetminmaxinfo(hWnd, message, wParam, lParam);break;case WM_PAINT:OnDraw(hWnd, message, wParam, lParam);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hWnd, message, wParam, lParam);}return 0;}void CWin::OnGetminmaxinfo(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){LPMINMAXINFO lpmmi;int width = m_nWidth, height = m_nHeight;lpmmi = (LPMINMAXINFO)lParam;width = width + 2*GetSystemMetrics(SM_CXFRAME);height = height +2*GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION);lpmmi->ptMaxTrackSize.x = lpmmi->ptMinTrackSize.x = width;lpmmi->ptMaxTrackSize.y = lpmmi->ptMinTrackSize.y = height;}void CWin::SetBkGnd(LPCTSTR BkGndFile){if(m_hBkGnd)DeleteObject(m_hBkGnd);if(BkGndFile)m_hBkGnd = (HBITMAP)LoadImage((HINSTANCE)GetWindowLong(m_hWnd, GWL_HINSTANCE),BkGndFile,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION | LR_LOADFROMFILE);elsem_hBkGnd = NULL;InvalidateRect(m_hWnd, NULL, false);UpdateWindow(m_hWnd);}