45fan.com - ·

: λҳ > Ƶ > Խ̳ > ĶѶVCѧϰ֪֮ʶȫ

VCѧϰ֪֮ʶȫ

2016-09-09 03:01:47 Դ:www.45fan.com С

VCѧϰ֪֮ʶȫ

VCС֪ʶܽᣡ

1 ͨӦóڵ ָ?
ڵ ָ뱣CWinThread::m_pMainWnd,AfxGetMainWndʵ֡
AfxGetMainWnd() ->ShowWindow(SW_SHOWMAXMIZED)
//ʹ.


2 ȷӦó·
Use GetModuleFileName Ӧó·Ȼȥִļ
Example:
TCHAR
exeFullPath[MAX_PATH] // MAX_PATHAPIж˰ɣ
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µĻʣӦ÷עķ·ҪһȽϿij򣬿Ӧȫ.


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,LPARAMLRESULT.
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 PreCreateWindowWS_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ṹرCEditViewES_AUTOHSCROLLWS_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 ڿͷ򣬷HTCAPTIONWindowsıС
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: : GetDescendantWindowAfxGetMainWndȡЩӴڵָ룺
//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 : : GetBarStyleCControlBar : : 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 δһ״Ĵ
ʹµSDKSetWindowRgnú滭Ϣ޶ڴڵһָʵʹڳΪָIJ״ ʹ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ᳫΪ޷Զı䣬Ҫ NotepadDzжȲ֪Notepadı,ʱFindWindowΪˣͨö windowsбİ취ʵ֡ڻе"Windows 95 APIԱָ"һбȽϸĽ,Ͳٶ˵֡

27 δʹģʽԻ
MFCģʽģʽԻװͬһУʹģʽԻҪԻҪIJ衣ȣʹԴ༭ԻԴʹClassWizardһCDialogࡣģʽģʽԻֹDzһģģʽԻͨCDialog : : EndDialog ֹģʽԻǵCWnd: : DestroyWindowֹģCDialog : : OnOKCDialog : : OnCancelEndDialog ,Ҫ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: : GetDescendantWindowAfxGetMainWndȡЩӴڵָ룺
//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ʽȥһ󻯰ťij򣬾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: : GetDescendantWindowAfxGetMainWndȡЩӴڵָ룺
//Get pointer to status bar .
CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ( )
—> GetDescendantWindow(AFX_IDW_STUTUS_BAR)

32 ʹܺͽֹĹʾ
CBRS_TOOLTIPSλʾʾҪʹֹܻ߽ʾҪû÷λͨCControlBar : : GetBarStyleCControlBar : : 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ѡ滭ˢӣҲøϢָˢӡ˵˴һɫԻIJ衣

ȣԻһ˳Ա
CBursh :class CMyFormView : public CFormView
{

private :
CBrush m_ brush // background brush


}

Σ Ĺ캯нˢӳʼΪҪıɫ
CMyFormView : : CMyFormView ( )
{
// Initialize background brush .
m_brush .CreateSolidBrush (RGB ( 0, 0, 255) )
}

ʹClassWizardWM_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ָ룬Ҫ֤ڿؼ֮ǰܳѹťΪ8Arial壺
//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 : : GetFortColorCOleControl : : 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: : DlgDirListCWnd: : DlgDirListComboBoxWindows ԶбϿõָĿ¼еļWindowsĿ¼еļϿУ
BOOL CSampleDig : : OnInitDialog ( )
{
CDialog : : OnInitDialog ( )
TCHAR szPath [MAX_PATH] = {"c://windows"}
int nReslt = DlgDirListComboBox (szPath, IDC_COMBO , IDC_CURIDIR, DDL_READWRITE DDL_READONLYDDL_HIDDEN DDL_SYSTEMDDL_ARCHIVE)
return TRUE
}

42 Ϊʲôתťؼת
ҪCSpinCtrl : : SetRange תťؼķΧתťؼȱʡΪ0ȱʡΪ100ζʱתؼֵ100Ϊ0תťؼķΧΪ0100
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_BITMAPBS_ICONҪλͼťť͵CButton : : SetBitmapCButton : : SetIconʱҪָBS_BITMAPBS_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)


}

ʹClassWizardWM_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 )
}

