QQ登录

只需要一步,快速开始

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

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

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

823

主题

3

听众

4048

积分

我的地盘我做主

该用户从未签到

发帖功臣 元老勋章

跳转到指定楼层
1#
发表于 2005-1-26 01:37 |只看该作者 |正序浏览
|招呼Ta 关注Ta
<>以下源码是快速创建不规则窗体的,还可以移动。3 Q7 J( m* g2 y
file://------------------------------------------------------------------------. X/ x* |' g( \$ t. e
<a>file://Unit1.h</A></P>. \2 v! B; j+ o5 r3 t* W
<>#ifndef Unit1H1 e+ N- S  J1 `% b# T* L
#define Unit1H
: I' f  S6 l. ^" pfile://------------------------------------------------------------------------
! b1 E  P6 v* r- ?#include &lt;Classes.hpp&gt;
; ?+ M" h% N$ N+ g# T. w#include &lt;Controls.hpp&gt;
& @  l# e: N% c/ W% t+ E#include &lt;StdCtrls.hpp&gt;6 ?2 `1 h( R2 i  p5 W& m
#include &lt;Forms.hpp&gt;: c+ I9 a4 X, T  `
#include &lt;ExtCtrls.hpp&gt;
  o9 D: ^) ~$ [/ q4 s( b#include &lt;Graphics.hpp&gt;
' O8 C8 u' {+ o9 K#include &lt;Menus.hpp&gt;8 O6 `9 d' x, ~
#include &lt;Dialogs.hpp&gt;& F7 ~) Q1 R0 I0 n
#include &lt;ExtDlgs.hpp&gt;/ f) P7 W# b4 R& d, W
file://---------------------------------------------------------------------------- `/ H$ P3 l2 C# }! S, f& J
class TForm1 : public TForm0 k* g# V5 c) M/ x3 }& ~: _
{9 M& ?3 o/ u, d
__published:    // IDE-managed Components6 W* S0 {$ k2 ^6 w$ s' _
        TImage *Image1;6 |$ k, T- S4 ]3 V4 T- ?, _* P
        TPopupMenu *PopupMenu1;6 `9 k( L& @0 O0 E% g
        TMenuItem *Exit1;8 \) x/ O" Q/ j$ B9 k5 O
        TOpenPictureDialog *OpenPictureDialog1;
! [6 o6 w2 @  M8 s9 C0 H, M- D. w& Z        TMenuItem *Open1;* U8 d+ b. }: p* v' B
        void __fastcall Exit1Click(TObject *Sender);2 R% W0 T; C: c9 h5 I% M/ @7 Y/ R
        void __fastcall FormCreate(TObject *Sender);
, @; @- X5 H- A, P' t" _        void __fastcall Image1MouseDown(TObject *Sender,& l' f/ H8 ~: q! E. v; s
          TMouseButton Button, TShiftState Shift, int X, int Y);, o4 f9 H  L3 ]1 ?8 I! T. v4 L
        void __fastcall Open1Click(TObject *Sender);5 u" r1 A- Q+ ~$ O* K/ e9 u2 u
private:    // User declarations& p; l& {& Z9 p- d6 V7 _& G
        void __fastcall BmpToRgn();
- z* I, K; g: J' S9 ~$ t* Xpublic:        // User declarations
' B6 N# j4 L& K& l7 s  l( D. K: A        __fastcall TForm1(TComponent* Owner);( C7 @3 b  Y/ L* N& s! C. `! }
};
# d& S: w3 \3 _0 d7 P; Ffile://---------------------------------------------------------------------------
7 y: i+ f5 F# Y+ q/ m( Oextern PACKAGE TForm1 *Form1;3 W: A, I$ I; A( g. r' h3 @
file://---------------------------------------------------------------------------
% b5 N- g8 L+ O& i# k#endif</P>
$ ?8 _3 M1 y  u' e+ C<>4 X2 I1 U: k5 }: q: K
<a>file://unit1.cpp</A>( `6 _7 N- Z0 d. y8 ^7 z
file://---------------------------------------------------------------------------</P>
/ R1 @1 q; M2 M: x7 @<>#include &lt;vcl.h&gt;" N& C% E8 k& n
#pragma hdrstop</P>
8 T( e( J# I- G( u! h<>#include "Unit1.h"
7 E* E8 M: H0 p7 p% H, Y$ C2 H0 x$ kfile://---------------------------------------------------------------------------
/ p, v% G# r9 g1 D8 B#pragma package(smart_init)) p* ]/ h9 i2 {7 o  I
#pragma resource "*.dfm") q5 \8 d! u3 u. d
TForm1 *Form1;
/ L& T( k: v9 u0 ?$ f3 u- sfile://---------------------------------------------------------------------------# Q$ K) S# T  Q* e2 y
__fastcall TForm1::TForm1(TComponent* Owner)
3 Z2 k% M4 |% D        : TForm(Owner). z4 F2 L$ T/ u. {9 r! e; y
{
* h, o0 k  u$ F- W}
1 D6 T% E- t! r: ]( H: p  K  Pfile://---------------------------------------------------------------------------6 ]0 x  W, n/ ^' p; q. r8 r
void __fastcall TForm1::Exit1Click(TObject *Sender)
6 e( ]+ t7 O0 M4 z6 C{
8 N) g/ k& T+ x; U8 K9 d( N  Close();
0 X* `7 H4 y. S4 H}
7 j% J6 X' L) z3 w& Pfile://---------------------------------------------------------------------------</P>
, \9 W7 x. V% \0 W. H5 f) H<>void __fastcall TForm1::FormCreate(TObject *Sender)
, @# n2 r3 J3 x0 U' f{</P>1 |$ q5 G5 ]! W- Q! [) m
<>BmpToRgn();8 G) b1 B( G- @( N8 V
}1 w0 i, Y0 M' B! R  I' }) L
file://---------------------------------------------------------------------------</P>+ |: X9 d8 }" y! n- C5 p( j/ W6 k
<>void __fastcall TForm1::Image1MouseDown(TObject *Sender,
4 P+ J  {7 M  y$ D% ^, ?2 T2 Y      TMouseButton Button, TShiftState Shift, int X, int Y)
. c+ m4 q% @1 }( B6 h{
, I4 l" t2 C& I& y. T) k$ Wif(Button == mbLeft)8 d7 ~) _7 I+ W+ S) s
        {0 x0 ^+ K) b0 L% R! b  j/ h! L
                ReleaseCapture();
9 @+ Z! n2 o9 f* |, ]" P                Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);
0 g% F+ q! U! J$ I' b        }9 j  J* x1 a- w/ ^" [
}
0 Z8 M! g# p. q4 v4 ~! ^file://---------------------------------------------------------------------------
4 `! g$ ]) J6 ~. ^void __fastcall TForm1::BmpToRgn()
3 ?% f6 |  W" ~6 o{
5 ?9 a1 M. R0 A$ L' i# c. W4 \$ hImage1-&gt;AutoSize=true;
! y! x2 }! F8 R" f$ H* YForm1-&gt;AutoSize=true;
& X& z/ |2 K4 R$ EForm1-&gt;BorderStyle=bsNone;
% y) ^5 ~* \& e( @9 O9 DTColor ColorKey=Image1-&gt;Canvas-&gtixels[0][0];
" \  L  Z, ~) o4 Z# ~2 b0 Wint x,y;( `. O+ H; t6 C5 t$ u
int l,r;; u$ A, x- P: v5 K* k
POINT *a;. `+ \9 M0 ^4 i- N
bool lb,rb;2 c, D3 K2 v; U. z/ k7 c5 o% I
HRGN WndRgn,TempRgn;</P>
! S/ c% x/ A6 s4 c1 [, ]<>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)" j+ H6 ^" \+ W0 c' T
{% L/ s2 {) l/ K- g, u% D; D
  ShowMessage("申请内存失败!");
. [, h2 l3 u- v% F$ W4 L* A  exit(0);
) W8 ?3 O. t- k}</P>
! d: V- D% q, b6 O' ?/ i9 k0 U3 z4 h<>l=0;r=Image1-&gt;Height*2-1;7 X( E: S) U- `( G
WndRgn=CreateRectRgn(0,0,Image1-&gt;Width,Image1-&gt;Height);</P>
$ o! Z8 h/ u9 C+ p. Y<>for(y=0;y&lt;Image1-&gt;Height;y++)
! k/ ~% P. r' G, a7 g6 F{% c1 u/ O# ~) P) |
  lb=true;
, d6 R' p0 ?: @  m- f1 h  for(x=0;x&lt;Image1-&gt;Width+1;x++)
) h0 P- b" [5 [5 V+ F9 ?) z    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey)9 p; l0 F+ k6 V( l7 p4 h/ p6 U
    {
6 d& k6 V% X- d9 _% j, L      a[l].x=x;3 ?2 P! d- q/ u4 k  b2 P
      a[l].y=y;
  s0 P7 |' k  s2 L      lb=false;
+ B7 ?! v% n1 H4 G( [      break;+ R) [5 e0 h' N  A
    }</P>
3 E0 {- }# m$ c" i% h<>  if(lb) a[l]=a[l-1];
% P- `" v" ~$ I3 T) `  l++;
8 U0 L( g3 I" Z2 T# `  rb=true;</P>9 d5 Q! A! A. W! C
<>  for(x=Image1-&gt;Width;x&gt;=0;x--)- V/ @5 C0 K1 F* `3 i
    if(Image1-&gt;Canvas-&gtixels[x][y]!=ColorKey), I7 K( x9 e+ L0 n$ {
      {
  Z& x1 @) u) T7 [8 Z4 a* k; }        a[r].x=x;/ n  R3 l& C, v! a& K3 ^  J' o
        a[r].y=y;
8 r: s( W  ~9 O0 M" n4 D) s  w        rb=false;/ h9 Z/ L' ^0 y. n% k: B3 i+ c; r
        break;5 ~4 M# q1 h4 h; W4 T/ r- ?
      }/ v& g' n9 q  }: \
  if(rb) a[r]=a[r+1];
8 B# i) l' F  a* I& f  r--;
9 ?4 v& B+ n4 [}</P>
. k* J' ^; ~. F! d0 J<>r=Image1-&gt;Height*2-1;" y( h  s& y! ?/ j
for(y=0;y&lt;Image1-&gt;Height-1;y++)6 G* V* A& A- ^5 K
{
* `% ~/ F; D. Y7 v6 y  for(x=a[y].x;x&lt;=a[r].x;x++): L' @5 @4 G6 }
    if(Image1-&gt;Canvas-&gtixels[x][y]==ColorKey)! P3 j! S+ m! G' T7 j% E
    {! E- ^% ~8 s# H
      TempRgn=CreateRectRgn(x,y,x+1,y+1);, u- D6 s" g, ~2 _' I
      CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);
/ m+ Z. @" T0 V! K1 @3 q) F      DeleteObject(TempRgn);/ D/ u- M: n  O* q9 X# c9 A* H
    }  q, C/ I  N5 A# o, Z
  r--;" A) I) Z8 a. T" {; F
}</P>" }5 G% R0 N3 d. w% W
<><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,ALTERNATE</A>);
8 h: v, G1 O* H* }" }) n  }TempRgn=CreatePolygonRgn(a,Image1-&gt;Height*2,WINDING);
8 }) @8 U. K+ k1 u, C' c8 A  ~* QCombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);+ Q  v+ B- o) i+ @* B) |
DeleteObject(TempRgn);: t9 J) H( _/ J$ f1 b  v6 \
delete a;2 l4 `" H# M- W9 d( Q1 C$ ^' R
SetWindowRgn(Handle,WndRgn,true);# k/ }: ^) W8 S& |2 {  X" t- ^
}</P>! i7 W) V* [7 N$ ?/ A
<>void __fastcall TForm1::Open1Click(TObject *Sender)
/ L  o( H- _( W7 Y9 l! }+ U{
) P# V+ U+ i* n  if(OpenPictureDialog1-&gt;Execute())0 o0 u, n* U1 Q4 C# |
  {% A1 h1 U# F# V* Z% Q7 P, g
      Image1-&gticture-&gt;LoadFromFile(OpenPictureDialog1-&gt;FileName);! e5 ^& M, G4 a6 Y$ r  }) Q' D
      BmpToRgn();
$ Z# I+ [6 X9 G1 u3 U" d! i  }
" t* t! c( \* g% x  b8 l0 l}
# \8 H9 w* _5 R0 R0 S  @4 p. kfile://---------------------------------------------------------------------------</P>2 e9 y2 C8 L$ W" Z
<>对啦,图片的背景色要相同。
+ k5 o. o3 ~4 c3 }图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>
9 U1 X6 |0 m) b5 d. y<>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------6 {( s3 B# R0 O: W* w. A: m
void __fastcall TForm1::FormPaint(省略)
* G  e( i1 \3 M% P! X{
# y. i9 x% x6 n7 I! |; D3 y    AnsiString ww;int bb;  T. G2 V/ q5 Q* L1 O+ Q+ q
    ww=AnsiString(Form1-&gt;Width/256);
0 e9 W* Q$ H* M" \    TRect MyRect;
. m4 L$ J  i' N4 F( U, G    MyRect.left=0;
" Q( v& v' {- F% c    for(bb=0;bb&lt;255;bb++)
9 B' t/ q0 k2 e- I1 G9 O5 Q    {# j5 n* k. B6 T+ M8 l+ [
        Form1-&gt;Canvas-&gt;Brush-&gt;Color=(TColor)RGB(0,0,bb);
: [8 f/ ^! O! u1 B7 x7 A        MyRect.top=bb*(ww.ToInt());
. a4 ~6 Y# ~7 ?        MyRect.right=Form1-&gt;Width;
) a  B: p8 A+ _# s: ~8 N6 s        MyRect.bottom=(bb+1)*(ww.ToInt());) o- [# O  S( {! z4 M
        Form1-&gt;Canvas-&gt;FillRect(MyRect);
( |; H3 l6 r, W- c% z    }
$ M( Q$ p6 M* O" v} </P>  Q7 W0 q& v0 a7 _) h$ M
<>file://---------------透明窗体--------------------------
+ {* k: Z" y  y! i8 O% Uvoid __fastcall TForm1::Button1Click(TObject *Sender)" S9 F  b' @' C6 w% w
{
8 B$ Z: A+ I# j3 [: ]7 ]$ \  TRect *rctClient,*rctFrame;
7 n# Z- S, d) o) z8 E( z& j* C  HRGN hClient,hFrame;
' X& f7 `- H1 R2 S' n  POINT *lpTL,*lpBR;! C4 q, a) h) i. ^4 C
  rctFrame=new TRect;5 Y* c" {, q4 K$ r! J
  rctClient=new TRect;* [' {; I6 N9 y9 r
  lpTL=new POINT;5 G: ~! {1 U2 f
  lpBR=new POINT;* s5 z' x4 @& [. d8 t
  GetWindowRect(Form1-&gt;Handle,rctFrame);' e: r2 l$ Y2 I3 g( L% z
  ::GetClientRect(Form1-&gt;Handle,rctClient);' c& p- [' D" ]. U: k
  lpTL-&gt;x=rctFrame-&gt;Left;lpTL-&gt;y=rctFrame-&gt;Top;
6 A* _2 n5 a9 k; H  lpBR-&gt;x=rctFrame-&gt;Right;lpBR-&gt;y=rctFrame-&gt;Bottom;</P>
5 ?, k% Z6 {$ Q2 X/ _8 y<>  ::ScreenToClient(Form1-&gt;Handle,lpTL);
$ l6 ^7 N. {' F9 i5 H% N  ::ScreenToClient(Form1-&gt;Handle,lpBR);. ]6 k$ f. C" W
  rctFrame-&gt;Left=lpTL-&gt;x;rctFrame-&gt;Top=lpTL-&gt;y;, ^+ c5 l7 [+ N5 p# H/ i& m
  rctFrame-&gt;Right=lpBR-&gt;x;rctFrame-&gt;Bottom=lpBR-&gt;y;
3 I; g$ _, f% @) z( @/ x  rctClient-&gt;Left=abs(rctFrame-&gt;Left);
( F5 U- y6 T* k  rctClient-&gt;Top=abs(rctFrame-&gt;Top);, Q- ^) ?3 n$ \" L$ g  p
  rctClient-&gt;Right=rctClient-&gt;Right+
2 U; a" W( N3 l/ d9 |; h' m( ~& |$ s                  abs(rctFrame-&gt;Left);1 W3 o3 r, p6 t$ u4 h( f: P
  rctClient-&gt;Bottom=rctClient-&gt;Bottom+0 l- D2 @' W5 |+ }4 k. ]+ S7 S
                    abs(rctFrame-&gt;Top);
" P7 b: g0 c1 y/ Y8 {  rctFrame-&gt;Right=rctFrame-&gt;Right+
4 ?; K( F0 y$ L0 P$ t5 P0 I                  abs(rctFrame-&gt;Left);/ \9 ]3 ]8 @' n. ^
  rctFrame-&gt;Bottom=rctFrame-&gt;Bottom+% {" H& t" y9 ^  M* y0 M6 O" Z
                  abs(rctFrame-&gt;top);
5 h- u  j+ n" u1 H; c) B3 {  rctFrame-&gt;Top=0;rctFrame-&gt;Left=0;</P>8 l8 W4 O3 }" [$ }5 f
<>  hClient=CreateRectRgn(rctClient-&gt;Left,rctClient-&gt;Top,
& r+ w8 l3 R9 l* w: d1 L) k                        rctClient-&gt;Right,rctClient-&gt;Bottom);2 D( F: i5 t3 d; E- Q( Q) S* W; v
  hFrame=CreateRectRgn(rctFrame-&gt;left,rctFrame-&gt;Top,
8 j1 ?, ^/ G4 r2 {8 C7 v( H                      rctFrame-&gt;Right,rctFrame-&gt;Bottom);
: L% N: v' t$ M3 T  CombineRgn(hFrame,hClient,hFrame,RGN_XOR);% d: _; d  S+ e. d
  SetWindowRgn(Form1-&gt;Handle,hFrame,true);</P>2 L7 J9 Y$ {5 a0 `5 A! k
<>- L: [3 A; I! X2 K% a, o! V/ v) O
  delete rctFrame;8 o6 C) _4 \2 l& r
  delete rctClient;
" @+ S4 g; T. P9 s8 z" `  delete lpTL,lpBR;</P>+ P( s% h4 B" s* ^8 Q
<>} </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 23:16 , Processed in 0.423270 second(s), 53 queries .

回顶部