QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:37 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
<>以下源码是快速创建不规则窗体的,还可以移动。& n. k2 j3 K: f) {$ k
file://------------------------------------------------------------------------
; _& D8 i* W2 G8 v<a>file://Unit1.h</A></P>4 W8 S7 ]2 T( m  l% d/ K+ V
<>#ifndef Unit1H
6 c% o+ @6 {: J: Z: w#define Unit1H
2 {2 f3 o: N" _2 W% afile://------------------------------------------------------------------------
0 U9 P6 A% u! C. p* y' e#include &lt;Classes.hpp&gt;
7 t- K' M. s7 a#include &lt;Controls.hpp&gt;9 z) r1 _& v) e
#include &lt;StdCtrls.hpp&gt;6 g& s! g6 L; j. p7 [
#include &lt;Forms.hpp&gt;
# s* h, G4 q$ m3 p#include &lt;ExtCtrls.hpp&gt;' @, o5 g7 v) |, I
#include &lt;Graphics.hpp&gt;, }3 `9 e1 K' p
#include &lt;Menus.hpp&gt;- u" d# u3 `, u
#include &lt;Dialogs.hpp&gt;
# n' @9 T& }: D* s" H#include &lt;ExtDlgs.hpp&gt;
/ Z: _; q5 c$ ]4 B, ?6 D6 dfile://---------------------------------------------------------------------------
1 S0 }4 {! J7 K2 h) n9 P8 ~class TForm1 : public TForm
% F8 a" y9 I9 B6 e{9 c+ P: Z$ O) M: a/ ^
__published:    // IDE-managed Components
+ U; }% o% D' C" n  t- h( Y: E! D        TImage *Image1;! x3 ^8 c3 D$ P* N5 l) S$ n
        TPopupMenu *PopupMenu1;3 i* M# ?$ u9 d- U* \
        TMenuItem *Exit1;* j: r$ X! e& J' d6 z$ T
        TOpenPictureDialog *OpenPictureDialog1;5 s! M# m9 q% K
        TMenuItem *Open1;
) q6 [2 U* `) \+ B5 Q2 F0 F        void __fastcall Exit1Click(TObject *Sender);
% w" Z0 a# a! W% {( ]5 Q" v2 n' `* }        void __fastcall FormCreate(TObject *Sender);
+ D5 B* z5 g4 C        void __fastcall Image1MouseDown(TObject *Sender,4 g+ j. f- M! u% O' y
          TMouseButton Button, TShiftState Shift, int X, int Y);
