QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 3089|回复: 0
打印 上一主题 下一主题

C++BUILDER中创建不规则窗体

[复制链接]
字体大小: 正常 放大
韩冰        

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:37 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>以下源码是快速创建不规则窗体的,还可以移动。1 X7 P8 `9 H1 d+ |8 }, d0 v* `
file://------------------------------------------------------------------------3 V$ }" u  z9 D1 f1 _6 ?9 s
<a>file://Unit1.h</A></P>* `( ]$ i) P( S5 `
<>#ifndef Unit1H
' K* q" p5 A/ I; H0 Q- F# t- C#define Unit1H
) |7 l; y2 t9 x4 S- z4 ~" Yfile://------------------------------------------------------------------------
1 v0 F7 [) ]8 R4 _2 A#include &lt;Classes.hpp&gt;* E2 W1 i. F/ F- M
#include &lt;Controls.hpp&gt;8 D& k: a4 Z* ]! q" K0 F
#include &lt;StdCtrls.hpp&gt;
: P, U" l( k3 i9 m#include &lt;Forms.hpp&gt;
) f8 N/ Y% x5 t" i& L+ \; u& g( t#include &lt;ExtCtrls.hpp&gt;) i& A! P! }6 ]& A/ j
#include &lt;Graphics.hpp&gt;
# u1 x) `1 [+ C; t0 C8 k#include &lt;Menus.hpp&gt;
6 g) O9 f* ~3 E2 L! ?4 x: S+ c: t#include &lt;Dialogs.hpp&gt;  w; ~* d* T" \' C# P! J, J
#include &lt;ExtDlgs.hpp&gt;1 Z* E0 B; A6 ^; `% E2 X: _
file://---------------------------------------------------------------------------% t9 B  d0 h, h. j
class TForm1 : public TForm9 F# G5 w$ x2 s5 i! ]
{
# H' P$ j% u$ h2 `# w4 C, j__published:    // IDE-managed Components
8 D7 `6 E% T9 }        TImage *Image1;
( A& j, {2 g  {) b  d/ c+ T        TPopupMenu *PopupMenu1;/ ^" E9 b: l- ?: c7 V9 W) V4 r* _3 L; h
        TMenuItem *Exit1;
8 [; L- {# S5 n$ q% b# T# s+ F        TOpenPictureDialog *OpenPictureDialog1;
. z) b+ J+ W. d7 \# u  g' `        TMenuItem *Open1;% v: u. c7 s# V& _3 x
        void __fastcall Exit1Click(TObject *Sender);
5 F6 x0 L# w$ Q. J/ w        void __fastcall FormCreate(TObject *Sender);& `; C1 V, W3 y3 @0 Y( Z
        void __fastcall Image1MouseDown(TObject *Sender,% J) E8 {7 h" D+ `$ z! M
          TMouseButton Button, TShiftState Shift, int X, int Y);, ~6 @  v& q+ v# ?% I
        void __fastcall Open1Click(TObject *Sender);( m9 v" a- L' \/ U" R
private:    // User declarations
. A+ z: @$ _7 S  z1 \6 V6 C        void __fastcall BmpToRgn();
$ S* m: o, F2 W9 F) r8 V- {public:        // User declarations+ m: b8 j0 m) H: ~# r' O+ q
        __fastcall TForm1(TComponent* Owner);8 n' G5 s( X6 b6 [6 E8 @! e
};
9 B# C# e% P; {0 C; n6 B" ^2 Nfile://---------------------------------------------------------------------------
7 r& c7 s/ \% }' r& L! ]1 M/ aextern PACKAGE TForm1 *Form1;% W* ]( ^3 t2 z( Q1 Q7 U* O
file://---------------------------------------------------------------------------. T# y! A2 c5 N' R4 A6 e
#endif</P>4 j) K3 {! {, |3 x
<>4 l) s2 s9 @, o# j; F; Z
<a>file://unit1.cpp</A>/ {( }, E! T+ Z' k+ R% l
file://---------------------------------------------------------------------------</P>: o  g7 z; a/ n' `0 z. m+ F4 ~
<>#include &lt;vcl.h&gt;2 R  Z' n& G. }$ k( S& N# i8 b
#pragma hdrstop</P>
! @+ S3 v  B) Q4 P) @<>#include "Unit1.h"; J; Y4 m! c3 I6 u) @- i
file://---------------------------------------------------------------------------! y( L5 D  {( J9 M! W$ @
#pragma package(smart_init)* o/ L/ A: Z  Y1 V0 o) y' v3 W3 w: K% \
#pragma resource "*.dfm"( R- e' B1 U) K0 Y5 p
TForm1 *Form1;
& W5 h5 o5 I! s$ b: e9 s/ Yfile://---------------------------------------------------------------------------
- A& o: }' x. ^0 \* ?' I6 l! P__fastcall TForm1::TForm1(TComponent* Owner)2 B( [$ H( j' e0 [2 B
        : TForm(Owner)
# P/ @1 t3 N' H{$ r# y7 }7 b- R4 a. n  ?  ]
}
4 u0 g, }, r8 ?file://---------------------------------------------------------------------------7 x' u. l2 C! o
void __fastcall TForm1::Exit1Click(TObject *Sender)
' ^. x6 b* n; R' o$ c) Z{0 f1 |7 ?$ }4 J' l/ |$ K( y
  Close();, }' o* q; G' }5 Z
}
2 n. U5 ^; z. A' w7 U- ifile://---------------------------------------------------------------------------</P>
) ?2 F& f0 i+ `' q+ I' V5 l8 f<>void __fastcall TForm1::FormCreate(TObject *Sender)
# g5 a, r; ~; E1 h+ R. I{</P>- z/ n  h; E* ]5 a; i
<>BmpToRgn();
, j: }% D( D9 a3 ~$ P3 _}: Q/ [+ l' |. p" O3 k+ e0 O
file://---------------------------------------------------------------------------</P>
/ U" Z8 k$ L" t2 R) D: ~9 O<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,
8 G: }. {/ N$ C# R, `* F      TMouseButton Button, TShiftState Shift, int X, int Y)( A& _9 {1 A' ]/ B$ q* Q9 e; A
{
/ O5 D3 L" \3 i- Oif(Button == mbLeft)
% P. f" a1 S) L        {- _1 V: f# r# |
                ReleaseCapture();
0 ]8 C/ L7 n- P: E+ C- [# e                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);- q) Z$ l- z1 ]* \( l  [
        }% |$ w9 e) o* R+ v0 |  z% p
}
( r+ o+ b, A$ c. bfile://---------------------------------------------------------------------------
; h% P0 f5 Q' ?3 f  O6 }void __fastcall TForm1::BmpToRgn()4 _& J* h6 |8 w
{9 |9 X* J" B1 q- p! [
Image1-&gt;AutoSize=true;' Q3 M4 N  `  V) y& U' ~, V% P5 A
Form1-&gt;AutoSize=true;* b1 C' j3 \5 Q9 [* v* G4 Z1 z/ Q
Form1-&gt;BorderStyle=bsNone;, f+ k5 Z& g# Y+ Y8 I
TColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];
* B: r) J% y% ]9 sint x,y;
4 m8 d/ {$ F$ A" w! a8 g( Gint l,r;
/ u: B0 t0 Z$ F5 A* R1 t0 ~" \POINT *a;
, W1 p9 w$ q, ?' Q7 M. C! }7 k( Ubool lb,rb;
1 n: c6 W8 g* T' f& hHRGN WndRgn,TempRgn;</P>
( @$ C" Q7 r% \$ @7 d+ _9 L<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)/ H: }( M. `  ~0 r
{
0 H9 B! J) I. q+ A1 e  ShowMessage("申请内存失败!");- y; k2 \0 e/ I( A+ O8 Z8 Y' ]& R
  exit(0);
9 Z. [% z# o# r( w9 [& V}</P>
: A6 r: a- w7 F! [<>l=0;r=Image1-&gt;Height*2-1;$ ?/ g( P8 O/ |9 z) w" H
WndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>
1 y; @4 T4 g3 J3 ^<>for(y=0;y&lt;Image1-&gt;Height;y++)6 _' H/ f$ e0 S! p/ ~
{) ?) n) L, x% i, B' g9 X  U
  lb=true;: t1 A4 N) m' h0 L! e6 {* E) ~
  for(x=0;x&lt;Image1-&gt;Width+1;x++)9 d3 w8 m6 C) L! B; [# @8 j/ v$ D
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)
6 i; _: @# q! f    {
  V3 b: j& v6 w, i* Z, X      a[l].x=x;' }* n7 O" s4 T8 L
      a[l].y=y;
9 o& Y2 n4 Z" v: a, ]! Z0 Z+ ]9 J" D      lb=false;
9 ?0 h5 |) G2 [. R      break;
. s) T7 a: q- i9 i8 p. M% P- L    }</P>8 S* G8 y5 M  T0 e9 ^8 A! h, {: S
<>  if(lb) a[l]=a[l-1];  ]! s( G- U' a
  l++;
( s$ A# v# p0 y  rb=true;</P>; o4 z6 J! v9 w
<>  for(x=Image1-&gt;Width;x&gt;=0;x--)% H  ^! O. f' W
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)) O1 E! _7 p; k
      {
9 D- A" W! ?1 r$ L        a[r].x=x;3 a" [0 q  n7 _& N8 t, d
        a[r].y=y;
: |8 b3 t! g- O$ X        rb=false;% Y8 O2 J4 I- s; f$ l2 X
        break;
/ v8 c' S1 @+ J' h      }
2 {- A+ J, J7 a  if(rb) a[r]=a[r+1];6 ?# H! k) Z/ Z$ ~4 @2 ]
  r--;: T) T# I! E+ H& s. B, V5 W$ P
}</P>3 e4 v: R$ N: @
<>r=Image1-&gt;Height*2-1;
* i' A5 v9 j" `5 d! _* V( _4 D; L' Rfor(y=0;y&lt;Image1-&gt;Height-1;y++)2 u. _7 n' b. w
{
$ v+ Y! T* S# r* l  for(x=a[y].x;x&lt;=a[r].x;x++)
# d4 U6 B% ~  |3 q/ E    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)- S& ?6 B* [. U4 Y
    {
* }2 U8 C4 C' r0 H* v5 Z      TempRgn=CreateRectRgn(x,y,x+1,y+1);; z5 ]2 A' S7 q* p
      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);3 [9 C- W# D: B$ B9 }
      DeleteObject(TempRgn);3 C8 _: {9 P! K+ g  e% \( H: N+ a
    }. h+ r3 z. c. g2 J# m$ J$ t' o
  r--;
* T. W  }9 A3 e+ _( ^. W1 ?! z}</P>
$ @) M4 `' Q+ x* `- [<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);
4 G1 f( d: L# s( v6 j) q# iTempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);
9 M. P; B9 v9 WCombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
; r. L0 \$ M) v( ?( |/ IDeleteObject(TempRgn);2 [1 n! V' ]& g6 j$ K
delete a;: v7 h9 C# G$ n, e2 k* o& g' r
SetWindowRgn(Handle,WndRgn,true);
, [5 T5 l* m  a. o* Q}</P>
0 D+ Z5 W8 V& Q  t8 A, |% d& W<>void __fastcall TForm1::Open1Click(TObject *Sender)1 j0 T2 T  A3 S% Q) B: K1 _: d; f
{
; c' N1 u' u8 @. F6 A- u- b5 F* J* Y: L  if(OpenPictureDialog1-&gt;Execute())( w- Z. d  Z2 L  ?) P2 b( H
  {
+ J  d7 Y+ S/ U! V' N      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);
) k( ~9 G+ }, z; J0 r      BmpToRgn();9 d$ j$ X6 T5 l2 s# p: o* p8 _
  }
, R0 S3 D- |" b/ R( V}
! _" `) S. M0 t4 ]file://---------------------------------------------------------------------------</P>
% Q  y( n0 @& E. @. G<>对啦,图片的背景色要相同。
4 W% v1 c$ A: c& T图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>
$ u) b! T$ |/ C2 K  g<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------) e; ~0 p& X; `0 O/ u: f# T9 ^
void __fastcall TForm1::FormPaint(省略)
+ b" ~9 D: O# e* y) l{
, S+ V& j; M$ `& v; s5 H    AnsiString ww;int bb;- t+ F! `  Z! E8 p# N7 {" e
    ww=AnsiString(Form1-&gt;Width/256);, t0 h- m; r- }; Q! j
    TRect MyRect;$ I4 i0 J# I. L( a" U6 n+ x
    MyRect.left=0;$ L; j2 `9 ^! |; G# Y" q3 q+ R
    for(bb=0;bb&lt;255;bb++)
$ [$ u% z# G4 X) d3 _    {: C( L. Y# c3 y8 H  [
        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);) K& c" X- a+ A3 h
        MyRect.top=bb*(ww.ToInt());+ D. x6 T- H  c: s; I. d6 Y
        MyRect.right=Form1-&gt;Width;0 E; o7 y8 ]1 N1 e
        MyRect.bottom=(bb+1)*(ww.ToInt());
