QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:37 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>以下源码是快速创建不规则窗体的,还可以移动。
8 s( w: e: y* q3 i1 V: S+ mfile://------------------------------------------------------------------------
* n2 x% w! x& n! s$ o  X! E<a>file://Unit1.h</A></P>9 K- X6 |$ D$ A; {2 _/ G
<>#ifndef Unit1H8 e5 S& c6 `5 }/ J
#define Unit1H
" s: g6 e( W2 ^# D- z( Nfile://------------------------------------------------------------------------: v+ J- i) b1 g$ G
#include &lt;Classes.hpp&gt;
5 m* d/ ^/ M1 n8 I#include &lt;Controls.hpp&gt;
0 D- Y; j5 W$ a* l#include &lt;StdCtrls.hpp&gt;. d) K+ s& g0 e/ g
#include &lt;Forms.hpp&gt;* }' @! x& ^7 J( L6 F
#include &lt;ExtCtrls.hpp&gt;0 r, F  c& V! g3 R" ?5 s' Z" n
#include &lt;Graphics.hpp&gt;
3 T3 B2 S3 G1 `- R/ `: B! n/ i#include &lt;Menus.hpp&gt;
7 x9 [' f6 `' t) w! S#include &lt;Dialogs.hpp&gt;; v- g" T& C$ p' t
#include &lt;ExtDlgs.hpp&gt;
$ I4 T$ k+ X0 u; Lfile://---------------------------------------------------------------------------9 c4 t/ A# s1 `# @6 L
class TForm1 : public TForm2 I+ q! I; d8 q: l
{( f& a% w2 r4 e/ ~4 R
__published:    // IDE-managed Components7 N( T& _% ~9 s' k% y& L
        TImage *Image1;  @) s) K; n) y6 {+ K" d4 x
        TPopupMenu *PopupMenu1;! g/ L- I+ O7 x6 P  b' o
        TMenuItem *Exit1;
4 x( R5 e" o5 R/ ]2 \& c, r, R% |        TOpenPictureDialog *OpenPictureDialog1;) Y: j* |( g8 H. B  {
        TMenuItem *Open1;! _+ s' ^) c; C+ J; J  u+ m
        void __fastcall Exit1Click(TObject *Sender);: e- K/ \9 x% e# N- q
        void __fastcall FormCreate(TObject *Sender);
. h0 c! z9 F6 f, n8 |        void __fastcall Image1MouseDown(TObject *Sender,% p& V7 V4 U& ~, s1 L
          TMouseButton Button, TShiftState Shift, int X, int Y);& v6 m5 x' ~: Y
        void __fastcall Open1Click(TObject *Sender);
: k2 e6 S/ n* j/ n6 tprivate:    // User declarations( y) Z/ H+ ?- B+ {% \' w9 d, q
        void __fastcall BmpToRgn();7 y$ _* a9 o9 Z8 _0 c( @. a
public:        // User declarations
& W( ~' f- o" v$ ^! X2 V        __fastcall TForm1(TComponent* Owner);
/ M6 O5 [3 V. F3 M, [};* p3 n& N- e0 T: T2 l$ ?
file://---------------------------------------------------------------------------- ^" d& C" o& C  ^, B4 `6 T6 Y! w6 n
extern PACKAGE TForm1 *Form1;
! E2 f! M8 }% i) e& F* [3 y5 Ofile://---------------------------------------------------------------------------9 v, q) i' v' O: t" W1 p6 r
#endif</P>( {+ P0 F5 Z/ V/ `. h* E4 p! E0 b: s
<>0 H9 J0 b$ y7 o% K
<a>file://unit1.cpp</A>
+ h; T  j2 K# ^) p1 w1 E: _& Ffile://---------------------------------------------------------------------------</P>
! B- k6 f# k) t8 g<>#include &lt;vcl.h&gt;) ]2 G1 B+ X9 m) k% M; \" |7 n
#pragma hdrstop</P>
, p# U0 f8 @% ^$ D/ D  B% g: b<>#include "Unit1.h"8 D4 S& l( ^4 S  \$ r2 ^5 k
file://---------------------------------------------------------------------------
3 [# u( ~* q4 H* G, \#pragma package(smart_init)
( T0 L5 D8 i: [; \#pragma resource "*.dfm"
- ]: q" J; [( r' i* M) U- A7 fTForm1 *Form1;/ l3 ^& g3 w6 W4 ]
file://---------------------------------------------------------------------------. e5 Y% s* v8 s6 B
__fastcall TForm1::TForm1(TComponent* Owner)
7 a9 q2 f! c9 b! o) J9 A        : TForm(Owner)) w- J  Y& Y2 v1 O% [
{1 ~* B# |- w; g0 W1 E) e* f
}
0 p! N6 w: a+ Sfile://---------------------------------------------------------------------------
: h$ u# R! f, ?9 ?  S" H/ n5 Yvoid __fastcall TForm1::Exit1Click(TObject *Sender)
  ]) G/ D3 y+ }6 i2 d{) {7 z/ h' d9 K
  Close();
: I: z, m6 I4 k0 ]9 ]}
' N4 B+ `( y3 c# tfile://---------------------------------------------------------------------------</P>- r0 R% N0 a% B
<>void __fastcall TForm1::FormCreate(TObject *Sender)& ^' N% j7 o- j' z1 g% X
{</P>
: I& m7 p* t; f. `6 M<>BmpToRgn();) R$ L9 a7 F$ z; {) O/ N" w0 j: M
}
( o9 u: a! p1 tfile://---------------------------------------------------------------------------</P>
8 }, m8 v) G7 \3 L# }2 l7 X. k" p<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,  F" R# v( h/ g+ }* B3 a: m
      TMouseButton Button, TShiftState Shift, int X, int Y)
( ], c' \8 @  F. m{1 z9 b' k4 r6 A! m
if(Button == mbLeft). P5 C9 h: ?4 z# H
        {
( [3 N  n. B! p5 k6 d                ReleaseCapture();
9 l' r. a. L) j4 _                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);: p7 Y3 C5 e& c! ~2 A% d4 h% O/ `
        }4 X  H" A, ]& {5 r
}
  o; ?9 l6 X& o  q/ xfile://---------------------------------------------------------------------------
