数学建模社区-数学中国

标题: C++BUILDER中创建不规则窗体 [打印本页]

作者: 韩冰    时间: 2005-1-26 01:37
标题: C++BUILDER中创建不规则窗体
<>以下源码是快速创建不规则窗体的,还可以移动。
% Y! Z: D$ t$ Cfile://------------------------------------------------------------------------
) u! x& H  l4 \+ Z4 I; Q" N- f<a>file://Unit1.h</A></P>
5 H' Z) s8 O$ w5 V( Q6 D<>#ifndef Unit1H
& g0 b1 K1 ]8 [# z' R#define Unit1H1 l2 I& ^& u$ G1 O; Z# W- p
file://------------------------------------------------------------------------. X. ^: n4 f0 V( I4 G3 f0 _
#include &lt;Classes.hpp&gt;
0 x  F8 ?2 L, y) e: C#include &lt;Controls.hpp&gt;7 W0 `* L& E9 b6 @
#include &lt;StdCtrls.hpp&gt;
5 S! q/ I. c( L: j  v/ e( s#include &lt;Forms.hpp&gt;8 x4 w9 k6 o6 c! L
#include &lt;ExtCtrls.hpp&gt;1 g+ A, s6 s# O) _7 h0 y
#include &lt;Graphics.hpp&gt;+ Z* |8 J) y+ ]$ |
#include &lt;Menus.hpp&gt;
$ C% M8 t' m5 J  u/ E/ U+ b#include &lt;Dialogs.hpp&gt;. X+ W/ }- V2 j: e3 w2 [
#include &lt;ExtDlgs.hpp&gt;) y/ m/ C! l5 _( `0 b' {
file://---------------------------------------------------------------------------
0 I/ @+ T4 j4 n0 R2 Lclass TForm1 : public TForm# I1 Q- ~: a) z2 n
{
1 g. F4 m- b: F" K* `; @/ H1 k__published:    // IDE-managed Components& e: E% k4 w( }$ C6 @& W/ u8 x0 t* |7 U
        TImage *Image1;9 `& V6 |/ e2 t, |, w
        TPopupMenu *PopupMenu1;
2 C3 M* ?# }( }) K3 T/ i* E        TMenuItem *Exit1;
' I8 s$ p/ \% a        TOpenPictureDialog *OpenPictureDialog1;
" h. k  V/ [% Q4 J. p! e" I        TMenuItem *Open1;
5 W6 h5 j3 }" S0 M! R2 L5 X        void __fastcall Exit1Click(TObject *Sender);
- y/ {2 w/ q  L4 y/ ^! J8 {        void __fastcall FormCreate(TObject *Sender);
! U9 C6 J0 f0 Q( v        void __fastcall Image1MouseDown(TObject *Sender,
: C) S+ F; U& {" m' g          TMouseButton Button, TShiftState Shift, int X, int Y);
  l+ x) [1 i& X$ P. M3 ~. }        void __fastcall Open1Click(TObject *Sender);: X/ j) ~6 F; b. \# ]
private:    // User declarations
/ v* l3 W/ `' r0 m$ Y3 O        void __fastcall BmpToRgn();
5 n; i! r8 @6 o  }public:        // User declarations8 N% k, }9 n7 |
        __fastcall TForm1(TComponent* Owner);
# x' `9 S3 \1 q8 R) e};
, ]# A2 J7 |0 B& R: _& B! \/ Yfile://---------------------------------------------------------------------------; O3 Y, q$ |; w# @
extern PACKAGE TForm1 *Form1;
1 D1 O( D. O/ c% M" gfile://---------------------------------------------------------------------------
5 W+ @# ~' U" s* x- t2 n#endif</P>% Y& b7 l; R/ X! _
<>
1 I4 i- f2 p% [: D1 J<a>file://unit1.cpp</A>
3 s/ \) }& C& h8 C% E6 u6 z, jfile://---------------------------------------------------------------------------</P>
1 _, I. A0 F/ v8 U' }; a: f) s/ D<>#include &lt;vcl.h&gt;" H. I0 m$ u1 X# U
#pragma hdrstop</P>1 A0 z2 d2 f. n, n! G
<>#include "Unit1.h"
/ S# R7 |0 ~. I$ l5 r# bfile://---------------------------------------------------------------------------8 Z; B( c) Q) Y2 v7 O2 T6 v
#pragma package(smart_init)
* m% G1 J) J7 x) f. R# ?#pragma resource "*.dfm"
# B  f+ A  S5 d6 lTForm1 *Form1;
: f8 e5 w5 S- n9 C( X/ k% a: |file://---------------------------------------------------------------------------! f( ]$ U5 J8 D9 \& h7 k
__fastcall TForm1::TForm1(TComponent* Owner)
9 ?0 [2 U3 e) R: \        : TForm(Owner)
- h3 Z" c# l8 ^  _( ?, }! E{$ c/ T9 T" w/ l8 U" N1 W- A
}
! @! \. W* G& i7 Q0 f6 hfile://---------------------------------------------------------------------------
; b( B/ v- t7 Z1 G/ E! Kvoid __fastcall TForm1::Exit1Click(TObject *Sender)
/ }9 I' i7 |) Q; P) L) z( |- v{
& w, D! ]  Z; z( N  Close();
3 A- Q; B% r, J; O  U( a1 v: P}0 B) X6 C% }/ i" t" [0 ]
file://---------------------------------------------------------------------------</P>
  q( V+ p+ G) ]<>void __fastcall TForm1::FormCreate(TObject *Sender)
