- 在线时间
- 0 小时
- 最后登录
- 2007-9-23
- 注册时间
- 2004-9-10
- 听众数
- 3
- 收听数
- 0
- 能力
- 0 分
- 体力
- 9975 点
- 威望
- 7 点
- 阅读权限
- 150
- 积分
- 4048
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1893
- 主题
- 823
- 精华
- 2
- 分享
- 0
- 好友
- 0

我的地盘我做主
该用户从未签到
 |
< >以下源码是快速创建不规则窗体的,还可以移动。
! J) c0 H: g" \: [0 k. H* u) v) Afile://------------------------------------------------------------------------
& \) |. L' |, W3 |/ ?<a>file://Unit1.h</A></P>4 Q+ s! @$ s6 k* A* p
< >#ifndef Unit1H
& L: k$ U& Q" V q4 R#define Unit1H1 g" Z, d4 P" Z+ x7 |3 ?
file://------------------------------------------------------------------------
8 P7 P5 |4 |( W( Y0 q' }#include <Classes.hpp>/ N0 P5 z k% g+ r2 N
#include <Controls.hpp>
# ^' {! b3 _& C. l9 h6 K% a* N) U#include <StdCtrls.hpp>
" Z c+ S7 ?2 ^% C E9 a#include <Forms.hpp>' k7 Q( [5 @' x' z! @9 H
#include <ExtCtrls.hpp>
0 k& N; u6 p) F% _5 c#include <Graphics.hpp>
- _6 g0 t1 F, A1 \#include <Menus.hpp>& J) A" w1 k) i0 p! d; V0 x
#include <Dialogs.hpp> ]4 `1 b$ \! i6 I2 j, Q6 e
#include <ExtDlgs.hpp>8 U: S, q4 Q, `# \( @! K: [
file://---------------------------------------------------------------------------
$ ^7 K3 o7 l0 a- Q2 P4 v* d, uclass TForm1 : public TForm
U' l& w; n$ R( r{
* x; j+ a; o; x4 S__published: // IDE-managed Components7 p3 A0 n( j; l+ l" m
TImage *Image1;& ?7 M6 z7 D/ P6 V
TPopupMenu *PopupMenu1;" u$ T7 ?6 u& i4 E* y, O
TMenuItem *Exit1;' q; r. u: ~6 Q$ G
TOpenPictureDialog *OpenPictureDialog1;+ j) B7 K. ]# Q7 A7 a
TMenuItem *Open1;
% Y$ y" e9 x1 N. G7 v void __fastcall Exit1Click(TObject *Sender); K0 `% B3 p( Z5 ~8 Q
void __fastcall FormCreate(TObject *Sender);
5 F' r" L! V$ w, ~, a0 h% h void __fastcall Image1MouseDown(TObject *Sender,/ ]4 s' A8 V% G, |& H, G
TMouseButton Button, TShiftState Shift, int X, int Y);; Q/ k4 B. b) u7 I( i U+ _
void __fastcall Open1Click(TObject *Sender);6 A n6 ^! j$ O4 R
private: // User declarations
2 Q* m; u4 _1 G4 {2 M1 M9 a( ` void __fastcall BmpToRgn();
" p4 R- b4 f/ j/ @5 gpublic: // User declarations
L$ c' e: U( S: y7 q0 S: c. y __fastcall TForm1(TComponent* Owner);6 W) _( H& q1 t- W' s) i
};
7 {1 w" A' B. ~9 S" L7 y& Efile://---------------------------------------------------------------------------
* e) W& E% h' M. E9 ?- Bextern PACKAGE TForm1 *Form1;* t+ G. A% M9 n; L" x
file://---------------------------------------------------------------------------" ~" Y0 u- l- G: b
#endif</P>
' ~: E* j) a1 _, U Q& O< >: z0 v& ?$ ^- w* [$ K T
<a>file://unit1.cpp</A>
5 t2 e4 q4 [1 dfile://---------------------------------------------------------------------------</P>
$ l2 n6 {5 |' j! S< >#include <vcl.h>
3 i1 v' G( z+ Y3 y) y8 j#pragma hdrstop</P>
# M2 Z0 U/ D0 p' l1 W. b9 {) u< >#include "Unit1.h"6 a4 o, l# c! h. o, `* ~
file://---------------------------------------------------------------------------
' `7 i, |! N$ W1 \5 d [7 V#pragma package(smart_init)
) ?& c) [6 P0 X+ e7 _% \6 h0 F#pragma resource "*.dfm"' q7 {/ A; X, g( y2 s5 ^/ B
TForm1 *Form1;
- J# y$ v6 l* d* b3 G! Q( ffile://---------------------------------------------------------------------------1 b/ D2 p* i% N' Q- T
__fastcall TForm1::TForm1(TComponent* Owner)6 P8 `) F( G& g$ O
: TForm(Owner)
; j" w. S/ e0 r( j0 p{; s1 `" S* d: U- g6 b( Q) ^1 p) G( F9 M
}
9 h$ }3 n) M4 m3 C9 lfile://---------------------------------------------------------------------------. Z: @4 E0 y" e
void __fastcall TForm1::Exit1Click(TObject *Sender)) p3 l) J2 s! n6 V0 O( E
{
3 E$ c$ U* Z6 Z$ S3 n! n1 d& a$ e% v Close();
% a3 ]: s/ V" o4 Z}
/ r, V" H- _- L( Hfile://---------------------------------------------------------------------------</P>. E0 D3 `. G4 h% N& Y _
< >void __fastcall TForm1::FormCreate(TObject *Sender)3 N4 L8 K- v! ?, K/ H
{</P>
: E6 K; f, h5 h& k) _< >BmpToRgn();
& b* Q) B: Y4 x% C8 X( P4 Q}
, X; k. H) G6 }7 L9 Ifile://---------------------------------------------------------------------------</P>
S4 H8 T% ^4 ~; @3 r! {< >void __fastcall TForm1::Image1MouseDown(TObject *Sender,
5 W- @& J! D* j. z( s+ k5 M% D/ f TMouseButton Button, TShiftState Shift, int X, int Y)
; j7 g/ V2 w5 x8 X{
3 m" K( T" S# Jif(Button == mbLeft)
{# T5 p) c1 V( I- ?9 O {
5 J, T( V4 E. S ReleaseCapture();( h6 g7 q4 H+ K. H" C
Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);
/ m& y/ T9 [/ [% s( A8 I }
3 ?1 v! b) X5 y" P5 M) h}0 H0 h0 [+ N# c8 H+ W( T6 F
file://---------------------------------------------------------------------------& P8 g7 ?! ?3 g4 U! Z3 _0 u% Z9 }+ u
void __fastcall TForm1::BmpToRgn()" y& s5 s% B; F0 j" J
{
* p: L3 `+ L( J' g; aImage1->AutoSize=true;# I( j7 L; D: C C- c" H
Form1->AutoSize=true;" \( `: l6 U- n- Y
Form1->BorderStyle=bsNone;0 \( u2 S7 _0 k- _# Q1 b
TColor ColorKey=Image1->Canvas-> ixels[0][0];0 z4 z. a" v) A) {8 u6 a: q
int x,y;
$ A; W( u, f2 B' L& y& @int l,r;3 f% W* \% ~$ L! R; u, e5 ^
POINT *a;
+ g* S! y) Q; }$ m% x+ }5 |/ U7 K0 lbool lb,rb;
# ? o; I' H' K' h# o# ~HRGN WndRgn,TempRgn;</P>$ H( `# I; T" I1 @
< >if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)
- ?- v3 c$ t% P* l9 E3 a{# Y8 c& B l' P$ L( m5 V1 ~7 Z
ShowMessage("申请内存失败!");
' e% c5 H: @1 E" U* j r exit(0);
( J) ]8 ^, n& N8 k' W4 @: v}</P>
2 c* T) l: ^0 `4 _1 n< >l=0;r=Image1->Height*2-1;- P5 k# K& i6 k) ~
WndRgn=CreateRectRgn(0,0,Image1->Width,Image1->Height);</P>
8 Z/ ~' g# j Y! R( I< >for(y=0;y<Image1->Height;y++)
) z- w7 ^1 U2 o+ D- d( ^7 ~. I{
3 L' Y# {. g3 x: f5 a1 a lb=true;2 {2 ?' A% x' a+ D# `, B3 B8 b @8 y
for(x=0;x<Image1->Width+1;x++)
9 |* i& T! o6 J6 w if(Image1->Canvas-> ixels[x][y]!=ColorKey)# h: L9 B$ `% x
{
; Z3 X" _! X' M7 c7 M a[l].x=x;
/ ~$ v$ E- u h" O3 N a[l].y=y;
) S; C) `7 u' W lb=false;
1 N8 l# d+ M; e6 y break;. v2 {5 I' ^& ^4 ~ N
}</P>
. }; V- R& y& L E" m. n N< > if(lb) a[l]=a[l-1];* n# o R" Z% ?" Q
l++;5 F" [% \- y3 N1 K/ E
rb=true;</P>7 o/ d- i! A B3 p; u' {
< > for(x=Image1->Width;x>=0;x--)
" X2 F6 c# Q5 t) _2 p: k if(Image1->Canvas-> ixels[x][y]!=ColorKey)
; l2 T) o% h# c/ e {
' W: h6 Z$ @8 b: q1 ~! N) S, c$ i a[r].x=x;
1 K" j. ]( D4 A8 b! d; _8 G- z a[r].y=y;
) x! e" \* a4 [; p rb=false;
5 _# c& C+ F8 X- S' h2 o break;
! S3 d# [; ?5 b" v+ j8 y }8 {7 U- s' o8 ]2 j
if(rb) a[r]=a[r+1];
+ a" [( i! w8 A5 F o* | r--;
: J$ j- _" z- K9 ?) ?+ l. x; F}</P>
* M8 ~7 N3 j* F/ M/ ^6 k+ }/ S< >r=Image1->Height*2-1; [* T8 k! H; q+ _7 C' k
for(y=0;y<Image1->Height-1;y++)% e9 N3 T6 Y' t: z% V
{! {0 C% I \. J' C. X
for(x=a[y].x;x<=a[r].x;x++)
3 V# A* B6 {- k% W if(Image1->Canvas-> ixels[x][y]==ColorKey)
K! p: ^ @/ ?. {' N {0 R0 v$ t# v8 w$ q
TempRgn=CreateRectRgn(x,y,x+1,y+1);7 A- i, b8 ]0 @6 J+ w7 E+ [! q+ P
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);
* x& D7 X: [' _ DeleteObject(TempRgn);$ ~# W8 }3 |5 \
}
0 `+ r8 g9 a% d6 O% v r--;
' S Y w: _# x0 i- X8 M6 {5 m}</P>
, _& V* F& K! g! h< ><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1->Height*2,ALTERNATE</A>);
1 d" V( _) h1 t: b% N5 |TempRgn=CreatePolygonRgn(a,Image1->Height*2,WINDING);
1 X6 r1 W& h! @; y& t6 nCombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
% U1 O4 D K" TDeleteObject(TempRgn);3 O: c- b% h1 o0 V- U$ U; U4 Q0 }
delete a;
+ \4 G3 i- L- R& x! HSetWindowRgn(Handle,WndRgn,true);
& E$ L: V- w# R4 A}</P>
8 X1 [) J$ o1 D# b. i( |8 r< >void __fastcall TForm1::Open1Click(TObject *Sender)! ]' A7 w9 z; v: V9 f# }6 p
{; O# O' N" O4 Q9 o6 M) H" q3 t
if(OpenPictureDialog1->Execute())
: x8 W5 _" j" y# u {
4 ]/ C5 l* o9 F [' n% }+ ? Image1-> icture->LoadFromFile(OpenPictureDialog1->FileName);! _& h1 w, F2 A% f g6 w
BmpToRgn();1 w0 D' g- x/ [/ D! i. Z3 l
}+ U$ z. U/ w+ Q: N3 W2 K7 z# p
}0 o, [: g3 S H: H3 ?2 |
file://---------------------------------------------------------------------------</P>
6 q0 ?" k8 I+ q* [) j< >对啦,图片的背景色要相同。
[/ ^; E! q6 `) |图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>
( g+ N# c3 {; Y( t: |1 b< >file://--------------------(一)为窗体背景增加渐变色(蓝)---------------! `% @( \( X$ h
void __fastcall TForm1::FormPaint(省略)
) y% ?0 }2 Y' M{0 v0 D+ i p, r k, y8 X9 ~6 h2 N8 N
AnsiString ww;int bb;
- p! Z+ Z- a, @# _ ww=AnsiString(Form1->Width/256);* s9 N8 t: E, ]+ t
TRect MyRect;5 g4 C; o7 X2 D
MyRect.left=0;
+ ?. i3 v) h; e+ i* Y/ W for(bb=0;bb<255;bb++)# Y& N; p( {3 W( E. u5 z& f1 i1 Q1 i0 a
{$ c8 f1 t8 W6 i0 J* W
Form1->Canvas->Brush->Color=(TColor)RGB(0,0,bb);# K; |. H) D( }, s( ]% E6 k
MyRect.top=bb*(ww.ToInt());
2 R2 L" |6 c. X7 N% V5 ]1 B) ~- j MyRect.right=Form1->Width;
R- Q* D- Y6 R0 K! p r4 v) h MyRect.bottom=(bb+1)*(ww.ToInt());
/ S* l6 K* R/ k. T7 _7 f Form1->Canvas->FillRect(MyRect);
# e. @3 H9 R' p } x2 d: c, S/ ?8 p! y* b# [
} </P>
5 F+ q" c( M" h: d, m6 c" [6 I< >file://---------------透明窗体--------------------------
2 v A4 {0 R8 a& k* Yvoid __fastcall TForm1::Button1Click(TObject *Sender)
" _7 v2 M# o+ |: A+ w6 Z{) _+ k; {( y7 {% q
TRect *rctClient,*rctFrame;3 Y: c0 Z1 g! b A3 Q
HRGN hClient,hFrame;/ A1 M: O5 _# o+ ]2 w% m. S
POINT *lpTL,*lpBR;! s' t2 ?3 T$ _! {# s
rctFrame=new TRect;) K: j+ u( c% P: o( u* C5 E- R) I6 s! Y
rctClient=new TRect;
; A; P/ J* u$ f( M7 p lpTL=new POINT;- @" V6 f) r! L- e, l" S8 d; T; d
lpBR=new POINT;: u. E" J# I5 j+ b5 Y
GetWindowRect(Form1->Handle,rctFrame);
. h9 ], D0 [$ J( K/ M ::GetClientRect(Form1->Handle,rctClient);- u) B/ k( B. W" }5 r0 \
lpTL->x=rctFrame->Left;lpTL->y=rctFrame->Top;
6 a# u/ H- \+ ]6 X: ] lpBR->x=rctFrame->Right;lpBR->y=rctFrame->Bottom;</P>4 X7 I) D( s' B5 `
< > ::ScreenToClient(Form1->Handle,lpTL);
' [) y+ Q+ w" }" E ::ScreenToClient(Form1->Handle,lpBR);4 v3 |% k% ?8 B% z/ W! \/ {
rctFrame->Left=lpTL->x;rctFrame->Top=lpTL->y;
- q" [+ H/ w/ K! p) o V rctFrame->Right=lpBR->x;rctFrame->Bottom=lpBR->y;6 _, O$ W5 `; L/ t( B
rctClient->Left=abs(rctFrame->Left);/ i) }# g \' T# G- c- f- C
rctClient->Top=abs(rctFrame->Top);5 E% `3 |6 P! {( z
rctClient->Right=rctClient->Right+
9 z# p8 Q& d5 P" }- G" \- \ abs(rctFrame->Left);# K' w8 M2 ?5 a0 n1 Y, P4 E6 P+ T
rctClient->Bottom=rctClient->Bottom+
5 |6 s: q1 H# J/ n2 D abs(rctFrame->Top);
+ g9 f3 J$ |+ \% A+ A7 C rctFrame->Right=rctFrame->Right+! b E6 k+ D8 I5 o- H l5 ?
abs(rctFrame->Left);
8 k# U' C3 `; a rctFrame->Bottom=rctFrame->Bottom+5 X* W7 Y$ b2 u- c* l& C
abs(rctFrame->top);; @* s: ?4 t2 Z G: _; z
rctFrame->Top=0;rctFrame->Left=0;</P> {' g0 I' D* n( b/ o
< > hClient=CreateRectRgn(rctClient->Left,rctClient->Top,
& b. ^9 h5 {, b: \! l, _/ d R rctClient->Right,rctClient->Bottom);
: E6 D5 P7 ?& h1 _+ s hFrame=CreateRectRgn(rctFrame->left,rctFrame->Top,
! }+ R+ F, b2 T rctFrame->Right,rctFrame->Bottom);+ M5 G( W' C7 P% H# y
CombineRgn(hFrame,hClient,hFrame,RGN_XOR);* J9 b" l; V) d6 x+ K8 B2 j
SetWindowRgn(Form1->Handle,hFrame,true);</P>
) n/ j1 P# \: k" G9 Y0 ?) r3 S h< >7 A* [6 W2 g7 |$ U; W1 D4 J) j* S, e
delete rctFrame;. n( }6 d7 q% Q7 L, o
delete rctClient;
' c4 B3 M4 C8 ~2 _3 [) o: a* K6 ` delete lpTL,lpBR;</P>
/ f1 b# c! S: _$ e' C< >} </P> |
zan
|