注:以下代码以一个名为CTest6Dlg的对话框类为例
GetWindowLong(this->m_hWnd, GWL_STYLE) |
WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
UpdateWindow();
在OnInitDialog中
方法一:
CMenu* menu = GetSystemMenu(FALSE);
menu->ModifyMenu(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED );
方法二:
CMenu* menu = GetSystemMenu(FALSE);
menu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
添加 WM_SETCURSOR 消息映射函数
BOOL CTest6Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_HELP));
return 0;
}
在CTest6App的InitInstance中添加
SetDialogBkColor(RGB(255,0,0), RGB(0,255,0));
导入自己的图标资源到工程中,把原来ID为 IDR_MAINFRAME 的资源删除,把新的图标的ID命名为IDR_MAINFRAME
BOOL CTest6App::InitInstance()
{
//...
int nResponse;
CLoginDlg loginDlg;
nResponse = loginDlg.DoModal();
if (nResponse == IDOK)
{
}
if (nResponse == IDCANCEL)
{
return FALSE;
}
CTest6Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK )
{
}
else if (nResponse == IDCANCEL)
{
}
return FALSE;
}
然后重载CLoginDlg对话框的哦OnOK(),在其中判断条件
void CLoginDlg::OnOK()
{
if (条件满足)
CDialog::OnOK();
else
AfxMessageBox(_T("invalid password!"));
}
方法一:添加以下代码到 OnInitDialog 中
if ( !m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1) )
{
TRACE0("Failed to Create Dialog Toolbar/n");
EndDialog(IDCANCEL);
}
CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); //
// Called to reposition and resize control bars in the client area of a window
// The reposQuery FLAG does not really traw the Toolbar. It only does the calculations.
// And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
//重新计算RECT大小
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
AFX_IDW_CONTROLBAR_LAST,
0,
reposQuery,
rcClientNew);
// All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// Offest to move all child controls after adding Tollbar
//计算移动的距离
CPoint ptOffset(rcClientNew.left-rcClientOld.left,
rcClientNew.top-rcClientOld.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD); //得到子窗口
while(pwndChild) // 处理所有子窗口
{
//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild,FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow,FALSE); // Redraw Window
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
方法二:
http://www.codeproject.com/dialog/dlgtoolstatusbar.asp
方法一:添加 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/
(1) 对话框的属性的必须是resizing的
(2) 打开classwizard->class info标签页->message filter中选择window
(3) 添加 WM_GETMINMAXINFO 消息映射
void CTest6Dlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
lpMMI->ptMinTrackSize = CPoint(200, 200);
}
Q103788:
Creating a Modeless Dialog Box with MFC Libraries
http://support.microsoft.com/kb/103788/EN-US/
Visual C++ MFC Samples
MODELESS Sample: Uses a CDialog Object as a Modeless Dialog Box
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/_sample_mfc_MODELESS.asp
Q242577:
You cannot change the state of a menu item from its command user-interface handler if the menu is attached to a dialog box in Visual C++
http://support.microsoft.com/kb/242577/en-us
Q141724:
Context-Sensitive Help in a CDialog Object
http://support.microsoft.com/kb/141724/en-us
msdn中的介绍
http://msdn2.microsoft.com/en-us/library/dyd1yfww.aspx
或者如果你要屏蔽按下F1后出现的“找不到*.hlp文件”的提示对话框
添加 WM_HELPINFO 消息映射
BOOL CTest6Dlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
return TRUE;
//return CDialog::OnHelpInfo(pHelpInfo);//屏蔽该句
}
MSDN上的解释如下:
{
CDlg2 dlg2;
dlg2.m_str = _T("你好"; )
dlg2.m_bJudge = TRUE;
dlg2.DoModal();
}
//Dlg2.h
public:
CString m_str;
BOOL m_bJudge;
//Dlg2.cpp
CDlg2::OnInitDialog()
{
if (m_bJudge)
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
//dlg1.cpp
#include "dlg2.h"
CDlg1::OnButton1()
{
CDlg2 dlg2;
dlg2.m_pDlg1 = this;
dlg2.DoModal();
}
//dlg2.h
class CDlg1;//添加dlg1类的声明
class CDlg2 : public CDialog
{
...
public:
CDlg1 *m_pDlg1;
}
//dlg2.cpp
#include "dlg1.h"
至此,你可以在dlg2.cpp中通过m_pDlg1操作CDlg1类中的成员变量了。
Q145994:
How to calculate dialog box units based on the current font in Visual C++
http://support.microsoft.com/kb/q145994/
Q125681:
How To Calculate Dialog Base Units with Non-System-Based Font
http://support.microsoft.com/kb/125681/en-us
当在程序中需要进行大数据量计算的时候(比如搜索磁盘,大数据量传输等),由于这些计算过程是在界面线程(UI Process)中,由此引发了界面线程的消息阻塞。我们创建一个工作线程(worker thread)来处理计算过程,以解决该问题。
下面是一个简单的创建一个工作线程的实现:
//xxxdlg.h
static UINT MyThread(LPVOID pParam);
CWinThread* pMyThread;
//xxxdlg.cpp
CXXXDlg::OnButton1()
{
pMyThread = AfxBeginThread(MyThread, this);
pMyThread = NULL;
}
UINT CXXXDlg::MyThread(LPVOID pParam)
{
CXXXDlg *pDlg = (CXXXDlg *)pParam;
//这里添加计算过程
return 0;
}
以把B对话框的资源插入到A对话框为例:
(1) 生成一个*.ogx文件
打开B工程,在ClassView中鼠标右键点击所需的对话框类,单击 "Add to Gallery"。
这时,会在 " C:/Program Files/Microsoft Visual Studio/Common/MSDev98/Gallery/工程B " 的目录下产生一个ogx文件。
(2) 插入该*.ogx文件
打开A工程,选择菜单Project->Add To Project->components and controls... ,选择刚生成的ogx文件,然后Insert。
1. 在任务栏隐藏对话框
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);2. 使对话框为顶层窗口
SetWindowPos(&this->wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);3. 在运行时添加最大化,最小化按钮
SetWindowLong(this->m_hWnd, GWL_STYLE,GetWindowLong(this->m_hWnd, GWL_STYLE) |
WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
UpdateWindow();
4. 使能对话框右上角关闭按钮
在OnInitDialog中
方法一:
CMenu* menu = GetSystemMenu(FALSE);
menu->ModifyMenu(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED );
方法二:
CMenu* menu = GetSystemMenu(FALSE);
menu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
5. 当对话框一部分在屏幕外时,显示全部对话框
SendMessage(DM_REPOSITION);6. 改变鼠标外形
添加 WM_SETCURSOR 消息映射函数
BOOL CTest6Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_HELP));
return 0;
}
7. 改变对话框背景色和文本颜色
在CTest6App的InitInstance中添加
SetDialogBkColor(RGB(255,0,0), RGB(0,255,0));
8. 改变对话框caption上的图标
导入自己的图标资源到工程中,把原来ID为 IDR_MAINFRAME 的资源删除,把新的图标的ID命名为IDR_MAINFRAME
9. 在主对话框显示前,显示一个login对话框
BOOL CTest6App::InitInstance()
{
//...
int nResponse;
CLoginDlg loginDlg;
nResponse = loginDlg.DoModal();
if (nResponse == IDOK)
{
}
if (nResponse == IDCANCEL)
{
return FALSE;
}
CTest6Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK )
{
}
else if (nResponse == IDCANCEL)
{
}
return FALSE;
}
然后重载CLoginDlg对话框的哦OnOK(),在其中判断条件
void CLoginDlg::OnOK()
{
if (条件满足)
CDialog::OnOK();
else
AfxMessageBox(_T("invalid password!"));
}
10. 在对话框中添加工具栏
方法一:添加以下代码到 OnInitDialog 中
if ( !m_wndToolBar.Create(this) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1) )
{
TRACE0("Failed to Create Dialog Toolbar/n");
EndDialog(IDCANCEL);
}
CRect rcClientOld; // 久客户区RECT
CRect rcClientNew; // 加入TOOLBAR后的CLIENT RECT
GetClientRect(rcClientOld); //
// Called to reposition and resize control bars in the client area of a window
// The reposQuery FLAG does not really traw the Toolbar. It only does the calculations.
// And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
//重新计算RECT大小
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
AFX_IDW_CONTROLBAR_LAST,
0,
reposQuery,
rcClientNew);
// All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
//所有的子窗口将被移动,以免被TOOLBAR覆盖
// Offest to move all child controls after adding Tollbar
//计算移动的距离
CPoint ptOffset(rcClientNew.left-rcClientOld.left,
rcClientNew.top-rcClientOld.top);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD); //得到子窗口
while(pwndChild) // 处理所有子窗口
{
//移动所有子窗口
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild,FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow); // 得到对话框RECT
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); // 修改对话框尺寸
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
MoveWindow(rcWindow,FALSE); // Redraw Window
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
方法二:
http://www.codeproject.com/dialog/dlgtoolstatusbar.asp
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("恢复"));
}
}
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/
21.控制对话框最大最小尺寸
(1) 对话框的属性的必须是resizing的
(2) 打开classwizard->class info标签页->message filter中选择window
(3) 添加 WM_GETMINMAXINFO 消息映射
void CTest6Dlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
lpMMI->ptMinTrackSize = CPoint(200, 200);
}
22. 创建无模式对话框
Q103788:
Creating a Modeless Dialog Box with MFC Libraries
http://support.microsoft.com/kb/103788/EN-US/
Visual C++ MFC Samples
MODELESS Sample: Uses a CDialog Object as a Modeless Dialog Box
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/_sample_mfc_MODELESS.asp
23.在对话框中改变菜单项状态(enable/disable, check/uncheck, change text)
Q242577:
You cannot change the state of a menu item from its command user-interface handler if the menu is attached to a dialog box in Visual C++
http://support.microsoft.com/kb/242577/en-us
24. 按下F1出现帮助
Q141724:
Context-Sensitive Help in a CDialog Object
http://support.microsoft.com/kb/141724/en-us
msdn中的介绍
http://msdn2.microsoft.com/en-us/library/dyd1yfww.aspx
或者如果你要屏蔽按下F1后出现的“找不到*.hlp文件”的提示对话框
添加 WM_HELPINFO 消息映射
BOOL CTest6Dlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
return TRUE;
//return CDialog::OnHelpInfo(pHelpInfo);//屏蔽该句
}
25. 对话框初始化设置输入焦点的问题
默认情况下,对话框初始化显示的焦点按照在对话框编辑期间设置的tab order的第一个控件来设置的。(设置tab order可在对话框的resource view中用Ctrl+D显示出来,点鼠标进行顺序设置)。如果想人为的改变初始化时的输入焦点,可在对话框的OnInitDialog中把return TRUE; 改为 return FALSE;MSDN上的解释如下:
Return Value
Specifies whether the application has set the input focus to one of the controls in the dialog box. If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box. The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.
26. 在对话框间传递数据
CDlg1::OnButton1(){
CDlg2 dlg2;
dlg2.m_str = _T("你好"; )
dlg2.m_bJudge = TRUE;
dlg2.DoModal();
}
//Dlg2.h
public:
CString m_str;
BOOL m_bJudge;
//Dlg2.cpp
CDlg2::OnInitDialog()
{
if (m_bJudge)
GetDlgItem(IDC_EDIT1)->SetWindowText(m_str);
}
27. 在 dlg1 中打开 dlg2 时,dlg2 能修改 dlg1 中的成员变量
//dlg1.cpp
#include "dlg2.h"
CDlg1::OnButton1()
{
CDlg2 dlg2;
dlg2.m_pDlg1 = this;
dlg2.DoModal();
}
//dlg2.h
class CDlg1;//添加dlg1类的声明
class CDlg2 : public CDialog
{
...
public:
CDlg1 *m_pDlg1;
}
//dlg2.cpp
#include "dlg1.h"
至此,你可以在dlg2.cpp中通过m_pDlg1操作CDlg1类中的成员变量了。
28. 改变对话框字体,对话框大小改变的问题
Q145994:
How to calculate dialog box units based on the current font in Visual C++
http://support.microsoft.com/kb/q145994/
Q125681:
How To Calculate Dialog Base Units with Non-System-Based Font
http://support.microsoft.com/kb/125681/en-us
29. 进行大数据量计算的时候,导致界面挂起无响应的问题
当在程序中需要进行大数据量计算的时候(比如搜索磁盘,大数据量传输等),由于这些计算过程是在界面线程(UI Process)中,由此引发了界面线程的消息阻塞。我们创建一个工作线程(worker thread)来处理计算过程,以解决该问题。
下面是一个简单的创建一个工作线程的实现:
//xxxdlg.h
static UINT MyThread(LPVOID pParam);
CWinThread* pMyThread;
//xxxdlg.cpp
CXXXDlg::OnButton1()
{
pMyThread = AfxBeginThread(MyThread, this);
pMyThread = NULL;
}
UINT CXXXDlg::MyThread(LPVOID pParam)
{
CXXXDlg *pDlg = (CXXXDlg *)pParam;
//这里添加计算过程
return 0;
}
30. 工程资源的合并
以把B对话框的资源插入到A对话框为例:
(1) 生成一个*.ogx文件
打开B工程,在ClassView中鼠标右键点击所需的对话框类,单击 "Add to Gallery"。
这时,会在 " C:/Program Files/Microsoft Visual Studio/Common/MSDev98/Gallery/工程B " 的目录下产生一个ogx文件。
(2) 插入该*.ogx文件
打开A工程,选择菜单Project->Add To Project->components and controls... ,选择刚生成的ogx文件,然后Insert。
这时B对话框资源和对话框类就插入到A中了。。