9 N+ u- w/ _( ?3 q9 o; A        Form1-&gt;Canvas-&gt;FillRect(MyRect);
/ v, \" [6 p! S7 G0 z9 i# z1 I    }& l9 S' ^1 A# m6 a
} </P>
8 b  q* t, E9 y+ o2 o1 W$ \& ]<>file://---------------透明窗体--------------------------' }- u- ]4 w! O& s: Y9 u. n* G
void __fastcall TForm1::Button1Click(TObject *Sender)) P, J, Z1 G# ]% \& {+ M
{
; q4 [. _: @7 k* @, C% M8 f  TRect *rctClient,*rctFrame;9 [! A, \; L+ t  {
  HRGN hClient,hFrame;$ p8 _$ r! ^  B, G: ~) L6 Y) A7 i
  POINT *lpTL,*lpBR;  y" Z  G& j) A
  rctFrame=new TRect;  W  m1 }' Y4 S+ J1 h0 A# C
  rctClient=new TRect;
* Z5 Q/ n9 ^2 {9 ^0 a  lpTL=new POINT;" k( F; H% P) v0 K' B' n7 N
  lpBR=new POINT;8 q7 b7 C6 K/ Z' l
  GetWindowRect(Form1-&gt;Handle,rctFrame);
* I6 E, v. N. H3 P9 n. O! }  ::GetClientRect(Form1-&gt;Handle,rctClient);
/ q3 l% o: s4 w9 }( l) z' o( E8 ^2 @  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;3 q4 e9 W+ {$ @3 @1 f; a
  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>
