模拟 MFC 的 CWnd 类的封装,出错的原因都是参数个数不匹配,而我调用的又不是 API 函数,而是类的成员函数,不知道为什么会不匹配.
======================================CWnd.h=====================================
#ifndef CWND_H
#define CWND_H
#include<windows.h>
class CWnd
{
public:
CWnd(HWND Temp=NULL):m_hWnd(Temp){}
BOOL CreateWindowEx(
DWORD dwExStyle,
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam
);
BOOL ShowWindow( HWND hWnd, int nCmdShow);
BOOL UpdateWindow( HWND hWnd );
public: HWND m_hWnd;
};
BOOL CWnd::CreateWindowEx(DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)
{
m_hWnd=::CreateWindowEx(dwExStyle, lpClassName,lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam);
if(NULL==m_hWnd)
return false;
else
return true;
}
BOOL CWnd::ShowWindow( int nCmdShow)
{
return ::ShowWindow( m_hWnd, nCmdShow);
}
BOOL CWnd::UpdateWindow()
{
return ::UpdateWindow( m_hWnd) ;
}
#endif
========================================winmain.cpp================================
#include"CWnd.h"
LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam,LPARAM lParam );
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
WNDCLASS wndclass;
wndclass.cbClsExtra =0;
wndclass.cbWndExtra =0;
wndclass.hbrBackground =(HBRUSH)COLOR_BACKGROUND;
wndclass.hCursor =LoadCursor(NULL,IDC_APPSTARTING);
wndclass.hIcon=NULL;
wndclass.hInstance =hInstance;
wndclass.lpszClassName =TEXT("First");
wndclass.lpszMenuName =NULL;
wndclass.style =CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc =WindowProc;
RegisterClass(&wndclass);
CWnd cwnd;
cwnd.CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("First"),TEXT("模拟CWnd类的封装"),WS_OVERLAPPEDWINDOW ,100,100,200,400,NULL,NULL,hInstance,NULL);
cwnd.ShowWindow(SW_SHOWNORMAL);
cwnd.UpdateWindow ();
MSG msg;
BOOL bRet;
while( (bRet = GetMessage( &msg, cwnd.m_hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
MessageBox(cwnd.m_hWnd ,TEXT("ERROR"),TEXT("WRONG"),MB_OK);
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam ;
}
LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch(uMsg)
{
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hwnd,&ps);
TextOut(hdc,0,50,TEXT("VC2005.net"), (int)strlen("VC2005.net"));
EndPaint(hwnd,&ps);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,TEXT("mouse click"),TEXT("Myapplication"),MB_OK);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,TEXT("你是否要退出程序?"),TEXT("Myapplication"),MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}