# j; ?  K: k" {void __fastcall TForm1::BmpToRgn()  s- e$ O; J9 d* `. o* L( {, L& t
{
1 _' Q( }; |% `4 J; gImage1-&gt;AutoSize=true;
+ C0 K; c5 F6 |% K; T6 U6 YForm1-&gt;AutoSize=true;
9 x% @4 ]  j( F. x2 k6 R5 y7 _Form1-&gt;BorderStyle=bsNone;
. a. V3 I" Z/ U- yTColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];
: H" J" O  {/ I& `/ J1 x/ m- cint x,y;# R* y% X* q. w' h  z, ~  L# E* ?
int l,r;
; }# B. _: V1 j8 zPOINT *a;
. Q3 d  t! m5 Q9 T* \" qbool lb,rb;
5 R0 Y% H+ c' F1 u! ~; t" C7 \HRGN WndRgn,TempRgn;</P>9 H0 A" P6 l# y
<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL), X- [* ^" S6 O  s5 o
{# r2 ^% n5 X' M, c
  ShowMessage("申请内存失败!");" Z1 H" M  T) d6 c- B/ y% ^: x
  exit(0);" j. d/ Q7 c; Y  N
}</P>. y+ X# ^8 P; l$ Z) l7 L
<>l=0;r=Image1-&gt;Height*2-1;
& t( T( r: N# {4 R" pWndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>& U7 ?6 \. A! D
<>for(y=0;y&lt;Image1-&gt;Height;y++)
4 d; E. S6 t: x; t. J" ^{% O4 b* E4 u0 _: h! ~- O
  lb=true;
( Z$ m! k  S0 X; F% U  for(x=0;x&lt;Image1-&gt;Width+1;x++)
* v9 }: ~4 e: |    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)
2 ^, G- f9 d: U2 U' m' b    {
+ e. A+ U7 q$ ]0 X0 t0 a$ r$ e* R      a[l].x=x;
% F( V; C' {- L7 Z2 M) o! k      a[l].y=y;" A! m3 R; V" t6 @" l5 \
      lb=false;! n$ y: S  e6 Q
      break;5 z  ?* R! s" [7 ~5 ~* t" J
    }</P>9 l  j9 ?" V& P# z3 s1 t
<>  if(lb) a[l]=a[l-1];' l% c; c( \8 A* m1 f/ V
  l++;( E" m/ z: w7 D- Y8 U- N) ?+ u
  rb=true;</P>
' ~0 N+ ~0 m  D; K6 u, x, L<>  for(x=Image1-&gt;Width;x&gt;=0;x--)
% L& K' T& [- i8 C, J    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)# Z8 P% x3 k& l$ R7 ~/ |
      {
7 q: q" w1 J' m9 J/ a- g2 `. v        a[r].x=x;
8 i: B9 @1 I! @5 V3 r        a[r].y=y;
, G* K5 {% r5 Q9 G2 |        rb=false;
% r' r7 O& m& H" e        break;* _( y% A. k" ~- N  G
      }  t# l8 A' q6 w4 M# G, a& U
  if(rb) a[r]=a[r+1];4 ^7 Z3 x: \: \1 ~
  r--;8 F2 t$ C$ X5 m
}</P>
+ R  O6 o3 p) W. V9 N0 c<>r=Image1-&gt;Height*2-1;
7 `! y8 [* ?( G& n. G4 ]; ~for(y=0;y&lt;Image1-&gt;Height-1;y++)
, ?+ @% _( p" w0 L$ E9 q0 A{" Y7 p( T4 I* {
  for(x=a[y].x;x&lt;=a[r].x;x++)3 g- g& [: e- k# ]1 \9 k
    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)
% s& t6 k( P: A6 t    {
& ]5 B  r" S# U      TempRgn=CreateRectRgn(x,y,x+1,y+1);
4 c( j$ {9 b, b7 v6 K      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);, l$ M6 c' x$ m# p- @* N1 |
      DeleteObject(TempRgn);) d. ^- E# F/ W& p6 \
    }
0 s' Y' B+ |( m( h$ X6 ^  r--;  M# y9 B2 l$ Q7 m6 f4 ^) X
}</P># s8 Z) b* C: F% p6 \+ a
<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);
* {. f* m2 S" C8 i9 T$ YTempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);& C7 S) r  H3 n: L
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);' h% g4 L# m( [4 {2 L
DeleteObject(TempRgn);
7 G, K5 Y* l) O! @! L2 X2 }. ^# Cdelete a;
5 _/ e5 P, S6 x9 dSetWindowRgn(Handle,WndRgn,true);
3 s, \5 Q; @8 o0 x) ^, ~1 d}</P>8 G, ]/ T9 R" o! n) H4 |0 N; t# k5 a
<>void __fastcall TForm1::Open1Click(TObject *Sender)& g: O4 }2 p" k# K) w
{
/ F0 o7 j( Y  d' K' ^2 B0 }+ o  if(OpenPictureDialog1-&gt;Execute())4 C: M, V7 D! @2 G% X1 B
  {" o0 e- i9 q( `( b6 A6 b
      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);1 D/ E# ]+ V6 W& t
      BmpToRgn();, r& {" ?  ?0 P
  }1 x: h' w9 s0 Q+ P0 G  W
}8 a, K3 n$ |% h/ c3 l
file://---------------------------------------------------------------------------</P>1 l% D2 H3 E; L- N' ]+ [; B( g
<>对啦,图片的背景色要相同。
/ U8 b. {/ }  B" M# ]3 W图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>, D; g& {! j+ A6 h) F- _3 z
<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------
8 \/ ^8 \' w9 U8 D/ Lvoid __fastcall TForm1::FormPaint(省略); l) Q9 o& P: o# J) }/ }
{6 g5 o) s% I8 t+ H* k$ n
    AnsiString ww;int bb;7 [. x( d. d, f' ?& C
    ww=AnsiString(Form1-&gt;Width/256);5 f* c8 c9 e0 i+ l1 P, F
    TRect MyRect;8 P! A' a$ y1 d* w! y
    MyRect.left=0;! }5 ]6 k- w9 c" d' M
    for(bb=0;bb&lt;255;bb++)
) e2 u$ K/ N6 h% r  A0 @+ n5 c& g2 A    {
7 U4 j7 W) q( p  c0 a( L) N        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);
) ~! Q# q9 e; p6 P* K% @& z        MyRect.top=bb*(ww.ToInt());7 K5 a  C' E' W
        MyRect.right=Form1-&gt;Width;
