+ k3 S) `# D; I7 ]( f voik CRoundDlg : : OnPaint ( ) 0 p* r5 |. W8 \# Y { ; R; w8 K; h! U# h! X$ U9 \$ X4 S CPaintDC de (this) // device context for painting9 p# J9 o% o' p0 \4 e" D$ N5 Z0 E
.) V: k: K0 Y8 Z/ w; c( F ^. g
//draw ellipse with out any border 4 E7 X/ z k1 X0 ? dc. SelecStockObject (NULL_PEN) 7 M8 G) s; V% J* K2 ?4 F. T //get the RGB colour components of the sphere color9 ^* k5 l* ~' {! K9 \4 M. ^' w
COLORREF color= RGB( 0 , 0 , 255) 9 E7 X3 y! _* n3 r) y/ ` BYTE byRed =GetRValue (color) & m! ^$ _4 I! a } BYTE byGreen = GetGValue (color)2 H0 U" {: V$ l0 r2 d4 N5 g1 n
BYTE byBlue = GetBValue (color) . I& n; m. d5 `4 S6 D9 ^. V1 H+ O . E) s5 ^8 D, q // get the size of the view window / S! p! p. h/ V/ s7 E) C Crect rect3 U/ e5 @' B1 ?7 x
GetClientRect (rect); ?, Y- C5 N/ t4 o$ z6 X
- {( E% \$ A; d0 i8 b5 C3 h // get minimun number of units 2 g8 v1 d8 m, F) k int nUnits =min (rect.right , rect.bottom ) : X7 t0 i4 [3 K! {0 E, x% y7 K2 J; z1 G" E* j D% G
//calculate he horiaontal and vertical step size # j; c/ G* E& a; _ float fltStepHorz = (float) rect.right /nUnits1 _3 r/ @# b Y9 k) _) @" K) L
float fltStepVert = (float) rect.bottom /nUnits, M+ u: ?7 J' M" x0 w* m: E
, A! a2 i4 F% `2 ]( j; V/ v0 ]6 a6 L; m/ A) B8 a' S
int nEllipse = nUnits/3 // calculate how many to9 ?+ M* d" I) M; R
draw, q, u& p; k( m% }2 L% ^: ]
int nIndex' K4 o( l; Q; D% G9 Q9 D" z5 L
// current ellipse that is being draw& Y( Q& R' x$ a8 b5 B/ x
2 z2 R: O1 ^: ~
CBrush brush * x& O. K4 q8 W' c) |7 P2 g // bursh used for ellipse fill color, P9 }& V/ J5 m$ a# T* _
CBrush *pBrushOld // previous- v3 m9 l( B7 {9 n( \6 D5 k" K
brush that was selected into dc / S3 Q- k5 J2 ~. ~2 L# y //draw ellipse , gradually moving towards upper-right % S! }9 C% U3 L, G0 f8 W: k1 G corner: P3 J ]+ G0 ^* ^! S3 i
for (nIndex = 0 nIndes < + nEllipse nIndes++) , Z6 g- Z/ K) @! c. k; D& n$ x {4 O* t, e8 `) J* q' ^) T0 V3 t1 ?) i
//creat solid brush 9 @; y' @, E5 t* v! x5 k) _ brush . CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ). # M( V( @3 ^" ^! l# S ( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue)% U3 \( E5 ^; G4 o3 u2 S3 l
/nEllipse ) ) )0 L; o; j8 {: m' o7 p$ [3 y& c
. M- \6 V. u4 ~+ q //select brush into dc 3 h! H9 B$ w/ x pBrushOld= dc .SelectObject (&brhsh), \- ?: W z1 F9 [' x# H; \
! r' l4 S3 m2 M //draw ellipse ( g$ ?% D, g, H3 [" Z. x dc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex , " p, F7 i5 x: C rect. right -( (int) fltStepHorz * nIndex )+ 1, 4 H8 u7 V, R( e2 ~ rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1)- L1 H. c+ [% y
: E4 q3 x; H& A+ u //delete the brush. z; N' Z- a' M. \4 d3 u8 w
brush.DelecteObject ( )8 `$ Z5 D7 L; [; m! k
} * i P8 l( }( `2 h1 j( a }1 w8 ]$ s# U# P
! ?$ r" ]& y) J2 r( A( M o 最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。0 r) R e& [ O0 z# i
/ C' \# X5 i) D4 H' v: B5 D
; s& g5 l+ w& z9 z
# p2 [* x9 Z$ D' i( S- }, `4 y* C5 M
UINT CRoundDlg : : OnNchitTest (Cpoint point ) 8 |) r9 U' z3 t7 S# c r$ O( k {( r; l% l3 ~- K9 C' l' s
//Let user move window by clickign anywhere on thewindow .7 S3 x, B7 u8 Y; Q& r/ |$ _" |# }! [
UINT nHitTest = CDialog : : OnNcHitTest (point) ' s# X: o* {9 r8 F rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest / H3 d' a# g# H9 K7 Q9 U3 z% E1 Q5 v7 E# P# ]
} # v: K% f# m3 s! v/ @5 a( r7 W" @ g3 m' V6 ` X7 E. h0 f
(25) 如何获取应用程序的 实例句柄?5 H+ z+ D# f; h- z
应用程序的实例句柄保存在CWinApp m_hInstance 中,可以这么调用AfxGetInstancdHandle获得句柄. 1 P) \$ i `2 @4 P + b9 s) I* m3 D. J9 f, Y/ S) G' y6 A# E; [* U9 v$ P, u, x
# v: i; F$ F% W3 k2 U( b, k+ Y
6 W8 w9 d8 c1 ~/ w: [- i! q Example: HANDLE hInstance=AfxGetInstanceHandle() / K' j6 J2 o3 A0 M: { - v0 i- {1 W2 v7 d' v5 N(26) 如何编程结束应用程序? 9 y- }& j- D h4 L 这是个很简单又是编程中经常要遇到的问题.1 w8 M% T& n# i, g
向窗口发送 WM_CLOSE消息,调用 CWnd::OnClose成员函数.允许对用户提示是否保存修改过的数据.+ t, n, D* B! N8 }# A
/ D, s6 H3 @6 L5 M( j- Z* L! X( o
[被屏蔽广告] / k7 h+ @, C8 ]% Q$ d9 G9 \5 [) H7 n+ w9 x9 g
系列文章:VC实用小知识总结(一) # ~) J0 z/ `$ |4 R7 N% p6 k! ~. a, T% m
( H/ B- Z# D8 }9 ^! S
8 x1 `4 q9 H3 A# ?+ \: V: [
' |6 s$ u2 M0 B: I! H0 }5 W
Example: AfxGetMainWindow()->SendMessage(WM_CLOSE)0 E% k8 i& _7 t2 n w& v! Y2 C1 m
) [/ T/ g! h0 u7 `, q& T& n: y2 p
还可以创建一个自定义的函数 Terminate Window4 t2 H5 _3 v$ h0 q$ q0 ]
- e. q3 R& g1 W8 Q& x' G7 _
: J. i+ A) q, o9 `9 w$ g9 B% l* T
! Z# c i0 c' i7 n; d! O; H (36) 如何改变对话或窗体视窗的背景颜色$ ?1 }/ g0 Z, v( q" E; y6 o
调用CWinApp : : SetDialogBkColor可以改变所有应用程序的背景颜色。第一个参数指定了背景颜色,第二个参数指定了文本颜色。下例将应用程序对话设置为蓝色背景和黄色文本。 8 O0 b' i7 {& p7 U o, i- L 2 X* M$ z7 V/ s1 p& Y. v- J0 Q9 D# Z
& L1 i: r9 l9 S
$ W0 y9 x& y3 x0 U: W BOOL CSampleApp : : InitInstance ( ) 9 v+ W% @) J+ n. @; e/ O/ k8 l1 a {$ X* F" [! h# b) Z" a4 I/ y+ G G
… % q% \8 t5 { A- u& C8 u2 n! e8 K! V& ~6 J: P
//use blue dialog with yellow text . 5 n! z5 f$ P, z4 l: l" m4 V, Z3 t SetDialogBkColor (RGB (0, 0, 255 ), RGB ( 255 ,255 , 0 ) )8 W/ w$ H$ f, T6 z" u% ]! Y
/ W% k- w. N- I6 B q/ H
… 0 A4 |- f* d. Q0 q8 p; U8 O } 6 _* G$ Y! v, q; o8 `5 n' k) A* z% A K o. k0 v' _0 S
需要重画对话(或对话的子控件)时,Windows向对话发送消息WM_CTLCOLOR,通常用户可以让Windows选择绘画背景的刷子,也可重置该消息指定刷子。下例说明了创建一个红色背景对话的步骤。8 B# m$ M8 l6 f* X' F" s
, b' w3 L: \" H0 r 首先,给对话基类增加一人成员变量 ) U1 I" x N# }. g/ E2 s% r5 f0 ]; p. z8 x
+ q; T. o1 n j3 {& |. f4 ? p$ v( A* p
# \# U1 z0 `1 k3 j2 L! c
CBursh :class CMyFormView : public CFormView" I( N4 U- }0 w* n$ L1 m; [4 J3 ]
{/ p' @7 u' r) `# M4 p
…5 R/ u. G& Z# E
8 I" }3 k* ?: x+ ^1 U0 i# ]% v2 h
private : 1 M/ V3 s, ?. k CBrush m_ brush // background brush 2 D: z) x. s* [% ^ V( V2 O1 k5 W$ x* A4 f( ~( m4 c& b
…4 c% K+ `- p2 o
}8 q; {' e$ C9 u5 K
7 t" u) ]/ k& e1 i0 X
其次, 在类的构造函数中将刷子初始化为所需要的背景颜色。 - Q! ?* l. |1 U `( |% Y& h* Y, [: h, i8 b
2 p P q v5 ^: z0 [6 @$ _5 I8 G! S( N# ?- r
% s6 D9 I! `7 I, e2 i5 ]6 c1 H
CMyFormView : : CMyFormView ( )# A2 J' L1 y' _( ~' S7 {5 w' L
{ 3 X9 g6 i) i0 s+ f6 f5 A // Initialize background brush . 5 _/ J6 W/ i: A: B m_brush .CreateSolidBrush (RGB ( 0, 0, 255) ) / J# Z1 @( z( t, i4 h2 T! b }8 ]/ B( R" p5 B3 }/ b
8 m8 ^: ]0 Q1 u" u* b" c7 q
最后,使用ClassWizard处理WM_CTLCOLOR消息并返回一个用来绘画对话背景的刷子句柄。注意:由于当重画对话控件时也要调用该函数,所以要检测nCtlColor参量。8 P/ p+ L }# O& v, `. k
+ P& t! Y" i- b$ G: H5 M" o+ c% J6 ~- k$ x+ R, |
6 g0 r" a7 [4 f' H: K$ m+ I
4 ?+ b9 w% ^# Q" l4 r+ ]
HBRUSH CMyFormView : : OnCtlColor (CDC* pDC , CWnd*pWnd , UINT nCtlColor% d: ]/ ~4 g: e' ` B: P+ J* \3 x
) 0 M# F6 x4 E2 o3 l ; w; W, Z3 j" l$ o" i {8 _' D7 k8 M- Y5 W9 F8 w
// Determine if drawing a dialog box . If we are, return +handle to ! h8 E& H- |& d8 G8 y3 [1 W$ Y& O- G //our own background brush . Otherwise let windows handle it . 6 {# F2 R' N; W. `8 _- f if (nCtlColor = = CTLCOLOR _ DLG ) $ l$ d8 {) k" t3 ]3 w6 r return (HBRUSH) m_brush.GetSafeHandle ( ) O9 @$ y/ ?5 q2 I% Z9 e( h t) }* \ ` return CFormView : : OnCtlColor (pDC, pWnd , nCtlColor8 K0 e4 V- m( H5 @
); s% e, w: Q2 x
} c5 o3 D( [% O" x3 x+ l. q% A7 Z% Z1 \
(37) 如何获取一个对话控件的指针 8 D; I& p F) {, z/ f 有两种方法。其一,调用CWnd: : GetDlgItem,获取一个CWnd*指针调用成员函数。下例调用GetDlgItem,将返回值传给一个CSpinButtonCtrl*以便调用CSpinButtonCtrl : : SetPos 函数: ^0 ^( K) M! t9 `* K6 j! \! F3 ]
! j& f g( c7 @& V
2 Z4 U- x. o5 {: v% ?: [, i- K) q/ r6 o8 t8 r, O5 P7 N$ ?- U
BOOL CSampleDialog : : OnInitDialog ( )8 g# f! e3 R0 x* h/ I
{/ @0 R+ y z J5 r
CDialog : : OnInitDialog ( ) ' u8 N7 w5 N* }4 Q& [" _9 f# }0 u4 m* _) V0 X) _. e% K
//Get pointer to spin button . 3 F( P, j8 r+ Y4 _ CSpinButtonCtrl * pSpin - ( CSpinButtonCtrl *) GetDlgItem(IDC_SPIN)4 N: f! C0 h S+ h9 {
ASSERT _ VALID (pSpin): n$ P( Y9 `1 z; g
//Set spin button's default position .! Z( Y' n! }- E$ S$ g) n' E
pSpin —> SetPos (10) % g6 s, f/ ?# u" K) }- L' k* x- k& t& m& K
return TRUE 3 f L1 [8 K2 G! k } , @5 p3 M3 n: i7 v3 w& K: P/ c/ w; C. x' F) W* l8 j; w1 c
其二, 可以使用ClassWizard将控件和成员变量联系起来。在ClassWizard中简单地选择Member Variables标签,然后选择Add Variable …按钮。如果在对话资源编辑器中,按下Ctrl键并双击控件即可转到Add Member Variable对话。 ; M0 f5 Z" @' ]6 x/ [% W 5 M8 e. ~% A$ O6 E6 z- L. e1 F d (38) 如何禁止和使能控件: K* C- O) j( w; j/ T; G
控件也是窗口,所以可以调用CWnd : : EnableWindow使能和禁止控件。/ Y4 W* I) R+ |( z. `
. l A8 g" R# w* I$ o' b7 U" G 0 y9 d. v4 y- L% s/ u ( n* J8 k) j4 ^5 t/ F% }# x% j4 T0 z6 } a5 P4 |, @
//Disable button controls . 7 j% U7 R% P M8 d m_wndOK.EnableWindow (FALSE ) 9 M( O. u, {9 H m_wndApply.EnableWindow (FALSE )" u* b7 h. h3 x! J! t) c$ K
6 L: ?: f+ J4 ^* l/ q& q
(39) 如何改变控件的字体- s1 u7 C6 T; F) J
由于控件是也是窗口,用户可以调用CWnd: : SetFont指定新字体。该函数用一个Cfont指针,要保证在控件撤消之前不能撤消字体对象。下例将下压按钮的字体改为8点Arial字体:/ ~- x) a8 `3 J d0 @$ a3 c
' d. V+ O' C; O9 K3 }7 G" m
! r- K) T3 d. l3 J0 @3 p' c3 [9 v* I: Z+ Z$ L9 g, p$ S0 ^
& v4 C3 Y5 j0 X' v6 A; O6 z( B
//Declare font object in class declaration (.H file ). , f& j7 Q+ Q( a1 X, Z. }4 F private : Cfont m_font* _, y6 v; C3 p: f+ b2 g: @/ O4 d
// Set font in class implementation (.Cpp file ). Note m_wndButton is a $ z# w" E/ S% V* _4 \% {: h //member variable added by ClassWizard.DDX routines hook the member - f" o+ R; `* N //variable to a dialog button contrlo.! D9 [" h# H5 ]( O% f
BOOL CSampleDialog : : OnInitDialog ( ) , N9 g8 a' G P( i, K* k m. l {' |1 i% }; G' V6 D6 O- e" q7 R
…1 i! l6 }5 P9 _+ n
//Create an 8-point Arial font 1 n: `6 x* S7 m3 Q m_font . CreateFont (MulDiv (8 , -pDC - b( C o8 ]6 W —> GetDeviceCaps(LOGPIXELSY) ,72). 0 , 0 , 0 , FW_NORMAL , 0 , 0,0, ANSI_CHARSER, OUT_STROKE_PRECIS , - a& F- `; R" E: Y. r 1 d3 b1 z8 m4 ]6 o9 l CLIP_STROKE _PRECIS , DRAFT _QUALITY6 @3 N9 s. S, N: f$ ~" R- o; Y
VARIABLE_PITCH |FF_SWISS, _T("Arial") )! {; n+ i0 e+ Z4 H4 U: G3 c. ?
& K. o/ R5 @. T2 Q7 ]& I! _
//Set font for push button .* S* k7 R) Z8 D" ^
m_wndButton . SetFont (&m _font ) ! o8 l) g2 e, L- w. F2 ~5 g# ?0 Z 4 l# `4 R% U$ ~1 i) L i3 U5 c1 F …! ^+ H, z; k9 O. r6 A) {8 `+ `
} ) F# H0 G1 a7 V) q) U6 W) l; k) ^: A8 W- b4 k/ l2 N7 y5 _
(40) 如何在OLE控件中使用OLE_COLOR数据类型 T9 [3 T9 ^4 D- Y6 R 诸如COleControl : : GetFortColor和COleControl : : GetBackColor等函数返回OLE _COLOR数据类型的颜色,而GDI对象诸如笔和刷子使用的是COLORREF数据类型,调用COleControl : : TranslateColor可以很容易地将OLE_COLOR类型改为COLORREF类型。下例创建了一个当前背景颜色的刷子: ( Z! q' K' {* z! W/ @, f r4 G2 \1 U8 F# t# C3 l: ?
8 \3 Y) n$ i( B, S9 m ?9 \ 1 i# B# r6 r( {1 O( N) S; E6 Q - O( R0 W1 }. E: I void CSampleControl : : OnDraw (CDC* pdc9 `3 d S/ P2 n. t1 i, s
const Crect& rcBounds , const Crect& rcInvalid ! M; n3 ^/ q. d# s2 Q4 ~: y ) # a. @- _! A( T( Q4 Z { t( [7 X" Y$ S2 s, J# o
//Create a brush of the cuttent background color.8 O2 k' p7 L6 B$ {6 |2 ~. C0 X6 U
CBrush brushBack (TranslateColor (GetBackColor () ) )8 B6 F. {" j1 R8 H' o8 V
1 P5 R2 P* V5 f: @, }- P //Paint the background using the current backgroundcolor . / q" ?) e8 H3 e+ z pdc—> FilllRect (rcBounds , &brushBack) + O2 x- b; V: O2 i/ F8 T: I: o* d' r0 Y; p; @! k4 Z ~7 [' F
//other drawign commands 8 G0 N4 Z, Q1 _- R- m, |, G3 B. z! C) A7 h& C3 D0 ]0 H9 N
… ! y2 R+ [1 q/ I' E8 s+ [) J }2 r8 X$ V0 p7 [