Ҫ޸ַܽ򵥵޸ĹnCharCEdit: : OnCharȻCEdit: : OnCharCWnd: : DefaultȡԭwParam lParam ֵDzеġҪ޸һַҪ޸nCharȻ޸ĹnCharCWnd: : 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 )
}

ʹClassWizardWM_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)
}

ҲԵµSDKGetSysColorBrushȡһϵͳɫˢӣñɫӴлһԲ
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)
}

ʹClassWizardWM_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:: SetBkmodeOPAQUEõǰıɫ䱳ߵCDC::SetBkModeTRANSPAARENTʹֲ䣬ַñģʽñģʽΪ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)
һ8Apial壺

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 ʾתı
ֻҪûʹTrueTypeGDIʻͿʾתı(ЩӲ豸Ҳ֧תդ壩LOGFONTṹеifEscapementԱָıкxĽǶȣǶȵĵλʮ֮һȶǶȣ磬ifEscapementΪ450ʾת45ȡΪȷеϵͳͬһתһҪifEscapementԱCLIP_LH_ANGLESλЩܷתʹ14Arialÿ15Ȼһ
void CSampleView:: OnDraw (CDC* pDC)
{
//Determine the size of the window.
CRect rcClient
GetClientRect (rcClient)

//Create sample string.
CString str (_T ("WheeeeI 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:: TabbedTextOutCDC:: 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ͬIJ˵ʹ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_UION_COMMANDIJ˵

//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ʾöԻ˵IDIDӦ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)


}

ڣѡϵͳ˵ʱûӦм⡣ʹClassWizardWM_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 ȷ˵ռݵIJ˵
ͨ򵥵ļͳʵ֡ȣûҪ򴰿ڵĸ߶ȺͿͻΣ򴰿ڵĸ߶мȥͻ߽Լĸ߶ȣ󣬳Բ˵ĸ߶ȡԱһ˵ռݵĴʵ֡

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 ûȷϵͳʾԪصɫ
SDKGetSysColorԻȡһضʾԪصɫ˵MFCCMainFrameWnd:: OnNcPaintеøúôڱɫ

void CMiniFrameWnd:: OnNcPaint ()
{

dc.SetTextColor (:: GetSysColor (m_bActive ? COLOR_CAPTIONTEXT : COLOR_INACTIVECAPTIONTEXT))


65 βѯϵͳ
Windows 3.1 SDKнܹSDKSystemParametersInfoøúԲѯϵͳ簴ظá˫ӳʱ䡢ͼԼ渲λͼȵȡ

//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 ȷǰĻֱ
SDKGetSystemMetricsúԼй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Լ㴰ڵбIsWindowVisibleGetWindowTextLengthԼ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 ȷWindowsWindowsϵͳĿ¼
SDKɸùܡGetWindowsDirectoryGetSystemDirectory˵ʹ

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 Ķļ
SDKGetTemPathȷʱļĿ¼úΪʱ·TMPûָTMPTMPȻ󷵻صǰĿ¼˵δһʱļ


//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 洰ڵָ롣˵MFCCFrameWnd::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 AppWizardSTDAFXļǸʲôõ?
Ҫ Э Ԥ ͷ ġͨ Ҫ ġ

76 ҵijCDWordArrayԼ10,000ʹ÷dzdzΪʲôô?
CDWordArray ģֻ Ϊ û ָ ߴ硣 ˣ Ԫ ʱ 䡣 ǣ ÿ Ԫ ʱ Ϊ 䡣 Ԫ أ Щ ʹ ǣ ʹ SetSize Ƶ ʡ 磬 Ϊ 500 ÿ ʱ 500 䣬 1 һ 499 Ԫ 䣬 Ҳ ȡ


77 ҸθıMDIܴڵӴڵĴСʹڴһĴС?
OnInitialUpdate GetParentFrame GetParentFrame һ ָ һ ָ 롣Ȼ MoveWindow

78 ҵijijЩ֣ҿԵ 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()

83PreCreateWindowǸʲôõ?
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 Ӧóȱʡϵͳ˵мһЩ?
ϵ ͳ ƣ


ĵַhttp://www.45fan.com/dnjc/73792.html
Tags ѧϰ ֪ʶ ռ
༭·
| ϵ | | վͼ | Sitemap | App | ض