. h. v3 Q* H+ F" b1 H4 z! e* n        MyRect.bottom=(bb+1)*(ww.ToInt());
- v5 _' z, |5 t$ r. c        Form1-&gt;Canvas-&gt;FillRect(MyRect);1 S; A% U; N% E
    }
  S8 I+ \' W9 s# |5 Q% Q} </P>
" ~  _) g8 n3 i! a: J- S/ y  l. v<>file://---------------透明窗体--------------------------
) |/ x/ ]" |4 c2 Wvoid __fastcall TForm1::Button1Click(TObject *Sender)7 e4 s6 f% q1 R* a; C) [( C
{$ X: ?" ]3 y+ L; [% W! n
  TRect *rctClient,*rctFrame;) A  Q. I7 ?3 l$ [, m
  HRGN hClient,hFrame;
# @; p9 g  \5 M6 R5 Z; M  POINT *lpTL,*lpBR;& H$ r& x8 M% K* N6 c0 a! U
  rctFrame=new TRect;
0 o9 F8 h0 J9 C  A  rctClient=new TRect;  N" O9 c+ H3 l
  lpTL=new POINT;2 C, w% Z! [9 X! ~1 O
  lpBR=new POINT;$ ^0 d# v* F: A' ^  ^9 [6 E
  GetWindowRect(Form1-&gt;Handle,rctFrame);
( p% K8 Z* X! c+ \0 @7 n  ::GetClientRect(Form1-&gt;Handle,rctClient);( @$ ^: e' S& R" h; g
  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;' ^* p) x) E% R) v4 D
  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>
* h$ ~- |  j4 Y/ \7 C# X<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);
" f' w4 ^+ a% x# K! x/ K4 q/ x  ::ScreenToClient(Form1-&gt;Handle,lpBR);
9 R+ x, _3 N9 e/ N  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;
& O8 w9 k  H; t; n- J+ m" d. D% e+ g  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;
6 r% f' X, _% Z9 n& q  rctClient-&gt;Left=abs(rctFrame-&gt;Left);7 [$ T4 G! D* A$ q5 J7 K
  rctClient-&gt;Top=abs(rctFrame-&gt;Top);
2 a; _3 c9 {2 g9 \! R  rctClient-&gt;Right=rctClient-&gt;Right+! X% ]" y* W! J  {
                  abs(rctFrame-&gt;Left);
- p0 n8 d$ r# p4 s! c: V2 u  rctClient-&gt;Bottom=rctClient-&gt;Bottom+
) N6 N' l6 x; {5 ~% O$ J# n2 Y                    abs(rctFrame-&gt;Top);% q; N/ k( C7 G8 x- |
  rctFrame-&gt;Right=rctFrame-&gt;Right+. C5 n' |3 d) ?2 w# q) j; s
                  abs(rctFrame-&gt;Left);1 E& H7 W0 T! s: ~  P
  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+
' k4 V* O7 U- F, `+ Q* z; z9 \2 C                  abs(rctFrame-&gt;top);9 l  d$ J8 S1 ]; A# x$ X8 y' A% i0 I
  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>0 S7 p5 _( ]$ l1 K8 P7 R
<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,
( v" ?5 u7 g4 [& J4 F9 W5 T8 F                        rctClient-&gt;Right,rctClient-&gt;Bottom);
8 q7 `$ Z5 V* P  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,
. q/ r" L# o% y$ Q# p                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);2 P, P# K6 `: `: B3 }$ W$ J1 X
  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);  |+ y+ w8 O3 l  V3 b$ [
  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>
: j* x  m/ o8 U) n! |7 N. ~<>
7 J1 ?$ F. O2 @% s1 G: J  delete rctFrame;
; t8 B" m* x  V) N7 `  delete rctClient;
, e$ Q% U5 n" V$ g+ ?9 B, e  H( T  delete lpTL,lpBR;</P>
) w; G; i$ e+ z2 C& u! n<>} </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, 2026-4-20 21:25 , Processed in 0.423161 second(s), 52 queries .

回顶部