16.對(duì)話框自動(dòng)??吭谄聊贿?/p>
const int DETASTEP = 50;
BOOL AdjustPos(CWnd *pWnd, CRect* lpRect)
{
//自動(dòng)靠邊
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 )
{
//如果調(diào)用不成功就利用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 )
{
//調(diào)整左
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 )
{
//調(diào)整上
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() )
{
//調(diào)整右
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() )
{
//調(diào)整下
pWnd->SetWindowPos(NULL ,iX,rcWA.bottom-rcW.Height(),0,0,SWP_NOSIZE);
lpRect->OffsetRect(0,rcWA.bottom-lpRect->bottom);
return TRUE;
}
return FALSE;
}
//然后在ONMOVEING事件中使用如下過程調(diào)用
CRect r=*pRect;
AdjustPos(this, &r);
*pRect=(RECT)r;
--------------------------------------------------------------------------------
17.單擊窗口任意位置都可拖動(dòng)窗口
方法一:
添加 WM_LBUTTONDOWN 的消息映射
void CTest6Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
CDialog::OnLButtonDown(nFlags, point);
}
方法二:
添加 WM_NCHITTEST 的消息映射
注意:在classwizard->message中找不到WM_NCHITTEST的,需要在選項(xiàng)卡class info->message filter中選擇window后該消息才會(huì)出現(xiàn)在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鍵實(shí)現(xiàn)焦點(diǎn)切換
BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg)
{
if ( pMsg->message == WM_KEYDOWN )
{
if ( pMsg->wParam == VK_RETURN )
pMsg->wParam = VK_TAB;
}
return CDialog::PreTranslateMessage(pMsg);
}
--------------------------------------------------------------------------------
19.在對(duì)話框添加快捷鍵
(1) 在CXXXApp中類中添加聲明
HACCEL m_haccel;
(2) 在resource view中右鍵點(diǎn)擊樹的根目錄,選擇insert,添加一個(gè)新的Accelerator,默認(rèn)ID為IDR_ACCELERATOR1。
在其中添加相應(yīng)菜單的快捷鍵。
(3) 在BOOL CXXXApp::InitInstance()中添加代碼
m_haccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
(4) 添加CXXXApp類的 ProcessMessageFilter 消息映射函數(shù)
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.對(duì)話框全屏
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.控制對(duì)話框最大最小尺寸
(1) 對(duì)話框的屬性的必須是resizing的
(2) 打開classwizard->class info標(biāo)簽頁->message filter中選擇window
(3) 添加 WM_GETMINMAXINFO 消息映射
void CTest6Dlg::OnGetMinMaxInfo(MINMAXINFO *lpMMI)
{
lpMMI->ptMinTrackSize = CPoint(200, 200);
}
--------------------------------------------------------------------------------
22. 創(chuàng)建無模式對(duì)話框
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.在對(duì)話框中改變菜單項(xiàng)狀態(tài)(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出現(xiàn)幫助
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后出現(xiàn)的“找不到*.hlp文件”的提示對(duì)話框
添加 WM_HELPINFO 消息映射
BOOL CTest6Dlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
return TRUE;
//return CDialog::OnHelpInfo(pHelpInfo);//屏蔽該句
}
--------------------------------------------------------------------------------
25. 對(duì)話框初始化設(shè)置輸入焦點(diǎn)的問題
默認(rèn)情況下,對(duì)話框初始化顯示的焦點(diǎn)按照在對(duì)話框編輯期間設(shè)置的tab order的第一個(gè)控件來設(shè)置的。(設(shè)置tab order可在對(duì)話框的resource view中用Ctrl+D顯示出來,點(diǎn)鼠標(biāo)進(jìn)行順序設(shè)置)。如果想人為的改變初始化時(shí)的輸入焦點(diǎn),可在對(duì)話框的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. 在對(duì)話框間傳遞數(shù)據(jù)
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 時(shí),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. 改變對(duì)話框字體,對(duì)話框大小改變的問題
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. 進(jìn)行大數(shù)據(jù)量計(jì)算的時(shí)候,導(dǎo)致界面掛起無響應(yīng)的問題
當(dāng)在程序中需要進(jìn)行大數(shù)據(jù)量計(jì)算的時(shí)候(比如搜索磁盤,大數(shù)據(jù)量傳輸?shù)?,由于這些計(jì)算過程是在界面線程(UI Process)中,由此引發(fā)了界面線程的消息阻塞。我們創(chuàng)建一個(gè)工作線程(worker thread)來處理計(jì)算過程,以解決該問題。
下面是一個(gè)簡(jiǎn)單的創(chuàng)建一個(gè)工作線程的實(shí)現(xiàn):
//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;
//這里添加計(jì)算過程
return 0;
}
--------------------------------------------------------------------------------
30. 工程資源的合并
以把B對(duì)話框的資源插入到A對(duì)話框?yàn)槔?/p>
(1) 生成一個(gè)*.ogx文件
打開B工程,在ClassView中鼠標(biāo)右鍵點(diǎn)擊所需的對(duì)話框類,單擊"Add to Gallery"。
這時(shí),會(huì)在 " C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Gallery\工程B " 的目錄下產(chǎn)生一個(gè)ogx文件。
(2) 插入該*.ogx文件
打開A工程,選擇菜單Project->Add To Project->components and controls... ,選擇剛生成的ogx文件,然后Insert。
這時(shí)B對(duì)話框資源和對(duì)話框類就插入到A中了。。
--------------------------------------------------------------------------------
31. 在網(wǎng)上可以找到很多有用的代碼,我只是把一些常用的功能列出鏈接,方便查看
http://support.microsoft.com
http://www.codeproject.com/dialog/
http://www.codeguru.com/Cpp/W-D/dislog/
改變對(duì)話框大小時(shí)同時(shí)改變控件大小
http://www.codeproject.com/dialog/easysize.asp
http://www.codeproject.com/dialog/resizabledialog.asp
http://www.vchelp.net/vchelp/archive.asp?type_id=5&class_id=1&cata_id=1&article_id=548&search_term=
http://www.vchelp.net/vchelp/archive.asp?type_id=5&class_id=1&cata_id=1&article_id=538&search_term=
--------------------------------------------------------------------------------
如何在可變大小(resizing)的對(duì)話框中實(shí)現(xiàn)滾動(dòng)窗口
Q262954:
How to create a resizeable dialog box with scroll bars in Visual C++
http://support.microsoft.com/default.aspx?scid=kb;en-us;262954
http://www.codeproject.com/dialog/scrollablechilddialog.asp
--------------------------------------------------------------------------------
從某一點(diǎn)或某一邊逐漸變大顯示對(duì)話框
http://www.codeproject.com/dialog/canidialog.asp
--------------------------------------------------------------------------------
一個(gè)重載的MessageBox類
http://www.codeproject.com/dialog/xmessagebox.asp
--------------------------------------------------------------------------------
option設(shè)置對(duì)話框(左邊是樹,右邊是子對(duì)話框)
實(shí)現(xiàn)原理:create多個(gè)child類型的對(duì)話框,然后全部hide,點(diǎn)擊左邊樹的item時(shí),顯示相應(yīng)子對(duì)話框。
Q103375:
MultiDlg.exe Demonstrates Dynamic Child Dialog Boxes
http://support.microsoft.com/kb/103375/en-us
http://www.codeproject.com/dialog/ezoptionsdlg.asp
http://www.codeproject.com/dialog/csettingsdlg.asp
http://www.codeguru.com/cpp/w-d/dislog/optionsdialogs/article.php/c1953/
http://www.codeguru.com/cpp/w-d/dislog/optionsdialogs/article.php/c2015/
--------------------------------------------------------------------------------
實(shí)現(xiàn)MSN的右下角的消息彈出提示窗口
http://www.codeproject.com/dialog/statusbarmsgwnd.asp
--------------------------------------------------------------------------------
Tip of the day(每日一貼)功能的實(shí)現(xiàn)
http://www.codeproject.com/dialog/XHTMLTipOfTheDay.asp
http://www.codeguru.com/cpp/w-d/dislog/tipoftheday/article.php/c4993/
--------------------------------------------------------------------------------
不規(guī)則對(duì)話框
http://www.codeproject.com/dialog/SimpleIrregular.asp
--------------------------------------------------------------------------------
擴(kuò)展和收縮對(duì)話框
http://www.codeproject.com/dialog/dlgexpand.asp
--------------------------------------------------------------------------------
對(duì)話框漸變色
http://www.codeproject.com/dialog/WinMakeInactive.asp
--------------------------------------------------------------------------------
屏幕捕捉
http://www.codeproject.com/dialog/screencap.asp
--------------------------------------------------------------------------------
對(duì)話框菜單添加“最近使用文件列表”功能
http://www.codeproject.com/dialog/rfldlg.asp
--------------------------------------------------------------------------------
關(guān)閉對(duì)話框時(shí),逐漸消失
http://www.codeguru.com/cpp/w-d/dislog/animation/article.php/c5063/
--------------------------------------------------------------------------------
對(duì)話框背景bitmap
http://www.codeguru.com/cpp/w-d/dislog/bitmapsimages/article.php/c1877/
--------------------------------------------------------------------------------
透明對(duì)話框
http://www.codeguru.com/cpp/w-d/dislog/miscellaneous/article.php/c5065/
http://www.codeguru.com/cpp/w-d/dislog/miscellaneous/article.php/c5019/
--------------------------------------------------------------------------------
在對(duì)話框中創(chuàng)建view
http://www.codeguru.com/cpp/w-d/dislog/article.php/c5009/
--------------------------------------------------------------------------------
Splash Screen
Q817372:
How to insert a splash screen in a dialog-based application by using Visual C++ .NET or Visual C++ 2005
http://support.microsoft.com/kb/817372/en-us
Q815376:
How to create and insert a splash screen in an SDI application or in an MDI application by using Visual C++ .NET or Visual C++ 2005
http://support.microsoft.com/kb/815376/en-us
http://www.codeguru.com/cpp/w-d/dislog/splashscreens/article.php/c2011/
http://www.codeguru.com/cpp/w-d/dislog/miscellaneous/article.php/c5019/
http://www.codeguru.com/cpp/w-d/dislog/splashscreens/article.php/c5029/
--------------------------------------------------------------------------------
分割對(duì)話框
http://www.codeguru.com/cpp/w-d/dislog/splitterwindowswithingdialogs/article.php/c4973/
http://www.codeguru.com/cpp/w-d/dislog/splitterwindowswithingdialogs/article.php/c2031/
http://www.codeguru.com/cpp/w-d/dislog/splitterwindowswithingdialogs/article.php/c1979/
--------------------------------------------------------------------------------
標(biāo)題欄Title Bar
http://www.codeguru.com/cpp/w-d/dislog/titlebar/article.php/c1897/
--------------------------------------------------------------------------------
添加狀態(tài)欄statusbar和工具欄toolbar
Q123158:
Adding Control Bars to Foundation Classes Dialogs
http://support.microsoft.com/kb/123158/en-us
Visual C++ MFC Samples
DLGCBR32 Sample: Demonstrates Adding a Status Bar and Toolbar to Dialog Boxes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/_sample_mfc_DLGCBR32.asp
http://www.codeguru.com/cpp/w-d/dislog/toolbarsandstatusbars/article.php/c1955/
http://www.codeguru.com/cpp/w-d/dislog/toolbarsandstatusbars/article.php/c1939/
http://www.codeguru.com/cpp/w-d/dislog/toolbarsandstatusbars/article.php/c1949/
--------------------------------------------------------------------------------
Tooltip
Q141758:
How to add tooltips for controls to an MFC modal dialog box
http://support.microsoft.com/kb/141758/en-us
http://www.codeguru.com/cpp/w-d/dislog/tooltipsfordialogcontrols/article.php/c2017/
http://www.codeguru.com/cpp/w-d/dislog/tooltipsfordialogcontrols/article.php/c1843/
http://www.codeguru.com/cpp/w-d/dislog/tooltipsfordialogcontrols/article.php/c1839/
http://www.codeproject.com/miscctrl/pptooltip.asp
--------------------------------------------------------------------------------
從對(duì)話框邊緣平滑彈出對(duì)話框
http://www.codeguru.com/cpp/w-d/dislog/miscellaneous/article.php/c5061/