PageControl元件如何透通? |
答題得分者是:hagar
|
Clarinet
高階會員 發表:9 回覆:97 積分:126 註冊:2002-05-08 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
Clarinet
高階會員 發表:9 回覆:97 積分:126 註冊:2002-05-08 發送簡訊給我 |
引言: http://delphi.ktop.com.tw/topic.php?TOPIC_ID=28788 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=28827 --- --<-<-<@感謝hagar大大的回覆!十分感謝!< > 不過,這些不是我要的結果< >,因為它們都是透通至DeskTop! 我的需求是希望能達到如TLabel、TSpeedButton的Transparent的效果! 我目前的解決方法是在每個TabSheet上都放一個TImage元件,然後,用複製的方法,複製Main Form上的底圖的相對位置,來做到類似透通的樣子! 而PageControl自已本身已有Canvas屬性,所以就直接用相同的方法來達到! 不過,pagecontrol卻有一個小麻煩,就是當有其他的程式、Form或messageDlg之類的檔在其前頭時,pagecontrol並不會重畫。 所以,就又用了另一個覺得不好的解決方法。就是放一個TTimer元件,每秒鐘重畫pagecontrol.Canvas,來達到! 上面這個方法雖然解決了我的需求,但覺得並不是一個很好的方法! 各位前輩有更好的方法能使 >
------
GPS(全球衛星定位系統) http://www.allgps.com |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
參考: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10262786.html
example1
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls,ComCtrls; type TForm1 = class(TForm) Button1: TButton; PageControl1: TPageControl; TabSheet1: TTabSheet; TabSheet2: TTabSheet; TabSheet3: TTabSheet; TabSheet4: TTabSheet; RadioButton1: TRadioButton; //component on tabsheet1 RadioButton2: TRadioButton; //component on tabsheet1 CheckBox1: TCheckBox; //component on tabsheet2 CheckBox2: TCheckBox; //component on tabsheet2 Memo1: TMemo; //component on tabsheet3 DateTimePicker1: TDateTimePicker; //component on tabsheet4 procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; Background: TBitmap; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin close end; Function TabWndProc( wnd: HWND; msg: Cardinal; wparam: WPARAM; lparam: LPARAM ): Integer; stdcall; var canvas: TCanvas; x, y: Integer; r: TRect; OldTabWndProc: Pointer; begin If Msg = WM_ERASEBKGND Then Begin // tile window background with bitmap Result := 1; GetClientRect( wnd, r ); canvas:= TCanvas.Create; try canvas.handle := HDC( wparam ); y := 0; While y < r.Bottom Do Begin x:= 0; While x < r.right Do Begin canvas.Draw( x, y, Background ); Inc( x, Background.Width ); End; Inc( y, Background.Height ); End; finally canvas.handle := 0; canvas.free; end; End Else Begin // pass message to old window proc OldTabWndProc := Pointer( GetWindowLong( wnd, GWL_USERDATA )); Result := CallWindowProc( OldTabWndProc, wnd, msg, wparam, lparam ); End; end; procedure TForm1.FormCreate(Sender: TObject); var i: integer; begin Background := TBitmap.Create; // could load bitmap from a resource here Background.Loadfromfile('C:\WINDOWS\KUGELN.BMP'); // subclass the tabsheets, store old window proc in GWL_USERDATA // field of window structure For i:= 0 to pagecontrol1.PageCount-1 do SetWindowLong( pagecontrol1.Pages[i].handle, GWL_USERDATA, SetWindowLong(pagecontrol1.Pages[i].handle, GWL_WNDPROC, Integer(@TabWndProc))); end; procedure TForm1.FormDestroy(Sender: TObject); begin Background.Free; end; end.example2 unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, OleCtrls,ComCtrls; type TForm1 = class(TForm) Button1: TButton; PageControl1: TPageControl; TabSheet1: TTabSheet; TabSheet2: TTabSheet; TabSheet3: TTabSheet; TabSheet4: TTabSheet; RadioButton1: TRadioButton; //component on tabsheet1 RadioButton2: TRadioButton; //component on tabsheet1 CheckBox1: TCheckBox; //component on tabsheet2 CheckBox2: TCheckBox; //component on tabsheet2 Memo1: TMemo; //component on tabsheet3 DateTimePicker1: TDateTimePicker; //component on tabsheet4 procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure PageControl1DrawTab(Control: TCustomTabControl; TabIndex: Integer; const Rect: TRect; Active: Boolean); private { Private declarations } FOldPageCOntrolWndProc: TWndMethod; procedure PageControlWndProc( Var Msg: TMessage ); public { Public declarations } end; var Form1: TForm1; Background: TBitmap; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); begin close end; Function TabWndProc( wnd: HWND; msg: Cardinal; wparam: WPARAM; lparam: LPARAM ): Integer; stdcall; var canvas: TCanvas; x, y: Integer; r: TRect; OldTabWndProc: Pointer; begin If Msg = WM_ERASEBKGND Then Begin // tile window background with bitmap Result := 1; GetClientRect( wnd, r ); canvas:= TCanvas.Create; try canvas.handle := HDC( wparam ); y := 0; While y < r.Bottom Do Begin x:= 0; While x < r.right Do Begin canvas.Draw( x, y, Background ); Inc( x, Background.Width ); End; Inc( y, Background.Height ); End; finally canvas.handle := 0; canvas.free; end; End Else Begin // pass message to old window proc OldTabWndProc := Pointer( GetWindowLong( wnd, GWL_USERDATA )); Result := CallWindowProc( OldTabWndProc, wnd, msg, wparam, lparam ); End; end; procedure TForm1.FormCreate(Sender: TObject); var i: integer; begin Background := TBitmap.Create; // could load bitmap from a resource here Background.Loadfromfile('C:\WINDOWS\KUGELN.BMP'); // subclass the tabsheets, store old window proc in GWL_USERDATA // field of window structure For i:= 0 to pagecontrol1.PageCount-1 do SetWindowLong( pagecontrol1.Pages[i].handle, GWL_USERDATA, SetWindowLong(pagecontrol1.Pages[i].handle, GWL_WNDPROC, Integer(@TabWndProc))); FoldPageControlWndProc := Pagecontrol1.WindowProc; Pagecontrol1.WindowProc := PageControlWndProc; end; procedure TForm1.FormDestroy(Sender: TObject); begin Background.Free; end; procedure TForm1.PageControlWndProc(var Msg: TMessage); var canvas: TCanvas; x, y: Integer; r: TRect; begin If msg.Msg = WM_ERASEBKGND Then Begin msg.Result := 1; r:= pagecontrol1.clientrect; canvas:= TCanvas.Create; try canvas.handle := HDC(msg.wparam); y := 0; While y < r.Bottom Do Begin x:= 0; While x < r.right Do Begin canvas.Draw( x, y, Background ); Inc( x, Background.Width ); End; Inc( y, Background.Height ); End; finally canvas.handle := 0; canvas.free; end; End Else FOldPageControlWndProc( Msg ); end; procedure TForm1.PageControl1DrawTab(Control: TCustomTabControl; TabIndex: Integer; const Rect: TRect; Active: Boolean); begin If Active Then Control.Canvas.Font.Style := [fsBold] Else Control.Canvas.Font.Style := []; Control.Canvas.Font.Color := clWhite; SetBKMode( control.canvas.handle, TRANSPARENT ); Control.Canvas.TextOut( rect.left 2, rect.top 2, (Control As TPageControl).Pages[TabIndex].Caption ); end; end.--- 歡迎光臨 KTop 研究院--<-<-<@ 發表人 - hagar 於 2003/08/17 08:39:48 |
Clarinet
高階會員 發表:9 回覆:97 積分:126 註冊:2002-05-08 發送簡訊給我 |
引言: 參考: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_10262786.html --- 歡迎光臨 KTop 研究院--<-<-<@ 發表人 - hagar 於 2003/08/17 08:39:48再次感謝hagar大大的回應! 測試了一下,好像可以達到小弟的要求!只要再稍為修改一下其相對位置! 在TabSheet方面可省下我之前的解決方法,就是每個TabSheet都要放一個TImage來解決,此範例可不用! 但在PageControl方面一樣有同樣的問題,就是有其他的MessageDlg或Form在其上方時,會造成畫面被覆蓋的情形,無法自動重繪其原來的,而造成類似殘影的畫面! 感謝hagar大大提供的另一種解決方法!十分感謝!
------
GPS(全球衛星定位系統) http://www.allgps.com |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
容我做一下筆記先,轉成c++的語法大概會像是……
[code cpp] //unit1.cpp //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- Graphics::TBitmap *bmp; LONG OldTabWndProc; int x,y; TRect r; int Result; LRESULT CALLBACK TabWndProc(HWND wnd, Cardinal msg, WPARAM wparam, LPARAM lparam) { if(msg == WM_ERASEBKGND){ Result = 1; GetClientRect(wnd, &r); TCanvas *canvas = new TCanvas(); try{ canvas->Handle = (HDC) wparam; y=0; while (y while(x x =bmp->Width; } y =bmp->Height; } } __finally{ canvas->Handle = 0; canvas->Free(); } } else{ OldTabWndProc = GetWindowLong(wnd, GWL_USERDATA); Result = CallWindowProc(FARPROC(OldTabWndProc), wnd, msg, wparam, lparam); } return Result; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { bmp =new Graphics::TBitmap; // could load bitmap from a resource here bmp->LoadFromFile("C:\\untitled.bmp"); // subclass the tabsheets, store old window proc in GWL_USERDATA // field of window structure for(int i=0; i { SetWindowLong(PageControl1->Pages[i]->Handle, GWL_USERDATA, SetWindowLong(PageControl1->Pages[i]->Handle, GWL_WNDPROC,(int)TabWndProc)); } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { bmp->Free(); } //--------------------------------------------------------------------------- [/code] [code cpp] //unit.h //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include #include #include <Forms.hpp><br />#include #include #include #include #include #include #include #include #include #include //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TPageControl *PageControl1; TTabSheet *TabSheet1; TTabSheet *TabSheet2; TTabSheet *TabSheet3; TTabSheet *TabSheet4; TButton *Button1; TButton *Button3; TEdit *Edit1; TLabel *Label1; void __fastcall Button1Click(TObject *Sender); void __fastcall FormCreate(TObject *Sender); void __fastcall FormDestroy(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |