QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:37 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>以下源码是快速创建不规则窗体的,还可以移动。/ B! b8 t7 c0 i" Z
file://------------------------------------------------------------------------+ E) U7 P! i% l  y* I
<a>file://Unit1.h</A></P>
7 F; E( f. T" z9 T& ^2 Q2 w<>#ifndef Unit1H2 M% u7 v; }  t3 b- ]) K
#define Unit1H$ _: J3 E3 c5 g, o. L
file://------------------------------------------------------------------------
- S) B* D9 Z. P; P" B* D#include &lt;Classes.hpp&gt;: h; ]# V! N4 o$ J1 s- V& E
#include &lt;Controls.hpp&gt;
# s% u  _$ V: n" @- w' S/ P#include &lt;StdCtrls.hpp&gt;6 z, g, T& K/ v* g. o7 {: F9 S
#include &lt;Forms.hpp&gt;: |3 c, o2 [4 n) X# R
#include &lt;ExtCtrls.hpp&gt;; h3 t6 y# `0 w3 R' A7 p- \! h
#include &lt;Graphics.hpp&gt;
" G( h8 x4 Z$ x#include &lt;Menus.hpp&gt;6 a' y& j; U3 a
#include &lt;Dialogs.hpp&gt;' l/ B3 H/ @5 ]. V
#include &lt;ExtDlgs.hpp&gt;" M) Q( X2 Q. U( g
file://---------------------------------------------------------------------------0 o8 e; O. E) F. Z  }, c+ A1 h
class TForm1 : public TForm- L0 {* U5 R9 O- h1 Q
{0 L5 h) _1 q- r. g# @% a
__published:    // IDE-managed Components7 r7 z$ A  a! J, c
        TImage *Image1;
4 n+ S9 @7 x6 o9 m3 U        TPopupMenu *PopupMenu1;
7 m7 h# J0 S% T% k1 U        TMenuItem *Exit1;
, D$ P6 u. f; [% B1 w2 `9 n) y        TOpenPictureDialog *OpenPictureDialog1;
( K4 O2 Y1 E8 x6 o4 x        TMenuItem *Open1;
% l/ l# ~) h3 L- g- E+ C        void __fastcall Exit1Click(TObject *Sender);6 B# [1 W1 N/ ^. Q
        void __fastcall FormCreate(TObject *Sender);+ ^8 v' Q9 E  \# L
        void __fastcall Image1MouseDown(TObject *Sender,; ?  F! o! c& h% X5 Y  S9 p9 E
          TMouseButton Button, TShiftState Shift, int X, int Y);" e" }1 W0 r5 U" G. T! o9 ]& y, B
        void __fastcall Open1Click(TObject *Sender);
3 ~& n# {/ Z; iprivate:    // User declarations
3 z% \4 ~+ d& }$ P3 H% Z        void __fastcall BmpToRgn();
2 J( T$ f1 S' T" k% |public:        // User declarations
; K; P3 k8 ?; r+ s4 Q5 s. y        __fastcall TForm1(TComponent* Owner);
/ Z  R: a( b9 S& H  @8 z};. Y$ {. R7 ^* Z0 }
file://---------------------------------------------------------------------------
, U1 s- q1 u. i; Yextern PACKAGE TForm1 *Form1;
+ @' Z, K6 J1 H- i' ^# |. Ifile://---------------------------------------------------------------------------/ ]! U+ ]! I& H
#endif</P>
& l" E( P# J3 a<>5 x, ^/ a4 \" \. D( p0 F! F! g
<a>file://unit1.cpp</A>
; }* ~5 e" ]4 D+ afile://---------------------------------------------------------------------------</P>
/ S1 @5 E  K+ J3 o<>#include &lt;vcl.h&gt;
9 f  q: y% b2 c6 z: ?+ Y#pragma hdrstop</P>7 e1 }, j) J+ {( G$ t1 n
<>#include "Unit1.h", z2 @# M- a) r. F
file://---------------------------------------------------------------------------
' F* h1 h: E& q4 o) I/ L#pragma package(smart_init)( Q" s5 i; o: N  E. ?: {, M
#pragma resource "*.dfm"
( m' A9 ]. U8 ETForm1 *Form1;' j" i3 p, {; X, w1 m" B
file://---------------------------------------------------------------------------
, P# }: t2 c; j0 K6 U* z  |6 t__fastcall TForm1::TForm1(TComponent* Owner); M0 l+ [6 c. t& F4 g
        : TForm(Owner)) _; Y) |* A- \: H9 |
{
) L7 E; h7 G/ l2 r3 c3 }1 ?}
' ^! x" c$ j- ]1 yfile://---------------------------------------------------------------------------
' @1 T0 q. b& @7 h4 k3 ivoid __fastcall TForm1::Exit1Click(TObject *Sender)
7 [/ F8 z3 C- |+ i7 r, O6 u( }{
' P* v) k2 q/ ^5 ?  Close();7 O8 I8 |. E0 B' l
}7 s' S5 O" Z' @; O
file://---------------------------------------------------------------------------</P>
8 W9 S1 [  {. D) g$ b<>void __fastcall TForm1::FormCreate(TObject *Sender)
- C% n, K0 C) B  \% I: m6 l7 s{</P>4 Y7 b& k6 c, W# T4 K
<>BmpToRgn();
6 Y* C* Q' i0 \$ U" a: [/ r}3 `3 ^  A, E% ^2 o
file://---------------------------------------------------------------------------</P>
9 j. p, m( e- j5 H  |; D<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,
5 d9 ~9 p1 _' c: L/ n) q      TMouseButton Button, TShiftState Shift, int X, int Y)
( \4 w" Z3 d2 c4 {1 X/ P% \{1 ?0 d) R/ k3 H( l* M( a% f
if(Button == mbLeft)
. [, `4 U( v6 j7 I" l  M. P5 M/ O        {2 C4 w2 H) Z( n& R
                ReleaseCapture();9 x2 r5 K8 \1 @2 m$ J, r. E2 ^
                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);
$ o- S, E2 w$ S        }0 E( l5 h. n4 X( I. @% E
}5 p' f2 Y; M0 ?) {2 C4 V
file://---------------------------------------------------------------------------& P+ o3 @* s& J/ R4 s! L  Z$ w2 G
void __fastcall TForm1::BmpToRgn()
/ q. h3 X) v5 _{, E6 a/ g8 b' R, v1 `
Image1-&gt;AutoSize=true;4 _7 R4 X7 @1 D$ Q( a% M7 ^# u
Form1-&gt;AutoSize=true;
( `  R1 o8 P5 @0 RForm1-&gt;BorderStyle=bsNone;7 F. U# J" y  [0 i. K1 F
TColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];7 H2 G7 ~+ Z, z# d% c. u, g! D
int x,y;% L# Q+ @6 K. Z  {! n" R: b  D2 m$ i
int l,r;
6 ], D4 f. ~# y5 hPOINT *a;
+ q1 ^$ x( Y2 ^9 S3 ~bool lb,rb;
9 l; Q0 Q" o& a. L8 q9 gHRGN WndRgn,TempRgn;</P>% B( \; X) W; n7 b- `3 m6 d
<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)
/ C! P. {+ j" c- |{
* I6 k+ x0 W: \7 B5 `% Y; @$ w2 R  ShowMessage("申请内存失败!");
/ E; f  v0 E4 P9 {  V- g  exit(0);( e0 U; ~/ [" H- R# F! b1 Z
}</P>& v9 S/ A) ^% v: G0 b
<>l=0;r=Image1-&gt;Height*2-1;
" ?: m1 G# R$ m( jWndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>0 ?7 A7 D, G7 _) c. M
<>for(y=0;y&lt;Image1-&gt;Height;y++)  R, U) x8 Q2 B) J$ l+ e/ Y- o
{. `8 L: ]7 w0 W. S3 u; j/ F
  lb=true;
3 @, @7 ?0 P$ B6 z* @  for(x=0;x&lt;Image1-&gt;Width+1;x++)9 o: H) H* S9 e% G' ~) ?7 G  m
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)" `$ g1 i9 s, O& Y. A. B
    {, l5 T7 n6 I* `5 g
      a[l].x=x;
+ N+ a1 }( @* x1 e0 i      a[l].y=y;
3 k' W: N: @* g      lb=false;
$ K* X& `2 ^2 Y6 J      break;
- s& F- k  F" T  L: h2 D# l% |/ p+ E    }</P>+ W" P4 _' c8 H
<>  if(lb) a[l]=a[l-1];4 Z4 ~7 a% D# p3 z- ?' C
  l++;
  @# o  A5 A9 }' u& x6 X4 Q  rb=true;</P>
4 P, x( u1 ^) f, T" q<>  for(x=Image1-&gt;Width;x&gt;=0;x--)
5 m$ @6 C4 w" c    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)
( D9 u  Q, {# e! x) Q" i      {
/ T: k2 }1 C. I, B7 f        a[r].x=x;3 l$ b# p' A: o# V
        a[r].y=y;& W+ O4 t+ M" _9 |
        rb=false;
6 F1 l. U/ j! H. x1 o        break;
) _- q& k8 c: d      }
$ W. |1 Z$ O2 L/ x5 j1 a' ?  if(rb) a[r]=a[r+1];
; f4 \0 a" x# R% x2 n1 V2 j  r--;
5 r0 J/ z9 T4 M& ^4 h% b* F- g}</P>
8 |7 e. N4 Z7 N2 n! |& W<>r=Image1-&gt;Height*2-1;+ r! |) i3 `. p+ t
for(y=0;y&lt;Image1-&gt;Height-1;y++), D& a# \3 h" \
{" l/ o4 |# ?2 s( h) h3 {
  for(x=a[y].x;x&lt;=a[r].x;x++)
0 m/ S/ t$ ?5 ]' o0 B/ I    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)7 t2 [. \) c! x- Z+ I
    {4 d9 U! N/ y$ S6 U: m+ I7 y* t! G
      TempRgn=CreateRectRgn(x,y,x+1,y+1);' I% x* m) Q2 h" |0 L/ ^
      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);
0 N; r3 Y1 W2 y! L      DeleteObject(TempRgn);
9 [/ f' w0 v; e. ^" E3 q    }7 G  |/ S1 F/ x; Q# n. [/ X
  r--;( ^) \6 d5 ?7 Z: e: M$ i
}</P>
4 q: ]) H" |) }7 N0 F<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);. R$ k7 ^! u8 n. [8 s; ~  S3 m$ ]
TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);
4 k  ^$ o3 y5 F1 ?' }' {CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
  C9 g3 ^4 r+ C; x) s8 ~DeleteObject(TempRgn);
; Z. m2 {- n. z1 i, d/ Wdelete a;* d3 Y. I( P  d) e# V
SetWindowRgn(Handle,WndRgn,true);" ~* M# R! K9 s4 C! K
}</P>
3 T9 |( O2 {2 L9 x9 ^<>void __fastcall TForm1::Open1Click(TObject *Sender)$ F% j* e* y7 r1 e
{5 e+ x$ v" z/ l; P
  if(OpenPictureDialog1-&gt;Execute())2 [- v! |. b  W7 \7 e
  {( L5 {, u' e0 u+ S9 c
      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);) I& w) I2 j# t+ H& O3 Q" K7 {
      BmpToRgn();1 t. o, ]4 ~% P4 K5 `
  }
7 r1 L/ _0 U) S8 ?5 h}1 G& Q: \9 P$ ~9 p! r
file://---------------------------------------------------------------------------</P>
4 K+ O9 I6 X0 o* L, n( o% L( |7 |<>对啦,图片的背景色要相同。
' B( R  d7 D! r8 J  ~, I图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>' _0 E; `% K! M8 U+ v2 j' Z* j
<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------
% M' G4 c& X8 Kvoid __fastcall TForm1::FormPaint(省略)
* V1 g& @1 ?! z/ ?{" |7 {" R- |  }( r
    AnsiString ww;int bb;
* l: x1 j9 j% J    ww=AnsiString(Form1-&gt;Width/256);( a, S, Z6 O& L: i  N( a* n2 w6 _
    TRect MyRect;
7 Q  _, ^) q2 n/ b, u    MyRect.left=0;, u6 \, x) d" |. p4 Q
    for(bb=0;bb&lt;255;bb++); E4 C' a7 t+ i' D& x. e) @
    {
4 p7 E. m8 A8 W6 t& Q+ C        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);9 u  U! p, [8 N3 X& g2 A) ?/ k
        MyRect.top=bb*(ww.ToInt());9 x' B! d( H9 ^: ]0 v. F4 p% a
        MyRect.right=Form1-&gt;Width;
6 r0 `* J& |& ]# P        MyRect.bottom=(bb+1)*(ww.ToInt());
6 a$ [5 v! g) w( ]6 v        Form1-&gt;Canvas-&gt;FillRect(MyRect);' }" N1 w/ q8 R
    }
  I' Q6 {! S% e) n$ G/ y& J3 L/ y} </P>: r, T" ~" j9 c7 K$ t8 f- O# H