5 F9 n8 _; T: U4 _! Q0 Z<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);
3 z/ F& H( m2 {# F2 ?+ p  ::ScreenToClient(Form1-&gt;Handle,lpBR);
+ }: D  ~8 H3 Z! Q( `, j2 d. r  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;& c- r* |! b( x4 z" g
  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;
, v! H( J+ d7 K- f  rctClient-&gt;Left=abs(rctFrame-&gt;Left);; {& O# ~/ ~- _( ?' w) h% _& Z
  rctClient-&gt;Top=abs(rctFrame-&gt;Top);& n# }- G+ H1 z8 q9 \
  rctClient-&gt;Right=rctClient-&gt;Right+# b3 Z7 X8 U; k! c. N4 k4 m
                  abs(rctFrame-&gt;Left);
2 s( P1 C: f5 |# H; Z3 K  rctClient-&gt;Bottom=rctClient-&gt;Bottom+
) e* |. ~+ S5 \0 {& ]; L+ k                    abs(rctFrame-&gt;Top);
5 q" K7 ~, x8 T1 N1 j, L  rctFrame-&gt;Right=rctFrame-&gt;Right+/ E; a: f( D4 g  _- z; ^0 Y2 Q1 N
                  abs(rctFrame-&gt;Left);
8 p6 ]: q# _1 _" E9 f  M* T6 y) w  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+  c" M& f  Z8 Z) R
                  abs(rctFrame-&gt;top);
/ H+ U) A: Y. R! `0 |  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>, \3 b* P9 X, s, P) v3 e% o0 I
<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,2 S" |+ @7 a6 u# V2 v
                        rctClient-&gt;Right,rctClient-&gt;Bottom);
+ [- H: p/ W. T% }. E& m  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,
( f/ J4 {* A6 D$ V% e9 V3 ]                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);
3 A; x4 |& T3 A! p* t6 P! t6 Z  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);( y7 v+ M3 \: k. R! f- S% b
  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>
. n$ A- k; ^: ]<>
5 k8 {. [* g5 U* W! o: V0 H  delete rctFrame;
8 E0 y" a7 t, J7 b) Z% w' G  delete rctClient;' i3 u  M6 P, b" |$ ]3 E7 q
  delete lpTL,lpBR;</P>
/ k/ n% F+ U2 I$ ]  s<>} </P>
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2025-5-11 17:40 , Processed in 0.396783 second(s), 51 queries .

回顶部