5 W4 B9 w2 G1 p# n- [: _3 N        void __fastcall Open1Click(TObject *Sender);0 h1 _( b, _+ h! A* }4 T
private:    // User declarations
% `1 N* V, Z5 a        void __fastcall BmpToRgn();
" Q+ H6 h  e7 O/ v7 kpublic:        // User declarations! ~0 }% ^, z* v) i! u
        __fastcall TForm1(TComponent* Owner);) M% d; D5 A. J& n4 F
};/ p1 @% c+ ~: ?: L2 I: o- \
file://---------------------------------------------------------------------------/ G7 F0 q5 J5 @2 W- h9 Y
extern PACKAGE TForm1 *Form1;
0 K0 b  I, Z  u; {file://---------------------------------------------------------------------------
) g8 `0 n$ F; ^1 G. g9 |#endif</P>
9 u1 R5 P0 ?' ^- L# }<>8 M. N8 o3 ]( r
<a>file://unit1.cpp</A>- P; V( ?" T* n" M7 K+ Y0 q
file://---------------------------------------------------------------------------</P>
7 ^# I$ Q* `( ^2 M<>#include &lt;vcl.h&gt;
% g& M2 E+ ^. Z( |/ e3 v/ o#pragma hdrstop</P>+ r# u* v' f* K* r3 e
<>#include "Unit1.h"
7 n/ x4 g, |) y7 l5 C4 ^file://---------------------------------------------------------------------------
9 s/ B7 o/ I$ N/ W. s' ^) u1 E#pragma package(smart_init)
1 @; y0 V: C3 ^: l9 w: |#pragma resource "*.dfm"9 _# U4 Q/ b; `' e: |
TForm1 *Form1;
# G: p" V4 Y3 M; H6 `0 q% i8 bfile://---------------------------------------------------------------------------6 b: c; ?& ?& h4 d5 B( c
__fastcall TForm1::TForm1(TComponent* Owner)
7 J1 m8 r$ K. p        : TForm(Owner)
( q' z' L$ b5 \{
9 r2 y' V  f8 H}
0 K" f+ \: u: Zfile://---------------------------------------------------------------------------
% f& @2 _  t0 g# P% q8 ?void __fastcall TForm1::Exit1Click(TObject *Sender)4 j% W# _; d' M; ?+ g
{0 v: W! ?9 T( l7 s  s' J/ |
  Close();
6 X8 S" @* x! i3 e}
; Y- D- p. t' L+ H9 F( f& x* Jfile://---------------------------------------------------------------------------</P>8 o/ a7 H5 [" w2 v. o
<>void __fastcall TForm1::FormCreate(TObject *Sender)8 N* D- K. |( g3 l
{</P>
6 i  N3 I6 T0 T* P) D<>BmpToRgn();/ t" w- J/ S2 K& ~8 N- T0 m
}! W1 f, ]! ^7 B, X* b% N) q
file://---------------------------------------------------------------------------</P>
1 L* r% {+ `4 M$ T0 T& t<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,0 a! W  x3 w7 I% \& @
      TMouseButton Button, TShiftState Shift, int X, int Y)/ y  i6 M3 D0 ?$ }( |
{
1 Y- x3 {7 i* F& _if(Button == mbLeft)
' X2 n; L5 A5 v" q: }        {
, Z/ y9 [& s/ D) l) o                ReleaseCapture();
$ A  W8 L4 C% Y                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);- C" [& c2 d3 T! c; i
        }5 p- U) y  a) {: E: Z3 U! F
}
( G: S5 @+ T" e$ Gfile://---------------------------------------------------------------------------
/ J4 ~# r  F+ ~* i+ i* ], `void __fastcall TForm1::BmpToRgn()4 j' U/ a; ?, X' t: v
{
$ g* W$ b* [( V' Z) r. \3 p1 sImage1-&gt;AutoSize=true;$ w  w" Q  y* J0 ]
Form1-&gt;AutoSize=true;
# N5 }7 i! l; D+ m: FForm1-&gt;BorderStyle=bsNone;) r+ q6 ?3 p  _! {5 ?
TColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];
7 Z1 ]( A6 L6 Z0 b% d9 e8 ]! Pint x,y;
* l" O% X% Z+ I) Vint l,r;
; \; d+ D8 K  X, PPOINT *a;: k5 ^: x: }9 J8 J
bool lb,rb;0 [  O7 C3 k4 d) j5 G9 a; O
HRGN WndRgn,TempRgn;</P>+ W; @% z# L# \9 x  \, p7 v
<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)( n2 a$ ]/ @! t9 h: Z$ v
{0 j3 v4 \+ h# a
  ShowMessage("申请内存失败!");% c; V+ S8 R# a  U4 N0 [
  exit(0);
$ O/ K7 a1 D* e3 p- w% i- U}</P>) i, X) V6 u! f' t3 L- x
<>l=0;r=Image1-&gt;Height*2-1;
  ]5 i# \# P- z# I8 KWndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>9 _6 B" }5 M9 U5 J
