建立名爲My的對話框程序.實現如下步驟:
在MyDlg.h中加入:
class CShowWindow1Dlg : public CDialog
{
public:
int m_cx;
int m_cy;
};
在MyDlg.cpp中:
//定義消息映象,處理鼠標單擊及激活
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
ON_WM_LBUTTONDOWN()//添加
ON_WM_ACTIVATE()//添加
END_MESSAGE_MAP()
void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rect;
GetClientRect(&rect);
rect.InflateRect(m_cx, m_cy);
//Release dialog if the user click outside it.
if(!rect.PtInRect(point))
{
EndDialog(IDCANCEL);
}
CDialog::OnLButtonDown(nFlags, point);
}
void CMyDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CDialog::OnActivate(nState, pWndOther, bMinimized);
if( nState == WA_ACTIVE || nState == WA_CLICKACTIVE)
SetCapture();
else
ReleaseCapture();
}
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
.....
OSVERSIONINFO info;
memset((char*)&info, 0, sizeof(OSVERSIONINFO));
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx(&info))
{ //we don't run on Win32s, so check only two values
if(info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{ //On windows 95
m_cx = GetSystemMetrics(SM_CXFIXEDFRAME);
m_cy = GetSystemMetrics(SM_CYFIXEDFRAME);
}
else
{ //On NT
m_cx = GetSystemMetrics(SM_CXDLGFRAME);
m_cy = GetSystemMetrics(SM_CYDLGFRAME);
}
}
}
說明:
1)WM_ACTIVATE消息在ClassWizard中沒有,按如下步驟添加,右擊CMyDlg類,選Add Windows Message Handle,接着在Filter for messages available to中選Window,在New Windows messages/events列表中就會出現WM_ACTIVATE,選中,點擊Add Handler
2)SM_CXDLGFRAME,SM_CYDLGFRAME NT中取得有WS_DLGFRAMEstyle風格的窗口的高和寬 95中已經廢棄而採用SM_CX_FIXEDFRAME和SM_CYFIXEDFRAME