下面是模拟窗口类CWnd的封装,当然只是很简单的模拟,创建窗口,显示窗口和更新窗口,下面是代码
#include<stdio.h>
#include<windows.h>
class CWnd
{
public:
CWnd();
BOOL CreateEX(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
);
BOOL ShowWindow(int ncmdShow);
BOOL UpdateWindow();
public:
HWND m_hWnd;
};
CWnd::CWnd()
{
m_hWnd=NULL;
}
BOOL CWnd::CreateEX(
DWORD dwExStyle, // extended window style
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
)
{
m_hWnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);
if(m_hWnd!=NULL)
return TRUE;
else
return FALSE;
}
BOOL CWnd::ShowWindow(int ncmdShow)
{
return ::ShowWindow(m_hWnd,ncmdShow);
}
BOOL CWnd::UpdateWindow()
{
return ::UpdateWindow(m_hWnd);
}
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WinSunProc;
wndclass.lpszClassName="kay&&dan";
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndclass);
//Create the Windows
CWnd wnd;
wnd.CreateEX(WS_EX_LEFTSCROLLBAR,"kay&&dan","My first windows program",WS_OVERLAPPEDWINDOW,30,40,400,600,NULL,NULL,hInstance,NULL);
//Show and update the Window
wnd.ShowWindow(SW_SHOWNORMAL);
wnd.UpdateWindow();
//the Message Recycle
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// 窗口过程函数
LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szchar[20];
sprintf(szchar,"char code is %d",wParam);
MessageBox(hwnd,szchar,"Attention",MB_OK);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse click","message",0);
HDC hdc;
hdc=GetDC(hwnd); //不能在响应WM_PAINT消息时调用
TextOut(hdc,50,50,"我的第一个Windows 程序",strlen("我的第一个Windows 程序"));
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDc;
PAINTSTRUCT ps;
hDc=BeginPaint(hwnd,&ps);
TextOut(hDc,0,50,"重画中。。。",strlen("重画中。。。"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否关闭窗口","消息",MB_YESNO));
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
还要说的就是,C++窗口类对象与窗口并不是一回事,他们之间唯一的关系就是C++窗口类对象内部定义了一个窗口句柄变量,保存了与这个C++窗口类对象相关的那个窗口的句柄。窗口在销毁时,与之对应的C++窗口类的对象销毁与否,要看其生命期是否结束。但C++窗口类对象销毁时,与之相关的窗口也将销毁。