(1) 如何通過代碼獲得應用程序主窗口的 指針?
主窗口的 指針保存在CWinThread::m_pMainWnd中,調(diào)用AfxGetMainWnd實現(xiàn)。
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//使程序最大化.
(2) 確定應用程序的路徑
Use GetModuleFileName 獲得應用程序的路徑,然后去掉可執(zhí)行文件名。
Example:
TCHAR
exeFullPath[MAX_PATH] // MAX_PATH在API中定義了吧,好象是
128
GetModuleFileName(NULL,exeFullPath,MAX_PATH)
(3) 如何在程序中獲得其他程序的 圖標?
兩種方法:
(1) SDK函數(shù) SHGetFileInfo 或使用 ExtractIcon獲得圖標資源的 handle,
(2) SDK函數(shù) SHGetFileInfo 獲得有關(guān)文件的很多信息,如大小圖標,屬性, 類型等.
Example(1):
在程序窗口左上角顯示 NotePad圖標.
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下的畫筆,應該用訪問注冊表的方法獲得其路徑,要作成一個比較考究的程序,考慮應該全面點.
(4) 獲得各種目錄信息
Windows目錄: Use "GetWindowsDirectory"
Windows下的system目錄: Use "GetSystemDirectory"
temp目錄: Use "GetTempPath"
當前目錄: 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) 如何改變窗口的圖標?
向窗口發(fā)送 WM_SECTION消息。
Example:
HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)
ASSERT(hIcon)
AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM)hIcon)
(7) 如何改變窗口的缺省風格?
重載 CWnd:: PreCreateWindow 并修改CREATESTRUCT結(jié)構(gòu)來指定窗口風格和其他創(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)
}
(8)
如何將窗口居中顯示?Call Function CWnd::
Center Windows
Example(1):
Center Window( ) //Relative to it's parent
// Relative
to Screen
Example(2):
Center Window(CWnd:: GetDesktopWindow( ))
//Relative to
Application's MainWindow
AfxGetMainWnd( ) ->
Center Window( )
(9) 如何讓窗口和 MDI窗口一啟動就最大化和最小化?
先說窗口。
在 InitStance 函數(shù)中設(shè)定 m_nCmdShow的取值.
m_nCmdShow=SW_SHOWMAXMIZED //最大化
m_nCmdShow=SW_SHOWMINMIZED //最小化
m_nCmdShow=SW_SHOWNORMAL //正常方式
MDI窗口:
如果是創(chuàng)建新的應用程序,可以用MFC AppWizard 的Advanced 按鈕并在MDI子窗口風格組中檢測最大化或最小化還可以重載 MDI Window 的PreCreateWindow函數(shù),設(shè)置WS_MAXMIZE or WS_MINMIZE
如果從 CMDIChildWnd派生,調(diào)用 OnInitialUpdate函數(shù)中的 CWnd::Show Window來指定 MDI Child Window的風格。
(10)
如何限制窗口的大小?也就是 FixedDialog形式。 Windows發(fā)送 WM_GETMAXMININFO消息來跟蹤, 響應它,在 OnGetMAXMININFO 中寫代碼:
(11) 如何使窗口不可見?
很簡單,用SW_HIDE 隱藏窗口,可以結(jié)合 FindWindow,ShowWindow控制.
(12) 如何創(chuàng)建一個字回繞的CEditView
重載CWnd : : PreCreateWindow和修改CREATESTRUCT結(jié)構(gòu),關(guān)閉CEditView對象的ES_AUTOHSCROLL和WS_HSCROLL風格位, 由于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)?
這么辦: 在恢復程序窗體大小時,Windows會發(fā)送WM_QUERY-OPEN消息,用 ClassWizard設(shè)置成員函數(shù)
OnQueryOpen() ,add following code:
Bool CMainFrame:: OnQueryOpen( )
{
Return false
}
(14) 移動窗口
調(diào)用CWnd : : SetWindowPos并指定SWP_NOSIZE標志。目的位置與父窗口有關(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標志, 也可調(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) 如何單擊除了窗口標題欄以外的區(qū)域使窗口移動
當窗口需要確定鼠標位置時Windows向窗口發(fā)送WM_NCHITTEST信息,可以處理該信息使Windows認為鼠標在窗口標題上。對于對話框和基于對話的應用程序,可以使用ClassWizard處理該信息并調(diào)用基類函數(shù), 如果函數(shù)返回HTCLIENT 則表明鼠標在客房區(qū)域,返回HTCAPTION表明鼠標在Windows的標題欄中。
UINT CSampleDialog : : OnNcHitTest (Cpoint point )
{
UINT nHitTest =Cdialog: : OnNcHitTest (point )
return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest
}
上述技術(shù)有兩點不利之處,
其一是在窗口的客戶區(qū)域雙擊時,窗口將極大;
其二, 它不適合包含幾個視窗的主框窗口。
還有一種方法,當用戶按下鼠標左鍵使主框窗口認為鼠標在其窗口標題上,使用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ù)也適用于對話框和基于對的應用程序,只是不必調(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) 如何改變窗口標題
調(diào)用CWnd : : SetWindowText可以改變?nèi)魏未翱冢ò丶┑臉祟}。
//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)常修改窗口的標題(注:控件也是窗口),應該考慮使用半文檔化的函數(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風格位,如果不希望在說明中自動添加文檔名, 必須禁止該風格位, 可以使用ClassWizard重置
CWnd: : PreCreateWindow并關(guān)閉FWS_ADDTOTITLE風格。
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風格將創(chuàng)建一個具有空標題的窗口,可以調(diào)用CWnd: : SetWindowText來設(shè)置標題。記住自己設(shè)置標題時要遵循接口風格指南。
(21) 如何獲取有關(guān)窗口正在處理的當前消息的信息
調(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標識符,工具條有一個AFX_IDW_TOOLBAR標識符,下例說明了如何通過一起調(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風格位,工具條將顯示工具提示,要使能或者禁止工具提示,需要設(shè)置或者清除該風格位。下例通過調(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ù)將繪畫和鼠標消息限定在窗口的一個指定的區(qū)域,實際上使窗口成為指定的不規(guī)則形狀。 使用AppWizard創(chuàng)建一個基于對的應用程序并使用資源編輯器從主對話資源中刪除所在的缺省控件、標題以及邊界。
給對話類增加一個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消息,使當擊打窗口的任何位置時能移動窗口。
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) 如何獲取應用程序的 實例句柄?
應用程序的實例句柄保存在CWinApp m_hInstance 中,可以這么調(diào)用AfxGetInstancdHandle獲得句柄.
Example: HANDLE hInstance=AfxGetInstanceHandle()
(26) 如何編程結(jié)束應用程序?
這是個很簡單又是編程中經(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ù)不是提倡的做法,因為它無法處理標題欄自動改變,比如我們要檢測 Notepad是不是已運行而事先不知道Notepad的標題欄,這時FindWindow就無能為力了,可以通過枚舉 windows任務列表的辦法來實現(xiàn)。在機械出版社"Windows 95 API開發(fā)人員指南"一書有比較詳細的介紹,這里就不再多說樂。
(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。下面的例子說明 了應用程序是如何創(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風格位,如果不希望在說明中自動添加文檔名, 必須禁止該風格位, 可以使用ClassWizard重置
CWnd: : PreCreateWindow并關(guān)閉FWS_ADDTOTITLE風格。
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風格將創(chuàng)建一個具有空標題的窗口,可以調(diào)用CWnd: : SetWindowText來設(shè)置標題。記住自己設(shè)置標題時要遵循接口風格指南。
(29) 如何在代碼中獲取工具條和狀態(tài)條的指針
缺省時, 工作框創(chuàng)建狀態(tài)條和工具條時將它們作為主框窗口的子窗口,狀態(tài)條有一個AFX_IDW_STATUS_BAR標識符,工具條有一個AFX_IDW_TOOLBAR標識符,下例說明了如何通過一起調(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) 怎樣加載其他的應用程序?
三個SDK函數(shù) winexec, shellexecute,createprocess可以使用。
WinExec最簡單,兩個參數(shù),前一個指定路徑,后一個指定顯示方式.后一個參數(shù)值得說一下,比如泥用 SW_SHOWMAXMIZED方式去加載一個無最大化按鈕的程序,就是Neterm,calc等等,就不會出現(xiàn)正常的窗體,但是已經(jīng)被加到任務列表里了。
ShellExecute較 WinExex靈活一點,可以指定工作目錄,下面的Example就是直接打開 c:\temp\1.txt,而不用加載與 txt文件關(guān)聯(lián)的應用程序,很多安裝程序完成后都會打開一個窗口,來顯示Readme or Faq,我猜就是這么作的啦.
ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOWMAXMIZED)
CreateProcess最復雜,一共有十個參數(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標識符,工具條有一個AFX_IDW_TOOLBAR標識符,下例說明了如何通過一起調(diào)用CWnd: : GetDescendantWindow和AfxGetMainWnd來獲取這些子窗口的指針:
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)
(32) 如何使能和禁止工具條的工具提示
如果設(shè)置了CBRS_TOOLTIPS風格位,工具條將顯示工具提示,要使能或者禁止工具提示,需要設(shè)置或者清除該風格位。下例通過調(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è)置工具條標題
工具條是一個窗口,所以可以在調(diào)用CWnd : : SetWindowText來設(shè)置標題,例子如下:
int CMainFrame : : OnCreate (LPCREATESTRUCT lpCreateStruct )
{
…
// Set the caption of the toolbar .
m_wndToolBar.SetWindowText (_T "Standdard")
(34) 如何使窗口始終在最前方?
BringWindowToTop(Handle)
SetWindowPos函數(shù),指定窗口的 最頂風格,用WS_EX_TOPMOST擴展窗口的風格
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的資源編輯器,在對話框中顯示位圖是很容易的, 只需將圖形控件拖到對話中并選擇適當屬性即可,用戶也可以顯示圖標、位圖以及增強型元文件。
(36) 如何改變對話或窗體視窗的背景顏色
調(diào)用CWinApp : : SetDialogBkColor可以改變所有應用程序的背景顏色。第一個參數(shù)指定了背景顏色,第二個參數(shù)指定了文本顏色。下例將應用程序?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,通常用戶可以讓Windows選擇繪畫背景的刷子,也可重置該消息指定刷子。下例說明了創(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消息并返回一個用來繪畫對話背景的刷子句柄。注意:由于當重畫對話控件時也要調(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標簽,然后選擇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)建了一個當前背景顏色的刷子:
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特性, 則必須保證在對話的標記順序中buddy窗口優(yōu)先于旋轉(zhuǎn)按鈕控件。從Layout菜單中選擇Tab Order菜單項(或者按下Crtl+D)可以設(shè)置對話的標簽順序。
(44) 如何用位圖顯示下壓按鈕
Windows 95按鈕有幾處新的創(chuàng)建風格,尤其是BS_BITMAP和BS_ICON,要想具有位圖按鈕,創(chuàng)建按鈕和調(diào)用CButton : : SetBitmap或CButton : : SetIcon時要指定BS_BITMAP或BS_ICON風格。
首先,設(shè)置按鈕的圖標屬性。然后,當對話初始化時調(diào)用CButton: : SetIcon。注意:下例用圖標代替位圖,使用位圖時要小心,因為不知道背景所有的顏色——并非每個人都使用淺灰色。
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 風格位和檢測框以及按鈕來創(chuàng)建一個三態(tài)下壓按鈕。這很容易,只需將檢測框和按鈕拖拉到對話中并指定屬性Push—like即可。不用任何附加程序就可以成為三態(tài)下壓按鈕。
(46) 如何動態(tài)創(chuàng)建控件
分配一個控件對象的實例并調(diào)用其Create成員函數(shù)。開發(fā)者最容易忽略兩件事:忘記指定WS_VISBLE標簽和在棧中分配控件對象。下例動態(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) 如何限制編輯框中的準許字符
如果用戶在編輯控件中只允許接收數(shù)字,可以使用一個標準的編輯控件并指定新的創(chuàng)建標志ES_NUMBERS,它是Windows 95新增加的標志,該標志限制 編輯控件只按收數(shù)字字符。如果用戶需要復雜的編輯控件,可以使用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新的消息反射在控件類中指定顏色。 當控件需要重新著色時,工作框調(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ū)ο蟮姆椒ā?丶幚碓撓⒉⒅付ɡL畫屬性更合情合理。消息反射允許用戶這樣做。通知消息首先發(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) 當向列表框中添加多個項時如何防止閃爍
調(diào)用CWnd::SetRedraw 清除重畫標志可以禁止CListBox(或者窗口)重畫。當向列表框添加幾個項時,用戶可以清除重畫標志,然后添加項,最后恢復重畫標志。為確保重畫列表框的新項,調(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)
}