======================================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;}
#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;}
声明处:
BOOL ShowWindow( HWND hWnd, int nCmdShow);
BOOL UpdateWindow( HWND hWnd );
实现处:
BOOL CWnd::ShowWindow( int nCmdShow)
{
return ::ShowWindow( m_hWnd, nCmdShow);
}BOOL CWnd::UpdateWindow()
{
return ::UpdateWindow( m_hWnd) ;
}
//声明与实现不一致声明处改成:
BOOL ShowWindow( int nCmdShow);
BOOL UpdateWindow( );
GetMessage 返回 -1 时,提示错误,少了一个break;来退出消息循环......
if (bRet == -1)
{
MessageBox(cwnd.m_hWnd ,TEXT("ERROR"),TEXT("WRONG"),MB_OK);
break;
}
为什么代码中GetMesage()会有返回 -1 呢?
MessageBox(cwnd.m_hWnd ,TEXT("ERROR"),TEXT("WRONG"),MB_OK);
用MessageBox(NULL ,TEXT("ERROR"),TEXT("WRONG"),MB_OK);也是弹出立即关闭,因为进程退出
{
break;
}
while (GetMessage(&msg, NULL, 0, 0)) //这样就避免了上面的情况,这下清楚了吧
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}BOOL GetMessage(
LPMSG lpMsg, // address of structure with message
HWND hWnd, // handle of window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);
hWnd
Handle to the window whose messages are to be retrieved. One value has a special meaning: Value Meaning
NULL GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread via PostThreadMessage.
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.WIN32COM.v10.en/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/getmessage.htm在Return Value和Res 中间有个Warning ,那段代码就是从Show Example 抄来的.在没看到那个Warning 前我一直当
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
是标准写法,但现在觉得这么写会不会有健壮性的隐患.
{
if (bRet == -1)
{
// handle the error and possibly exit(这里可以做处理,比如exit(0),提高健壮性)
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
http://www.bc-cn.net/bbs/dispbbs.asp?boardID=55&ID=36185有人提过
{
if (msg == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread via PostThreadMessage.
能接受包括主窗口句柄在类所有的消息,当然也有WM_QUIT.至于显示出错原因,没必要了,这样的调用很少出错,MSDN上也说了
To get extended error information, call GetLastError.
------------------------------------------------------------------------------------
关键是循环没有终止,因为 GetMessage( &msg, cwnd.m_hWnd, 0, 0 )) 收不到WM_QUIT消息,
GetMessage( &msg,NULL, 0, 0 )); //这里的NULL如果传入了NULL句柄, 则表示
GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread via PostThreadMessage.句柄无效和句柄为NULL 不是一个概念
DestroyWindow(hwnd)之后,句柄无效,但句柄!=NULL,这有点象指针一样,释放指针,但指针!=NULL,其实说白了,句柄也是一种指针.. (VC是开发环境当然和C++中一样了)
我把 WM_PAINT 消息处理做了些改动,通不过了,为什么? case WM_PAINT:
//PAINTSTRUCT ps;
HDC hdc;
//hdc=BeginPaint(hwnd,&ps);
CPaintDC hdc=::GetDC(hwnd);
TextOut(hdc,0,50,TEXT("VC2005.net"), (int)strlen("VC2005.net"));
//EndPaint(hwnd,&ps);
break;
否则会引起冲突.......要么学它的CPaintDC也来个封装
The CPaintDC class is a device-context class derived from CDC. Header file: Afxwin.h
#include <AFXWIN.H>
工程设置成Use MFC in a Shared dll就行了但你的不行,你定义了CWnd和MFC中的CWnd冲突
//PAINTSTRUCT ps;
HDC hdc;
hdc=::GetDC(hwnd);
//hdc=BeginPaint(hwnd,&ps);
//CPaintDC hdc=::GetDC(hwnd);
TextOut(hdc,0,50,TEXT("VC2005.net"), (int)strlen("VC2005.net"));
//EndPaint(hwnd,&ps);
break;