Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198468
  • 博文数量: 264
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 2740
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-03 13:25
文章分类

全部博文(264)

文章存档

2011年(1)

2009年(263)

我的朋友

分类: C/C++

2009-06-04 13:32:36

可以使用新的SDK函数SetWindowRgn。该函数将绘画和鼠标消息限定在窗口的一个指定的区域,实际上使窗口成为指定的不规则形状。  
- ^4 C' h6 g/ O. u* ]+ a4 |! v; `0 D$ D7 K
使用AppWizard创建一个基于对话框的应用程序并使用资源编辑器从主对话资源中删除有的缺省控件、标题以及边界。  3 N1 a- Z" h/ @4 i

* X1 P( v2 E9 G" n0 Z给对话类增加一个CRgn数据成员,以后要使用该数据成员建立窗口区域。  ( X% ~: H/ E- |- P
Class CRoundDlg : public CDialog  8 B2 [* m0 P( N: u
{ …  + L4 C9 e0 e/ Q1 @) X
private :  ! D2 @8 D& m% |- f) u0 O/ U
Crgn m_rgn : // window region  8 x7 ?0 A) i$ t5 n5 S! a
…}  + {' ~$ Z* }" I* G
修改OnInitDialog函数建立一个椭圆区域并调用SetWindowRgn将该区域分配给窗口:  
4 y- R, K/ K& s% B( c7 kBOOL CRoundDlg : : OnInitDialog ( )  4 a; V! f! v" M8 Q, H
{  
2 ~; T) o, u! t2 ~3 {+ i7 V) w
( \3 G. k" D9 eCDialog : : OnInitDialog ( )  , k6 y9 a: I1 f0 n1 l% A2 u
0 W8 R$ _/ T. @  T
//Get size of dialog .  
) L9 |9 F3 h2 e$ \+ J
$ h; @2 u& h1 J  Q/ B) Z# kCRect rcDialog ;  
8 |  [: O4 N/ l$ {/ N6 @: `% ]2 m: K  y
GetClientRect (rcDialog )  
- \+ R+ b: q% h- q$ a
0 h5 N: _- ^! H// Create region and assign to window .  3 e) R( J0 q# c& h, D" p

; x# y1 v# t1 f9 P9 b. w% K& Mm_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width( ),  
5 s0 w% E' u! p# k* C
9 T5 k, ~6 \: z" NrcDialog.Height ( ) )  
; D3 ?: C; U; P
& ^! {- [2 ~. u. i: QSetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn ,TRUE );  
# r) T1 ^1 |( g9 S5 I& n
/ |" K( b9 w" nreturn TRUE  : Q0 Z, n. y9 V
}  
# `* q5 n" S+ A9 J通过建立区域和调用SetWindowRgn,已经建立一个不规则形状的窗口,下面的例子程序是修改OnPaint函数使窗口形状看起来象一个球形体。  
5 w7 `% [1 O' \- E. m) Q" p) m0 zvoik CRoundDlg : : OnPaint ( )  0 w9 b7 B3 A; ]5 Y1 h
{  
2 |' j' A/ v0 Q7 p0 g) iCPaintDC de (this) // device context for painting.  
- s: a3 j' n" o  V- S//draw ellipse with out any border  ; e8 f- o/ B% l3 w1 ?
dc. SelecStockObject (NULL_PEN)  
( B! n9 U2 ^0 y( n1 O# j9 S//get the RGB colour components of the sphere color  : d2 n7 Y! O7 x: F9 m8 g1 ^
COLORREF color= RGB( 0 , 0 , 255)  
. f8 X* H0 A; FBYTE byRed =GetRValue (color)  ' L0 d8 @2 c7 [2 U- F# F. N
BYTE byGreen = GetGValue (color)  6 {; {* r- S) y1 U" L" m' |5 ]
BYTE byBlue = GetBValue (color)  / c/ y( `8 x1 P: T0 F- m
// get the size of the view window Crect  : b, P  W0 A% g# I
rect GetClientRect (rect)    b# R3 ^( e* b/ l& o
// get minimun number of units  9 M3 q& k+ T" g2 n
int nUnits =min (rect.right , rect.bottom )  
, n2 J* ~- A4 E9 z' l  T//calculate he horiaontal and vertical step size  
: Y" N0 h( ^7 S( zfloat fltStepHorz = (float) rect.right /nUnits  
8 W0 T: Y! ^5 afloat fltStepVert = (float) rect.bottom /nUnits  + e" @" j# H# U5 i
int nEllipse = nUnits/3 // calculate how many todraw int nIndex  
, i! t( t8 i$ R" Q0 [$ W// current ellipse that is being draw  & D) k3 p, @& T
CBrush brush  8 S/ z1 Z3 v; t7 I6 M
// bursh used for ellipse fill color  
( Z; o! S' N- {2 H: U8 ^  hCBrush *pBrushOld // previous brush that was selected into dc  
1 Z3 G0 o5 v5 w//draw ellipse , gradually moving towards upper-rightcorner  9 \* ?% }7 H& w( h/ |- \
for (nIndex = 0 nIndes < + nEllipse nIndes++)  
. u9 ^* ]3 A( U{ //creat solid brush brush .  3 `, `! ?: z) K+ o4 y$ e
CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ).  
: t4 Z8 a) \- L, d& f* j- W7 j; K9 M! a7 w
( ( nIndex * byGreen ) /nEllipse ), ( (nIndex*byBlue)/nEllipse ) ) )  
$ g" [7 c# {1 Z' R6 l8 j2 C; [//select brush into dc  $ n. E6 ?) S5 d+ J; z  m9 D7 u. f+ O
pBrushOld= dc .SelectObject (&brhsh)  
* F; i$ |8 L4 H4 B# j//draw ellipse  , |+ @; X+ W1 w* {; G6 Y, c/ d' a
dc .Ellipse ( (int) fltStepHorz * 2, (int)fltStepVert * nIndex ,  0 F8 |3 Z$ _. W. `" I; _6 K3 a; s  s
rect. right -( (int)fltStepHorz * nIndex )+ 1, rect . bottom -( (int)  ; k  g7 p/ E% C2 V. N7 O6 j% I
fltStepVert * (nIndex *2) ) +1)  
% w2 E* |7 y3 k! d$ H* n6 U. |//delete the brush    x; A* C! V: ]* V8 e8 J
brush.DelecteObject ( )    h3 p- r" S+ \5 E
} }  
& E7 @5 Y+ Z* K. P! n& p+ T7 N# C最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。  
9 ]% [5 Y) U9 c! K7 IUINT CRoundDlg : : OnNchitTest (Cpoint point )  " n1 d0 ?9 r( M. _+ t' O
{  
% P: f5 y' p, [//Let user move window by clickign anywhere on thewindow .  
0 h6 l+ x3 b0 T, u2 ?% HUINT nHitTest = CDialog : : OnNcHitTest (point)  % U/ B0 B9 n+ j" d# t- B
rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest  
2 n7 l: |3 q/ P5 x/ @5 I+ w}
阅读(182) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~