& A  W8 l& w, ]2 d{</P>8 }7 y. l; d6 C, D' N% z1 n3 z' x) V9 C
<>BmpToRgn();) ?: x/ q* g7 b0 e2 s+ N, E$ t/ N; {
}. p. H7 n% x0 Q" \  z) D
file://---------------------------------------------------------------------------</P>; _# l7 |! J& p( j
<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,
* x) k0 R3 z1 j1 E( q8 f% T. \* ^      TMouseButton Button, TShiftState Shift, int X, int Y), j. B7 C! `( N' V
{
2 j) V% z) [  J* Vif(Button == mbLeft)
3 c( ]% p, o# b8 x8 E6 k+ x        {
+ p( l7 R( L4 v8 ]+ a/ r                ReleaseCapture();2 z6 Z; F4 h8 v  f" q, A
                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);
7 l3 l/ q" D" A        }/ w$ T) B8 W5 u# K1 X0 e6 Y2 ~/ j
}5 l! o* P) Z; N3 G
file://---------------------------------------------------------------------------' G% k* s! F8 }! h4 r, l5 M
void __fastcall TForm1::BmpToRgn()) j$ n: G8 y5 `
{: Y; C- r8 K6 _1 G) n
Image1-&gt;AutoSize=true;0 Z( Z' [2 q: e6 L8 E
Form1-&gt;AutoSize=true;
+ j4 u% j0 v8 @+ j$ kForm1-&gt;BorderStyle=bsNone;
4 l0 L. _' l, A0 i; x& nTColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];) w, `* R9 ^  z
int x,y;! A/ w. E# {. H. d1 V# |
int l,r;
1 L4 v) k, m/ ~) O% [9 bPOINT *a;
$ D: @0 I5 y2 X5 N: V% Nbool lb,rb;8 h8 x7 Q$ @( |1 r1 L0 Y
HRGN WndRgn,TempRgn;</P>  w  w  f9 |! A& d+ z
<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)# g; I- V1 l; Z% b( U; N
{
3 \" K0 i" T2 G/ z  ~  ShowMessage("申请内存失败!");
6 y, l- @# F7 A  exit(0);
# f8 M; d$ n9 W. C7 s; Q5 H}</P>; u1 [+ T6 j$ v( M( U$ t. Q% f' S) M
<>l=0;r=Image1-&gt;Height*2-1;; Z; U3 d/ X* h% b
WndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>2 M; ~7 {% h1 V9 |/ G
<>for(y=0;y&lt;Image1-&gt;Height;y++)
6 T8 I+ t/ o. x# K7 h1 I0 ^{
* v1 ^& q- [3 c4 T4 g' Y  lb=true;
6 x. ?9 `  ^# Y& R$ Y: M4 S* ~  for(x=0;x&lt;Image1-&gt;Width+1;x++)/ ?+ \+ [" ]. c- p  H9 A7 Y
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)
2 g5 X1 e+ d. }0 Y$ w3 M    {
$ {1 Y8 g. J. e- d6 d, F8 a      a[l].x=x;$ o% P- `; A8 K* {' f  [0 |: H' N
      a[l].y=y;
, o# {6 Q' c4 @. q) `* W      lb=false;5 u. o* N5 @$ {$ k6 M# G) d3 u
      break;
$ A* T! \7 F: r8 Q& X* t( |    }</P>; d# r3 k+ S/ o( z* L1 H# Y% o: ]
<>  if(lb) a[l]=a[l-1];
3 c3 d) [. Q. R6 R5 Z  l++;
  z- l8 d% C$ e  rb=true;</P>
0 C2 q1 @  Z  }6 a; W<>  for(x=Image1-&gt;Width;x&gt;=0;x--): V% V# T& Q9 x& {  q- |! w) f8 ^
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)$ |3 [0 W# n3 C- E8 F+ ?6 N
      {* i8 Q6 e' |, ?: d8 W
        a[r].x=x;" B: }9 H8 K$ K0 I2 p9 l
        a[r].y=y;: m- h& u& }  @! Z7 c; G+ q2 P6 p2 Q
        rb=false;
* A- T5 D; M+ o/ r  j) b& `0 z; p        break;' H) b+ _/ L+ f- Y: ]! M
      }/ e# {. E; E* B9 R
  if(rb) a[r]=a[r+1];. s2 d" N2 O; e. L- {
  r--;! [, F6 P" ^) ~1 k4 o; Z
}</P>: h1 B1 p8 a( @0 W& A3 n
<>r=Image1-&gt;Height*2-1;7 n$ a: C5 c* m8 ^
for(y=0;y&lt;Image1-&gt;Height-1;y++)6 c& s8 @8 M' H; |4 y. H& D
{
8 N9 n4 G3 F" Z  T& A3 C6 o  for(x=a[y].x;x&lt;=a[r].x;x++)0 y4 u- b3 y* r8 Z7 e6 v# M! _
    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)
+ m5 J  l- W9 l! k    {* i- h4 P- F5 }
      TempRgn=CreateRectRgn(x,y,x+1,y+1);% ^: o$ f& K# w3 x# {
      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);: g8 o! |' d/ `9 J+ Q
      DeleteObject(TempRgn);
/ m  z$ r* f* j. c/ w    }5 L2 m% G! t- u/ s( w% I( m. @
  r--;0 h0 i, n* p( j* G' w
}</P>
) a. z2 Z  d2 M5 n  R3 ]<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);
* Z  C( g1 e( K7 PTempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);
8 I9 Z' K" R; E. V# vCombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);9 h7 ~& y  J' _5 i
DeleteObject(TempRgn);3 f6 J+ t1 u( \6 p+ v3 F7 n! S8 A
delete a;
( {8 M7 s7 `) Q/ m% dSetWindowRgn(Handle,WndRgn,true);
2 X5 ^& @8 q$ o& n. h}</P>
& K( z+ \/ D, q9 u9 H- c! D  d+ A<>void __fastcall TForm1::Open1Click(TObject *Sender)
5 [9 T' ?0 d5 w{% x/ @2 G- [& j* s$ C4 k/ M4 g
  if(OpenPictureDialog1-&gt;Execute())0 ]  |/ L+ A7 c  |' `5 g1 ]
  {
  `# ?& c# m) l, [7 s( X      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);
7 O9 `+ F1 S5 H5 s& d      BmpToRgn();
9 ~: n( Q$ k3 A) X3 u( E- H4 ]  }6 Q& K4 O& S9 D7 d
}
$ T3 d' {$ z  T8 ]+ F; `file://---------------------------------------------------------------------------</P>
4 P, d) F8 v1 j$ \8 f, W<>对啦,图片的背景色要相同。; g5 q7 f- L' X) _  c" l. D8 W4 L
图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>* K8 ]  q- K; a% [# m1 \5 E, g
<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------
" u* p, B# M) s% i2 _void __fastcall TForm1::FormPaint(省略)7 H9 h4 ~" E# K  j
{
1 i! K& E  b3 {0 A* g8 i* @    AnsiString ww;int bb;
7 H  {9 s5 W2 T& k    ww=AnsiString(Form1-&gt;Width/256);9 \0 [* ?! y1 N2 |2 T) x9 @0 ?- e
    TRect MyRect;$ v$ C3 y2 i* s
    MyRect.left=0;* U" i  ~$ B) e7 c: Z, D
    for(bb=0;bb&lt;255;bb++)$ j1 _, c. e' h* Y
    {
/ i! o" Y1 L& m+ l5 ], h        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);: u0 ]. v  F  p; m4 d/ M0 U' G2 {( J
        MyRect.top=bb*(ww.ToInt());$ o5 q( @" v  N/ `  \
        MyRect.right=Form1-&gt;Width;
- E2 b) Z2 Y8 H7 C$ ]; m: C" C8 b        MyRect.bottom=(bb+1)*(ww.ToInt());
3 Q. M2 `6 g5 r8 N3 b        Form1-&gt;Canvas-&gt;FillRect(MyRect);
' E) O/ g$ L  [9 p    }
7 b  C; w' D" ?: L, p} </P>& C0 z/ v' `: k5 m, \; V
<>file://---------------透明窗体--------------------------
- W& ^& v% b) o0 U6 lvoid __fastcall TForm1::Button1Click(TObject *Sender)
; K) T; P2 `; ?" w{3 G& r3 e5 |2 x* P7 Q
  TRect *rctClient,*rctFrame;8 j6 {& M; o- E4 F7 |) l
  HRGN hClient,hFrame;
5 k. {" M  J9 a! x1 n" Z: C$ `  POINT *lpTL,*lpBR;& o% w% [! t' R& a' m% E* W
  rctFrame=new TRect;* b. W" g. W" f% x8 T, e' o
  rctClient=new TRect;0 m. ]( {& C- D8 R
  lpTL=new POINT;* Z# ~$ }4 u- ], t% Q
  lpBR=new POINT;7 I; P! ]! S; |
  GetWindowRect(Form1-&gt;Handle,rctFrame);
( n+ }2 m/ [7 z2 ~9 E) Y  ::GetClientRect(Form1-&gt;Handle,rctClient);6 u5 {* Y4 m8 N8 E
  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;
0 E% ]! w% u; _; @" d6 l! `  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>  L9 F6 t5 N+ a0 Q+ e, n) h
<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);
2 ~; n3 L7 N/ Z* ?+ E1 r  ::ScreenToClient(Form1-&gt;Handle,lpBR);
  ~$ {6 w: Q3 R2 m0 V& ^; ]) {( |+ e  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;0 J4 G4 B- ]0 N: `) y/ ?
  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;
' W2 Z( k' m# \: M# z  rctClient-&gt;Left=abs(rctFrame-&gt;Left);' \$ R/ U* \. u3 d
  rctClient-&gt;Top=abs(rctFrame-&gt;Top);
( d1 p4 J2 c: t2 R! A  rctClient-&gt;Right=rctClient-&gt;Right+
. ^, W0 v6 G9 l0 }" H# p                  abs(rctFrame-&gt;Left);
  Q2 W9 Z( V- [) V# v, X  rctClient-&gt;Bottom=rctClient-&gt;Bottom+
9 d/ _9 W0 r; v1 c1 d$ j                    abs(rctFrame-&gt;Top);
) \7 T* ?' E# C' S1 b% r  rctFrame-&gt;Right=rctFrame-&gt;Right+1 d) P! \1 ~# r! t
                  abs(rctFrame-&gt;Left);
1 z( U1 C4 A$ i3 A" {  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+
. [, k6 n1 k& z" [( N3 C                  abs(rctFrame-&gt;top);7 s# t* t2 A1 f1 M
  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>% ]6 ^' \1 B& I+ I% ~
<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,# R  H9 B1 T0 X" ]$ A# Y$ @
                        rctClient-&gt;Right,rctClient-&gt;Bottom);8 N$ {  i+ X7 ~: l/ g- W8 |1 i5 _& [
  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,
, _6 `4 o$ J. G; N                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);) N) q2 k6 L) |$ t" |4 J; s
  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);
" z$ |/ D3 A* _9 }# x  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>
6 N8 B7 ^( t. O9 C( e  w2 T3 i<>
7 `9 l: e9 q6 v6 H7 V# U2 k7 u; v8 W! {  delete rctFrame;) E0 e3 q* R0 W1 Z
  delete rctClient;
7 T2 F6 g5 F: I- U% L  delete lpTL,lpBR;</P>
. C: w& L  ?1 F$ F) n3 x% p) x6 o0 y<>} </P>




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5