作者在 2008-08-12 09:30:37 发布以下内容
11.响应对话框的最大化、最小化、关闭、恢复事件
方法一:添加 WM_SYSCOMMAND 消息映射函数
void CTest6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ( (nID & 0xFFF0) == IDM_ABOUTBOX )
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
if ( nID == SC_MAXIMIZE )
{
AfxMessageBox(_T("最大化"));
}
else if ( nID == SC_MINIMIZE )
{
AfxMessageBox(_T("最小化"));
}
else if ( nID == SC_CLOSE )
{
AfxMessageBox(_T("关闭"));
}
CDialog::OnSysCommand(nID, lParam);
}
方法二:添加 WM_SIZE 消息映射函数
void CTest6Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if ( nType == SIZE_MAXIMIZED )
{
AfxMessageBox(_T("最大化"));
}
else if ( nType == SIZE_MINIMIZED )
{
AfxMessageBox(_T("最小化"));
}
else if ( nType == SIZE_RESTORED )
{
AfxMessageBox(_T("恢复"));
}
}
PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
PostMessage(WM_SYSCOMMAND, SC_CLOSE);
方法一:
(1) 重载OnCancel和OnOk,屏蔽其中的CDialog::OnCancel()和CDialog::OnOk();
(2) 添加以下代码
void CTest6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout; //if you have an about dialog
dlgAbout.DoModal();
}
else if ((nID & 0xFFF0) == SC_CLOSE)
{
//用户点击右上角"X"
EndDialog(IDOK);
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
方法二:
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
switch(pMsg->wParam)
{
case VK_ESCAPE:
return TRUE; //直接返回TRUE
break;
case VK_RETURN:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
方法三:
Q122489:
How to Disable Default Pushbutton Handling for MFC Dialog
http://support.microsoft.com/kb/122489/en-us
处理PreTranslateMessage消息
以下代码示例只演示了键盘WM_KEYDOWN消息,你也可以处理鼠标消息,比如WM_LBUTTONDOWN,WM_LBUTTONUP,WM_RBUTTONDOWN等。
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
/**********************************************************/
/* 当焦点在combobox(drop down风格)的edit上,响应回车 */
/***********************************************************/
if ( pMsg->message == WM_KEYDOWN )
{
switch( pMsg->wParam )
{
case VK_RETURN:
CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
if(pMsg->hwnd == pEdit->m_hWnd )
{
AfxMessageBox("在combobox的edit中按下了Enter!");
}
return TRUE;
}
}
/****************************************/
/* ALT为WM_SYSKEYDOWN */
/****************************************/
if( pMsg->message == WM_SYSKEYDOWN )
{
switch( pMsg->wParam )
{
case VK_F1:
if(::GetKeyState(VK_MENU) < 0)//ALT+F1
{
AfxMessageBox("按下了ALT+F1");
return TRUE;
}
}
}
/****************************************/
/* 在clistctrl中按ctrl+A选中所有项 */
/****************************************/
if( pMsg->message == WM_KEYDOWN )
{
if(pMsg->hwnd == GetDlgItem(IDC_LIST1)->m_hWnd)
{
switch( pMsg->wParam )
{
case 65://A
if(::GetKeyState(VK_CONTROL) < 0)//Shift+enter
{
for(int i=0; i<m_list.GetItemCount(); i++)
{
m_list.SetItemState(i, LVIS_SELECTED|LVIS_FOCUSED,
LVIS_SELECTED|LVIS_FOCUSED);
}
}
return TRUE;
}
}
}
/****************************************/
/* 当焦点在combobox,弹出自定义菜单 */
/****************************************/
if(pMsg->message == WM_RBUTTONDOWN)
{
CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
if(pMsg->hwnd == pEdit->m_hWnd)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
ScreenToClient(&point);
ClientToScreen(&point);
CMenu menu;
VERIFY( menu.LoadMenu( IDR_MENU1 ) );
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup != NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
}
return CDialog::PreTranslateMessage(pMsg);
}
添加 WM_SHOWWINDOW 的消息映射
void CTest6Dlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
if ( GetStyle() & WS_VISIBLE )
{
CDialog::OnShowWindow(bShow, nStatus);
}
else
{
long Style = ::GetWindowLong(*this, GWL_STYLE);
::SetWindowLong(*this, GWL_STYLE, Style | WS_VISIBLE);
CDialog::OnShowWindow(SW_HIDE, nStatus);
}
}
const int DETASTEP = 50;
BOOL AdjustPos(CWnd *pWnd, CRect* lpRect)
{
//自动靠边
int iSX = GetSystemMetrics(SM_CXFULLSCREEN);
int iSY = GetSystemMetrics(SM_CYFULLSCREEN);
RECT rWorkArea;
BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0);
CRect rcWA;
if ( !bResult )
{
//如果调用不成功就利用GetSystemMetrics获取屏幕面积
rcWA = CRect(0,0,iSX,iSY);
}
else
rcWA = rWorkArea;
int iX = lpRect->left;
int iY = lpRect->top;
if ( iX < rcWA.left + DETASTEP && iX!=rcWA.left )
{
//调整左
pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.left-iX,0);
AdjustPos(lpRect);
return TRUE;
}
if ( iY < rcWA.top + DETASTEP && iY!=rcWA.top )
{
//调整上
pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.top-iY);
AdjustPos(lpRect);
return TRUE;
}
if ( iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width() )
{
//调整右
pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.right-lpRect->right,0);
AdjustPos(lpRect);
return TRUE;
}
if ( iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect->Height() )
{
//调整下
pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
return TRUE;
}
return FALSE;
}
//然后在ONMOVEING事件中使用如下过程调用
CRect r=*pRect;
AdjustPos(this, &r);
*pRect=(RECT)r;
添加 WM_LBUTTONDOWN 的消息映射
void CTest6Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
CDialog::OnLButtonDown(nFlags, point);
}
方法二:
添加 WM_NCHITTEST 的消息映射
注意:在classwizard->message中找不到WM_NCHITTEST的,需要在选项卡class info->message filter中选择window后该消息才会出现在message中。
void CTest6Dlg::OnNCHitTest(CPoint point)
{
return HTCAPTION;
// return CDialog::OnNCHitTest(point);
}
或者参考
http://msdn.microsoft.com/msdnmag/issues/02/12/CQA/default.aspx
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
if ( pMsg->wParam == VK_RETURN )
pMsg->wParam = VK_TAB;
}
return CDialog::PreTranslateMessage(pMsg);
}
(1) 在CXXXApp中类中添加声明
HACCEL m_haccel;
(2) 在resource view中右键点击树的根目录,选择insert,添加一个新的Accelerator,默认ID为IDR_ACCELERATOR1。
在其中添加相应菜单的快捷键。
(3) 在BOOL CXXXApp::InitInstance()中添加代码
m_haccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
(4) 添加CXXXApp类的 ProcessMessageFilter 消息映射函数
BOOL CTest6App::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if ( m_haccel )
{
if ( ::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg) )
return TRUE;
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
或者参考
Q100770:
How to use accelerator keys and a main menu on the dialog box in Visual C++
http://support.microsoft.com/kb/100770/en-us
Adding Hot Keys to your Application
http://msdn.microsoft.com/msdnmag/issues/1200/c/default.aspx
int cx, cy;
HDC dc = ::GetDC(NULL);
cx = GetDeviceCaps(dc,HORZRES) + GetSystemMetrics(SM_CXBORDER);
cy = GetDeviceCaps(dc,VERTRES) + GetSystemMetrics(SM_CYBORDER);
::ReleaseDC(0,dc);
// Remove caption and border
SetWindowLong(m_hWnd, GWL_STYLE,
GetWindowLong(m_hWnd, GWL_STYLE) & (~(WS_CAPTION | WS_BORDER)));
// Put window on top and expand it to fill screen
::SetWindowPos(m_hWnd, HWND_TOPMOST,
-(GetSystemMetrics(SM_CXBORDER)+1),
-(GetSystemMetrics(SM_CYBORDER)+1),
cx+1,cy+1, SWP_NOZORDER);
或参考
http://www.codeguru.com/cpp/w-d/dislog/dialog-basedapplications/article.php/c1837/
方法一:添加 WM_SYSCOMMAND 消息映射函数
void CTest6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ( (nID & 0xFFF0) == IDM_ABOUTBOX )
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
if ( nID == SC_MAXIMIZE )
{
AfxMessageBox(_T("最大化"));
}
else if ( nID == SC_MINIMIZE )
{
AfxMessageBox(_T("最小化"));
}
else if ( nID == SC_CLOSE )
{
AfxMessageBox(_T("关闭"));
}
CDialog::OnSysCommand(nID, lParam);
}
方法二:添加 WM_SIZE 消息映射函数
void CTest6Dlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
if ( nType == SIZE_MAXIMIZED )
{
AfxMessageBox(_T("最大化"));
}
else if ( nType == SIZE_MINIMIZED )
{
AfxMessageBox(_T("最小化"));
}
else if ( nType == SIZE_RESTORED )
{
AfxMessageBox(_T("恢复"));
}
}
12.代码实现窗口最小化,最大化,关闭
PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
PostMessage(WM_SYSCOMMAND, SC_CLOSE);
13.按下ESC和ENTER键时禁止关闭对话框
方法一:
(1) 重载OnCancel和OnOk,屏蔽其中的CDialog::OnCancel()和CDialog::OnOk();
(2) 添加以下代码
void CTest6Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout; //if you have an about dialog
dlgAbout.DoModal();
}
else if ((nID & 0xFFF0) == SC_CLOSE)
{
//用户点击右上角"X"
EndDialog(IDOK);
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
方法二:
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
switch(pMsg->wParam)
{
case VK_ESCAPE:
return TRUE; //直接返回TRUE
break;
case VK_RETURN:
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
方法三:
Q122489:
How to Disable Default Pushbutton Handling for MFC Dialog
http://support.microsoft.com/kb/122489/en-us
14.在对话框中处理键盘鼠标消息
处理PreTranslateMessage消息
以下代码示例只演示了键盘WM_KEYDOWN消息,你也可以处理鼠标消息,比如WM_LBUTTONDOWN,WM_LBUTTONUP,WM_RBUTTONDOWN等。
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
/**********************************************************/
/* 当焦点在combobox(drop down风格)的edit上,响应回车 */
/***********************************************************/
if ( pMsg->message == WM_KEYDOWN )
{
switch( pMsg->wParam )
{
case VK_RETURN:
CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
if(pMsg->hwnd == pEdit->m_hWnd )
{
AfxMessageBox("在combobox的edit中按下了Enter!");
}
return TRUE;
}
}
/****************************************/
/* ALT为WM_SYSKEYDOWN */
/****************************************/
if( pMsg->message == WM_SYSKEYDOWN )
{
switch( pMsg->wParam )
{
case VK_F1:
if(::GetKeyState(VK_MENU) < 0)//ALT+F1
{
AfxMessageBox("按下了ALT+F1");
return TRUE;
}
}
}
/****************************************/
/* 在clistctrl中按ctrl+A选中所有项 */
/****************************************/
if( pMsg->message == WM_KEYDOWN )
{
if(pMsg->hwnd == GetDlgItem(IDC_LIST1)->m_hWnd)
{
switch( pMsg->wParam )
{
case 65://A
if(::GetKeyState(VK_CONTROL) < 0)//Shift+enter
{
for(int i=0; i<m_list.GetItemCount(); i++)
{
m_list.SetItemState(i, LVIS_SELECTED|LVIS_FOCUSED,
LVIS_SELECTED|LVIS_FOCUSED);
}
}
return TRUE;
}
}
}
/****************************************/
/* 当焦点在combobox,弹出自定义菜单 */
/****************************************/
if(pMsg->message == WM_RBUTTONDOWN)
{
CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
if(pMsg->hwnd == pEdit->m_hWnd)
{
DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );
ScreenToClient(&point);
ClientToScreen(&point);
CMenu menu;
VERIFY( menu.LoadMenu( IDR_MENU1 ) );
CMenu* popup = menu.GetSubMenu(0);
ASSERT( popup != NULL );
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this );
}
}
return CDialog::PreTranslateMessage(pMsg);
}
15.对话框启动即隐藏
添加 WM_SHOWWINDOW 的消息映射
void CTest6Dlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
if ( GetStyle() & WS_VISIBLE )
{
CDialog::OnShowWindow(bShow, nStatus);
}
else
{
long Style = ::GetWindowLong(*this, GWL_STYLE);
::SetWindowLong(*this, GWL_STYLE, Style | WS_VISIBLE);
CDialog::OnShowWindow(SW_HIDE, nStatus);
}
}
16.对话框自动停靠在屏幕边
const int DETASTEP = 50;
BOOL AdjustPos(CWnd *pWnd, CRect* lpRect)
{
//自动靠边
int iSX = GetSystemMetrics(SM_CXFULLSCREEN);
int iSY = GetSystemMetrics(SM_CYFULLSCREEN);
RECT rWorkArea;
BOOL bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0);
CRect rcWA;
if ( !bResult )
{
//如果调用不成功就利用GetSystemMetrics获取屏幕面积
rcWA = CRect(0,0,iSX,iSY);
}
else
rcWA = rWorkArea;
int iX = lpRect->left;
int iY = lpRect->top;
if ( iX < rcWA.left + DETASTEP && iX!=rcWA.left )
{
//调整左
pWnd->SetWindowPos(NULL,rcWA.left,iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.left-iX,0);
AdjustPos(lpRect);
return TRUE;
}
if ( iY < rcWA.top + DETASTEP && iY!=rcWA.top )
{
//调整上
pWnd->SetWindowPos(NULL ,iX,rcWA.top,0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.top-iY);
AdjustPos(lpRect);
return TRUE;
}
if ( iX + lpRect->Width() > rcWA.right - DETASTEP && iX !=rcWA.right-lpRect->Width() )
{
//调整右
pWnd->SetWindowPos(NULL ,rcWA.right-rcW.Width(),iY,0,0,SWP_NOSIZE);
lpRect->OffsetRect(rcWA.right-lpRect->right,0);
AdjustPos(lpRect);
return TRUE;
}
if ( iY + lpRect->Height() > rcWA.bottom - DETASTEP && iY !=rcWA.bottom-lpRect->Height() )
{
//调整下
pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
return TRUE;
}
return FALSE;
}
//然后在ONMOVEING事件中使用如下过程调用
CRect r=*pRect;
AdjustPos(this, &r);
*pRect=(RECT)r;
17.单击窗口任意位置都可拖动窗口
方法一:
添加 WM_LBUTTONDOWN 的消息映射
void CTest6Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
CDialog::OnLButtonDown(nFlags, point);
}
方法二:
添加 WM_NCHITTEST 的消息映射
注意:在classwizard->message中找不到WM_NCHITTEST的,需要在选项卡class info->message filter中选择window后该消息才会出现在message中。
void CTest6Dlg::OnNCHitTest(CPoint point)
{
return HTCAPTION;
// return CDialog::OnNCHitTest(point);
}
或者参考
http://msdn.microsoft.com/msdnmag/issues/02/12/CQA/default.aspx
18.用Enter键替换Tab键实现焦点切换
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
if ( pMsg->wParam == VK_RETURN )
pMsg->wParam = VK_TAB;
}
return CDialog::PreTranslateMessage(pMsg);
}
19.在对话框添加快捷键
(1) 在CXXXApp中类中添加声明
HACCEL m_haccel;
(2) 在resource view中右键点击树的根目录,选择insert,添加一个新的Accelerator,默认ID为IDR_ACCELERATOR1。
在其中添加相应菜单的快捷键。
(3) 在BOOL CXXXApp::InitInstance()中添加代码
m_haccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
(4) 添加CXXXApp类的 ProcessMessageFilter 消息映射函数
BOOL CTest6App::ProcessMessageFilter(int code, LPMSG lpMsg)
{
if ( m_haccel )
{
if ( ::TranslateAccelerator(m_pMainWnd->m_hWnd, m_haccel, lpMsg) )
return TRUE;
}
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
或者参考
Q100770:
How to use accelerator keys and a main menu on the dialog box in Visual C++
http://support.microsoft.com/kb/100770/en-us
Adding Hot Keys to your Application
http://msdn.microsoft.com/msdnmag/issues/1200/c/default.aspx
20.对话框全屏
int cx, cy;
HDC dc = ::GetDC(NULL);
cx = GetDeviceCaps(dc,HORZRES) + GetSystemMetrics(SM_CXBORDER);
cy = GetDeviceCaps(dc,VERTRES) + GetSystemMetrics(SM_CYBORDER);
::ReleaseDC(0,dc);
// Remove caption and border
SetWindowLong(m_hWnd, GWL_STYLE,
GetWindowLong(m_hWnd, GWL_STYLE) & (~(WS_CAPTION | WS_BORDER)));
// Put window on top and expand it to fill screen
::SetWindowPos(m_hWnd, HWND_TOPMOST,
-(GetSystemMetrics(SM_CXBORDER)+1),
-(GetSystemMetrics(SM_CYBORDER)+1),
cx+1,cy+1, SWP_NOZORDER);
或参考
http://www.codeguru.com/cpp/w-d/dislog/dialog-basedapplications/article.php/c1837/