<>for(y=0;y&lt;Image1-&gt;Height;y++)2 t; A/ }4 E, D
{, q  e, _7 a1 H" p  f1 b  ]
  lb=true;
1 |( q4 d: H: D+ q7 n4 Y  for(x=0;x&lt;Image1-&gt;Width+1;x++)
7 b# U4 P; p" Z4 Q    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)8 f3 D/ F  y0 e' K& c8 x6 |1 d
    {5 v6 _8 X, X( v' U7 r# t
      a[l].x=x;
7 w% q, G0 a) ?) o' I      a[l].y=y;7 D; ^% V" U3 J# Z
      lb=false;' y! r( g8 h( a7 k. x
      break;9 O0 e9 c) i& f8 o, g
    }</P>+ n; [- r0 L# r. ~7 V4 B
<>  if(lb) a[l]=a[l-1];
& Q7 P! l0 M9 b. e+ p# t  l++;$ d+ a$ G; A+ D  B$ W
  rb=true;</P>1 x, y) |$ {1 h: K$ c
<>  for(x=Image1-&gt;Width;x&gt;=0;x--)7 Y$ L* D% a1 x6 l* _* B6 q
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)/ F5 F* Q$ r* U1 b( d/ w
      {& F7 v, z2 j" X" O! I( ]
        a[r].x=x;" o8 I( I+ w5 c- I
        a[r].y=y;
1 J3 q; M8 p! \1 y9 ]4 ~  Z0 N        rb=false;1 M7 i5 k1 p: r" k
        break;. g; L3 f4 j: k4 Y1 \( O" O
      }( a: N* Y9 v8 B! c+ `$ O3 f
  if(rb) a[r]=a[r+1];$ P1 r9 A9 C! M0 V
  r--;
! v1 u2 @& V. u+ I}</P>1 R% c0 G& r4 J. A# _
<>r=Image1-&gt;Height*2-1;& @7 x) X. P( b; }  X  b2 r
for(y=0;y&lt;Image1-&gt;Height-1;y++)  O+ H4 e, @3 D/ [! Q5 Z# c3 A' ?
{
0 e9 r9 _1 f( X  for(x=a[y].x;x&lt;=a[r].x;x++)4 Q4 T/ E" R5 x7 N6 P8 |# H( ^$ \
    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)- [; H/ I, W5 V: [) q
    {( c3 K- h6 L* {
      TempRgn=CreateRectRgn(x,y,x+1,y+1);( Z5 P* A* m" P) _' i+ a% S* t
      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);
: x) a) ?5 M4 }% y      DeleteObject(TempRgn);- t& G* g2 s- C  @
    }/ v; A% t* L/ Q# w2 B/ [
  r--;( k# Q6 Y& _( \$ v
}</P>& M+ e& q% J) y7 o, _
<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);
/ L) y4 a1 r: u- a* X+ uTempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);7 c; k, `! Y) U
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
4 P' }6 W& O0 o1 l$ wDeleteObject(TempRgn);* F4 Z4 s! Q2 U) h1 S7 K/ K2 r1 q
delete a;
9 R' l  p7 K1 J) H5 n- n/ l" iSetWindowRgn(Handle,WndRgn,true);. L! m4 M* \+ \4 G9 z& F$ G
}</P>$ E* {+ E5 W: }) k+ P: {9 v! Y
<>void __fastcall TForm1::Open1Click(TObject *Sender)
* \! s: M' {& l: b3 U5 Q{2 ?) M' _2 q# }# N0 j
  if(OpenPictureDialog1-&gt;Execute())
* V- ~+ Y  c' B1 _) u  {% k. G  E4 d9 w! F2 L
      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);
