自訂元件的 Canvas 問題 |
尚未結案
|
dhliu
一般會員 ![]() ![]() 發表:18 回覆:24 積分:8 註冊:2003-03-05 發送簡訊給我 |
請教各位先進, 我自訂一個元件, 繼承自 TWinControl, 欲將輸出結果經由 Canvas 輸出至螢幕上, 但是只要一執行 Canvas 的指令之後, 不是出現 "Canvas does not allow drawing", 就是出現 "Canvas does not have parent" (好像是這樣) 的訊息, 我參考了 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=22596
的方式, 卻不知要怎麼下手
程式的宣告部分如下
class PACKAGE TMyComponent : public TWinControl
{
public:
__fastcall TMyComponent(TComponent* Owner);
TCanvas *DrawCanvas;
void __fastcall Rectangle(int Top, int Left, int Bottom, int Right);
} MyComponent.cpp
static inline void ValidCtrCheck(TMyComponent *)
{
new TMyComponent(NULL);
} __fastcall TMyComponent::TMyComponent(TComponent* Owner)
: TWinControl(Owner)
{
} void __fastcall TMyComponent::Rectangle(int Top, int Left, int Bottom, int Right)
{
Canvas->Rectangle(Top,Left,Bottom,Right); //一到這裡就有錯誤訊息
} 煩請各位指導, 我為這個問題傷腦筋好久了
|
dhliu
一般會員 ![]() ![]() 發表:18 回覆:24 積分:8 註冊:2003-03-05 發送簡訊給我 |
我先自問自答一番...
目前我是用很笨的方法, 在呼叫這個元件時直接把 Form 的 Handle 先傳給元件
//宣告的部分 void __fastcall TMyComponent::Rectangle(int Handle, int Top, int Left, int Bottom, int Right) { Canvas->Brush->Color = clBlack; Canvas->Rectangle(Top,Left,Bottom,Right); } //程式的部分 void __fastcall TForm1::Button1Click(TObject *Sender) { TMyComponent *MyComponent = new TMyComponent(this); //Handle 宣告在 TMyComponent 裡, 不過似乎只能用 int 而無法用 HWND/HDC 來宣告... MyComponent->Rectangle(Canvas->Handle, Top,Left,Height,Width); }這樣的話, 程式就可以正常執行了, 但是這時又衍生一個問題, 如果在 Design Time 下, 又要如何取得該時期的 Handle 呢? |
aquarius
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:3 回覆:347 積分:330 註冊:2003-05-21 發送簡訊給我 |
|
dhliu
一般會員 ![]() ![]() 發表:18 回覆:24 積分:8 註冊:2003-03-05 發送簡訊給我 |
唔... 我好像有地方打錯了
class PACKAGE TMyComponent : public TWinControl { private: int BoxHandle; public: __fastcall TMyComponent(TComponent* Owner); TCanvas *Canvas; void __fastcall Rectangle(int Top, int Left, int Bottom, int Right); __published: __property int Handle = {read = BoxHandle, write = SetHandle, nodefault}; } __fastcall TMyComponent::TMyComponent(TComponent* Owner) : TWinControl(Owner) { Canvas = new TCanvas; } //MyComponent.cpp void __fastcall TMyComponent::Rectangle(int Top, int Left, int Bottom, int Right) { Canvas->Handle = (HDC) BoxHandle; Canvas->Rectangle(Top,Left,Bottom,Right); } void __fastcall TMyComponent::SetHandle(int hWnd) { BoxHandle = hWnd; } //Form1.cpp void __fastcall TForm1::Button1Click(TObject *Sender) { MyComponent->Handle = (int) Canvas->Handle; MyComponent->Rectangle(10,10,20,20); }這樣寫的話, 執行是可以正常了, 但是必須先將 Form 的 Handle 傳到元件中, 還是不知道要怎麼樣取得Form1 和 Design Time 的 Handle... |
aquarius
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:3 回覆:347 積分:330 註冊:2003-05-21 發送簡訊給我 |
|
cmf
尊榮會員 ![]() ![]() ![]() ![]() ![]() 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
// Hierarchy : TWinControl ==> TCustomControl ==> TMyComponent class PACKAGE TMyComponent : public TCustomControl
{
public: void __fastcall Rectangle(int Top, int Left, int Bottom, int Right);
} //........................................
//MyComponent.cpp
void __fastcall TMyComponent::Rectangle(int Top, int Left, int Bottom, int Right)
{
Canvas->Rectangle(Top,Left,Bottom,Right);
} 每天省下一包菸的錢 愛心1000元餵飽一名非洲飢餓兒童
http://www.worldvision.org.tw/edm/30hffan/30hf1000.htm
![]()
------
︿︿ |
dhliu
一般會員 ![]() ![]() 發表:18 回覆:24 積分:8 註冊:2003-03-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |