(1) 如何通過代碼獲得應(yīng)用程序主窗口的 指針?
主窗口的 指針保存在CWinThread::m_pMainWnd中,調(diào)用AfxGetMainWnd實現(xiàn)。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//使程序最大化.
(2) 確定應(yīng)用程序的路徑
Use GetModuleFileName 獲得應(yīng)用程序的路徑,然后去掉可執(zhí)行文件名。
Example:
TCHAR
exeFullPath[MAX_PATH] // MAX_PATH在API中定義了吧,好象是
128
GetModuleFileName(NULL,exeFullPath,MAX_PATH)
(3) 如何在程序中獲得其他程序的 圖標(biāo)?
兩種方法:
(1) SDK函數(shù) SHGetFileInfo 或使用 ExtractIcon獲得圖標(biāo)資源的 handle,
(2) SDK函數(shù) SHGetFileInfo 獲得有關(guān)文件的很多信息,如大小圖標(biāo),屬性, 類型等.
Example(1):
在程序窗口左上角顯示 NotePad圖標(biāo).
void CSampleView:
OnDraw(CDC * pDC)
{
if( :: SHGetFileInfo(_T("c:\\pwin95\\notepad.exe"),0,
&stFileInfo,sizeof(stFileInfo),SHGFI_ICON))
{
pDC ->DrawIcon(10,10,stFileInfo.hIcon)
}
}
Example(2):同樣功能,Use ExtractIcon Function
void CSampleView:: OnDraw(CDC *pDC)
{
HICON hIcon=:: ExtractIcon(AfxGetInstanceHandle(),_T
("NotePad.exe"),0)
if (hIcon &&hIcon!=(HICON)-1)
pDC->DrawIcon(10,10,hIcon)
}
說明: 獲得notepad.exe的路徑正規(guī)上來說用GetWindowsDirectory函數(shù)得到, 如果是調(diào)用 win95下的畫筆,應(yīng)該用訪問注冊表的方法獲得其路徑,要作成一個比較考究的程序,考慮應(yīng)該全面點.
(4) 獲得各種目錄信息
Windows目錄: Use "GetWindowsDirectory"
Windows下的system目錄: Use "GetSystemDirectory"
temp目錄: Use "GetTempPath"
當(dāng)前目錄: Use "GetCurrentDirectory"
請注意前兩個函數(shù)的第一個參數(shù)為目錄變量名,后一個為緩沖區(qū)后兩個相反.
(5) 如何自定義消息
1) 手工定義消息,可以這么寫
#define WM_MY_MESSAGE(WM_USER+100),
MS 推薦的至少是 WM_USER+100
(2)寫消息處理函數(shù),用
WPARAM,LPARAM返回LRESULT.
LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)
{
temp目錄: Use "GetTempPath"
//加入你的處理函數(shù) irectory"
}
(6) 如何改變窗口的圖標(biāo)?
向窗口發(fā)送 WM_SETICON消息。
Example:
HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)
ASSERT(hIcon)
AfxGetMainWnd() ->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon)
(7) 如何改變窗口的缺省風(fēng)格?
重載 CWnd:: PreCreateWindow 并修改CREATESTRUCT結(jié)構(gòu)來指定窗口風(fēng)格和其他創(chuàng)建信息.
Example: Delete "Max" Button and Set Original
Window‘s Position and Size
BOOL CMainFrame:: PreCreateWindow
(CREATESTRUCT &cs)
{
cs.style &=~WS_MAXINIZEMOX
cs.x=cs.y=0
cs.cx=GetSystemMetrics(SM_CXSCREEN/2)
cs.cy=GetSystemMetrics(SM_CYSCREEN/2)
return CMDIFramewnd ::PreCreateWindow(cs)
}
(9) 如何讓窗口和 MDI窗口一啟動就最大化和最小化?
先說窗口。
在 InitStance 函數(shù)中設(shè)定 m_nCmdShow的取值.
m_nCmdShow=SW_SHOWMAXMIZED //最大化
m_nCmdShow=SW_SHOWMINMIZED //最小化
m_nCmdShow=SW_SHOWNORMAL //正常方式
MDI窗口:
如果是創(chuàng)建新的應(yīng)用程序,可以用MFC AppWizard 的Advanced 按鈕并在MDI子窗口風(fēng)格組中檢測最大化或最小化還可以重載 MDI Window 的PreCreateWindow函數(shù),設(shè)置WS_MAXMIZE or WS_MINMIZE
如果從 CMDIChildWnd派生,調(diào)用 OnInitialUpdate函數(shù)中的 CWnd::Show Window來指定 MDI Child Window的風(fēng)格。
(11) 如何使窗口不可見?
很簡單,用SW_HIDE 隱藏窗口,可以結(jié)合 FindWindow,ShowWindow控制.
(12) 如何創(chuàng)建一個字回繞的CEditView
重載CWnd : : PreCreateWindow和修改CREATESTRUCT結(jié)構(gòu),關(guān)閉CEditView對象的ES_AUTOHSCROLL和WS_HSCROLL風(fēng)格位, 由于CEditView : : PreCreateWindow顯示設(shè)置cs. style,調(diào)用基類函數(shù)后要修改cs . style。
BOOL CSampleEDitView : : PreCreateWindow (CREATESTRUCT&cs)
{
//First call basse class function .
BOOL bResutl =CEditView : : PreCreateWindow (cs)
// Now specify the new window style .
cs.style &= ~ (ES_AUTOHSCROLL |WS_HSCROLL)
return bResult
}
(13) 如何使程序保持極小狀態(tài)?
這么辦: 在恢復(fù)程序窗體大小時,Windows會發(fā)送WM_QUERY-OPEN消息,用 ClassWizard設(shè)置成員函數(shù)
OnQueryOpen() ,add following code:
Bool CMainFrame:: OnQueryOpen( )
{
Return false
}
(14) 移動窗口
調(diào)用CWnd : : SetWindowPos并指定SWP_NOSIZE標(biāo)志。目的位置與父窗口有關(guān)(頂層窗口與屏幕有關(guān))。調(diào)用CWnd : : MoveWindow時必須要指定窗口的大小。
//Move window to positoin 100 , 100 of its parent window .
SetWindowPos (NULL, 100 , 100 , 0 , 0 , SWP_NOSIZE |SWP_NOAORDER)
(15) 通用控件的顯示窗口
MFC提供了幾個CView派生的視窗類, 封裝了通用控件的功能,但仍然使用工作框文檔顯示窗口體系結(jié)構(gòu):CEditView封裝了編輯控件,CTreeView保持了樹列表控件,CListView封裝了列表顯示窗口控件,CRichEditView可以處理多種編輯控件。
(16) 重置窗口的大小
調(diào)用CWnd: : SetWindowPos并指定SWP_NOMOVE標(biāo)志, 也可調(diào)用CWnd : : MoveWindow 但必須指定窗口的位置。
// Get the size of the window .
Crect reWindow
GetWindowRect (reWindow )
//Make the window twice as wide and twice as tall .
SetWindowPos (NULL , 0 , 0 , reWindow . Width ( ) *2,
reWindow . Height () * 2,
SWP_NOMOVE |SWP_NOZORDER )
(17) 如何單擊除了窗口標(biāo)題欄以外的區(qū)域使窗口移動
當(dāng)窗口需要確定鼠標(biāo)位置時Windows向窗口發(fā)送WM_NCHITTEST信息,可以處理該信息使Windows認(rèn)為鼠標(biāo)在窗口標(biāo)題上。對于對話框和基于對話的應(yīng)用程序,可以使用ClassWizard處理該信息并調(diào)用基類函數(shù), 如果函數(shù)返回HTCLIENT 則表明鼠標(biāo)在客房區(qū)域,返回HTCAPTION表明鼠標(biāo)在Windows的標(biāo)題欄中。
UINT CSampleDialog : : OnNcHitTest (Cpoint point )
{
UINT nHitTest =Cdialog: : OnNcHitTest (point )
return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest
}
上述技術(shù)有兩點不利之處,
其一是在窗口的客戶區(qū)域雙擊時,窗口將極大;
其二, 它不適合包含幾個視窗的主框窗口。
還有一種方法,當(dāng)用戶按下鼠標(biāo)左鍵使主框窗口認(rèn)為鼠標(biāo)在其窗口標(biāo)題上,使用ClassWizard在視窗中處理WM_LBUTTODOWN信息并向主框窗口發(fā)送一個WM_NCLBUTTONDOWN信息和一個單擊測試HTCAPTION。
void CSampleView : : OnLButtonDown (UINT nFlags , Cpoint point
)
{
CView : : OnLButtonDow (nFlags , pont )
//Fool frame window into thinking somene clicked
on
its caption bar .
GetParentFrame ( ) —> PostMessage (
WM_NCLBUTTONDOWN ,
HTCAPTION , MAKELPARAM (poitn .x , point .y) )
}
該技術(shù)也適用于對話框和基于對的應(yīng)用程序,只是不必調(diào)用
CWnd: :GetParentFrame 。
void CSampleDialog : : OnLbuttonDown (UINT nFlags, Cpoint point )
{
Cdialog : : OnLButtonDow (nFlags, goint )
//Fool dialog into thinking simeone clicked on its
caption bar .
PostMessage (WM_NCLBUTTONDOWN , HTCAPTION , MAKELPARM (point.x
, point. y
) )
}
(18) 如何改變視窗的背景顏色
Windows向窗口發(fā)送一個WM_ERASEBKGND消息通知該窗口擦除背景,可以使用ClassWizard重載該消息的缺省處理程序來擦除背景(實際是畫),并返回TRUE以防止Windows擦除窗口。
//Paint area that needs to be erased.
BOOL CSampleView : : OnEraseBkgnd (CDC* pDC)
{
// Create a pruple brush.
CBrush Brush (RGB (128 , 0 , 128) )
// Select the brush into the device context .
CBrush* pOldBrush = pDC—>SelcetObject (&brush)
// Get the area that needs to be erased .
CRect reClip
pDC—>GetCilpBox (&rcClip)
//Paint the area.
pDC—> PatBlt (rcClip.left , rcClip.top , rcClip.Width ( ) , rcClip.Height( ) , PATCOPY )
//Unselect brush out of device context .
pDC—>SelectObject (pOldBrush )
// Return nonzero to half fruther processing .
return TRUE
}
(19) 如何改變窗口標(biāo)題
調(diào)用CWnd : : SetWindowText可以改變?nèi)魏未翱冢ò丶┑臉?biāo)題。
//Set title for application‘s main frame window .
AfxGetMainWnd ( ) —> SetWindowText (_T("Application title") )
//Set title for View‘s MDI child frame window .
GetParentFrame ( ) —> SetWindowText ("_T ("MDI Child Frame new title")
)
//Set title for dialog‘s push button control.
GetDigitem (IDC_BUTTON) —> SetWindowText (_T ("Button new title ") )
如果需要經(jīng)常修改窗口的標(biāo)題(注:控件也是窗口),應(yīng)該考慮使用半文檔化的函數(shù)AfxSetWindowText。該函數(shù)在AFXPRIV.H中說明,在WINUTIL.CPP中實現(xiàn),在聯(lián)機幫助中找不到它,它在AFXPRIV.H中半文檔化, 在以后發(fā)行的MFC中將文檔化。
AfxSetWindowText的實現(xiàn)如下:
voik AFXAPI AfxSetWindowText (HWND hWndCtrl , LPCTSTR IpszNew )
{
itn nNewLen= Istrlen (Ipaznew)
TCHAR szOld [256]
//fast check to see if text really changes (reduces
flash in the
controls )
if (nNewLen >_contof (szOld)
|| : : GetWindowText (hWndCrtl, szOld , _countof (szOld) !=nNewLen
|| Istrcmp (szOld , IpszNew)! = 0
{
//change it
: : SetWindowText(hWndCtrl , IpszNew )
}
}
(20) 如何防止主框窗口在其說明中顯示活動的文檔名
創(chuàng)建主框窗口和MDI子窗口進通常具有FWS_ADDTOTITLE風(fēng)格位,如果不希望在說明中自動添加文檔名, 必須禁止該風(fēng)格位, 可以使用ClassWizard重置
CWnd: : PreCreateWindow并關(guān)閉FWS_ADDTOTITLE風(fēng)格。
BOOL CMainFrame : : PreCreateWindow (CREATESTRUCT&cs)
{
//Turn off FWS_ADDTOTITLE in main frame .
cs.styel & = ~FWS_ADDTOTITLE
return CMDIFrameWnd : : PreCreateWindow (cs )
}
關(guān)閉MDI子窗口的FWS _ADDTOTITLE風(fēng)格將創(chuàng)建一個具有空標(biāo)題的窗口,可以調(diào)用CWnd: : SetWindowText來設(shè)置標(biāo)題。記住自己設(shè)置標(biāo)題時要遵循接口風(fēng)格指南。
(21) 如何獲取有關(guān)窗口正在處理的當(dāng)前消息的信息
調(diào)用CWnd: : GetCurrentMessage可以獲取一個MSG指針。例如,可以使用ClassWizard將幾個菜單項處理程序映射到一個函數(shù)中,然后調(diào)用GetCurrentMessage來確定所選中的菜單項。
viod CMainFrame : : OnCommmonMenuHandler ( )
{
//Display selected menu item in debug window .
TRACE ("Menu item %u was selected . \n" ,
(22) 如何在代碼中獲取工具條和狀態(tài)條的指針
缺省時, 工作框創(chuàng)建狀態(tài)條和工具條時將它們作為主框窗口的子窗口,狀態(tài)條有一個AFX_IDW_STATUS_BAR標(biāo)識符,工具條有一個AFX_IDW_TOOLBAR標(biāo)識符,下例說明了如何通過一起調(diào)用CWnd: : GetDescendantWindow和AfxGetMainWnd來獲取這些子窗口的指針:
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)
//Get pointer to toolbar .
CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_TOOLBAR)
(23) 如何使能和禁止工具條的工具提示
如果設(shè)置了CBRS_TOOLTIPS風(fēng)格位,工具條將顯示工具提示,要使能或者禁止工具提示,需要設(shè)置或者清除該風(fēng)格位。下例通過調(diào)用CControlBar : : GetBarStyle和CControlBar : : SetBarStyle建立一個完成此功能的成員函數(shù):
void CMainFrame : : EnableToolTips ( BOOL bDisplayTips )
{
ASSERT_VALID (m_wndToolBar)
DWORD dwStyle = m _wndToolBar.GetBarStyle ( )
if (bDisplayTips) dwStyle |=CBRS_TOOLTIPS
else
dwStyle & = ~CBRS_TOOLTIPS
m_wndToolBar.SetBarStyle (dwStyle )
}
(24) 如何創(chuàng)建一個不規(guī)則形狀的窗口
可以使用新的SDK函數(shù)SetWindowRgn。該函數(shù)將繪畫和鼠標(biāo)消息限定在窗口的一個指定的區(qū)域,實際上使窗口成為指定的不規(guī)則形狀。 使用AppWizard創(chuàng)建一個基于對的應(yīng)用程序并使用資源編輯器從主對話資源中刪除所在的缺省控件、標(biāo)題以及邊界。
給對話類增加一個CRgn數(shù)據(jù)成員,以后要使用該數(shù)據(jù)成員建立窗口區(qū)域。
Class CRoundDlg : public CDialog
{
…
private :
Crgn m_rgn : // window region
…
}
修改OnInitDialog函數(shù)建立一個橢圓區(qū)域并調(diào)用SetWindowRgn將該區(qū)域分配給窗口:
BOOL CRoundDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//Get size of dialog .
CRect rcDialog
GetClientRect (rcDialog )
// Create region and assign to window .
m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width( ) , rcDialog.Height ( ) )
SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn ,TRUE )
return TRUE
}
通過建立區(qū)域和調(diào)用SetWindowRgn,已經(jīng)建立一個不規(guī)則形狀的窗口,下面的例子程序是修改OnPaint函數(shù)使窗口形狀看起來象一個球形體。
voik CRoundDlg : : OnPaint ( )
{
CPaintDC de (this) // device context for painting
.
//draw ellipse with out any border
dc. SelecStockObject (NULL_PEN)
//get the RGB colour components of the sphere color
COLORREF color= RGB( 0 , 0 , 255)
BYTE byRed =GetRValue (color)
BYTE byGreen = GetGValue (color)
BYTE byBlue = GetBValue (color)
// get the size of the view window
Crect rect
GetClientRect (rect)
// get minimun number of units
int nUnits =min (rect.right , rect.bottom )
//calculate he horiaontal and vertical step size
float fltStepHorz = (float) rect.right /nUnits
float fltStepVert = (float) rect.bottom /nUnits
int nEllipse = nUnits/3 // calculate how many to
draw
int nIndex
// current ellipse that is being draw
CBrush brush
// bursh used for ellipse fill color
CBrush *pBrushOld // previous
brush that was selected into dc
//draw ellipse , gradually moving towards upper-right
corner
for (nIndex = 0 nIndes < + nEllipse nIndes++)
{
//creat solid brush
brush . CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ).
( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue)
/nEllipse ) ) )
//select brush into dc
pBrushOld= dc .SelectObject (&brhsh)
//draw ellipse
dc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex ,
rect. right -( (int) fltStepHorz * nIndex )+ 1,
rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1)
//delete the brush
brush.DelecteObject ( )
}
}
最后,處理WM_NCHITTEST消息,使當(dāng)擊打窗口的任何位置時能移動窗口。
UINT CRoundDlg : : OnNchitTest (Cpoint point )
{
//Let user move window by clickign anywhere on thewindow .
UINT nHitTest = CDialog : : OnNcHitTest (point)
rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest
}
(25) 如何獲取應(yīng)用程序的 實例句柄?
應(yīng)用程序的實例句柄保存在CWinApp m_hInstance 中,可以這么調(diào)用AfxGetInstancdHandle獲得句柄.
Example: HANDLE hInstance=AfxGetInstanceHandle()
(26) 如何編程結(jié)束應(yīng)用程序?
這是個很簡單又是編程中經(jīng)常要遇到的問題.
向窗口發(fā)送 WM_CLOSE消息,調(diào)用 CWnd::OnClose成員函數(shù).允許對用戶提示是否保存修改過的數(shù)據(jù).
Example: AfxGetMainWindow()->SendMessage(WM_CLOSE)
還可以創(chuàng)建一個自定義的函數(shù) Terminate Window
void Terminate Window(LPCSTR pCaption)
{
CWnd *pWnd=Cwnd::FindWindow(NULL,pCaption)
if (pWnd)
pWnd ->SendMessage(WM_CLOSE)
}
說明: FindWindow函數(shù)不是提倡的做法,因為它無法處理標(biāo)題欄自動改變,比如我們要檢測 Notepad是不是已運行而事先不知道Notepad的標(biāo)題欄,這時FindWindow就無能為力了,可以通過枚舉 windows任務(wù)列表的辦法來實現(xiàn)。在機械出版社"Windows 95 API開發(fā)人員指南"一書有比較詳細(xì)的介紹,這里就不再多說樂。
(27) 如何創(chuàng)建和使用無模式對話框
MFC將模式和無模式對話封裝在同一個類中,但是使用無模式對話需要幾個對話需要幾個額處的步驟。首先,使用資源編輯器建立對話資源并使用ClassWizard創(chuàng)建一個CDialog的派生類。模式和無模式對話的中止是不一樣的:模式對話通過調(diào)用CDialog : : EndDialog 來中止,無模式對話則是調(diào)用CWnd: : DestroyWindow來中止的,函數(shù)CDialog : : OnOK和CDialog : : OnCancel調(diào)用EndDialog ,所以需要調(diào)用DestroyWindow并重置無模式對話的函數(shù)。
void CSampleDialog : : OnOK ( )
{
// Retrieve and validate dialog data .
if (! UpdateData (TRUE) )
{
// the UpdateData rountine
will set focus to correct item TRACEO (" UpdateData failed during dialog termination .\n")
return
}
//Call DestroyWindow instead of EndDialog .
DestroyWindow ( )
}
void CSampleDialog : : OnCancel ( )
{
//Call DestroyWindow instead of EndDialog .
DestroyWindow ( )
}
其次,需要正確刪除表示對話的C++對象。對于模式對來說,這很容易,需要創(chuàng)建函數(shù)返回后即可刪除C++對象;無模式對話不是同步的,創(chuàng)建函數(shù)調(diào)用后立即返回,因而用戶不知道何時刪除C++對象。撤銷窗口時工作框調(diào)用CWnd : : PostNcDestroy,可以重置該函數(shù)并執(zhí)行清除操作,諸如刪除this指針。
void CSampleDialog : : PostNcDestroy ( )
{
// Declete the C++ object that represents this dialog.
delete this
最后,要創(chuàng)建無模式對話??梢哉{(diào)用CDialog : : DoModal創(chuàng)建一個模式對放,要創(chuàng)建一個無模式對話則要調(diào)用CDialog: : Create。下面的例子說明 了應(yīng)用程序是如何創(chuàng)建無模式對話的: 象;無模式對話不是同步的,創(chuàng)建函數(shù)調(diào)用后立即返回,
void CMainFrame : : OnSampleDialog ( )
{
//Allocate a modeless dialog object .
CSampleDilog * pDialog =new CSampleDialog
ASSERT_VALID (pDialog) Destroy ( )
//Create the modeless dialog . represents this dialog.
BOOL bResult = pDialog —> Creste (IDD_IDALOG)
ASSERT (bResult )
}
(28) 如何防止主框窗口在其說明中顯示活動的文檔名
創(chuàng)建主框窗口和MDI子窗口進通常具有FWS_ADDTOTITLE風(fēng)格位,如果不希望在說明中自動添加文檔名, 必須禁止該風(fēng)格位, 可以使用ClassWizard重置
CWnd: : PreCreateWindow并關(guān)閉FWS_ADDTOTITLE風(fēng)格。
BOOL CMainFrame : : PreCreateWindow (CREATESTRUCT&cs)
{
//Turn off FWS_ADDTOTITLE in main frame .
cs.styel & = ~FWS_ADDTOTITLE
return CMDIFrameWnd : : PreCreateWindow (cs )
}
關(guān)閉MDI子窗口的FWS _ADDTOTITLE風(fēng)格將創(chuàng)建一個具有空標(biāo)題的窗口,可以調(diào)用CWnd: : SetWindowText來設(shè)置標(biāo)題。記住自己設(shè)置標(biāo)題時要遵循接口風(fēng)格指南。
(29) 如何在代碼中獲取工具條和狀態(tài)條的指針
缺省時, 工作框創(chuàng)建狀態(tài)條和工具條時將它們作為主框窗口的子窗口,狀態(tài)條有一個AFX_IDW_STATUS_BAR標(biāo)識符,工具條有一個AFX_IDW_TOOLBAR標(biāo)識符,下例說明了如何通過一起調(diào)用CWnd: : GetDescendantWindow和AfxGetMainWnd來獲取這些子窗口的指針:
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)
//Get pointer to toolbar .
CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_TOOLBAR)
(30) 怎樣加載其他的應(yīng)用程序?
三個SDK函數(shù) winexec, shellexecute,createprocess可以使用。
WinExec最簡單,兩個參數(shù),前一個指定路徑,后一個指定顯示方式.后一個參數(shù)值得說一下,比如泥用 SW_SHOWMAXMIZED方式去加載一個無最大化按鈕的程序,就是Neterm,calc等等,就不會出現(xiàn)正常的窗體,但是已經(jīng)被加到任務(wù)列表里了。
ShellExecute較 WinExex靈活一點,可以指定工作目錄,下面的Example就是直接打開 c:\temp\1.txt,而不用加載與 txt文件關(guān)聯(lián)的應(yīng)用程序,很多安裝程序完成后都會打開一個窗口,來顯示Readme or Faq,我猜就是這么作的啦.
ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOWMAXMIZED)
CreateProcess最復(fù)雜,一共有十個參數(shù),不過大部分都可以用NULL代替,它可以指定進程的安全屬性,繼承信息,類的優(yōu)先級等等.來看個很簡單的Example:
STARTUPINFO stinfo
//啟動窗口的信息
PROCESSINFO procinfo //進程的信息
CreateProcess(NULL,_T("notepad.exe"),NULL,NULL.FALSE,
NORMAL_PRIORITY_
CLASS,NULL,NULL, &stinfo,&procinfo)
(31) 如何在代碼中獲取工具條和狀態(tài)條的指針
缺省時, 工作框創(chuàng)建狀態(tài)條和工具條時將它們作為主框窗口的子窗口,狀態(tài)條有一個AFX_IDW_STATUS_BAR標(biāo)識符,工具條有一個AFX_IDW_TOOLBAR標(biāo)識符,下例說明了如何通過一起調(diào)用CWnd: : GetDescendantWindow和AfxGetMainWnd來獲取這些子窗口的指針:
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)
(32) 如何使能和禁止工具條的工具提示
如果設(shè)置了CBRS_TOOLTIPS風(fēng)格位,工具條將顯示工具提示,要使能或者禁止工具提示,需要設(shè)置或者清除該風(fēng)格位。下例通過調(diào)用CControlBar : : GetBarStyle和CControlBar : : SetBarStyle建立一個完成此功能的成員函數(shù):
void CMainFrame : : EnableToolTips ( BOOL bDisplayTips )
{
ASSERT_VALID (m_wndToolBar)
DWORD dwStyle = m _wndToolBar.GetBarStyle ( )
if (bDisplayTips) dwStyle |=CBRS_TOOLTIPS
else
dwStyle & = ~CBRS_TOOLTIPS
m_wndToolBar.SetBarStyle (dwStyle )
}
//Get pointer to toolbar .
CToolBar * pToolBar = (CToolBar * ) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_TOOLBAR)
(33) 如何設(shè)置工具條標(biāo)題
工具條是一個窗口,所以可以在調(diào)用CWnd : : SetWindowText來設(shè)置標(biāo)題,例子如下:
int CMainFrame : : OnCreate (LPCREATESTRUCT lpCreateStruct )
{
…
// Set the caption of the toolbar .
m_wndToolBar.SetWindowText (_T "Standdard")
(34) 如何使窗口始終在最前方?
BringWindowToTop(Handle)
SetWindowPos函數(shù),指定窗口的 最頂風(fēng)格,用WS_EX_TOPMOST擴展窗口的風(fēng)格
Example:
void ToggleTopMost(
CWnd *pWnd)
{
ASSERT_VALID(pWnd)
pWnd ->SetWindowPos(pWnd-> GetStyle( ) &WS_EX_TOPMOST)?
&wndNoTopMOST: &wndTopMost,0,0,0,0,SSP_NOSIZE|WSP_NOMOVE)
}
(35) 如何在對話框中顯示一個位圖
這要歸功于Win 32先進的靜態(tài)控件和Microsoft的資源編輯器,在對話框中顯示位圖是很容易的, 只需將圖形控件拖到對話中并選擇適當(dāng)屬性即可,用戶也可以顯示圖標(biāo)、位圖以及增強型元文件。
(36) 如何改變對話或窗體視窗的背景顏色
調(diào)用CWinApp : : SetDialogBkColor可以改變所有應(yīng)用程序的背景顏色。第一個參數(shù)指定了背景顏色,第二個參數(shù)指定了文本顏色。下例將應(yīng)用程序?qū)υ捲O(shè)置為藍色背景和黃色文本。
BOOL CSampleApp : : InitInstance ( )
{
…
//use blue dialog with yellow text .
SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) )
…
}
需要重畫對話(或?qū)υ挼淖涌丶r,Windows向?qū)υ挵l(fā)送消息WM_CTLCOLOR,通常用戶可以讓W(xué)indows選擇繪畫背景的刷子,也可重置該消息指定刷子。下例說明了創(chuàng)建一個紅色背景對話的步驟。
首先,給對話基類增加一人成員變量
CBursh :class CMyFormView : public CFormView
{
…
private :
CBrush m_ brush // background brush
…
}
其次, 在類的構(gòu)造函數(shù)中將刷子初始化為所需要的背景顏色。
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255) )
}
最后,使用ClassWizard處理WM_CTLCOLOR消息并返回一個用來繪畫對話背景的刷子句柄。注意:由于當(dāng)重畫對話控件時也要調(diào)用該函數(shù),所以要檢測nCtlColor參量。
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor
)
{
// Determine if drawing a dialog box . If we are, return +handle to
//our own background brush . Otherwise let windows handle it .
if (nCtlColor = = CTLCOLOR _ DLG )
return (HBRUSH) m_brush.GetSafeHandle ( )
return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor
)
}
(37) 如何獲取一個對話控件的指針
有兩種方法。其一,調(diào)用CWnd: : GetDlgItem,獲取一個CWnd*指針調(diào)用成員函數(shù)。下例調(diào)用GetDlgItem,將返回值傳給一個CSpinButtonCtrl*以便調(diào)用CSpinButtonCtrl : : SetPos 函數(shù):
BOOL CSampleDialog : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//Get pointer to spin button .
CSpinButtonCtrl * pSpin - ( CSpinButtonCtrl *) GetDlgItem(IDC_SPIN)
ASSERT _ VALID (pSpin)
//Set spin button‘s default position .
pSpin —> SetPos (10)
return TRUE
}
其二, 可以使用ClassWizard將控件和成員變量聯(lián)系起來。在ClassWizard中簡單地選擇Member Variables標(biāo)簽,然后選擇Add Variable …按鈕。如果在對話資源編輯器中,按下Ctrl鍵并雙擊控件即可轉(zhuǎn)到Add Member Variable對話。
(38) 如何禁止和使能控件
控件也是窗口,所以可以調(diào)用CWnd : : EnableWindow使能和禁止控件。
//Disable button controls .
m_wndOK.EnableWindow (FALSE )
m_wndApply.EnableWindow (FALSE )
(39) 如何改變控件的字體
由于控件是也是窗口,用戶可以調(diào)用CWnd: : SetFont指定新字體。該函數(shù)用一個Cfont指針,要保證在控件撤消之前不能撤消字體對象。下例將下壓按鈕的字體改為8點Arial字體:
//Declare font object in class declaration (.H file ).
private : Cfont m_font
// Set font in class implementation (.Cpp file ). Note m_wndButton is a
//member variable added by ClassWizard.DDX routines hook the member
//variable to a dialog button contrlo.
BOOL CSampleDialog : : OnInitDialog ( )
{
…
//Create an 8-point Arial font
m_font . CreateFont (MulDiv (8 , -pDC
—> GetDeviceCaps(LOGPIXELSY) ,72). 0 , 0 , 0 , FW_NORMAL , 0 , 0,0, ANSI_CHARSER, OUT_STROKE_PRECIS ,
CLIP_STROKE _PRECIS , DRAFT _QUALITY
VARIABLE_PITCH |FF_SWISS, _T("Arial") )
//Set font for push button .
m_wndButton . SetFont (&m _font )
…
}
(40) 如何在OLE控件中使用OLE_COLOR數(shù)據(jù)類型
諸如COleControl : : GetFortColor和COleControl : : GetBackColor等函數(shù)返回OLE _COLOR數(shù)據(jù)類型的顏色,而GDI對象諸如筆和刷子使用的是COLORREF數(shù)據(jù)類型,調(diào)用COleControl : : TranslateColor可以很容易地將OLE_COLOR類型改為COLORREF類型。下例創(chuàng)建了一個當(dāng)前背景顏色的刷子:
void CSampleControl : : OnDraw (CDC* pdc
const Crect& rcBounds , const Crect& rcInvalid
)
{
//Create a brush of the cuttent background color.
CBrush brushBack (TranslateColor (GetBackColor () ) )
//Paint the background using the current backgroundcolor .
pdc—> FilllRect (rcBounds , &brushBack)
//other drawign commands
…
}
(41) 在不使用通用文件打開對話的情況下如何顯示一個文件列表
調(diào)用CWnd: : DlgDirList或者CWnd: : DlgDirListComboBox,Windows 將自動地向列表框或組合框填充可用的驅(qū)動器名或者指定目錄中的文件,下例將Windows目錄中的文件填充在組合框中:
BOOL CSampleDig : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
TCHAR szPath [MAX_PATH] = {"c:\\windows"}
int nReslt = DlgDirListComboBox (szPath, IDC_COMBO , IDC_CURIDIR, DDL_READWRITE |DDL_READONLY|DDL_HIDDEN| DDL_SYSTEM|DDL_ARCHIVE)
return TRUE
}
(42) 為什么旋轉(zhuǎn)按鈕控件看起來倒轉(zhuǎn)
需要調(diào)用CSpinCtrl : : SetRange 設(shè)置旋轉(zhuǎn)按鈕控件的范圍,旋轉(zhuǎn)按鈕控件的缺省上限為0,缺省下限為100,這意味著增加時旋轉(zhuǎn)按控件的值由100變?yōu)?。下例將旋轉(zhuǎn)按鈕控件的范圍設(shè)置為0到100:
BOOL CAboutDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//set the lower and upper limit of the spin button
m_wndSpin . SetRange ( 0 ,100 )
return TRUE
}
Visual C++ 4.0 Print對話中的Copise旋轉(zhuǎn)按鈕控件也有同樣的問題:按下Up按鈕時拷貝的數(shù)目減少,而按下Down 按鈕時拷貝的數(shù)目增加。
(43) 為什么旋轉(zhuǎn)按鈕控件不能自動地更新它下面的編輯控件
如果使用旋轉(zhuǎn)按鈕的autu buddy特性, 則必須保證在對話的標(biāo)記順序中buddy窗口優(yōu)先于旋轉(zhuǎn)按鈕控件。從Layout菜單中選擇Tab Order菜單項(或者按下Crtl+D)可以設(shè)置對話的標(biāo)簽順序。
(44) 如何用位圖顯示下壓按鈕
Windows 95按鈕有幾處新的創(chuàng)建風(fēng)格,尤其是BS_BITMAP和BS_ICON,要想具有位圖按鈕,創(chuàng)建按鈕和調(diào)用CButton : : SetBitmap或CButton : : SetIcon時要指定BS_BITMAP或BS_ICON風(fēng)格。
首先,設(shè)置按鈕的圖標(biāo)屬性。然后,當(dāng)對話初始化時調(diào)用CButton: : SetIcon。注意:下例用圖標(biāo)代替位圖,使用位圖時要小心,因為不知道背景所有的顏色——并非每個人都使用淺灰色。
BOOL CSampleDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//set the images for the push buttons .
BOOL CSampleDlg : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
//set the images for the push buttons .
m_wndButton1.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION1))
m_wndButton2.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION2))
m_wndButton3.SetIcon (AfxGetApp ( ) —> LoadIcon (IDI _ IPTION3))
return TRUE
}
(45) 如何一個創(chuàng)建三態(tài)下壓按鈕
可以使用新的BS_PUSHBUTTON 風(fēng)格位和檢測框以及按鈕來創(chuàng)建一個三態(tài)下壓按鈕。這很容易,只需將檢測框和按鈕拖拉到對話中并指定屬性Push—like即可。不用任何附加程序就可以成為三態(tài)下壓按鈕。
(46) 如何動態(tài)創(chuàng)建控件
分配一個控件對象的實例并調(diào)用其Create成員函數(shù)。開發(fā)者最容易忽略兩件事:忘記指定WS_VISBLE標(biāo)簽和在棧中分配控件對象。下例動態(tài)地創(chuàng)建一個下壓按鈕控件:
//In class declaration (.H file ).
private : CButton* m _pButton
//In class implementation (.cpp file ) .
m_pButton =new CButton
ASSERT_VALID (m_pButton)
m_pButton —>Create (_T ("Button Title ") , WS_CHILD |WS_VISIBLE |BS_PUSHBUTTON. Crect ( 0, 0, 100 , 24) , this , IDC _MYBUTTON )
(47) 如何限制編輯框中的準(zhǔn)許字符
如果用戶在編輯控件中只允許接收數(shù)字,可以使用一個標(biāo)準(zhǔn)的編輯控件并指定新的創(chuàng)建標(biāo)志ES_NUMBERS,它是Windows 95新增加的標(biāo)志,該標(biāo)志限制 編輯控件只按收數(shù)字字符。如果用戶需要復(fù)雜的編輯控件,可以使用Microsoft 的屏蔽編輯控件,它是一個很有用的OLE定制控件。
如果希望不使用OLE 定制控件自己處理字符,可以派生一個CEdit類并處理WM_CHAR消息,然后從編輯控件中過濾出特定的字符。首先,使用ClassWizard建立一個 CEdit的派生類,其次,在對話類中指定一個成員變量將編輯控件分類在OnInitdialog 中調(diào)用CWnd: : SubclassDlgItem .
//In your dialog class declaration (.H file )
private : CMyEdit m_wndEdit // Instance of your new edit control .
//In you dialog class implementation (.CPP file )
BOOL CSampleDialog : : OnInitDialog ( )
{
…
//Subclass the edit lontrod .
m_wndEdit .SubclassDlgItem (IDC_EDIT,this)
…
}
使用ClassWizard處理WM_CHAR消息,計算nChar參量并決定所執(zhí)行的操作,用戶可以確定是否修改、傳送字符。下例說明了如何顯示字母字符,如果字符是字母字符,則調(diào)用CWnd OnChar,否則不調(diào)用OnChar.
//Only display alphabetic dharacters .
void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UITN nFlags )
{
//Determine if nChar is an alphabetic character.
if (: : IsCharAlpha ( ( TCHAR) nChar ) )
CEdit : : OnChar (nChar, nRepCnt , nFlags )
}
如果要修改字符,則不能僅僅簡單地用修改過的nChar調(diào)用CEdit: : OnChar,然后CEdit: : OnChar調(diào)用CWnd: : Default獲取原來的wParam 和lParam 的值,這樣是不行的。要修改一個字符,需要首先修改nChar,然后用修改過的nChar調(diào)用CWnd: : DefWindowProc。下例說明了如何將字符轉(zhuǎn)變?yōu)榇髮懀?br>//Make all characters uppercase
void CMyEdit : : OnChar (UINT nChar , UINT nRepCnt , UINT nFlags )
{
//Make sure character is uppercase .
if (: : IsCharAlpha ( .( TCHAR) nChar)
nChar=: : CharUpper(nChar )
//Bypass default OnChar processing and directly call
//default window proc.
DefWindProc (WM_CHAR, nChar , MAKELPARAM (nRepCnt, nFlags ))
}
(48) 如何改變控件的顏色
有兩種方法。其一,可以在父類中指定控件的顏色,或者利用MFC4.0新的消息反射在控件類中指定顏色。 當(dāng)控件需要重新著色時,工作框調(diào)用父窗口(通常是對話框)的CWnd: : OnCrtlColor,可以在父窗口類中重置該函數(shù)并指定控件的新的繪畫屬性。例如,下述代碼將對話中的所有編輯控件文本顏色改為紅色:
HBRUSH CAboutDig : : OnCtlColor (CDC * pDCM , CWnd * pWnd , UINT nCtlColor)
{
HBRUSH hbr = CDialog : : OnCtlColor (pDC, pWnd , nCtlColor )
//Draw red text for all edit controls .
if (nCtlColor= = CTLCOLOR_EDIT )
pDC —> SetTextColor (RGB (255, 0 , 0 , ) )
return hbr
}
然而,由于每個父窗口必須處理通知消息并指定每個控件的繪畫屬性,所以,這種方法不是完全的面向?qū)ο蟮姆椒?。控件處理該消息并指定繪畫屬性更合情合理。消息反射允許用戶這樣做。通知消息首先發(fā)送給父窗口,如果父窗口沒有處理則發(fā)送給控件。創(chuàng)建一個定制彩色列表框控件必須遵循下述步驟。
首先,使用ClassWizard 創(chuàng)建一個CListBox 的派生類并為該類添加下述數(shù)據(jù)成員。
class CMyListBox publilc CListBox
{
…
private
COLORREF m_clrFor // foreground color
COLORREF m_clrBack //background color
Cbrush m_brush //background brush
…
}
其次,在類的構(gòu)造函數(shù)中,初始化數(shù)據(jù)中。
CMyListBox : : CMyListBox ()
{
//Initialize data members .
m_clrFore =RGB (255 , 255 , 0) //yellow text
m_clrBack=RGB (0 , 0 , 255) // blue background
m_brush . CreateSolidBrush (m _clrBack )
}
最后,使用ClassWizard處理反射的WM_CTLCOLOR(=WM_CTLCOLOR)消息并指定新的繪畫屬性。
HBRUSH CMyListBox : : CtlColor (CDC* pDC, UINT nCtlColor )
{
pDC—>SetTextColor (m_clrFore)
pDC—>SetBkColor (m_clrBack)
return (HBRUSH) m_brush.GetSafeHandle ()
}
現(xiàn)在,控件可以自己決定如何繪畫,與父窗口無關(guān)。
(49) 當(dāng)向列表框中添加多個項時如何防止閃爍
調(diào)用CWnd::SetRedraw 清除重畫標(biāo)志可以禁止CListBox(或者窗口)重畫。當(dāng)向列表框添加幾個項時,用戶可以清除重畫標(biāo)志,然后添加項,最后恢復(fù)重畫標(biāo)志。為確保重畫列表框的新項,調(diào)用SetRedraw (TRUE) 之后調(diào)用CWnd::Invalidate。
//Disable redrawing.
pListBox->SetRedraw (FALSE)
//Fill in the list box gere
//Enable drwing and make sure list box is redrawn.
pListBox->SetRedraw (TRUE)
pListBox->Invalidate ()
(50) 如何向編輯控件中添加文本
由于沒有CEdit:: AppendText函數(shù),用戶只好自己做此項工作。調(diào)用CEdit:: SetSel移動到編輯控件末尾,然后調(diào)用CEdit:: ReplaceSel添加文本。下例是AppendText 的一種實現(xiàn)方法:
void CMyEdit:: AppendText (LPCSTR pText)
{
int nLen=GetWindowTextLength ()
SetFocus ()
SetSel (nLen, nLen)
ReplaceSel (pText)
}
(51) 如何訪問預(yù)定義的GDI對象
可以通過調(diào)用CDC:: SlectStockObject使用Windows的幾個預(yù)定義的對象,諸如刷子、筆以及字體。下例使用了Windows預(yù)定義的筆和刷子GDI對象在視窗中畫一個橢圓。
//Draw ellipse using stock black pen and gray brush.
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine size of view.
CRect rcView
GetClientRect (rcView)
//Use stock black pen and stock gray brush to draw ellipse.
pDC->SelectStockObject (BLACK_PEN)
pDC->SelectStockObject (GRAY_BRUSH)
//Draw the ellipse.
pDC->Ellipse (reView)
}
也可以調(diào)用新的SDK函數(shù)GetSysColorBrush獲取一個系統(tǒng)顏色刷子,下例用背景色在視窗中畫一個橢圓:
void CsampleView:: OnDraw (CDC* pDC)
{
//Determine size of view.
CRect rcView
GetClientRect (rcView)
//Use background color for tooltips brush.
CBrush * pOrgBrush=pDC->SelectObject ( CBrush ::FromHandle( ::GetSysColorBrush (COLOR_INFOBK)))
//Draw the ellipse.
pDC->Ellipse (rcView)
//Restore original brush.
pDC->SelectObject (pOrgBrush)
}
(52) 如何獲取GDI對象的屬性信息
可以調(diào)用GDIObject:: GetObject。這個函數(shù)將指定圖表設(shè)備的消息寫入到緩沖區(qū)。下例創(chuàng)建了幾個有用的輔助函數(shù)。
//Determine if font is bold.
BOOL IsFontBold (const CFont&font)
{
LOGFONT stFont
font.GetObject (sizeof (LOGFONT), &stFont)
return (stFont.lfBold)? TRUE: FALSE
}
//Return the size of a bitmap.
CSize GetBitmapSize (const CBitmap&bitmap)
{
BITMAP stBitmap
bitmap.GetObject (sizeof (BITMAP), &stBitmap)
return CSize (stBitmap.bmWidth, stBitmap.bmHeight)
}
//Create a pen with the same color as a brush.
BOOL CreatePenFromBrush (Cpen&pen, cost Cbrush&brush)
{
LOGBRUSH stBrush
brush.Getobject (sizeof (LOGBRUSH), &stBrush)
return pen. Createpen (PS_SOLID, 0, stBrush.ibColor)
}
(53) 如何實現(xiàn)一個橡皮區(qū)矩形
CRectTracker是一個很有用的類,可以通過調(diào)用CRectTracker::TrackRubberBand 響應(yīng)WM_LBUTTONDOWN消息來創(chuàng)建一個橡皮區(qū)矩形。
下例表明使用CRectTracker移動和重置視窗中的藍色橢圓的大小是很容易的事情。
首先,在文件檔中聲明一個CRectTracker數(shù)據(jù)成員:
class CSampleView : Public CView
{
…
public :
CrectTracker m_tracker
…
}
其次,在文檔類的構(gòu)造函數(shù)中初始化CRectTracker 對象:
CSampleDoc:: CSampleDOC ()
{
//Initialize tracker position, size and style.
m_tracker.m_rect.SetRect (0, 0, 10, 10)
m_tracker.m_nStyle=CRectTracker:: resizeInside | CRectTracker ::dottedLine
}
然后,在OnDraw函數(shù)中畫橢圓和蹤跡矩形:
void CSampleView:: OnDraw (CDC* pDC)
{
CSampleDoc* pDoc=GetDocument ()
ASSERT_VALID (pDoc)
//Select blue brush into device context.
CBrush brush (RGB (0, 0, 255))
CBrush* pOldBrush=pDC->SelectObject (&brush)
//draw ellipse in tracking rectangle.
Crect rcEllipse
pDoc->m_tracker.GetTrueRect (rcEllipse)
pDC->Ellipse (rcEllipse)
//Draw tracking rectangle.
pDoc->m_tracker.Draw (pDC)
//Select blue brush out of device context.
pDC->Selectobject (pOldBrush)
}
最后,使用ClassWizard處理WM_LBUTTONDOWN消息,并增加下述代碼。該段代碼根據(jù)鼠標(biāo)擊鍵情況可以拖放、移動或者重置橢圓的大小。
void CSampleView::OnLButtonDown (UINT nFlags, CPoint point)
{
//Get pointer to document.
CSampleDoc* pDoc=GetDocument ()
ASSERT_VALID (pDoc)
//If clicked on ellipse, drag or resize it.Otherwise create a
//rubber-band rectangle nd create a new ellipse.
BOOL bResult=pDoc->m_tracker.HitTest (point)!= CRectTracker::hitNothing
//Tracker rectangle changed so update views.
if (bResult)
{
pDoc->m_tracker.Track (this,point,TRue)
pDoc->SetModifiedFlag ()
pDoc->UpdateAllViews (NULL)
}
else
pDoc->m-tracker.TrackRubberBand(this,point,TRUE)
CView:: onLButtonDown (nFlags,point)
}
(54) 如何更新翻轉(zhuǎn)背景顏色的文本
調(diào)用CDC:: SetBkmode并傳送OPAQUE用當(dāng)前的背景顏色填充背景,或者調(diào)用CDC::SetBkMode并傳送TRANSPAARENT使背景保持不變,這兩種方法都可以設(shè)置背景模式。下例設(shè)置背景模式為TRANSPARENT,可以兩次更新串,用花色帶黑陰影更新文本。黑色串在紅色串之后,但由于設(shè)置了背景模式仍然可見。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determint size of view.
CRect rcView
GetClientRect (rcVieew)
//Create sample string to display.
CString str (_T ("Awesome Shadow Text..."))
//Set the background mode to transparent.
pDC->SetBKMode (TRANSPARENT)
//Draw black shadow text.
rcView.OffsetRect (1, 1)
pDc->SetTextColor (RGB (0, 0, 0))
pDC->DrawText (str, str.GetLength (), rcView, DT_SINGLELINE | DT_CENTER | DT_VCENTER)
//Draw red text.
rcView.OffsetRect (-1,-1)
pDc->SetTextColor (RGB (255, 0, 0))
pDC->DrawText (str, str.GetLength (), rcView, DT_SINGLELINE | DT_CENTER | DT_VCENTER)
}
(55) 如何創(chuàng)建一個具有特定點大小的字體
可以指定字體邏輯單位的大小,但有時指定字體的點的大小可能會更方便一些。可以如下將字體的點轉(zhuǎn)換為字體的高度:
int nHeigth=mulDiv (nPointSize, -dc.GetDeviceCaps (LOGPIXELSY), 72)
下例創(chuàng)建了一個8點的Apial字體:
…
CClientDC dc (AqfxGetMainWnd ())
m_font. CreateFont (MulDiv (8, -dc.GetDeviceCaps (LOGPIXELSY), 72), 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_STROKE_PRECIS, CLIP_STROKE_PRECIS, DRAFT_QUALITY, VARIABLE_PITCH | FF-SWISS,_T("Arial"))
(56) 如何計算一個串的大小
函數(shù)CDC:: Det text Extent 根據(jù)當(dāng)前選擇的字體計算一個串的高度和寬度。如果使用的不是系統(tǒng)字體而是其他字體,則在調(diào)用GetTextExtent之前將字體選進設(shè)備上下文中是很重要的,否則計算高度和寬度時將依據(jù)系統(tǒng)字體,由此得出的結(jié)果當(dāng)然是不正確的。下述樣板程序當(dāng)改變下壓按鈕的標(biāo)題時動態(tài)調(diào)整按鈕的大小,按鈕的大小由按鈕的字體和標(biāo)題的大小而定。響應(yīng)消息WM_SETTEXT時調(diào)用OnSetText,該消息使用ON_MESSAE宏指令定義的用戶自定義消息。
LRESULT CMyButton:: OnSettext (WPARAM wParam, LPARAM lParam)
{
//Pass message to window procedure.
LRESULT bResult=CallWindowProc (*GetSuperWndProcAddr(), m_hWnd, GetCurrentMessage() ->message,wParam,lParam)
//Get title of push button.
CString strTitle
GetWindowText (strTitle)
//Select current font into device context.
CDC* pDC=GetDc ()
CFont*pFont=GetFont ()
CFont*pOldFont=pDC->SelectObject (pFont)
//Calculate size of title.
CSize size=pDC->GetTextExent (strTitle,strTitle.GetLength())
//Adjust the button‘s size based on its title.
//Add a 5-pixel border around the button.
SetWindowPos (NULL, 0, 0, size.cx+10, size.cy+10, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE)
//Clean up.
pDC->SelectFont (pOldFont)
ReleaseDC (pDC)
return bResult
}
(57) 如何顯示旋轉(zhuǎn)文本
只要用戶使用TrueType或者GDI筆或字體就可以顯示旋轉(zhuǎn)文本(有些硬件設(shè)備也支持旋轉(zhuǎn)光柵字體)。LOGFONT結(jié)構(gòu)中的ifEscapement成員指定了文本行和x軸的角度,角度的單位是十分之一度而不是度,例如,ifEscapement為450表示字體旋轉(zhuǎn)45度。為確保所有的字體沿坐標(biāo)系統(tǒng)的同一方向旋轉(zhuǎn),一定要設(shè)置ifEscapement成員的CLIP_LH_ANGLES位,否則,有些字體可能反向旋轉(zhuǎn)。下例使用了14點Arial字體每間隔15度畫一個串。
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine the size of the window.
CRect rcClient
GetClientRect (rcClient)
//Create sample string.
CString str (_T ("Wheeee...I am rotating!"))
//Draw transparent, red text.
pDC->SetBkMode (TRANSPARENT)
pDC->SetTextColor (RGB (255,0,0))
CFont font
//font object
LOGFONT stFont //font definition
//Set font attributes that will not change.
memset (&stFont, 0, sizeof (LOGFONT))
stFont.ifheight=MulDiv (14, -pDC->GetDeviceCaps(LOGPIXELSY), 72)
stFont.ifWeight=FW_NORMAL
stFont.ifClipPrecision=LCIP_LH_ANGLES
strcpy (stFont.lfFaceName, "Arial")
//Draw text at 15degree intervals.
for (int nAngle=0 nAngle<3600 nAngle+=150)
{
//Specify new angle.
stFont.lfEscapement=nAngle
//Create and select font into dc.
font.CreateFontIndirect(&stfont)
CFont* pOldFont=pDC ->SelectObject(&font)
//Draw the text.
pDC->SelectObject(pOldFont)
font.DelectObjext()
}
}
(58) 如何正確顯示包含標(biāo)簽字符的串
調(diào)用GDI文本繪畫函數(shù)時需要展開標(biāo)簽字符,這可以通過調(diào)用CDC:: TabbedTextOut或者CDC:: DrawText并指定DT_EXPANDTABS標(biāo)志來完成。TabbedTextOut函數(shù)允許指定標(biāo)簽位的數(shù)組,下例指定每20設(shè)備單位展開一個標(biāo)簽:
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ()
ASSERT_VALID (pDoC)
CString str
str.Format (_T ("Cathy\tNorman\tOliver"))
int nTabStop=20 //tabs are every 20 pixels
pDC->TabbedtextOut (10, 10, str, 1, &nTabStop, 10)
}
(59) 如何快速地格式化一個CString對象
調(diào)用CString:: Format,該函數(shù)和printf函數(shù)具有相同的參數(shù),下例說明了如何使用Format函數(shù):
//Get size of window.
CRect rcWindow
GetWindowRect (rcWindow)
//Format message string.
CString strMessage
strMessage.Format (_T ("Window Size (%d, %d)"),
rcWindow.Width (), rcWindow.Height ())
//Display the message.
MessageBox (strmessage)
(60) 串太長時如何在其末尾顯示一個省略號
調(diào)用CDC:: DrawText并指定DT_END_ELLIPSIS標(biāo)志,這樣就可以用小略號取代串末尾的字符使其適合于指定的邊界矩形。如果要顯示路徑信息,指定DT_END_ELLIPSIS標(biāo)志并省略號取代串中間的字符。
void CSampleView:: OnDraw (CDC* pDC)
{
CTestDoc* pDoc=GetDocument ()
ASSERT_VALID (pDoc)
//Add ellpsis to end of string if it does not fit
pDC->Drawtext (CString ("This is a long string"), CRect (10, 10, 80, 30), DT_LEFT | DT_END_ELLIPSIS)
//Add ellpsis to middle of string if it does not fit
pDC->DrawText (AfxgetApp () ->m_pszhelpfilePath, CRect (10, 40, 200, 60), DT_LEFT | DT_PATH_ELLIPSIS)
}
(61) 為什么即使調(diào)用EnableMenuItem菜單項后,菜單項還處于禁止?fàn)顟B(tài)
需要將CFrameWnd:: m_bAutomenuEnable設(shè)置為FALSE,如果該數(shù)據(jù)成員為TRUE(缺省值),工作框?qū)⒆詣拥亟箾]有ON_UPDATE_COMMAND_UI或者ON_COMMAND的菜單項。
//Disable MFC from automatically disabling menu items.
m_bAuoMenuEnable=FALSE
//Now enable the menu item.
CMenu* pMenu=GetMenu ()
ASSERT_VALID (pMenu)
pMenu->EnableMenuItem (ID_MENU_ITEM,MF_BYCOMMAND | MF_ENABLED)
(62) 如何給系統(tǒng)菜單添加一個菜單項
給系統(tǒng)菜單添加一個菜單項需要進行下述三個步驟:
首先,使用Resource Symbols對話(在View菜單中選擇Resource Symbols...可以顯示該對話)定義菜單項ID,該ID應(yīng)大于0x0F而小于0xF000;
其次,調(diào)用CWnd::GetSystemMenu獲取系統(tǒng)菜單的指針并調(diào)用CWnd:: Appendmenu將菜單項添加到菜單中。下例給系統(tǒng)菜單添加兩個新的
int CMainFrame:: OnCreate (LPCREATESTRUCT lpCreateStruct)
{
…
//Make sure system menu item is in the right range.
ASSERT (IDM_MYSYSITEM &0xFFF0)==IDM_MYSYSITEM)
ASSERT (IDM-MYSYSITEM<0xF000)
//Get pointer to system menu.
CMenu* pSysmenu=GetSystemmenu (FALSE)
ASSERT_VALID (pSysMenu)
//Add a separator and our menu item to system menu.
CString StrMenuItem (_T ("New menu item"))
pSysMenu->Appendmenu (MF_SEPARATOR)
pSysMenu->AppendMenu (MF_STRING, IDM_MYSYSITEM, strMenuitem)
…
}
現(xiàn)在,選擇系統(tǒng)菜單項時用戶應(yīng)進行檢測。使用ClassWizard處理WM_SYSCOMMAND消息并檢測用戶菜單的nID參數(shù):
void CMainFrame:: OnSysCommand (UINT nID,LPARAM lParam)
{
//Determine if our system menu item was selected.
if ( (nID & 0xFFF0)==IDM_MYSYSITEM)
{
//TODO-process system menu item
}
else
CMDIFrameWnd ::OnSysCommand (nID, lParam)
}
最后,一個設(shè)計良好的UI應(yīng)用程序應(yīng)當(dāng)在系統(tǒng)菜單項加亮?xí)r在狀態(tài)條顯示一個幫助信息,這可以通過增加一個包含系統(tǒng)菜單基ID的串表的入口來實現(xiàn)。
(63) 如何確定頂層菜單所占據(jù)的菜單行數(shù)
這可以通過簡單的減法和除法來實現(xiàn)。首先,用戶需要計算主框窗口的高度和客戶區(qū);其次,從主框窗口的高度中減去客戶區(qū)、框邊界以及標(biāo)題的高度;最后,除以菜單欄的高度。下例成員函數(shù)是一個計算主框菜單所占據(jù)的行數(shù)的代碼實現(xiàn)。
int CMainFrame:: GetMenuRows ()
{
CRect rcFrame,rcClient
GetWindowRect (rcFrame)
GetClientRect (rcClient)
return (rcFrame.Height () -rcClient.Height () - :: GetSystemMetrics(SM_CYCAPTION) - (:: getSystemMetrics(SM_CYFRAME) *2)) / :: GetSystemMetrics(SM_CYMENU)
}
(64) 在用戶環(huán)境中如何確定系統(tǒng)顯示元素的顏色
調(diào)用SDK函數(shù)GetSysColor可以獲取一個特定顯示元素的顏色。下例說明了如何在MFC函數(shù)CMainFrameWnd:: OnNcPaint中調(diào)用該函數(shù)設(shè)置窗口標(biāo)題顏色。
void CMiniFrameWnd:: OnNcPaint ()
{
…
dc.SetTextColor (:: GetSysColor (m_bActive ? COLOR_CAPTIONTEXT : COLOR_INACTIVECAPTIONTEXT))
…
(65) 如何查詢和設(shè)置系統(tǒng)參數(shù)
在Windows 3.1 SDK中介紹過SDK函數(shù)SystemParametersInfo,調(diào)用該函數(shù)可以查詢和設(shè)置系統(tǒng)參數(shù),諸如按鍵的重復(fù)速率設(shè)置、鼠標(biāo)雙擊延遲時間、圖標(biāo)字體以及桌面覆蓋位圖等等。
//Create a font that is used for icon titles.
LOGFONT stFont
∶: SystemParametersInfo (SPIF_GETICONTITLELOGFONT, sizeof (LOGFONT), &stFont, SPIF_SENDWININICHANGE)
m_font.CreateFontIndirect (&stFont)
//Change the wallpaper to leaves.bmp.
∶ : SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, _T (" forest.bmp"), SPIF_UPDATEINIFILE)
(66) 如何確定當(dāng)前屏幕分辨率
調(diào)用SDK函數(shù)GetSystemMetrics,該函數(shù)可以檢索有關(guān)windows顯示信息,諸如標(biāo)題大小、邊界大小以及滾動條大小等等。
//Initialize CSize object with screen size.
CSize sizeScreen (GetSystemMetrics (SM_CXSCREEN),
GetSystemMetrics (SM_CYSCREEN))
(67) 如何使用一個預(yù)定義的Windows光標(biāo)
調(diào)用CWinApp:: LoadStandardCursor并傳送光標(biāo)標(biāo)識符。
BOOL CSampleDialog:: OnSetCursor (CWnd* pWnd,
UINT nHitTest, UINT
message)
{
//Display wait cursor if busy.
if (m_bBusy)
{
SetCursor (AfxGetApp () ->LoadStandardCursor (IDC_WAIT))
return TRUE
}
return CDialog:: OnSetCursor (pWnd. nHitTest,message)
}
(68) 如何檢索原先的Task Manager應(yīng)用程序使用的任務(wù)列表
原先的Task Manager應(yīng)用程序顯示頂層窗口的列表。為了顯示該列表,窗口必須可見、包含一個標(biāo)題以及不能被其他窗口擁有。調(diào)用CWnd:: GetWindow可以檢索頂層窗口的列表,調(diào)用IsWindowVisible、GetWindowTextLength以及GetOwner可以確定窗口是否應(yīng)該在列表中。下例將把TaskManager窗口的標(biāo)題填充到列表中。
void GetTadkList (CListBox&list)
{
CString strCaption
//Caption of window.
list.ResetContent ()
//Clear list box.
//Get first Window in window list.
ASSERT_VALID (AfxGetMainWnd ())
CWnd* pWnd=AfxGetMainWnd () ->GetWindow (GW_HWNDFIRST)
//Walk window list.
while (pWnd)
{
// I window visible, has a caption, and does not have an owner?
if (pWnd ->IsWindowVisible()
&& pWnd ->GetWindowTextLength ()
&&! pWnd ->GetOwner ())
{
//Add caption o window to list box.
pWnd ->GetWindowText (strCaption)
list.AddString (strCaption)
}
//Get next window in window list.
pWnd=pWnd ->GetWindow(GW_HWNDNEXT)
}
}
(69) 如何確定Windows和Windows系統(tǒng)目錄
有兩個SDK函數(shù)可以完成該功能。GetWindowsDirectory和GetSystemDirectory,下例說明了如何使用這兩個函數(shù):
TCHAR szDir [MAX_PATH]
//Get the full path of the windows directory.
∶ : GetWindowsDirectory (szDir, MAX_PATH)
TRACE ("Windows directory %s\n", szDir)
//Get the full path of the windows system directory.
∶ : GetSystemDirectory (szDir, MAX_PATH)
TRACE ("Windows system directory %s\n", szDir)
(70) 在哪兒創(chuàng)建臨文件
調(diào)用SDK函數(shù)GetTemPath可以確定臨時文件的目錄,該函數(shù)首先為臨時路徑檢測TMP環(huán)境變量:如果沒有指定TMP,檢測TMP環(huán)境變量,然后返回到當(dāng)前目錄。下例說明了如何創(chuàng)建一個臨時文件。
…
//get unique temporary file.
CString strFile
GetUniqueTempName (strFile)
TRY
{
//Create file and write data.Note that file is closed
//in the destructor of the CFile object.
CFile file (strFile,CFile ::modeCreate | CFile:: modeWrite)
//write data
}
CATCH (CFileException, e)
{
//error opening file
}
END_CATCH
…
Void GetuniqueTempName (CString& strTempName)
{
//Get the temporary files directory.
TCHAR szTempPath [MAX_PATH]
DWORD dwResult=:: GetTempPath (MAX_PATH, szTempPath)
ASSERT (dwResult)
//Create a unique temporary file.
TCHAR szTempFile [MAX_PATH]
UINT nResult=GetTempFileName (szTempPath, _T ("~ex"),0,szTempfile)
ASSERT (nResult)
strTempName=szTempFile
}
(71) 我怎樣才能建立一個等待光標(biāo)?
調(diào) 用 BeginWaitCursor 函 數(shù) 來 啟 動 等 待 光 標(biāo),調(diào) 用 EndWaitCursor 函 數(shù) 來 結(jié) 束 等 待 光 標(biāo)。要 注 意,二 者 都 要 調(diào) 用 app 的 成 員 函 數(shù),如 下 所 示:
AfxGetApp()->BeginWaitCursor();
// 要做的事
AfxGetApp()->EndWaitCursor();
(72) 我在MDI框架中有個 form 視窗。它有個取消按鈕,我需要當(dāng)用戶按取消按鈕時可關(guān)閉form視窗。我應(yīng)該如何關(guān)閉該文檔?
調(diào) 用 OnCloseDocument 函 數(shù)。
(73) 如何訪問桌面窗口
靜態(tài)函數(shù)CWnd:: GetDesktopWindow 返回桌面窗口的指針。下例說明了MFC函數(shù)CFrameWnd::BeginModalStae是如何使用該函數(shù)進入內(nèi)部窗口列表的。
void CFrameWnd::BeginModalState ()
{
…
//first count all windows that need to be disabled
UINT nCount=0
HWND hWnd= :: GetWindow (:: GetDesktopWindow(), GW_CHILD)
while (hWnd!=NULL)
{
if (:: IsWindowEnabled (hwnd)
&& CWnd::FromHandlePermanent (hWnd)!=NULL
&& AfxIsDescendant (pParent->m_hWnd, hWnd)
&& :: SendMessage (hWnd, WM_DISABLEMODAL, 0, 0)==0)
{
++nCount
}
hWnd=:: GetWindow (hWnd, GW_HWNDNEXT)
}
…
(74) 什么是COLORREF? 我該怎樣用它?
COLORREF 是 一 個 32-bit 整 型 數(shù) 值,它 代 表 了 一 種 顏 色。你 可 以 使 用 RGB 函 數(shù) 來 初 始 化 COLORREF。例 如:
COLORREF color = RGB(0, 255, 0);
RGB 函 數(shù) 接 收 三 個 0-255 數(shù) 值,一 個 代 表 紅 色, 一 個 代 表 綠 色, 一 個 代 表 藍 色。在 上 面的 例 子 中, 紅 色 和 藍 色 值 都 為 0,所 以 在 該 顏 色 中 沒 有 紅 色 和 藍 色。綠 色 為 最 大 值 255。所 以 該 顏 色 為 綠 色。0,0,0 為 黑 色,255,255,255 為 白 色。
另 一 種 初 始 化 COLORREF 的 方 法 如 下 所 示:
CColorDialog colorDialog;
COLORREF color;
if( colorDialog.DoModal() == IDOK )
{
color = colorDialog.GetColor();
}
這 段 代 碼 使 用 了 MFC 中 的 顏 色 對 話 框,它 需 要 文 件。
(75) AppWizard所產(chǎn)生的STDAFX文件是干什么用的?
它 主 要 是 協(xié) 助 產(chǎn) 生 預(yù) 編 譯 頭 文 件 的。通 常 你 是 不 需 要 修 改 它 的。
(76) 我在我的程序中是了CDWordArray。我向它添加了約10,000個整數(shù),這使得它變得非常非常慢。為什么會這么糟?
CDWordArray 是 很 好 用 的,只 是 因 為 你 沒 有 指 定 數(shù) 組 的最大尺寸。因 此,當(dāng) 你 添 加 新 元 素 時,該 類 會 從 堆 中 重 新 分 配 空 間。不 幸 的 是,該 類 會 在 每 次 插 入 新 元 素 時 都 為 數(shù) 組 重 新 分 配 空 間。如 果 你 向 它 添 加 了 很 多 新 元 素,所 有 這 些 分 配 和 復(fù) 制 數(shù) 組 的 操 作 會 就 會 使 它 變 慢。解 決 該 問 題 的 方 法 是,你 可 以 使 用 SetSize 函 數(shù) 的 第 二 個 參 數(shù) 來 改 變 這 種 重 新 分 配 的 頻 率。例 如,如 果 你 把 該 參 數(shù) 設(shè) 置 為 500,則 每 次 數(shù) 組 空 間 超 出 時 它 才 重 新 分 配 并 添 加 500 個 新 空 間,而 不 是 1 個。這 樣 一 來,你 就 可 以 不 用 重 新 分 配 而 添 加 了 另 外 499 個 元 素 空 間,這 也 會 大 大 提 高 程 序 的 運 行 速 度。
(77) 我該如何改變MDI框架窗口的子窗口的大小以使在窗口以一定的大小打開?
在 視 中 的 OnInitialUpdate 函 數(shù) 中 調(diào) 用 GetParentFrame 函 數(shù)。GetParentFrame 會 返 回 一 指 向 一 保 存 有 該 視 的 框 架 窗 口 的 指 針。然 后 調(diào) 用 在 框 架 窗 口 上 調(diào) 用 MoveWindow。
(78) 在我的程序的某些部分,我可以調(diào)用 MessageBox 函數(shù)來建立一個信息對話框,例如在視類中。但是,在其它部分我卻不能,如文檔類中。為什么?我怎樣才能在我的應(yīng)用程序類中建立一個信息對話框?
MessageBox 函 數(shù) 來 自 CWnd 類,所 以 你 只 能 在 從 CWnd 繼 承 的 類 ( 如 CView ) 中 調(diào) 用 它。但 是,MFC 也 提 供 了 AfxMessageBox 函 數(shù),你 可 以 在 任 何 地 方 調(diào) 用 它。
(79) 我需要在我的程序中設(shè)置全局變量,以使文檔中的所有類都能訪問。我應(yīng)該吧它放到哪兒?
把 該 變 量 放 到 該 應(yīng) 用 程 序 類 的 頭 文 件 中 的 attribute 處。然 后,在 程 序 的 任 何 地 方,你 都 可 以 用 下 面 的 方 法 來 訪 問 該 變 量:
CMyApp *app = (CMyApp *)AfxGetApp();
app->MyGlobalVariable = ...
(80) 我聽說MFC可以發(fā)現(xiàn)內(nèi)存漏洞,我怎樣使用該特性?
如 果 你 在 Debug 菜 單 中 的 Go 選 項 ( 不 是 Project 菜 單 中 的 Execute 選 項 ) 來 運 行 你 的 應(yīng) 用 程 序,MFC 應(yīng) 該 在 程 序 終 止 時 報 告 內(nèi) 存 漏 洞。如 果 沒 有,那 么 試 試 運 行 MFC Tracer 工 具 程 序 ( 在 VC++ 程 序 組 中 ),并 啟 動 跟 蹤。然 后 返 回 應(yīng) 用 程 序。\
(81) 我怎樣才能在我的應(yīng)用程序中循環(huán)瀏覽已經(jīng)打開的文檔?
使用CDocTemplate中未公開的GetFirstDocPosition()和GetNextDoc()函數(shù)。
(82)才能在我的應(yīng)用程序中循環(huán)瀏覽已經(jīng)打開的視?
使 用 CDocument 中 未 公 開 的 GetFirstViewPosition() 和 GetNextView() 函 數(shù)。
(83)數(shù)PreCreateWindow是干什么用的?
PreCreateWindow 允 許 你 在 調(diào) 用 CreateWindow 之 前 來 改 變 窗 口 屬 性。
(84)該怎樣防止MFC在窗口標(biāo)題欄上把文檔名預(yù)置成應(yīng)用程序名?
在 PreCreateWindow 函 數(shù) 中 刪 除 FWS_PREFIXTITLE 標(biāo) 志 的 窗 口 樣 式:
cs.style &= ~FWS_PREFIXTITLE;
(85) 我應(yīng)該怎樣防止MFC在窗口標(biāo)題欄上添加文檔名?
在 PreCreateWindow 函 數(shù) 中 刪 除 FWS_ADDTOTITLE 標(biāo) 志 的 窗 口 樣 式:
cs.style &= ~FWS_ADDTOTITLE ;
(86) 我應(yīng)該如何改變視窗口的大小?
因 為 視 窗 口 實 際 上 是 框 架 窗 口 的 子 窗 口,所 以 你 必 須 改 變 框 架 窗 口 的 大 小,而 不 是 改 變 視 窗 口。使 用 CView 類 中 的 GetParentFrame() 函 數(shù) 獲 得 指 向 框 架 窗 口 的 指 針,然 后 調(diào) 用 MoveWindow() 函 數(shù) 來 改 變 框 架 的 大 小。這 會 使 變 尺 寸 的 視 充 滿 框 架 窗 口。
(87) 我有一無模式對話框。我怎樣才能在窗口退出時刪除CDialog對象?
把“delete this”加 到 PostNcDestroy 中。這 主 要 用 在 需 要 自 動 刪 除 對 象 的 場 合。
(88) 為什么把“delete this”放在PostNcDestroy中而不是OnNcDestroy?
OnNcDestroy 只 被 已 建 立 的 窗 口 調(diào) 用。如 果 建 立 窗 口 失 敗 ( 如 PreCreateWindow ),則 沒 有 窗 口 處 來 發(fā) 送 WM_NCDESTROY 消 息。PostNcDestroy 是 在 對 象 窗 口 被 完 全 刪 除,在 OnNcDestroy 后,甚 至 在 窗 口 建 立 失 敗 之 后 調(diào) 用 的。
(89) File菜單中的MRU列表是從哪兒來的?列表中的名字放在哪兒了?我怎樣才能改變列表中項目的最大值?
在 應(yīng) 用 程 序 類 的 InitInstance 函 數(shù) 中 對 LoadStdProfileSettings 的 調(diào) 用 中。該 調(diào) 用 接 受 一 個 參 數(shù) ( 在 缺 省 情 況 下 如 果 沒 有 傳 遞 值 則 為 4 )。MRU 文 件 名 是 從 INI 文 件 中 調(diào) 用 的。如 果 你 有 帶 有 ID_FILE_MRU_FILE1 的 ID 的 菜 單 選 項,它 會 為 調(diào) 入 的 MRU 列 表 所 替 換。如 果 你 改 變 傳 遞 給 LoadStdProfileSettings 的 數(shù) 值 ( 最 大 為 16 ),則 你 就 改 變 了 所 裝 如 文 件 名 的 最 大 值。
(90) 我在菜單中添加了新的項。但是,當(dāng)我選該項時,在狀態(tài)欄上沒有出現(xiàn)任何提示信息。為什么?
打 開 資 源 文 件 中 的 菜 單 模 板。打 開 新 菜 單 選 項 的 屬 性 對 話 框。在 對 話 框 的 底 部 的 Prompt 編 輯 框 中 ,你 可 以 如 下 指 定 狀 態(tài) 欄 上 的 提 示 信 息 和 工 具 欄 上 的 提 示 信 息 ( 如 果 你 已 經(jīng) 建 立 的 工 具 欄 按 鈕 ):
Status bar string\nFlying tag
/////////////////////////////強,再加上一些常用的
一、打開CD-ROM
mciSendString("Set cdAudio door open wait",NULL,0,NULL);
二、關(guān)閉CD_ROM
mciSendString("Set cdAudio door closed wait",NULL,0,NULL);
三、關(guān)閉計算機
OSVERSIONINFO OsVersionInfo; //包含操作系統(tǒng)版本信息的數(shù)據(jù)結(jié)構(gòu)
OsVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&OsVersionInfo); //獲取操作系統(tǒng)版本信息
if(OsVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
//Windows98,調(diào)用ExitWindowsEx()函數(shù)重新啟動計算機
DWORD dwReserved;
ExitWindowsEx(EWX_REBOOT,dwReserved); //可以改變第一個參數(shù),實現(xiàn)注銷用戶、
//關(guān)機、關(guān)閉電源等操作
// 退出前的一些處理程序
}
四、重啟計算機
typedef int (CALLBACK *SHUTDOWNDLG)(int); //顯示關(guān)機對話框函數(shù)的指針
HINSTANCE hInst = LoadLibrary("shell32.dll"); //裝入shell32.dll
SHUTDOWNDLG ShutDownDialog; //指向shell32.dll庫中顯示關(guān)機對話框函數(shù)的指針
if(hInst != NULL)
{
//獲得函數(shù)的地址并調(diào)用之
ShutDownDialog = (SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);
(*ShutDownDialog)(0);
}
五、枚舉所有字體
LOGFONT lf;
lf.lfCharSet = DEFAULT_CHARSET; // Initialize the LOGFONT structure
strcpy(lf.lfFaceName,"");
CClientDC dc (this);
//Enumerate the font families
::EnumFontFamiliesEx((HDC) dc,&lf,
(FONTENUMPROC) EnumFontFamProc,(LPARAM) this,0);
//枚舉函數(shù)
int CALLBACK EnumFontFamProc(LPENUMLOGFONT lpelf,LPNEWTEXTMETRIC lpntm,DWORD nFontType,long lparam)
{
// Create a pointer to the dialog window
CDay7Dlg* pWnd = (CDay7Dlg*) lparam;
// add the font name to the list box
pWnd ->m_ctlFontList.AddString(lpelf ->elfLogFont.lfFaceName);
// Return 1 to continue font enumeration
return 1;
}
其中m_ctlFontList是一個列表控件變量
六、一次只運行一個程序?qū)嵗?,如果已運行則退出
if( FindWindow(NULL,"程序標(biāo)題")) exit(0);
七、得到當(dāng)前鼠標(biāo)所在位置
CPoint pt;
GetCursorPos(&pt); //得到位置
八、上下文菜單事件觸發(fā)事件:OnContextMenu事件
九、顯示和隱藏程序菜單
CWnd *pWnd=AfxGetMainWnd();
if(b_m) //隱藏菜單
{
pWnd->SetMenu(NULL);
pWnd->DrawMenuBar();
b_m=false;
}
else
{
CMenu menu;
menu.LoadMenu(IDR_MAINFRAME); ////顯示菜單 也可改變菜單項
pWnd->SetMenu(&menu);
pWnd->DrawMenuBar();
b_m=true;
menu.Detach();
}
十、獲取可執(zhí)行文件的圖標(biāo)
HICON hIcon=::ExtractIcon(AfxGetInstanceHandle(),_T("NotePad.exe"),0);
if (hIcon &&hIcon!=(HICON)-1)
{
pDC->DrawIcon(10,10,hIcon);
}
DestroyIcon(hIcon);
十一、窗口自動靠邊程序演示
BOOL AdjustPos(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)
{
//如果調(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(&r);
*pRect=(RECT)r;