8 s5 X* X$ i8 U7 [      BmpToRgn();
6 W) a# t* ^- B  }; [) e  B+ J% P, `
}
" h/ z4 K2 Q1 h5 rfile://---------------------------------------------------------------------------</P>
. W+ G7 o; V7 J2 q! m+ Q<>对啦,图片的背景色要相同。* R4 T, t3 R; m
图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>
+ i4 N' y) S3 `. n7 Q0 O<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------% D2 ^# V0 C$ x$ p; P$ Z
void __fastcall TForm1::FormPaint(省略)
4 }' n9 R* \% r$ j5 A$ n6 s{
6 f* U5 c% H- k: z1 |; V0 |0 k    AnsiString ww;int bb;
3 Q+ S6 R: j, _; M7 @, r    ww=AnsiString(Form1-&gt;Width/256);  _, j7 P& n& }# w& D; B
    TRect MyRect;" `! f% R# Z; w
    MyRect.left=0;! ?5 l- e2 P% G7 C( d
    for(bb=0;bb&lt;255;bb++). t$ Y% l8 ^3 O2 l4 E# w
    {! D3 Z; i+ a9 G
        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);
( I6 `' L: m/ p        MyRect.top=bb*(ww.ToInt());
7 V- h( J. u/ I5 N5 F' l        MyRect.right=Form1-&gt;Width;/ ~5 ~$ j' Q1 @* U' @4 O+ Q
        MyRect.bottom=(bb+1)*(ww.ToInt());7 l8 F' G& e& O9 Z1 Z- _, j
        Form1-&gt;Canvas-&gt;FillRect(MyRect);
* x; n8 K* O3 K    }
' I7 b% u' ?! {) S. p* j} </P>8 F9 U- h  O( m5 [. J" {/ L
<>file://---------------透明窗体--------------------------
3 e1 M4 p$ r/ e8 Ovoid __fastcall TForm1::Button1Click(TObject *Sender); N8 y' H  _+ p7 T( ]5 V
{
" ~9 }4 W3 C: J; W  TRect *rctClient,*rctFrame;
+ R/ F5 N+ W2 T$ }% d* J' t  HRGN hClient,hFrame;" I+ ?  z9 I8 k* @% r
  POINT *lpTL,*lpBR;
8 }- f% u! o$ V1 e- W% ~4 w) l  rctFrame=new TRect;
  n1 u9 N& G2 p, J  rctClient=new TRect;2 f# e: D3 J* J8 m% U8 U
  lpTL=new POINT;
! ^$ U: [$ G! e# r5 t  lpBR=new POINT;
  M+ C# }. t; ^6 ^- d* ^) q4 p  GetWindowRect(Form1-&gt;Handle,rctFrame);7 E* q  ^, f" Q0 e, P) k
  ::GetClientRect(Form1-&gt;Handle,rctClient);8 i$ a2 S0 z3 h1 J' n3 X
  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;
# O0 Z1 X' P: Y2 |1 ]) g  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>
& o0 r, F' Z1 J- Y. U/ }. J<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);5 Y: x4 W9 ^( V$ ~0 Q# L
  ::ScreenToClient(Form1-&gt;Handle,lpBR);
. s, ?8 K: v" o8 Y  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;
! h0 M9 Q6 e1 N. [* _  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;
9 V3 ~4 Z. u4 G1 l4 |/ P# X8 n  rctClient-&gt;Left=abs(rctFrame-&gt;Left);' a. `! ~5 {5 Y( K4 g0 }% u
  rctClient-&gt;Top=abs(rctFrame-&gt;Top);0 b* M5 I) B. s3 Z- D" B9 F' k
  rctClient-&gt;Right=rctClient-&gt;Right+
9 |+ W, M) \! c/ L! V( n                  abs(rctFrame-&gt;Left);: }- n8 `$ W/ O$ v$ m6 \' L1 q7 P9 b
  rctClient-&gt;Bottom=rctClient-&gt;Bottom+3 f0 Q- F1 t/ {7 [
                    abs(rctFrame-&gt;Top);
4 C, V' ?) t, X0 ^# g  rctFrame-&gt;Right=rctFrame-&gt;Right+( C# X; W3 W- `1 E; A
                  abs(rctFrame-&gt;Left);
) M" d- U' Q# R0 I  D  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+  ~( Q5 O$ q; U) G7 L1 e; ?
                  abs(rctFrame-&gt;top);. m- w5 Y+ ~. v& ~( G
  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>
) P8 ?, u7 X, A& A/ N+ o' Q1 I<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,# r: f! G- ^0 o$ ^& S6 d
                        rctClient-&gt;Right,rctClient-&gt;Bottom);0 b+ S, y4 U6 T; s) {1 X
  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,# j3 m, P3 m0 [' q7 G# f, J9 {
                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);
" Z) V  `9 o  F% o! n  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);: a/ P# d: ?* p( y6 ^- i
  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>! L2 K! V3 u$ g" U' A
<>
2 I2 d7 i' {" W4 V  v0 j% t  delete rctFrame;
+ C6 t6 l5 s+ Q0 f  }" m2 h: Q! ]: r  delete rctClient;, s( l$ N  j8 A; O' m' W2 T7 d+ u
  delete lpTL,lpBR;</P>8 v. E( c4 e$ M: H7 L
<>} </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-7-25 23:39 , Processed in 0.347801 second(s), 52 queries .

回顶部