VCѧϰ֪֮ʶ´óÈ«
VCѧϰ֪֮ʶ´óÈ«
VCС֪ʶ×ܽᣡ
£¨1£© ÈçºÎͨ¹ý´úÂë»ñµÃÓ¦ÓóÌÐòÖ÷´°¿ÚµÄ Ö¸Õë?
Ö÷´°¿ÚµÄ Ö¸Õë±£´æÔÚCWinThread::m_pMainWndÖÐ,µ÷ÓÃAfxGetMainWndʵÏÖ¡£
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//ʹ³ÌÐò×î´ó»¯.
£¨2£© È·¶¨Ó¦ÓóÌÐòµÄ·¾¶
Use GetModuleFileName »ñµÃÓ¦ÓóÌÐòµÄ·¾¶£¬È»ºóÈ¥µô¿ÉÖ´ÐÐÎļþÃû¡£
Example:
TCHAR
exeFullPath[MAX_PATH] // MAX_PATHÔÚAPIÖж¨ÒåÁË°É£¬ºÃÏóÊÇ
128
GetModuleFileName(NULL,exeFullPath,MAX_PATH)
£¨3£© ÈçºÎÔÚ³ÌÐòÖлñµÃÆäËû³ÌÐòµÄ ͼ±ê?
Á½ÖÖ·½·¨:
(1) SDKº¯Êý SHGetFileInfo »òʹÓà ExtractIcon»ñµÃͼ±ê×ÊÔ´µÄ handle,
(2) SDKº¯Êý SHGetFileInfo »ñµÃÓйØÎļþµÄºÜ¶àÐÅÏ¢,Èç´óСͼ±ê,ÊôÐÔ, ÀàÐ͵È.
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µÄ·¾¶Õý¹æÉÏÀ´ËµÓÃGetWindowsDirectoryº¯ÊýµÃµ½, Èç¹ûÊǵ÷Óà win95ϵĻ±Ê£¬Ó¦¸ÃÓ÷ÃÎÊ×¢²á±íµÄ·½·¨»ñµÃÆä·¾¶£¬Òª×÷³ÉÒ»¸ö±È½Ï¿¼¾¿µÄ³ÌÐò£¬¿¼ÂÇÓ¦¸ÃÈ«Ãæµã.
£¨4£© »ñµÃ¸÷ÖÖĿ¼ÐÅÏ¢
WindowsĿ¼: Use "GetWindowsDirectory"
WindowsϵÄsystemĿ¼: Use "GetSystemDirectory"
tempĿ¼: Use "GetTempPath"
µ±Ç°Ä¿Â¼: Use "GetCurrentDirectory"
Çë×¢ÒâÇ°Á½¸öº¯ÊýµÄµÚÒ»¸ö²ÎÊýΪĿ¼±äÁ¿Ãû£¬ºóÒ»¸öΪ»º³åÇøºóÁ½¸öÏà·´.
£¨5£© ÈçºÎ×Ô¶¨ÒåÏûÏ¢
1) ÊÖ¹¤¶¨ÒåÏûÏ¢£¬¿ÉÒÔÕâôд
#define WM_MY_MESSAGE(WM_USER+100),
MS ÍƼöµÄÖÁÉÙÊÇ WM_USER+100
(2)дÏûÏ¢´¦Àíº¯Êý,ÓÃ
WPARAM,LPARAM·µ»ØLRESULT.
LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)
{
tempĿ¼: Use "GetTempPath"
//¼ÓÈëÄãµÄ´¦Àíº¯Êý irectory"
}
£¨6£© ÈçºÎ¸Ä±ä´°¿ÚµÄͼ±ê?
Ïò´°¿Ú·¢ËÍ WM_SECTIONÏûÏ¢¡£
Example:
HICON hIcon=AfxGetApp() ->LoadIcon(IDI_ICON)
ASSERT(hIcon)
AfxGetMainWnd() ->SendMessage(WM_SECTION,TRUE,(LPARAM)hIcon)
£¨7£© ÈçºÎ¸Ä±ä´°¿ÚµÄȱʡ·ç¸ñ?
ÖØÔØ CWnd:: PreCreateWindow ²¢ÐÞ¸ÄCREATESTRUCT½á¹¹À´Ö¸¶¨´°¿Ú·ç¸ñºÍÆäËû´´½¨ÐÅÏ¢.
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 º¯ÊýÖÐÉ趨 m_nCmdShowµÄÈ¡Öµ.
m_nCmdShow=SW_SHOWMAXMIZED //×î´ó»¯
m_nCmdShow=SW_SHOWMINMIZED //×îС»¯
m_nCmdShow=SW_SHOWNORMAL //Õý³£·½Ê½
MDI´°¿Ú:
Èç¹ûÊÇ´´½¨ÐµÄÓ¦ÓóÌÐò,¿ÉÒÔÓÃMFC AppWizard µÄAdvanced °´Å¥²¢ÔÚMDI×Ó´°¿Ú·ç¸ñ×éÖмì²â×î´ó»¯»ò×îС»¯»¹¿ÉÒÔÖØÔØ MDI Window µÄPreCreateWindowº¯Êý£¬ÉèÖÃWS_MAXMIZE or WS_MINMIZE
Èç¹û´Ó CMDIChildWndÅÉÉú,µ÷Óà OnInitialUpdateº¯ÊýÖÐµÄ CWnd::Show WindowÀ´Ö¸¶¨ MDI Child WindowµÄ·ç¸ñ¡£
£¨10£© ÈçºÎÏÞÖÆ´°¿ÚµÄ´óС?
Ò²¾ÍÊÇ FixedDialogÐÎʽ¡£ Windows·¢ËÍ WM_GETMAXMININFOÏûÏ¢À´¸ú×Ù, ÏìÓ¦Ëü,ÔÚ OnGetMAXMININFO ÖÐд´úÂë:
£¨11£© ÈçºÎʹ´°¿Ú²»¿É¼û£¿
ºÜ¼òµ¥,ÓÃSW_HIDE Òþ²Ø´°¿Ú£¬¿ÉÒÔ½áºÏ FindWindow,ShowWindow¿ØÖÆ.
£¨12£© ÈçºÎ´´½¨Ò»¸ö×Ö»ØÈƵÄCEditView
ÖØÔØCWnd : : PreCreateWindowºÍÐÞ¸ÄCREATESTRUCT½á¹¹£¬¹Ø±ÕCEditView¶ÔÏóµÄES_AUTOHSCROLLºÍWS_HSCROLL·ç¸ñ룬 ÓÉÓÚCEditView : : PreCreateWindowÏÔʾÉèÖÃcs. style£¬µ÷ÓûùÀຯÊýºóÒªÐÞ¸Ä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£© ÈçºÎʹ³ÌÐò±£³Ö¼«Ð¡×´Ì¬?
Õâô°ì: ÔÚ»Ö¸´³ÌÐò´°Ìå´óСʱ£¬Windows»á·¢ËÍWM_QUERY-OPENÏûÏ¢£¬Óà ClassWizardÉèÖóÉÔ±º¯Êý
OnQueryOpen() ,add following code:
Bool CMainFrame:: OnQueryOpen( )
{
Return false
}
£¨14£© Òƶ¯´°¿Ú
µ÷ÓÃCWnd : : SetWindowPos²¢Ö¸¶¨SWP_NOSIZE±êÖ¾¡£Ä¿µÄλÖÃÓ븸´°¿ÚÓйأ¨¶¥²ã´°¿ÚÓëÆÁÄ»Óйأ©¡£µ÷ÓÃ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ÅÉÉúµÄÊÓ´°À࣬ ·â×°ÁËͨÓÿؼþµÄ¹¦ÄÜ£¬µ«ÈÔȻʹÓù¤×÷¿òÎĵµÏÔʾ´°¿ÚÌåϵ½á¹¹£ºCEditView·â×°Á˱༿ؼþ£¬CTreeView±£³ÖÁËÊ÷Áбí¿Ø¼þ£¬CListView·â×°ÁËÁбíÏÔʾ´°¿Ú¿Ø¼þ£¬CRichEditView¿ÉÒÔ´¦Àí¶àÖֱ༿ؼþ¡£
£¨16£© ÖØÖô°¿ÚµÄ´óС
µ÷ÓÃCWnd: : SetWindowPos²¢Ö¸¶¨SWP_NOMOVE±êÖ¾£¬ Ò²¿Éµ÷ÓÃ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£© ÈçºÎµ¥»÷³ýÁË´°¿Ú±êÌâÀ¸ÒÔÍâµÄÇøÓòʹ´°¿ÚÒƶ¯
µ±´°¿ÚÐèҪȷ¶¨Êó±êλÖÃʱWindowsÏò´°¿Ú·¢ËÍWM_NCHITTESTÐÅÏ¢£¬¿ÉÒÔ´¦Àí¸ÃÐÅϢʹWindowsÈÏΪÊó±êÔÚ´°¿Ú±êÌâÉÏ¡£¶ÔÓÚ¶Ô»°¿òºÍ»ùÓÚ¶Ô»°µÄÓ¦ÓóÌÐò£¬¿ÉÒÔʹÓÃClassWizard´¦Àí¸ÃÐÅÏ¢²¢µ÷ÓûùÀຯÊý£¬ Èç¹ûº¯Êý·µ»ØHTCLIENT Ôò±íÃ÷Êó±êÔÚ¿Í·¿ÇøÓò£¬·µ»ØHTCAPTION±íÃ÷Êó±êÔÚWindowsµÄ±êÌâÀ¸ÖС£
UINT CSampleDialog : : OnNcHitTest (Cpoint point )
{
UINT nHitTest =Cdialog: : OnNcHitTest (point )
return (nHitTest = =HTCLIENT)? HTCAPTION : nHitTest
}
ÉÏÊö¼¼ÊõÓÐÁ½µã²»ÀûÖ®´¦£¬
ÆäÒ»ÊÇÔÚ´°¿ÚµÄ¿Í»§ÇøÓòË«»÷ʱ£¬´°¿Ú½«¼«´ó£»
Æä¶þ£¬ Ëü²»Êʺϰüº¬¼¸¸öÊÓ´°µÄÖ÷¿ò´°¿Ú¡£
»¹ÓÐÒ»ÖÖ·½·¨£¬µ±Óû§°´ÏÂÊó±ê×ó¼üʹÖ÷¿ò´°¿ÚÈÏΪÊó±êÔÚÆä´°¿Ú±êÌâÉÏ£¬Ê¹ÓÃClassWizardÔÚÊÓ´°Öд¦ÀíWM_LBUTTODOWNÐÅÏ¢²¢ÏòÖ÷¿ò´°¿Ú·¢ËÍÒ»¸ö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) )
}
¸Ã¼¼ÊõÒ²ÊÊÓÃÓÚ¶Ô»°¿òºÍ»ùÓÚ¶ÔµÄÓ¦ÓóÌÐò£¬Ö»ÊDz»±Øµ÷ÓÃ
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Ïò´°¿Ú·¢ËÍÒ»¸ö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£© ÈçºÎ¸Ä±ä´°¿Ú±êÌâ
µ÷ÓÃCWnd : : SetWindowText¿ÉÒԸıäÈκδ°¿Ú£¨°üÀ¨¿Ø¼þ£©µÄ±êÌâ¡£
//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 ") )
Èç¹ûÐèÒª¾³£Ð޸Ĵ°¿ÚµÄ±êÌ⣨ע£º¿Ø¼þÒ²ÊÇ´°¿Ú£©£¬Ó¦¸Ã¿¼ÂÇʹÓðëÎĵµ»¯µÄº¯ÊýAfxSetWindowText¡£¸Ãº¯ÊýÔÚAFXPRIV.HÖÐ˵Ã÷£¬ÔÚWINUTIL.CPPÖÐʵÏÖ£¬ÔÚÁª»ú°ïÖúÖÐÕÒ²»µ½Ëü£¬ËüÔÚAFXPRIV.HÖаëÎĵµ»¯£¬ ÔÚÒÔºó·¢ÐеÄMFCÖн«Îĵµ»¯¡£
AfxSetWindowTextµÄʵÏÖÈçÏ£º
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£© ÈçºÎ·ÀÖ¹Ö÷¿ò´°¿ÚÔÚÆä˵Ã÷ÖÐÏÔʾ»î¶¯µÄÎĵµÃû
´´½¨Ö÷¿ò´°¿ÚºÍMDI×Ó´°¿Ú½øͨ³£¾ßÓÐFWS_ADDTOTITLE·ç¸ñ룬Èç¹û²»Ï£ÍûÔÚ˵Ã÷ÖÐ×Ô¶¯Ìí¼ÓÎĵµÃû£¬ ±ØÐë½ûÖ¹¸Ã·ç¸ñ룬 ¿ÉÒÔʹÓÃClassWizardÖØÖÃ
CWnd: : PreCreateWindow²¢¹Ø±ÕFWS_ADDTOTITLE·ç¸ñ¡£
BOOL CMainFrame : : PreCreateWindow (CREATESTRUCT&cs)
{
//Turn off FWS_ADDTOTITLE in main frame .
cs.styel & = ~FWS_ADDTOTITLE
return CMDIFrameWnd : : PreCreateWindow (cs )
}
¹Ø±ÕMDI×Ó´°¿ÚµÄFWS _ADDTOTITLE·ç¸ñ½«´´½¨Ò»¸ö¾ßÓпձêÌâµÄ´°¿Ú£¬¿ÉÒÔµ÷ÓÃCWnd: : SetWindowTextÀ´ÉèÖñêÌâ¡£¼Çס×Ô¼ºÉèÖñêÌâʱҪ×ñѽӿڷç¸ñÖ¸ÄÏ¡£
£¨21£© ÈçºÎ»ñÈ¡Óйش°¿ÚÕýÔÚ´¦ÀíµÄµ±Ç°ÏûÏ¢µÄÐÅÏ¢
µ÷ÓÃCWnd: : GetCurrentMessage¿ÉÒÔ»ñÈ¡Ò»¸öMSGÖ¸Õë¡£ÀýÈ磬¿ÉÒÔʹÓÃClassWizard½«¼¸¸ö²Ëµ¥Ïî´¦Àí³ÌÐòÓ³Éäµ½Ò»¸öº¯ÊýÖУ¬È»ºóµ÷ÓÃGetCurrentMessageÀ´È·¶¨ËùÑ¡ÖеIJ˵¥Ïî¡£
viod CMainFrame : : OnCommmonMenuHandler ( )
{
//Display selected menu item in debug window .
TRACE ("Menu item %u was selected . /n" ,
£¨22£© ÈçºÎÔÚ´úÂëÖлñÈ¡¹¤¾ßÌõºÍ״̬ÌõµÄÖ¸Õë
ȱʡʱ£¬ ¹¤×÷¿ò´´½¨×´Ì¬ÌõºÍ¹¤¾ßÌõʱ½«ËüÃÇ×÷ΪÖ÷¿ò´°¿ÚµÄ×Ó´°¿Ú£¬×´Ì¬ÌõÓÐÒ»¸öAFX_IDW_STATUS_BAR±êʶ·û£¬¹¤¾ßÌõÓÐÒ»¸öAFX_IDW_TOOLBAR±êʶ·û£¬ÏÂÀý˵Ã÷ÁËÈçºÎͨ¹ýÒ»Æðµ÷ÓÃ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£© ÈçºÎʹÄܺͽûÖ¹¹¤¾ßÌõµÄ¹¤¾ßÌáʾ
Èç¹ûÉèÖÃÁËCBRS_TOOLTIPS·ç¸ñ룬¹¤¾ßÌõ½«ÏÔʾ¹¤¾ßÌáʾ£¬ÒªÊ¹ÄÜ»òÕß½ûÖ¹¹¤¾ßÌáʾ£¬ÐèÒªÉèÖûòÕßÇå³ý¸Ã·ç¸ñλ¡£ÏÂÀýͨ¹ýµ÷ÓÃCControlBar : : GetBarStyleºÍCControlBar : : SetBarStyle½¨Á¢Ò»¸öÍê³É´Ë¹¦ÄܵijÉÔ±º¯Êý£º
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£© ÈçºÎ´´½¨Ò»¸ö²»¹æÔòÐÎ×´µÄ´°¿Ú
¿ÉÒÔʹÓÃеÄSDKº¯ÊýSetWindowRgn¡£¸Ãº¯Êý½«»æ»ºÍÊó±êÏûÏ¢ÏÞ¶¨ÔÚ´°¿ÚµÄÒ»¸öÖ¸¶¨µÄÇøÓò£¬Êµ¼ÊÉÏʹ´°¿Ú³ÉΪָ¶¨µÄ²»¹æÔòÐÎ×´¡£ ʹÓÃAppWizard´´½¨Ò»¸ö»ùÓÚ¶ÔµÄÓ¦ÓóÌÐò²¢Ê¹ÓÃ×ÊÔ´±à¼Æ÷´ÓÖ÷¶Ô»°×ÊÔ´ÖÐɾ³ýËùÔÚµÄȱʡ¿Ø¼þ¡¢±êÌâÒÔ¼°±ß½ç¡£
¸ø¶Ô»°ÀàÔö¼ÓÒ»¸öCRgnÊý¾Ý³ÉÔ±£¬ÒÔºóҪʹÓøÃÊý¾Ý³ÉÔ±½¨Á¢´°¿ÚÇøÓò¡£
Class CRoundDlg : public CDialog
{
…
private :
Crgn m_rgn : // window region
…
}
ÐÞ¸ÄOnInitDialogº¯Êý½¨Á¢Ò»¸öÍÖÔ²ÇøÓò²¢µ÷ÓÃSetWindowRgn½«¸ÃÇøÓò·ÖÅä¸ø´°¿Ú£º
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
}
ͨ¹ý½¨Á¢ÇøÓòºÍµ÷ÓÃSetWindowRgn£¬ÒѾ½¨Á¢Ò»¸ö²»¹æÔòÐÎ×´µÄ´°¿Ú£¬ÏÂÃæµÄÀý×Ó³ÌÐòÊÇÐÞ¸ÄOnPaintº¯Êýʹ´°¿ÚÐÎ×´¿´ÆðÀ´ÏóÒ»¸öÇòÐÎÌå¡£
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 ÖÐ,¿ÉÒÔÕâôµ÷ÓÃAfxGetInstancdHandle»ñµÃ¾ä±ú.
Example: HANDLE hInstance=AfxGetInstanceHandle()
£¨26£© ÈçºÎ±à³Ì½áÊøÓ¦ÓóÌÐò?
ÕâÊǸöºÜ¼òµ¥ÓÖÊDZà³ÌÖо³£ÒªÓöµ½µÄÎÊÌâ.
Ïò´°¿Ú·¢ËÍ WM_CLOSEÏûÏ¢,µ÷Óà CWnd::OnClose³ÉÔ±º¯Êý.ÔÊÐí¶ÔÓû§ÌáʾÊÇ·ñ±£´æÐ޸ĹýµÄÊý¾Ý.
Example: AfxGetMainWindow()->SendMessage(WM_CLOSE)
»¹¿ÉÒÔ´´½¨Ò»¸ö×Ô¶¨ÒåµÄº¯Êý Terminate Window
void Terminate Window(LPCSTR pCaption)
{
CWnd *pWnd=Cwnd::FindWindow(NULL,pCaption)
if (pWnd)
pWnd ->SendMessage(WM_CLOSE)
}
˵Ã÷: FindWindowº¯Êý²»ÊÇÌᳫµÄ×ö·¨£¬ÒòΪËüÎÞ·¨´¦Àí±êÌâÀ¸×Ô¶¯¸Ä±ä£¬±ÈÈçÎÒÃÇÒª¼ì²â NotepadÊDz»ÊÇÒÑÔËÐжøÊÂÏȲ»ÖªµÀNotepadµÄ±êÌâÀ¸,ÕâʱFindWindow¾ÍÎÞÄÜΪÁ¦ÁË£¬¿ÉÒÔͨ¹ýö¾Ù windowsÈÎÎñÁбíµÄ°ì·¨À´ÊµÏÖ¡£ÔÚ»úе³ö°æÉç"Windows 95 API¿ª·¢ÈËÔ±Ö¸ÄÏ"Ò»ÊéÓбȽÏÏêϸµÄ½éÉÜ,ÕâÀï¾Í²»ÔÙ¶à˵ÀÖ¡£
£¨27£© ÈçºÎ´´½¨ºÍʹÓÃÎÞģʽ¶Ô»°¿ò
MFC½«Ä£Ê½ºÍÎÞģʽ¶Ô»°·â×°ÔÚͬһ¸öÀàÖУ¬µ«ÊÇʹÓÃÎÞģʽ¶Ô»°ÐèÒª¼¸¸ö¶Ô»°ÐèÒª¼¸¸ö¶î´¦µÄ²½Öè¡£Ê×ÏÈ£¬Ê¹ÓÃ×ÊÔ´±à¼Æ÷½¨Á¢¶Ô»°×ÊÔ´²¢Ê¹ÓÃClassWizard´´½¨Ò»¸öCDialogµÄÅÉÉúÀࡣģʽºÍÎÞģʽ¶Ô»°µÄÖÐÖ¹ÊDz»Ò»ÑùµÄ£ºÄ£Ê½¶Ô»°Í¨¹ýµ÷ÓÃCDialog : : EndDialog À´ÖÐÖ¹£¬ÎÞģʽ¶Ô»°ÔòÊǵ÷ÓÃCWnd: : DestroyWindowÀ´ÖÐÖ¹µÄ£¬º¯ÊýCDialog : : OnOKºÍCDialog : : OnCancelµ÷ÓÃEndDialog ,ËùÒÔÐèÒªµ÷ÓÃDestroyWindow²¢ÖØÖÃÎÞģʽ¶Ô»°µÄº¯Êý¡£
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++¶ÔÏ󡣶ÔÓÚģʽ¶ÔÀ´Ëµ£¬ÕâºÜÈÝÒ×£¬ÐèÒª´´½¨º¯Êý·µ»Øºó¼´¿Éɾ³ýC++¶ÔÏó£»ÎÞģʽ¶Ô»°²»ÊÇͬ²½µÄ£¬´´½¨º¯Êýµ÷ÓúóÁ¢¼´·µ»Ø£¬Òò¶øÓû§²»ÖªµÀºÎʱɾ³ýC++¶ÔÏó¡£³·Ïú´°¿Úʱ¹¤×÷¿òµ÷ÓÃCWnd : : PostNcDestroy£¬¿ÉÒÔÖØÖøú¯Êý²¢Ö´ÐÐÇå³ý²Ù×÷£¬ÖîÈçɾ³ýthisÖ¸Õë¡£
void CSampleDialog : : PostNcDestroy ( )
{
// Declete the C++ object that represents this dialog.
delete this
×îºó£¬Òª´´½¨ÎÞģʽ¶Ô»°¡£¿ÉÒÔµ÷ÓÃCDialog : : DoModal´´½¨Ò»¸öģʽ¶Ô·Å£¬Òª´´½¨Ò»¸öÎÞģʽ¶Ô»°ÔòÒªµ÷ÓÃCDialog: : Create¡£ÏÂÃæµÄÀý×Ó˵Ã÷ ÁËÓ¦ÓóÌÐòÊÇÈçºÎ´´½¨ÎÞģʽ¶Ô»°µÄ£º Ïó£»ÎÞģʽ¶Ô»°²»ÊÇͬ²½µÄ£¬´´½¨º¯Êýµ÷ÓúóÁ¢¼´·µ»Ø£¬
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£© ÈçºÎ·ÀÖ¹Ö÷¿ò´°¿ÚÔÚÆä˵Ã÷ÖÐÏÔʾ»î¶¯µÄÎĵµÃû
´´½¨Ö÷¿ò´°¿ÚºÍMDI×Ó´°¿Ú½øͨ³£¾ßÓÐFWS_ADDTOTITLE·ç¸ñ룬Èç¹û²»Ï£ÍûÔÚ˵Ã÷ÖÐ×Ô¶¯Ìí¼ÓÎĵµÃû£¬ ±ØÐë½ûÖ¹¸Ã·ç¸ñ룬 ¿ÉÒÔʹÓÃClassWizardÖØÖÃ
CWnd: : PreCreateWindow²¢¹Ø±ÕFWS_ADDTOTITLE·ç¸ñ¡£
BOOL CMainFrame : : PreCreateWindow (CREATESTRUCT&cs)
{
//Turn off FWS_ADDTOTITLE in main frame .
cs.styel & = ~FWS_ADDTOTITLE
return CMDIFrameWnd : : PreCreateWindow (cs )
}
¹Ø±ÕMDI×Ó´°¿ÚµÄFWS _ADDTOTITLE·ç¸ñ½«´´½¨Ò»¸ö¾ßÓпձêÌâµÄ´°¿Ú£¬¿ÉÒÔµ÷ÓÃCWnd: : SetWindowTextÀ´ÉèÖñêÌâ¡£¼Çס×Ô¼ºÉèÖñêÌâʱҪ×ñѽӿڷç¸ñÖ¸ÄÏ¡£
£¨29£© ÈçºÎÔÚ´úÂëÖлñÈ¡¹¤¾ßÌõºÍ״̬ÌõµÄÖ¸Õë
ȱʡʱ£¬ ¹¤×÷¿ò´´½¨×´Ì¬ÌõºÍ¹¤¾ßÌõʱ½«ËüÃÇ×÷ΪÖ÷¿ò´°¿ÚµÄ×Ó´°¿Ú£¬×´Ì¬ÌõÓÐÒ»¸öAFX_IDW_STATUS_BAR±êʶ·û£¬¹¤¾ßÌõÓÐÒ»¸öAFX_IDW_TOOLBAR±êʶ·û£¬ÏÂÀý˵Ã÷ÁËÈçºÎͨ¹ýÒ»Æðµ÷ÓÃ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º¯Êý winexec, shellexecute,createprocess¿ÉÒÔʹÓá£
WinExec×î¼òµ¥£¬Á½¸ö²ÎÊý£¬Ç°Ò»¸öÖ¸¶¨Â·¾¶£¬ºóÒ»¸öÖ¸¶¨ÏÔʾ·½Ê½.ºóÒ»¸ö²ÎÊýÖµµÃ˵һÏ£¬±ÈÈçÄàÓà SW_SHOWMAXMIZED·½Ê½È¥¼ÓÔØÒ»¸öÎÞ×î´ó»¯°´Å¥µÄ³ÌÐò£¬¾ÍÊÇNeterm,calcµÈµÈ£¬¾Í²»»á³öÏÖÕý³£µÄ´°Ì壬µ«ÊÇÒѾ±»¼Óµ½ÈÎÎñÁбíÀïÁË¡£
ShellExecute½Ï WinExexÁé»îÒ»µã£¬¿ÉÒÔÖ¸¶¨¹¤×÷Ŀ¼,ÏÂÃæµÄExample¾ÍÊÇÖ±½Ó´ò¿ª c:/temp/1.txt,¶ø²»ÓüÓÔØÓë txtÎļþ¹ØÁªµÄÓ¦ÓóÌÐò,ºÜ¶à°²×°³ÌÐòÍê³Éºó¶¼»á´ò¿ªÒ»¸ö´°¿Ú£¬À´ÏÔʾReadme or Faq,ÎҲ¾ÍÊÇÕâô×÷µÄÀ².
ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c://temp"),SW_SHOWMAXMIZED)
CreateProcess×ÔÓ£¬Ò»¹²ÓÐÊ®¸ö²ÎÊý£¬²»¹ý´ó²¿·Ö¶¼¿ÉÒÔÓÃNULL´úÌ棬Ëü¿ÉÒÔÖ¸¶¨½ø³ÌµÄ°²È«ÊôÐÔ£¬¼Ì³ÐÐÅÏ¢£¬ÀàµÄÓÅÏȼ¶µÈµÈ.À´¿´¸öºÜ¼òµ¥µÄExample:
STARTUPINFO stinfo
//Æô¶¯´°¿ÚµÄÐÅÏ¢
PROCESSINFO procinfo //½ø³ÌµÄÐÅÏ¢
CreateProcess(NULL,_T("notepad.exe"),NULL,NULL.FALSE,
NORMAL_PRIORITY_
CLASS,NULL,NULL, &stinfo,&procinfo)
£¨31£© ÈçºÎÔÚ´úÂëÖлñÈ¡¹¤¾ßÌõºÍ״̬ÌõµÄÖ¸Õë
ȱʡʱ£¬ ¹¤×÷¿ò´´½¨×´Ì¬ÌõºÍ¹¤¾ßÌõʱ½«ËüÃÇ×÷ΪÖ÷¿ò´°¿ÚµÄ×Ó´°¿Ú£¬×´Ì¬ÌõÓÐÒ»¸öAFX_IDW_STATUS_BAR±êʶ·û£¬¹¤¾ßÌõÓÐÒ»¸öAFX_IDW_TOOLBAR±êʶ·û£¬ÏÂÀý˵Ã÷ÁËÈçºÎͨ¹ýÒ»Æðµ÷ÓÃCWnd: : GetDescendantWindowºÍAfxGetMainWndÀ´»ñÈ¡ÕâЩ×Ó´°¿ÚµÄÖ¸Õ룺
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)
£¨32£© ÈçºÎʹÄܺͽûÖ¹¹¤¾ßÌõµÄ¹¤¾ßÌáʾ
Èç¹ûÉèÖÃÁËCBRS_TOOLTIPS·ç¸ñ룬¹¤¾ßÌõ½«ÏÔʾ¹¤¾ßÌáʾ£¬ÒªÊ¹ÄÜ»òÕß½ûÖ¹¹¤¾ßÌáʾ£¬ÐèÒªÉèÖûòÕßÇå³ý¸Ã·ç¸ñλ¡£ÏÂÀýͨ¹ýµ÷ÓÃCControlBar : : GetBarStyleºÍCControlBar : : SetBarStyle½¨Á¢Ò»¸öÍê³É´Ë¹¦ÄܵijÉÔ±º¯Êý£º
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£© ÈçºÎÉèÖù¤¾ßÌõ±êÌâ
¹¤¾ßÌõÊÇÒ»¸ö´°¿Ú£¬ËùÒÔ¿ÉÒÔÔÚµ÷ÓÃCWnd : : SetWindowTextÀ´ÉèÖñêÌ⣬Àý×ÓÈçÏ£º
int CMainFrame : : OnCreate (LPCREATESTRUCT lpCreateStruct )
{
…
// Set the caption of the toolbar .
m_wndToolBar.SetWindowText (_T "Standdard")
£¨34£© ÈçºÎʹ´°¿ÚʼÖÕÔÚ×îÇ°·½?
BringWindowToTop(Handle)
SetWindowPosº¯Êý£¬Ö¸¶¨´°¿ÚµÄ ×·ç¸ñ,ÓÃ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ÏȽøµÄ¾²Ì¬¿Ø¼þºÍMicrosoftµÄ×ÊÔ´±à¼Æ÷£¬ÔÚ¶Ô»°¿òÖÐÏÔʾλͼÊǺÜÈÝÒ׵ģ¬ Ö»Ð轫ͼÐοؼþÍϵ½¶Ô»°Öв¢Ñ¡ÔñÊʵ±ÊôÐÔ¼´¿É£¬Óû§Ò²¿ÉÒÔÏÔʾͼ±ê¡¢Î»Í¼ÒÔ¼°ÔöÇ¿ÐÍÔªÎļþ¡£
£¨36£© ÈçºÎ¸Ä±ä¶Ô»°»ò´°ÌåÊÓ´°µÄ±³¾°ÑÕÉ«
µ÷ÓÃCWinApp : : SetDialogBkColor¿ÉÒԸıäËùÓÐÓ¦ÓóÌÐòµÄ±³¾°ÑÕÉ«¡£µÚÒ»¸ö²ÎÊýÖ¸¶¨Á˱³¾°ÑÕÉ«£¬µÚ¶þ¸ö²ÎÊýÖ¸¶¨ÁËÎı¾ÑÕÉ«¡£ÏÂÀý½«Ó¦ÓóÌÐò¶Ô»°ÉèÖÃΪÀ¶É«±³¾°ºÍ»ÆÉ«Îı¾¡£
BOOL CSampleApp : : InitInstance ( )
{
…
//use blue dialog with yellow text .
SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) )
…
}
ÐèÒªÖØ»¶Ô»°£¨»ò¶Ô»°µÄ×ӿؼþ£©Ê±£¬WindowsÏò¶Ô»°·¢ËÍÏûÏ¢WM_CTLCOLOR£¬Í¨³£Óû§¿ÉÒÔÈÃWindowsÑ¡Ôñ»æ»±³¾°µÄË¢×Ó£¬Ò²¿ÉÖØÖøÃÏûÏ¢Ö¸¶¨Ë¢×Ó¡£ÏÂÀý˵Ã÷ÁË´´½¨Ò»¸öºìÉ«±³¾°¶Ô»°µÄ²½Öè¡£
Ê×ÏÈ£¬¸ø¶Ô»°»ùÀàÔö¼ÓÒ»È˳ÉÔ±±äÁ¿
CBursh :class CMyFormView : public CFormView
{
…
private :
CBrush m_ brush // background brush
…
}
Æä´Î£¬ ÔÚÀàµÄ¹¹Ô캯ÊýÖн«Ë¢×Ó³õʼ»¯ÎªËùÐèÒªµÄ±³¾°ÑÕÉ«¡£
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255) )
}
×îºó£¬Ê¹ÓÃClassWizard´¦ÀíWM_CTLCOLORÏûÏ¢²¢·µ»ØÒ»¸öÓÃÀ´»æ»¶Ô»°±³¾°µÄË¢×Ó¾ä±ú¡£×¢Ò⣺ÓÉÓÚµ±ÖØ»¶Ô»°¿Ø¼þʱҲҪµ÷Óøú¯Êý£¬ËùÒÔÒª¼ì²â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£© ÈçºÎ»ñÈ¡Ò»¸ö¶Ô»°¿Ø¼þµÄÖ¸Õë
ÓÐÁ½ÖÖ·½·¨¡£ÆäÒ»£¬µ÷ÓÃCWnd: : GetDlgItem£¬»ñÈ¡Ò»¸öCWnd*Ö¸Õëµ÷ÓóÉÔ±º¯Êý¡£ÏÂÀýµ÷ÓÃGetDlgItem£¬½«·µ»ØÖµ´«¸øÒ»¸öCSpinButtonCtrl*ÒÔ±ãµ÷ÓÃCSpinButtonCtrl : : SetPos º¯Êý£º
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½«¿Ø¼þºÍ³ÉÔ±±äÁ¿ÁªÏµÆðÀ´¡£ÔÚClassWizardÖмòµ¥µØÑ¡ÔñMember Variables±êÇ©£¬È»ºóÑ¡ÔñAdd Variable …°´Å¥¡£Èç¹ûÔÚ¶Ô»°×ÊÔ´±à¼Æ÷ÖУ¬°´ÏÂCtrl¼ü²¢Ë«»÷¿Ø¼þ¼´¿Éתµ½Add Member Variable¶Ô»°¡£
£¨38£© ÈçºÎ½ûÖ¹ºÍʹÄܿؼþ
¿Ø¼þÒ²ÊÇ´°¿Ú£¬ËùÒÔ¿ÉÒÔµ÷ÓÃCWnd : : EnableWindowʹÄܺͽûÖ¹¿Ø¼þ¡£
//Disable button controls .
m_wndOK.EnableWindow (FALSE )
m_wndApply.EnableWindow (FALSE )
£¨39£© ÈçºÎ¸Ä±ä¿Ø¼þµÄ×ÖÌå
ÓÉÓڿؼþÊÇÒ²ÊÇ´°¿Ú£¬Óû§¿ÉÒÔµ÷ÓÃCWnd: : SetFontÖ¸¶¨ÐÂ×ÖÌå¡£¸Ãº¯ÊýÓÃÒ»¸ö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Êý¾ÝÀàÐÍ
ÖîÈçCOleControl : : GetFortColorºÍCOleControl : : GetBackColorµÈº¯Êý·µ»ØOLE _COLORÊý¾ÝÀàÐ͵ÄÑÕÉ«£¬¶øGDI¶ÔÏóÖîÈç±ÊºÍË¢×ÓʹÓõÄÊÇCOLORREFÊý¾ÝÀàÐÍ£¬µ÷ÓÃCOleControl : : TranslateColor¿ÉÒÔºÜÈÝÒ׵ؽ«OLE_COLORÀàÐ͸ÄΪCOLORREFÀàÐÍ¡£ÏÂÀý´´½¨ÁËÒ»¸öµ±Ç°±³¾°ÑÕÉ«µÄË¢×Ó£º
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£© ÔÚ²»Ê¹ÓÃͨÓÃÎļþ´ò¿ª¶Ô»°µÄÇé¿öÏÂÈçºÎÏÔʾһ¸öÎļþÁбí
µ÷ÓÃCWnd: : DlgDirList»òÕßCWnd: : DlgDirListComboBox£¬Windows ½«×Ô¶¯µØÏòÁбí¿ò»ò×éºÏ¿òÌî³ä¿ÉÓõÄÇý¶¯Æ÷Ãû»òÕßÖ¸¶¨Ä¿Â¼ÖеÄÎļþ£¬ÏÂÀý½«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£© ΪʲôÐýת°´Å¥¿Ø¼þ¿´ÆðÀ´µ¹×ª
ÐèÒªµ÷ÓÃCSpinCtrl : : SetRange ÉèÖÃÐýת°´Å¥¿Ø¼þµÄ·¶Î§£¬Ðýת°´Å¥¿Ø¼þµÄȱʡÉÏÏÞΪ0£¬È±Ê¡ÏÂÏÞΪ100£¬ÕâÒâζ×ÅÔö¼ÓʱÐýת°´¿Ø¼þµÄÖµÓÉ100±äΪ0¡£ÏÂÀý½«Ðýת°´Å¥¿Ø¼þµÄ·¶Î§ÉèÖÃΪ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Ðýת°´Å¥¿Ø¼þÒ²ÓÐͬÑùµÄÎÊÌ⣺°´ÏÂUp°´Å¥Ê±¿½±´µÄÊýÄ¿¼õÉÙ£¬¶ø°´ÏÂDown °´Å¥Ê±¿½±´µÄÊýÄ¿Ôö¼Ó¡£
£¨43£© ΪʲôÐýת°´Å¥¿Ø¼þ²»ÄÜ×Ô¶¯µØ¸üÐÂËüÏÂÃæµÄ±à¼¿Ø¼þ
Èç¹ûʹÓÃÐýת°´Å¥µÄautu buddyÌØÐÔ£¬ Ôò±ØÐë±£Ö¤ÔÚ¶Ô»°µÄ±ê¼Ç˳ÐòÖÐbuddy´°¿ÚÓÅÏÈÓÚÐýת°´Å¥¿Ø¼þ¡£´ÓLayout²Ëµ¥ÖÐÑ¡ÔñTab Order²Ëµ¥Ï»òÕß°´ÏÂCrtl+D£©¿ÉÒÔÉèÖöԻ°µÄ±êǩ˳Ðò¡£
£¨44£© ÈçºÎÓÃλͼÏÔʾÏÂѹ°´Å¥
Windows 95°´Å¥Óм¸´¦ÐµĴ´½¨·ç¸ñ£¬ÓÈÆäÊÇBS_BITMAPºÍBS_ICON£¬ÒªÏë¾ßÓÐλͼ°´Å¥£¬´´½¨°´Å¥ºÍµ÷ÓÃCButton : : SetBitmap»òCButton : : SetIconʱҪָ¶¨BS_BITMAP»òBS_ICON·ç¸ñ¡£
Ê×ÏÈ£¬ÉèÖð´Å¥µÄͼ±êÊôÐÔ¡£È»ºó£¬µ±¶Ô»°³õʼ»¯Ê±µ÷ÓÃCButton: : SetIcon¡£×¢Ò⣺ÏÂÀýÓÃͼ±ê´úÌæλͼ£¬Ê¹ÓÃλͼʱҪСÐÄ£¬ÒòΪ²»ÖªµÀ±³¾°ËùÓеÄÑÕÉ«——²¢·Çÿ¸öÈ˶¼Ê¹ÓÃdz»ÒÉ«¡£
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£© ÈçºÎÒ»¸ö´´½¨Èý̬ÏÂѹ°´Å¥
¿ÉÒÔʹÓÃеÄBS_PUSHBUTTON ·ç¸ñλºÍ¼ì²â¿òÒÔ¼°°´Å¥À´´´½¨Ò»¸öÈý̬ÏÂѹ°´Å¥¡£ÕâºÜÈÝÒ×£¬Ö»Ð轫¼ì²â¿òºÍ°´Å¥ÍÏÀµ½¶Ô»°Öв¢Ö¸¶¨ÊôÐÔPush—like¼´¿É¡£²»ÓÃÈκθ½¼Ó³ÌÐò¾Í¿ÉÒÔ³ÉΪÈý̬ÏÂѹ°´Å¥¡£
£¨46£© ÈçºÎ¶¯Ì¬´´½¨¿Ø¼þ
·ÖÅäÒ»¸ö¿Ø¼þ¶ÔÏóµÄʵÀý²¢µ÷ÓÃÆäCreate³ÉÔ±º¯Êý¡£¿ª·¢Õß×îÈÝÒ׺öÂÔÁ½¼þÊ£ºÍü¼ÇÖ¸¶¨WS_VISBLE±êÇ©ºÍÔÚÕ»ÖзÖÅä¿Ø¼þ¶ÔÏó¡£ÏÂÀý¶¯Ì¬µØ´´½¨Ò»¸öÏÂѹ°´Å¥¿Ø¼þ£º
//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£© ÈçºÎÏÞÖƱ༿òÖеÄ×¼Ðí×Ö·û
Èç¹ûÓû§Ôڱ༿ؼþÖÐÖ»ÔÊÐí½ÓÊÕÊý×Ö£¬¿ÉÒÔʹÓÃÒ»¸ö±ê×¼µÄ±à¼¿Ø¼þ²¢Ö¸¶¨ÐµĴ´½¨±êÖ¾ES_NUMBERS,ËüÊÇWindows 95ÐÂÔö¼ÓµÄ±êÖ¾£¬¸Ã±êÖ¾ÏÞÖÆ ±à¼¿Ø¼þÖ»°´ÊÕÊý×Ö×Ö·û¡£Èç¹ûÓû§ÐèÒª¸´Ôӵı༿ؼþ£¬¿ÉÒÔʹÓÃMicrosoft µÄÆÁ±Î±à¼¿Ø¼þ£¬ËüÊÇÒ»¸öºÜÓÐÓõÄOLE¶¨Öƿؼþ¡£
Èç¹ûÏ£Íû²»Ê¹ÓÃOLE ¶¨Öƿؼþ×Ô¼º´¦Àí×Ö·û£¬¿ÉÒÔÅÉÉúÒ»¸öCEditÀಢ´¦ÀíWM_CHARÏûÏ¢£¬È»ºó´Ó±à¼¿Ø¼þÖйýÂ˳öÌض¨µÄ×Ö·û¡£Ê×ÏÈ£¬Ê¹ÓÃClassWizard½¨Á¢Ò»¸ö CEditµÄÅÉÉúÀ࣬Æä´Î£¬ÔÚ¶Ô»°ÀàÖÐÖ¸¶¨Ò»¸ö³ÉÔ±±äÁ¿½«±à¼¿Ø¼þ·ÖÀàÔÚOnInitdialog Öе÷ÓÃ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²ÎÁ¿²¢¾ö¶¨ËùÖ´ÐеIJÙ×÷£¬Óû§¿ÉÒÔÈ·¶¨ÊÇ·ñÐ޸ġ¢´«ËÍ×Ö·û¡£ÏÂÀý˵Ã÷ÁËÈçºÎÏÔʾ×Öĸ×Ö·û£¬Èç¹û×Ö·ûÊÇ×Öĸ×Ö·û£¬Ôòµ÷ÓÃCWnd OnChar£¬·ñÔò²»µ÷ÓÃ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µ÷ÓÃCEdit: : OnChar£¬È»ºóCEdit: : OnCharµ÷ÓÃCWnd: : Default»ñÈ¡ÔÀ´µÄwParam ºÍlParam µÄÖµ£¬ÕâÑùÊDz»Ðеġ£ÒªÐÞ¸ÄÒ»¸ö×Ö·û£¬ÐèÒªÊ×ÏÈÐÞ¸ÄnChar£¬È»ºóÓÃÐ޸ĹýµÄnCharµ÷ÓÃCWnd: : DefWindowProc¡£ÏÂÀý˵Ã÷ÁËÈçºÎ½«×Ö·ûת±äΪ´óд£º
//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еÄÏûÏ¢·´ÉäÔڿؼþÀàÖÐÖ¸¶¨ÑÕÉ«¡£ µ±¿Ø¼þÐèÒªÖØÐÂ×Åɫʱ£¬¹¤×÷¿òµ÷Óø¸´°¿Ú£¨Í¨³£ÊǶԻ°¿ò£©µÄCWnd: : OnCrtlColor,¿ÉÒÔÔÚ¸¸´°¿ÚÀàÖÐÖØÖøú¯Êý²¢Ö¸¶¨¿Ø¼þµÄеĻæ»ÊôÐÔ¡£ÀýÈ磬ÏÂÊö´úÂ뽫¶Ô»°ÖеÄËùÓб༿ؼþÎı¾ÑÕÉ«¸ÄΪºìÉ«£º
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
}
È»¶ø£¬ÓÉÓÚÿ¸ö¸¸´°¿Ú±ØÐë´¦Àí֪ͨÏûÏ¢²¢Ö¸¶¨Ã¿¸ö¿Ø¼þµÄ»æ»ÊôÐÔ£¬ËùÒÔ£¬ÕâÖÖ·½·¨²»ÊÇÍêÈ«µÄÃæÏò¶ÔÏóµÄ·½·¨¡£¿Ø¼þ´¦Àí¸ÃÏûÏ¢²¢Ö¸¶¨»æ»ÊôÐÔ¸üºÏÇéºÏÀí¡£ÏûÏ¢·´ÉäÔÊÐíÓû§ÕâÑù×ö¡£Í¨ÖªÏûÏ¢Ê×ÏÈ·¢Ë͸ø¸¸´°¿Ú£¬Èç¹û¸¸´°¿ÚûÓд¦ÀíÔò·¢Ë͸ø¿Ø¼þ¡£´´½¨Ò»¸ö¶¨ÖƲÊÉ«Áбí¿ò¿Ø¼þ±ØÐë×ñÑÏÂÊö²½Öè¡£
Ê×ÏÈ£¬Ê¹ÓÃClassWizard ´´½¨Ò»¸öCListBox µÄÅÉÉúÀಢΪ¸ÃÀàÌí¼ÓÏÂÊöÊý¾Ý³ÉÔ±¡£
class CMyListBox publilc CListBox
{
…
private
COLORREF m_clrFor // foreground color
COLORREF m_clrBack //background color
Cbrush m_brush //background brush
…
}
Æä´Î£¬ÔÚÀàµÄ¹¹Ô캯ÊýÖУ¬³õʼ»¯Êý¾ÝÖС£
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 ()
}
ÏÖÔÚ£¬¿Ø¼þ¿ÉÒÔ×Ô¼º¾ö¶¨ÈçºÎ»æ»£¬Ó븸´°¿ÚÎ޹ء£
£¨49£© µ±ÏòÁбí¿òÖÐÌí¼Ó¶à¸öÏîʱÈçºÎ·ÀÖ¹ÉÁ˸
µ÷ÓÃCWnd::SetRedraw Çå³ýÖØ»±êÖ¾¿ÉÒÔ½ûÖ¹CListBox£¨»òÕß´°¿Ú£©ÖØ»¡£µ±ÏòÁбí¿òÌí¼Ó¼¸¸öÏîʱ£¬Óû§¿ÉÒÔÇå³ýÖØ»±êÖ¾£¬È»ºóÌí¼ÓÏ×îºó»Ö¸´ÖØ»±êÖ¾¡£ÎªÈ·±£ÖØ»Áбí¿òµÄÐÂÏµ÷ÓÃSetRedraw (TRUE) Ö®ºóµ÷ÓÃ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º¯Êý£¬Óû§Ö»ºÃ×Ô¼º×ö´ËÏ×÷¡£µ÷ÓÃCEdit:: SetSelÒƶ¯µ½±à¼¿Ø¼þĩβ£¬È»ºóµ÷ÓÃCEdit:: ReplaceSelÌí¼ÓÎı¾¡£ÏÂÀýÊÇAppendText µÄÒ»ÖÖʵÏÖ·½·¨£º
void CMyEdit:: AppendText (LPCSTR pText)
{
int nLen=GetWindowTextLength ()
SetFocus ()
SetSel (nLen, nLen)
ReplaceSel (pText)
}
£¨51£© ÈçºÎ·ÃÎÊÔ¤¶¨ÒåµÄGDI¶ÔÏó
¿ÉÒÔͨ¹ýµ÷ÓÃCDC:: SlectStockObjectʹÓÃWindowsµÄ¼¸¸öÔ¤¶¨ÒåµÄ¶ÔÏó£¬ÖîÈçË¢×Ó¡¢±ÊÒÔ¼°×ÖÌå¡£ÏÂÀýʹÓÃÁËWindowsÔ¤¶¨ÒåµÄ±ÊºÍË¢×Ó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)
}
Ò²¿ÉÒÔµ÷ÓÃеÄSDKº¯ÊýGetSysColorBrush»ñÈ¡Ò»¸öϵͳÑÕÉ«Ë¢×Ó£¬ÏÂÀýÓñ³¾°É«ÔÚÊÓ´°Öлһ¸öÍÖÔ²£º
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¶ÔÏóµÄÊôÐÔÐÅÏ¢
¿ÉÒÔµ÷ÓÃGDIObject:: GetObject¡£Õâ¸öº¯Êý½«Ö¸¶¨Í¼±íÉ豸µÄÏûϢдÈëµ½»º³åÇø¡£ÏÂÀý´´½¨Á˼¸¸öÓÐÓõĸ¨Öúº¯Êý¡£
//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£© ÈçºÎʵÏÖÒ»¸öÏðƤÇø¾ØÐÎ
CRectTrackerÊÇÒ»¸öºÜÓÐÓõÄÀ࣬¿ÉÒÔͨ¹ýµ÷ÓÃCRectTracker::TrackRubberBand ÏìÓ¦WM_LBUTTONDOWNÏûÏ¢À´´´½¨Ò»¸öÏðƤÇø¾ØÐΡ£
ÏÂÀý±íÃ÷ʹÓÃCRectTrackerÒƶ¯ºÍÖØÖÃÊÓ´°ÖеÄÀ¶É«ÍÖÔ²µÄ´óСÊǺÜÈÝÒ×µÄÊÂÇé¡£
Ê×ÏÈ£¬ÔÚÎļþµµÖÐÉùÃ÷Ò»¸öCRectTrackerÊý¾Ý³ÉÔ±£º
class CSampleView : Public CView
{
…
public :
CrectTracker m_tracker
…
}
Æä´Î£¬ÔÚÎĵµÀàµÄ¹¹Ô캯ÊýÖгõʼ»¯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º¯ÊýÖлÍÖÔ²ºÍ×Ù¼£¾ØÐΣº
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ÏûÏ¢£¬²¢Ôö¼ÓÏÂÊö´úÂë¡£¸Ã¶Î´úÂë¸ù¾ÝÊó±ê»÷¼üÇé¿ö¿ÉÒÔÍÏ·Å¡¢Òƶ¯»òÕßÖØÖÃÍÖÔ²µÄ´óУ
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£© ÈçºÎ¸üзת±³¾°ÑÕÉ«µÄÎı¾
µ÷ÓÃCDC:: SetBkmode²¢´«ËÍOPAQUEÓõ±Ç°µÄ±³¾°ÑÕÉ«Ìî³ä±³¾°£¬»òÕßµ÷ÓÃCDC::SetBkMode²¢´«ËÍTRANSPAARENTʹ±³¾°±£³Ö²»±ä£¬ÕâÁ½ÖÖ·½·¨¶¼¿ÉÒÔÉèÖñ³¾°Ä£Ê½¡£ÏÂÀýÉèÖñ³¾°Ä£Ê½ÎªTRANSPARENT£¬¿ÉÒÔÁ½´Î¸üд®£¬Óû¨É«´øºÚÒõÓ°¸üÐÂÎı¾¡£ºÚÉ«´®ÔÚºìÉ«´®Ö®ºó£¬µ«ÓÉÓÚÉèÖÃÁ˱³¾°Ä£Ê½ÈÔÈ»¿É¼û¡£
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£© ÈçºÎ´´½¨Ò»¸ö¾ßÓÐÌض¨µã´óСµÄ×ÖÌå
¿ÉÒÔÖ¸¶¨×ÖÌåÂß¼µ¥Î»µÄ´óС£¬µ«ÓÐʱָ¶¨×ÖÌåµÄµãµÄ´óС¿ÉÄÜ»á¸ü·½±ãһЩ¡£¿ÉÒÔÈçϽ«×ÖÌåµÄµãת»»Îª×ÖÌåµÄ¸ß¶È£º
int nHeigth=mulDiv (nPointSize, -dc.GetDeviceCaps (LOGPIXELSY), 72)
ÏÂÀý´´½¨ÁËÒ»¸ö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£© ÈçºÎ¼ÆËãÒ»¸ö´®µÄ´óС
º¯ÊýCDC:: Det text Extent ¸ù¾Ýµ±Ç°Ñ¡ÔñµÄ×ÖÌå¼ÆËãÒ»¸ö´®µÄ¸ß¶ÈºÍ¿í¶È¡£Èç¹ûʹÓõIJ»ÊÇϵͳ×ÖÌå¶øÊÇÆäËû×ÖÌ壬ÔòÔÚµ÷ÓÃGetTextExtent֮ǰ½«×ÖÌåÑ¡½øÉ豸ÉÏÏÂÎÄÖÐÊǺÜÖØÒªµÄ£¬·ñÔò¼ÆËã¸ß¶ÈºÍ¿í¶Èʱ½«ÒÀ¾Ýϵͳ×ÖÌ壬Óɴ˵óöµÄ½á¹ûµ±È»ÊDz»ÕýÈ·µÄ¡£ÏÂÊöÑù°å³ÌÐòµ±¸Ä±äÏÂѹ°´Å¥µÄ±êÌâʱ¶¯Ì¬µ÷Õû°´Å¥µÄ´óС£¬°´Å¥µÄ´óСÓÉ°´Å¥µÄ×ÖÌåºÍ±êÌâµÄ´óС¶ø¶¨¡£ÏìÓ¦ÏûÏ¢WM_SETTEXTʱµ÷ÓÃ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£© ÈçºÎÏÔʾÐýתÎı¾
Ö»ÒªÓû§Ê¹ÓÃTrueType»òÕßGDI±Ê»ò×ÖÌå¾Í¿ÉÒÔÏÔʾÐýתÎı¾(ÓÐЩӲ¼þÉ豸Ҳ֧³ÖÐýת¹âÕ¤×ÖÌ壩¡£LOGFONT½á¹¹ÖеÄifEscapement³ÉÔ±Ö¸¶¨ÁËÎı¾ÐкÍxÖáµÄ½Ç¶È£¬½Ç¶ÈµÄµ¥Î»ÊÇÊ®·ÖÖ®Ò»¶È¶ø²»ÊǶȣ¬ÀýÈ磬ifEscapementΪ450±íʾ×ÖÌåÐýת45¶È¡£ÎªÈ·±£ËùÓеÄ×ÖÌåÑØ×ø±êϵͳµÄͬһ·½ÏòÐýת£¬Ò»¶¨ÒªÉèÖÃifEscapement³ÉÔ±µÄCLIP_LH_ANGLES룬·ñÔò£¬ÓÐЩ×ÖÌå¿ÉÄÜ·´ÏòÐýת¡£ÏÂÀýʹÓÃÁË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£© ÈçºÎÕýÈ·ÏÔʾ°üº¬±êÇ©×Ö·ûµÄ´®
µ÷ÓÃGDIÎı¾»æ»º¯ÊýʱÐèÒªÕ¹¿ª±êÇ©×Ö·û£¬Õâ¿ÉÒÔͨ¹ýµ÷ÓÃCDC:: TabbedTextOut»òÕßCDC:: DrawText²¢Ö¸¶¨DT_EXPANDTABS±êÖ¾À´Íê³É¡£TabbedTextOutº¯ÊýÔÊÐíÖ¸¶¨±êǩλµÄÊý×飬ÏÂÀýÖ¸¶¨Ã¿20É豸µ¥Î»Õ¹¿ªÒ»¸ö±êÇ©£º
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¶ÔÏó
µ÷ÓÃCString:: Format£¬¸Ãº¯ÊýºÍprintfº¯Êý¾ßÓÐÏàͬµÄ²ÎÊý£¬ÏÂÀý˵Ã÷ÁËÈçºÎʹÓÃFormatº¯Êý£º
//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£© ´®Ì«³¤Ê±ÈçºÎÔÚÆäĩβÏÔʾһ¸öÊ¡ÂÔºÅ
µ÷ÓÃCDC:: DrawText²¢Ö¸¶¨DT_END_ELLIPSIS±êÖ¾£¬ÕâÑù¾Í¿ÉÒÔÓÃСÂÔºÅÈ¡´ú´®Ä©Î²µÄ×Ö·ûʹÆäÊʺÏÓÚÖ¸¶¨µÄ±ß½ç¾ØÐΡ£Èç¹ûÒªÏÔʾ·¾¶ÐÅÏ¢£¬Ö¸¶¨DT_END_ELLIPSIS±êÖ¾²¢Ê¡ÂÔºÅÈ¡´ú´®ÖмäµÄ×Ö·û¡£
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£© Ϊʲô¼´Ê¹µ÷ÓÃEnableMenuItem²Ëµ¥Ïîºó£¬²Ëµ¥Ï´¦ÓÚ½ûֹ״̬
ÐèÒª½«CFrameWnd:: m_bAutomenuEnableÉèÖÃΪFALSE£¬Èç¹û¸ÃÊý¾Ý³ÉԱΪTRUE£¨È±Ê¡Öµ£©£¬¹¤×÷¿ò½«×Ô¶¯µØ½ûֹûÓÐ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£© ÈçºÎ¸øϵͳ²Ëµ¥Ìí¼ÓÒ»¸ö²Ëµ¥Ïî
¸øϵͳ²Ëµ¥Ìí¼ÓÒ»¸ö²Ëµ¥ÏîÐèÒª½øÐÐÏÂÊöÈý¸ö²½Ö裺
Ê×ÏÈ£¬Ê¹ÓÃResource Symbols¶Ô»°£¨ÔÚView²Ëµ¥ÖÐÑ¡ÔñResource Symbols£®£®£®¿ÉÒÔÏÔʾ¸Ã¶Ô»°£©¶¨Òå²Ëµ¥ÏîID£¬¸ÃIDÓ¦´óÓÚ0x0F¶øСÓÚ0xF000£»
Æä´Î£¬µ÷ÓÃCWnd::GetSystemMenu»ñȡϵͳ²Ëµ¥µÄÖ¸Õë²¢µ÷ÓÃCWnd:: Appendmenu½«²Ëµ¥ÏîÌí¼Óµ½²Ëµ¥ÖС£ÏÂÀý¸øϵͳ²Ëµ¥Ìí¼ÓÁ½¸öеÄ
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)
…
}
ÏÖÔÚ£¬Ñ¡Ôñϵͳ²Ëµ¥ÏîʱÓû§Ó¦½øÐмì²â¡£Ê¹ÓÃClassWizard´¦ÀíWM_SYSCOMMANDÏûÏ¢²¢¼ì²âÓû§²Ëµ¥µÄnID²ÎÊý£º
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)
}
×îºó£¬Ò»¸öÉè¼ÆÁ¼ºÃµÄUIÓ¦ÓóÌÐòÓ¦µ±ÔÚϵͳ²Ëµ¥Ïî¼ÓÁÁʱÔÚ״̬ÌõÏÔʾһ¸ö°ïÖúÐÅÏ¢£¬Õâ¿ÉÒÔͨ¹ýÔö¼ÓÒ»¸ö°üº¬ÏµÍ³²Ëµ¥»ùIDµÄ´®±íµÄÈë¿ÚÀ´ÊµÏÖ¡£
£¨63£© ÈçºÎÈ·¶¨¶¥²ã²Ëµ¥ËùÕ¼¾ÝµÄ²Ëµ¥ÐÐÊý
Õâ¿ÉÒÔͨ¹ý¼òµ¥µÄ¼õ·¨ºÍ³ý·¨À´ÊµÏÖ¡£Ê×ÏÈ£¬Óû§ÐèÒª¼ÆËãÖ÷¿ò´°¿ÚµÄ¸ß¶ÈºÍ¿Í»§Çø£»Æä´Î£¬´ÓÖ÷¿ò´°¿ÚµÄ¸ß¶ÈÖмõÈ¥¿Í»§Çø¡¢¿ò±ß½çÒÔ¼°±êÌâµÄ¸ß¶È£»×îºó£¬³ýÒԲ˵¥À¸µÄ¸ß¶È¡£ÏÂÀý³ÉÔ±º¯ÊýÊÇÒ»¸ö¼ÆËãÖ÷¿ò²Ëµ¥ËùÕ¼¾ÝµÄÐÐÊýµÄ´úÂëʵÏÖ¡£
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£© ÔÚÓû§»·¾³ÖÐÈçºÎÈ·¶¨ÏµÍ³ÏÔʾԪËصÄÑÕÉ«
µ÷ÓÃSDKº¯ÊýGetSysColor¿ÉÒÔ»ñÈ¡Ò»¸öÌض¨ÏÔʾԪËصÄÑÕÉ«¡£ÏÂÀý˵Ã÷ÁËÈçºÎÔÚMFCº¯ÊýCMainFrameWnd:: OnNcPaintÖе÷Óøú¯ÊýÉèÖô°¿Ú±êÌâÑÕÉ«¡£
void CMiniFrameWnd:: OnNcPaint ()
{
…
dc.SetTextColor (:: GetSysColor (m_bActive ? COLOR_CAPTIONTEXT : COLOR_INACTIVECAPTIONTEXT))
…
£¨65£© ÈçºÎ²éѯºÍÉèÖÃϵͳ²ÎÊý
ÔÚWindows 3.1 SDKÖнéÉܹýSDKº¯ÊýSystemParametersInfo£¬µ÷Óøú¯Êý¿ÉÒÔ²éѯºÍÉèÖÃϵͳ²ÎÊý£¬ÖîÈç°´¼üµÄÖظ´ËÙÂÊÉèÖá¢Êó±êË«»÷ÑÓ³Ùʱ¼ä¡¢Í¼±ê×ÖÌåÒÔ¼°×ÀÃ渲¸ÇλͼµÈµÈ¡£
//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£© ÈçºÎÈ·¶¨µ±Ç°ÆÁÄ»·Ö±æÂÊ
µ÷ÓÃSDKº¯ÊýGetSystemMetrics£¬¸Ãº¯Êý¿ÉÒÔ¼ìË÷ÓйØwindowsÏÔʾÐÅÏ¢£¬ÖîÈç±êÌâ´óТ±ß½ç´óСÒÔ¼°¹ö¶¯Ìõ´óСµÈµÈ¡£
//Initialize CSize object with screen size.
CSize sizeScreen (GetSystemMetrics (SM_CXSCREEN),
GetSystemMetrics (SM_CYSCREEN))
£¨67£© ÈçºÎʹÓÃÒ»¸öÔ¤¶¨ÒåµÄWindows¹â±ê
µ÷ÓÃCWinApp:: LoadStandardCursor²¢´«Ë͹â±ê±êʶ·û¡£
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Ó¦ÓóÌÐòʹÓõÄÈÎÎñÁбí
ÔÏȵÄTask ManagerÓ¦ÓóÌÐòÏÔʾ¶¥²ã´°¿ÚµÄÁÐ±í¡£ÎªÁËÏÔʾ¸ÃÁÐ±í£¬´°¿Ú±ØÐë¿É¼û¡¢°üº¬Ò»¸ö±êÌâÒÔ¼°²»Äܱ»ÆäËû´°¿ÚÓµÓС£µ÷ÓÃCWnd:: GetWindow¿ÉÒÔ¼ìË÷¶¥²ã´°¿ÚµÄÁÐ±í£¬µ÷ÓÃIsWindowVisible¡¢GetWindowTextLengthÒÔ¼°GetOwner¿ÉÒÔÈ·¶¨´°¿ÚÊÇ·ñÓ¦¸ÃÔÚÁбíÖС£ÏÂÀý½«°ÑTaskManager´°¿ÚµÄ±êÌâÌî³äµ½ÁбíÖС£
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ϵͳĿ¼
ÓÐÁ½¸öSDKº¯Êý¿ÉÒÔÍê³É¸Ã¹¦ÄÜ¡£GetWindowsDirectoryºÍGetSystemDirectory£¬ÏÂÀý˵Ã÷ÁËÈçºÎʹÓÃÕâÁ½¸öº¯Êý£º
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£© ÔÚÄĶù´´½¨ÁÙÎļþ
µ÷ÓÃSDKº¯ÊýGetTemPath¿ÉÒÔÈ·¶¨ÁÙʱÎļþµÄĿ¼£¬¸Ãº¯ÊýÊ×ÏÈΪÁÙʱ·¾¶¼ì²âTMP»·¾³±äÁ¿£ºÈç¹ûûÓÐÖ¸¶¨TMP£¬¼ì²âTMP»·¾³±äÁ¿£¬È»ºó·µ»Øµ½µ±Ç°Ä¿Â¼¡£ÏÂÀý˵Ã÷ÁËÈçºÎ´´½¨Ò»¸öÁÙʱÎļþ¡£
…
//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£© ÎÒÔõÑù²ÅÄܽ¨Á¢Ò»¸öµÈ´ý¹â±ê?
µ÷ Óà BeginWaitCursor º¯ Êý À´ Æô ¶¯ µÈ ´ý ¹â ±ê£¬µ÷ Óà EndWaitCursor º¯ Êý À´ ½á Êø µÈ ´ý ¹â ±ê¡£Òª ×¢ Ò⣬¶þ Õß ¶¼ Òª µ÷ Óà app µÄ ³É Ô± º¯ Êý£¬Èç Ï Ëù ʾ:
AfxGetApp()->BeginWaitCursor();
// Òª×öµÄÊÂ
AfxGetApp()->EndWaitCursor();
£¨72£© ÎÒÔÚMDI¿ò¼ÜÖÐÓиö form ÊÓ´°¡£ËüÓиöÈ¡Ïû°´Å¥£¬ÎÒÐèÒªµ±Óû§°´È¡Ïû°´Å¥Ê±¿É¹Ø±ÕformÊÓ´°¡£ÎÒÓ¦¸ÃÈçºÎ¹Ø±Õ¸ÃÎĵµ?
µ÷ Óà OnCloseDocument º¯ Êý¡£
£¨73£© ÈçºÎ·ÃÎÊ×ÀÃæ´°¿Ú
¾²Ì¬º¯ÊýCWnd:: GetDesktopWindow ·µ»Ø×ÀÃæ´°¿ÚµÄÖ¸Õë¡£ÏÂÀý˵Ã÷ÁËMFCº¯ÊýCFrameWnd::BeginModalStaeÊÇÈçºÎʹÓøú¯Êý½øÈëÄÚ²¿´°¿ÚÁбíµÄ¡£
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 Õû ÐÍ Êý Öµ£¬Ëü ´ú ±í ÁË Ò» ÖÖ ÑÕ É«¡£Äã ¿É ÒÔ Ê¹ Óà RGB º¯ Êý À´ ³õ ʼ »¯ COLORREF¡£Àý È磺
COLORREF color = RGB(0, 255, 0);
RGB º¯ Êý ½Ó ÊÕ Èý ¸ö 0-255 Êý Öµ£¬Ò» ¸ö ´ú ±í ºì É«£¬ Ò» ¸ö ´ú ±í ÂÌ É«£¬ Ò» ¸ö ´ú ±í À¶ É«¡£ÔÚ ÉÏ ÃæµÄ Àý ×Ó ÖУ¬ ºì É« ºÍ À¶ É« Öµ ¶¼ Ϊ 0£¬Ëù ÒÔ ÔÚ ¸Ã ÑÕ É« ÖРû ÓÐ ºì É« ºÍ À¶ É«¡£ÂÌ É« Ϊ ×î ´ó Öµ 255¡£Ëù ÒÔ ¸Ã ÑÕ É« Ϊ ÂÌ É«¡£0,0,0 Ϊ ºÚ É«£¬255,255,255 Ϊ °× É«¡£
Áí Ò» ÖÖ ³õ ʼ »¯ COLORREF µÄ ·½ ·¨ Èç Ï Ëù ʾ£º
CColorDialog colorDialog;
COLORREF color;
if( colorDialog.DoModal() == IDOK )
{
color = colorDialog.GetColor();
}
Õ⠶Π´ú Âë ʹ Óà ÁË MFC ÖÐ µÄ ÑÕ É« ¶Ô »° ¿ò£¬Ëü Ðè Òª ÎÄ ¼þ¡£
£¨75£© AppWizardËù²úÉúµÄSTDAFXÎļþÊǸÉʲôÓõÄ?
Ëü Ö÷ Òª ÊÇ Ð Öú ²ú Éú Ô¤ ±à Òë Í· ÎÄ ¼þ µÄ¡£Í¨ ³£ Äã ÊÇ ²» Ðè Òª ÐÞ ¸Ä Ëü µÄ¡£
£¨76£© ÎÒÔÚÎҵijÌÐòÖÐÊÇÁËCDWordArray¡£ÎÒÏòËüÌí¼ÓÁËÔ¼10,000¸öÕûÊý£¬ÕâʹµÃËü±äµÃ·Ç³£·Ç³£Âý¡£ÎªÊ²Ã´»áÕâôÔã?
CDWordArray ÊÇ ºÜ ºÃ Óà µÄ£¬Ö» ÊÇ Òò Ϊ Äã û ÓÐ Ö¸ ¶¨ Êý ×é µÄ×î´ó³ß´ç¡£Òò ´Ë£¬µ± Äã Ìí ¼Ó Ð Ԫ ËØ Ê±£¬¸Ã Àà »á ´Ó ¶Ñ ÖÐ ÖØ Ð ·Ö Åä ¿Õ ¼ä¡£²» ÐÒ µÄ ÊÇ£¬¸Ã Àà »á ÔÚ Ã¿ ´Î ²å Èë РԪ ËØ Ê± ¶¼ Ϊ Êý ×é ÖØ Ð ·Ö Åä ¿Õ ¼ä¡£Èç ¹û Äã Ïò Ëü Ìí ¼Ó ÁË ºÜ ¶à РԪ ËØ£¬Ëù ÓÐ Õâ Щ ·Ö Åä ºÍ ¸´ ÖÆ Êý ×é µÄ ²Ù ×÷ »á ¾Í »á ʹ Ëü ±ä Âý¡£½â ¾ö ¸Ã ÎÊ Ìâ µÄ ·½ ·¨ ÊÇ£¬Äã ¿É ÒÔ Ê¹ Óà SetSize º¯ Êý µÄ µÚ ¶þ ¸ö ²Î Êý À´ ¸Ä ±ä Õâ ÖÖ ÖØ Ð ·Ö Åä µÄ Æµ ÂÊ¡£Àý È磬Èç ¹û Äã °Ñ ¸Ã ²Î Êý Éè Öà Ϊ 500£¬Ôò ÿ ´Î Êý ×é ¿Õ ¼ä ³¬ ³ö ʱ Ëü ²Å ÖØ Ð ·Ö Åä ²¢ Ìí ¼Ó 500 ¸ö Р¿Õ ¼ä£¬¶ø ²» ÊÇ 1 ¸ö¡£Õâ Ñù Ò» À´£¬Äã ¾Í ¿É ÒÔ ²» Óà ÖØ Ð ·Ö Åä ¶ø Ìí ¼Ó ÁË Áí Íâ 499 ¸ö Ôª ËØ ¿Õ ¼ä£¬Õâ Ò² »á ´ó ´ó Ìá ¸ß ³Ì Ðò µÄ ÔË ÐÐ ËÙ ¶È¡£
£¨77£© ÎÒ¸ÃÈçºÎ¸Ä±äMDI¿ò¼Ü´°¿ÚµÄ×Ó´°¿ÚµÄ´óСÒÔʹÔÚ´°¿ÚÒÔÒ»¶¨µÄ´óС´ò¿ª?
ÔÚ ÊÓ ÖÐ µÄ OnInitialUpdate º¯ Êý ÖÐ µ÷ Óà GetParentFrame º¯ Êý¡£GetParentFrame »á ·µ »Ø Ò» Ö¸ Ïò Ò» ±£ ´æ ÓÐ ¸Ã ÊÓ µÄ ¿ò ¼Ü ´° ¿Ú µÄ Ö¸ Õ롣Ȼ ºó µ÷ Óà ÔÚ ¿ò ¼Ü ´° ¿Ú ÉÏ µ÷ Óà MoveWindow¡£
£¨78£© ÔÚÎҵijÌÐòµÄijЩ²¿·Ö£¬ÎÒ¿ÉÒÔµ÷Óà MessageBox º¯ÊýÀ´½¨Á¢Ò»¸öÐÅÏ¢¶Ô»°¿ò£¬ÀýÈçÔÚÊÓÀàÖС£µ«ÊÇ£¬ÔÚÆäËü²¿·ÖÎÒÈ´²»ÄÜ£¬ÈçÎĵµÀàÖС£ÎªÊ²Ã´£¿ÎÒÔõÑù²ÅÄÜÔÚÎÒµÄÓ¦ÓóÌÐòÀàÖн¨Á¢Ò»¸öÐÅÏ¢¶Ô»°¿ò£¿
MessageBox º¯ Êý À´ ×Ô CWnd À࣬Ëù ÒÔ Äã Ö» ÄÜ ÔÚ ´Ó CWnd ¼Ì ³Ð µÄ Àà ( Èç CView ) ÖÐ µ÷ Óà Ëü¡£µ« ÊÇ£¬MFC Ò² Ìá ¹© ÁË AfxMessageBox º¯ Êý£¬Äã ¿É ÒÔ ÔÚ ÈÎ ºÎ µØ ·½ µ÷ Óà Ëü¡£
£¨79£© ÎÒÐèÒªÔÚÎҵijÌÐòÖÐÉèÖÃÈ«¾Ö±äÁ¿£¬ÒÔʹÎĵµÖеÄËùÓÐÀ඼ÄÜ·ÃÎÊ¡£ÎÒÓ¦¸Ã°ÉËü·Åµ½ÄĶù?
°Ñ ¸Ã ±ä Á¿ ·Å µ½ ¸Ã Ó¦ Óà ³Ì Ðò Àà µÄ Í· ÎÄ ¼þ ÖÐ µÄ attribute ´¦¡£È» ºó£¬ÔÚ ³Ì Ðò µÄ ÈÎ ºÎ µØ ·½£¬Äã ¶¼ ¿É ÒÔ Óà Ï Ãæ µÄ ·½ ·¨ À´ ·Ã ÎÊ ¸Ã ±ä Á¿£º
CMyApp *app = (CMyApp *)AfxGetApp();
app->MyGlobalVariable = ...
£¨80£© ÎÒÌý˵MFC¿ÉÒÔ·¢ÏÖÄڴ橶´£¬ÎÒÔõÑùʹÓøÃÌØÐÔ?
Èç ¹û Äã ÔÚ Debug ²Ë µ¥ ÖÐ µÄ Go Ñ¡ Ïî ( ²» ÊÇ Project ²Ë µ¥ ÖÐ µÄ Execute Ñ¡ Ïî ) À´ ÔË ÐÐ Äã µÄ Ó¦ Óà ³Ì Ðò£¬MFC Ó¦ ¸Ã ÔÚ ³Ì Ðò ÖÕ Ö¹ ʱ ±¨ ¸æ ÄÚ ´æ © ¶´¡£Èç ¹û û ÓУ¬ÄÇ Ã´ ÊÔ ÊÔ ÔË ÐÐ MFC Tracer ¹¤ ¾ß ³Ì Ðò ( ÔÚ VC++ ³Ì Ðò ×é ÖÐ )£¬²¢ Æô ¶¯ ¸ú ×Ù¡£È» ºó ·µ »Ø Ó¦ Óà ³Ì Ðò¡£
£¨81£© ÎÒÔõÑù²ÅÄÜÔÚÎÒµÄÓ¦ÓóÌÐòÖÐÑ»·ä¯ÀÀÒѾ´ò¿ªµÄÎĵµ?
ʹÓÃCDocTemplateÖÐδ¹«¿ªµÄGetFirstDocPosition()ºÍGetNextDoc()º¯Êý¡£
£¨82£©²ÅÄÜÔÚÎÒµÄÓ¦ÓóÌÐòÖÐÑ»·ä¯ÀÀÒѾ´ò¿ªµÄÊÓ?
ʹ Óà CDocument ÖРδ ¹« ¿ª µÄ GetFirstViewPosition() ºÍ GetNextView() º¯ Êý¡£
£¨83£©ÊýPreCreateWindowÊǸÉʲôÓõÄ?
PreCreateWindow ÔÊ Ðí Äã ÔÚ µ÷ Óà CreateWindow Ö® Ç° À´ ¸Ä ±ä ´° ¿Ú Êô ÐÔ¡£
£¨84£©¸ÃÔõÑù·ÀÖ¹MFCÔÚ´°¿Ú±êÌâÀ¸ÉÏ°ÑÎĵµÃûÔ¤ÖóÉÓ¦ÓóÌÐòÃû?
ÔÚ PreCreateWindow º¯ Êý ÖРɾ ³ý FWS_PREFIXTITLE ±ê Ö¾ µÄ ´° ¿Ú Ñù ʽ£º
cs.style &= ~FWS_PREFIXTITLE;
£¨85£© ÎÒÓ¦¸ÃÔõÑù·ÀÖ¹MFCÔÚ´°¿Ú±êÌâÀ¸ÉÏÌí¼ÓÎĵµÃû?
ÔÚ PreCreateWindow º¯ Êý ÖРɾ ³ý FWS_ADDTOTITLE ±ê Ö¾ µÄ ´° ¿Ú Ñù ʽ£º
cs.style &= ~FWS_ADDTOTITLE ;
£¨86£© ÎÒÓ¦¸ÃÈçºÎ¸Ä±äÊÓ´°¿ÚµÄ´óС?
Òò Ϊ ÊÓ ´° ¿Ú ʵ ¼Ê ÉÏ ÊÇ ¿ò ¼Ü ´° ¿Ú µÄ ×Ó ´° ¿Ú£¬Ëù ÒÔ Äã ±Ø Ðë ¸Ä ±ä ¿ò ¼Ü ´° ¿Ú µÄ ´ó С£¬¶ø ²» ÊÇ ¸Ä ±ä ÊÓ ´° ¿Ú¡£Ê¹ Óà CView Àà ÖÐ µÄ GetParentFrame() º¯ Êý »ñ µÃ Ö¸ Ïò ¿ò ¼Ü ´° ¿Ú µÄ Ö¸ Õ룬Ȼ ºó µ÷ Óà MoveWindow() º¯ Êý À´ ¸Ä ±ä ¿ò ¼Ü µÄ ´ó УÕâ »á ʹ ±ä ³ß ´ç µÄ ÊÓ ³ä Âú ¿ò ¼Ü ´° ¿Ú¡£
£¨87£© ÎÒÓÐÒ»ÎÞģʽ¶Ô»°¿ò¡£ÎÒÔõÑù²ÅÄÜÔÚ´°¿ÚÍ˳öʱɾ³ýCDialog¶ÔÏó?
°Ñ“delete this”¼Ó µ½ PostNcDestroy ÖС£Õâ Ö÷ Òª Óà ÔÚ Ðè Òª ×Ô ¶¯ ɾ ³ý ¶Ô Ïó µÄ ³¡ ºÏ¡£
£¨88£© Ϊʲô°Ñ“delete this”·ÅÔÚPostNcDestroyÖжø²»ÊÇOnNcDestroy?
OnNcDestroy Ö» ±» ÒÑ ½¨ Á¢ µÄ ´° ¿Ú µ÷ Óá£Èç ¹û ½¨ Á¢ ´° ¿Ú ʧ °Ü ( Èç PreCreateWindow )£¬Ôò û ÓÐ ´° ¿Ú ´¦ À´ ·¢ ËÍ WM_NCDESTROY Ïû Ï¢¡£PostNcDestroy ÊÇ ÔÚ ¶Ô Ïó ´° ¿Ú ±» Íê È« ɾ ³ý£¬ÔÚ OnNcDestroy ºó£¬Éõ ÖÁ ÔÚ ´° ¿Ú ½¨ Á¢ ʧ °Ü Ö® ºó µ÷ Óà µÄ¡£
£¨89£© File²Ëµ¥ÖеÄMRUÁбíÊÇ´ÓÄĶùÀ´µÄ£¿ÁбíÖеÄÃû×Ö·ÅÔÚÄĶùÁË£¿ÎÒÔõÑù²ÅÄܸıäÁбíÖÐÏîÄ¿µÄ×î´óÖµ£¿
ÔÚ Ó¦ Óà ³Ì Ðò Àà µÄ InitInstance º¯ Êý ÖÐ ¶Ô LoadStdProfileSettings µÄ µ÷ Óà ÖС£¸Ã µ÷ Óà ½Ó ÊÜ Ò» ¸ö ²Î Êý ( ÔÚ È± Ê¡ Çé ¿ö Ï Èç ¹û û ÓÐ ´« µÝ Öµ Ôò Ϊ 4 )¡£MRU ÎÄ ¼þ Ãû ÊÇ ´Ó INI ÎÄ ¼þ ÖÐ µ÷ Óà µÄ¡£Èç ¹û Äã ÓÐ ´ø ÓÐ ID_FILE_MRU_FILE1 µÄ ID µÄ ²Ë µ¥ Ñ¡ ÏËü »á Ϊ µ÷ Èë µÄ MRU ÁÐ ±í Ëù Ìæ »»¡£Èç ¹û Äã ¸Ä ±ä ´« µÝ ¸ø LoadStdProfileSettings µÄ Êý Öµ ( ×î ´ó Ϊ 16 )£¬Ôò Äã ¾Í ¸Ä ±ä ÁË Ëù ×° Èç ÎÄ ¼þ Ãû µÄ ×î ´ó Öµ¡£
£¨90£© ÎÒÔڲ˵¥ÖÐÌí¼ÓÁËеÄÏî¡£µ«ÊÇ£¬µ±ÎÒÑ¡¸ÃÏîʱ£¬ÔÚ״̬À¸ÉÏûÓгöÏÖÈκÎÌáʾÐÅÏ¢¡£ÎªÊ²Ã´?
´ò ¿ª ×Ê Ô´ ÎÄ ¼þ ÖÐ µÄ ²Ë µ¥ Ä£ °å¡£´ò ¿ª Р²Ë µ¥ Ñ¡ Ïî µÄ Êô ÐÔ ¶Ô »° ¿ò¡£ÔÚ ¶Ô »° ¿ò µÄ µ× ²¿ µÄ Prompt ±à ¼ ¿ò ÖÐ £¬Äã ¿É ÒÔ Èç Ï ָ ¶¨ ×´ ̬ À¸ ÉÏ µÄ Ìá ʾ ЊϢ ºÍ ¹¤ ¾ß À¸ ÉÏ µÄ Ìá ʾ ЊϢ ( Èç ¹û Äã ÒÑ ¾ ½¨ Á¢ µÄ ¹¤ ¾ß À¸ °´ Å¥ ):
Status bar string/nFlying tag
£¨91£© ÎÒÔõÑù²ÅÄÜÔÚÓ¦ÓóÌÐòµÄȱʡϵͳ²Ëµ¥ÖмÓÉÏһЩ¶«Î÷?
ϵ ͳ ²Ë µ¥ Óë Æä Ëü ²Ë µ¥ Àà ËÆ£¬Äã ¿É ÒÔ