<>file://---------------透明窗体--------------------------9 s1 V5 ~3 f1 G" U- a
void __fastcall TForm1::Button1Click(TObject *Sender)
+ V6 D  K+ ]! U  c) b1 K4 J3 p' m{
" y( G4 b7 ?( I" ?& `& ~  TRect *rctClient,*rctFrame;
: C/ z" \' S9 R* F  s, |2 j  HRGN hClient,hFrame;' F: }7 u0 L: G8 r5 W
  POINT *lpTL,*lpBR;. t2 J# n9 K" b) ], w
  rctFrame=new TRect;4 q' c/ I- A0 Q: U6 o
  rctClient=new TRect;
/ M  F( A8 g; E; r0 a/ o- [9 R0 w# `& l3 a  lpTL=new POINT;. q8 V2 v( {2 F8 B9 ?( l
  lpBR=new POINT;
6 j$ P+ R  G% S5 n! J8 A  GetWindowRect(Form1-&gt;Handle,rctFrame);# i0 c: Y+ R5 {3 ^
  ::GetClientRect(Form1-&gt;Handle,rctClient);4 @  S2 w9 F8 M
  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;* J# n( E/ W) \
  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>
1 k9 n' \: F9 H2 f* M5 [<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);; K4 _4 K. C% [
  ::ScreenToClient(Form1-&gt;Handle,lpBR);
& ^5 E* T( n$ U$ |* _) `  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;
* h( C1 r( N2 k; B" d% Z  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;3 I2 G. B: u, |* ~1 p& P+ I
  rctClient-&gt;Left=abs(rctFrame-&gt;Left);9 f% \, ]% ?+ W! i" P/ d
  rctClient-&gt;Top=abs(rctFrame-&gt;Top);5 S. K* @  ~* b+ t8 X
  rctClient-&gt;Right=rctClient-&gt;Right+
3 r0 }6 E6 \% x9 X$ K5 O, F& Q: e4 d                  abs(rctFrame-&gt;Left);
; ?5 ]! s! A( X) O  rctClient-&gt;Bottom=rctClient-&gt;Bottom+: C( _1 u: t1 M' W8 [
                    abs(rctFrame-&gt;Top);
& L8 m; c9 Z, r& G5 r8 j  rctFrame-&gt;Right=rctFrame-&gt;Right+
) I5 P: X. y& r& d9 O1 S                  abs(rctFrame-&gt;Left);
. o* C+ P3 V# r, z) B1 F  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+
$ y7 V6 b( P- A, W: ^                  abs(rctFrame-&gt;top);, J$ |- P* ^6 F! g$ l$ ?
  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>
8 z/ Z5 d2 H9 y8 ^  B! |<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,% O6 R' C* [/ c+ F
                        rctClient-&gt;Right,rctClient-&gt;Bottom);# O! ]' B% t: H9 q
  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,2 B3 E3 ^1 d6 Y
                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);9 z) _( g$ l$ X. n4 X) h! `0 h
  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);
$ ~# }- p# V% w4 L9 c+ c  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>% X8 j& f: z$ P' j
<>
. d' ^7 o/ Y" n: {( G; a  delete rctFrame;  V/ L5 }: s* l! N
  delete rctClient;
- `' W2 C. s' \1 x) }  delete lpTL,lpBR;</P>
, Q& L3 W. {2 L! U& b<>} </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-5-26 04:15 , Processed in 0.293098 second(s), 52 queries .

回顶部