全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2710
推到 Plurk!
推到 Facebook!

【BCB】【問題】無法動態改變自製元件的屬性

答題得分者是:axsoft
beball
一般會員


發表:7
回覆:4
積分:2
註冊:2004-01-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-13 13:29:41 IP:219.84.xxx.xxx 未訂閱
大家好    我遇到了一個問題,希望有人幫忙解決.. 同事用 BCB5 寫了一個 button 的元件.. 但是在其中一些人的電腦上使用這個 button 時.. 卻無法動態的去變更這個元件的屬性(例如 font, cursor...等等) 會出現 Access violation 的訊息.. 而同樣的元件在某些人的電腦上卻可以做到.. 請問這是元件上的寫法有誤,還是我們的BCB哪裡設定出了問題呢?    麻煩知道的人告知一下,謝謝....
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-13 15:33:20 IP:61.218.xxx.xxx 未訂閱
引言: 大家好 我遇到了一個問題,希望有人幫忙解決.. 同事用 BCB5 寫了一個 button 的元件.. 但是在其中一些人的電腦上使用這個 button 時.. 卻無法動態的去變更這個元件的屬性(例如 font, cursor...等等) 會出現 Access violation 的訊息.. 而同樣的元件在某些人的電腦上卻可以做到.. 請問這是元件上的寫法有誤,還是我們的BCB哪裡設定出了問題呢? 麻煩知道的人告知一下,謝謝.... < face="Verdana, Arial, Helvetica">
    beball您好:    1.建議您把您寫的Button元件貼上來.
2.我想可能是您的
 __property TFont font={read=FGetfont,write=FSetfont,nodefaullt};
  問題.
/*生活是一種藝術,用心生活才能享受生活*/
beball
一般會員


發表:7
回覆:4
積分:2
註冊:2004-01-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-13 16:50:58 IP:219.84.xxx.xxx 未訂閱
beball您好:    1.建議您把您寫的Button元件貼上來.
2.我想可能是您的
 __property TFont font={read=FGetfont,write=FSetfont,nodefaullt};
  問題.
你好,不是很懂您的意思,我把片段程式貼上來,請您幫我看看.. class PACKAGE TGButton : public TGraphicControl { public: AnsiString FCaption; bool FChecked, FShowCaption; TFont *FFont; TPicture *FPicture; void __fastcall SetCaption(AnsiString value); void __fastcall SetChecked(bool value); void __fastcall SetEnabled(bool value); void __fastcall SetFont(TFont *value); void __fastcall SetShowCaption(bool value); void __fastcall SetPicture(TPicture *value); protected: TNotifyEvent FOnMouseEnter, FOnMouseLeave; void __fastcall WndProc(TMessage &Message); virtual void __fastcall Paint(void); public: __fastcall TGButton(TComponent* Owner); __fastcall ~TGButton(); __published: __property Anchors; __property AnsiString Caption = {read = FCaption, write = SetCaption}; __property bool Checked = {read = FChecked, write = SetChecked}; __property Cursor; __property Enabled; __property TFont *Font = {read = FFont, write = SetFont}; __property TPicture *Picture = {read = FPicture, write = SetPicture}; __property bool ShowCaption = {read = FShowCaption, write = SetShowCaption}; __property ShowHint; __property Visible; __property OnClick; __property OnMouseDown; __property OnMouseMove; __property OnMouseUp; __property TNotifyEvent OnMouseEnter = {read = FOnMouseEnter, write = FOnMouseEnter}; __property TNotifyEvent OnMouseLeave = {read = FOnMouseLeave, write = FOnMouseLeave}; }; 還需要什麼資訊的話,請再跟我說..
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-13 18:07:53 IP:61.218.xxx.xxx 未訂閱
因網路問題造成重複Post...請相關人員幫忙刪除 發表人 -
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-13 18:10:29 IP:61.218.xxx.xxx 未訂閱
beball您好:
看您得程式宣告片段實在看不出你實作部分動了哪些手腳,如果要別人幫忙建議您把完整的內容貼出..    給您一個範例您參考看看:        CGiButton元件檔案下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=15083796&CC=337344    CGiButton.h
 
#ifndef CGiButtonH
#define CGiButtonH
//---------------------------------------------------------------------------
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TRGB : public TPersistent
 {
  private:
        int FpasR, FpasG, FpasB;
        TNotifyEvent FOnChange;
  public:
        __fastcall TRGB();
  protected:
        virtual void __fastcall Change();
        virtual void __fastcall SetpasR(int Value);
        virtual void __fastcall SetpasG(int Value);
        virtual void __fastcall SetpasB(int Value);
  __published:
        __property int pasR =  {read=FpasR, write=SetpasR, default = 1};
        __property int pasG =  {read=FpasG, write=SetpasG, default = 1};
        __property int pasB =  {read=FpasB, write=SetpasB, default = 1};
        __property TNotifyEvent OnChange = {read=FOnChange, write=FOnChange};
 };
//---------------------------------------------------------------------------
class PACKAGE TCGiButton : public TCustomControl
{
private:
        TRGB *FRGB;
        TFont *FFont;
        bool BtnEnfonce;
        bool BtnEnfonceBis;
        bool MouseSurBtn;
        bool ALeFocus;
protected:
        void __fastcall RGBChange(TObject *Sender);
        void __fastcall Paint();
        DYNAMIC void __fastcall DoEnter();
        DYNAMIC void __fastcall DoExit();
        DYNAMIC void __fastcall MouseDown(TMouseButton Button,
                                      Classes::TShiftState Shift, int X, int Y);
        DYNAMIC void __fastcall MouseUp(TMouseButton Button,
                                      Classes::TShiftState Shift, int X, int Y);
        DYNAMIC void __fastcall KeyDown(Word &Key, Classes::TShiftState Shift);
        DYNAMIC void __fastcall KeyUp(Word &Key, Classes::TShiftState Shift);
        bool __fastcall CanResize(int &NewWidth, int &NewHeight);
        virtual void __fastcall SetFont(TFont *Value);
        virtual void __fastcall SetRGB(TRGB *Value);
        void __fastcall FontChanged(TObject *Sender);
        virtual void __fastcall MouseLeave(TMessage &Msg);
        virtual void __fastcall MouseEnter(TMessage &Msg);
        virtual void __fastcall TextChanged(TMessage &Msg);
BEGIN_MESSAGE_MAP
    MESSAGE_HANDLER(CM_MOUSELEAVE, TMessage, MouseLeave)
    MESSAGE_HANDLER(CM_MOUSEENTER, TMessage, MouseEnter)
    MESSAGE_HANDLER(CM_TEXTCHANGED, TMessage, TextChanged)
END_MESSAGE_MAP(TCustomControl)
public:
        __fastcall TCGiButton(TComponent* Owner);
        virtual __fastcall ~TCGiButton();
__published:
        __property Caption;
        __property OnClick;
        __property OnKeyUp;
        __property OnKeyPress;
        __property OnKeyDown;
        __property OnEnter;
        __property OnExit;
        __property TabOrder;
        __property TabStop;
        // nouvelles propriétés :
        __property TFont *Font =  {read=FFont, write=SetFont};
        __property TRGB *CRGB =  {read=FRGB, write=SetRGB};    };
//---------------------------------------------------------------------------
#endif    CGiButton.cpp
#include 
#pragma hdrstop
#include "CGiButton.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
//  ValidCtrCheck est utilisé pour vérifier que les composants créés n'ont
// aucune fonction virtuelle pure.
//
static inline void ValidCtrCheck(TCGiButton *)
{
        new TCGiButton(NULL);
}
//---------------------------------------------------------------------------
// Implémentation de la classe TRGB
//---------------------------------------------------------------------------
__fastcall TRGB::TRGB() : TPersistent()
{
     FpasR = 1;
     FpasG = 1;
     FpasB = 1;
}
//---------------------------------------------------------------------------
void __fastcall TRGB::Change()
{
     if(FOnChange) FOnChange(this);
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasR(int Value)
{
     if (Value < 6 && Value >= 0) FpasR = Value;
     Change();
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasG(int Value)
{
     if (Value < 6 && Value >= 0) FpasG = Value;
     Change();
}
//---------------------------------------------------------------------------
void __fastcall TRGB::SetpasB(int Value)
{
     if (Value < 6 && Value >= 0) FpasB = Value;
     Change();
}    //---------------------------------------------------------------------------
// Implémentation tu composant TCGiButton
//---------------------------------------------------------------------------
__fastcall TCGiButton::TCGiButton(TComponent* Owner)
        : TCustomControl(Owner)
{
     Width = 75;
     Height = 25;
     TabStop = true;
     BtnEnfonce = false;
     BtnEnfonceBis = false;
     MouseSurBtn = false;
     ALeFocus = false;
     FFont = new TFont();
     FFont->OnChange = FontChanged;
     FRGB = new TRGB();
     FRGB->OnChange = RGBChange;
     DoubleBuffered = true;
}
//---------------------------------------------------------------------------
__fastcall TCGiButton::~TCGiButton()
{
     delete FFont;
     delete FRGB;
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::Paint()
{
   RECT CoRect;
   CoRect.left = 0;
   CoRect.top = 0;
   CoRect.right = Width;
   CoRect.bottom = Height;
   Canvas->Font = Font;
   int h = Canvas->TextHeight(Caption);
   int y = (Height-h)/2;
   int l = Canvas->TextWidth(Caption);
   int x = (Width-l)/2;       Canvas->Brush->Style = bsClear;
   if (BtnEnfonce && MouseSurBtn || BtnEnfonceBis)
   {
     for (int y=0; y < Height; y++)
      {
        Canvas->MoveTo(0, y);
        Canvas->LineTo(Width, y );
        Canvas->Pen->Color= (TColor)(clWhite - (Height-y)*CRGB->pasR*0x10000 -
                           (Height-y)*CRGB->pasG*0x100 - (Height-y)*CRGB->pasB);
      }
     Canvas->TextOut(x+1,y+1,Caption);
   }
   else
   {
     for (int y=0; y < Height; y++)
      {
        Canvas->MoveTo(0, y);
        Canvas->LineTo(Width, y );
        Canvas->Pen->Color= (TColor)(clWhite - y*CRGB->pasR*0x10000 -
                                             y*CRGB->pasG*0x100 - y*CRGB->pasB);
      }
     Canvas->TextOut(x,y,Caption);
   }       Canvas->Pen->Color = clBlack;
   Canvas->Brush->Color = clWhite;
   if (ALeFocus)
   {
     TRect FocRect;
     FocRect.Top = 4;
     FocRect.Left = 4;
     FocRect.Right = Width - 4;
     FocRect.Bottom = Height - 4;
     Canvas->DrawFocusRect(FocRect);
   }
   Canvas->Brush->Color = clBlack;
   Canvas->FrameRect(CoRect);
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseDown(TMouseButton Button,
                                       Classes::TShiftState Shift, int X, int Y)
{
     BtnEnfonce = true;
     MouseSurBtn = true;
     SetFocus();
     ALeFocus = true;
     Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseUp(TMouseButton Button,
                                       Classes::TShiftState Shift, int X, int Y)
{
     BtnEnfonce = false;
     Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::KeyDown(Word &Key, Classes::TShiftState Shift)
{
     TCustomControl::KeyDown(Key, Shift);
     if (Key == 32)
      {
       BtnEnfonceBis = true;
       Invalidate();
      }
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::KeyUp(Word &Key, Classes::TShiftState Shift)
{         
     TCustomControl::KeyUp(Key, Shift);
     if (Key == 32)
      {
       BtnEnfonceBis = false;
       Click();
       Invalidate();
      }
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::DoEnter()
{
     TCustomControl::DoEnter();
     ALeFocus = true;
     Invalidate(); // Pour afficher le rectangle de focus.
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::DoExit()
{
     TCustomControl::DoExit();
     ALeFocus = false;
     Invalidate(); // Pour effacer le rectangle de focus.
}
//---------------------------------------------------------------------------  
void __fastcall TCGiButton::SetFont(TFont *Value)
{
     FFont->Assign(Value);
     Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::SetRGB(TRGB *Value)
{
     FRGB->Assign(Value);
     Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::FontChanged(TObject *Sender)
{
     Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseLeave(TMessage &Msg)
{
    if(BtnEnfonce)
     {
       MouseSurBtn = false;
       Invalidate();
     }
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::MouseEnter(TMessage &Msg)
{
    if(BtnEnfonce)
     {
       MouseSurBtn = true;
       Invalidate();
     }  
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::TextChanged(TMessage &Msg)
{
     Invalidate(); // Pour afficher la modification du texte.
}
//---------------------------------------------------------------------------
void __fastcall TCGiButton::RGBChange(TObject *Sender)
{
     int h ;
     h = (CRGB->pasR > CRGB->pasG) ? CRGB->pasR : CRGB->pasG ;
     h = (h > CRGB->pasB) ? h : CRGB->pasB;
     if (h>0) if (Height > (255/h)) Height = 255/h;
     Invalidate();
}
//---------------------------------------------------------------------------
bool __fastcall TCGiButton::CanResize(int &NewWidth, int &NewHeight)
{
     int h ;
     h = (CRGB->pasR > CRGB->pasG) ? CRGB->pasR : CRGB->pasG ;
     h = (h > CRGB->pasB) ? h : CRGB->pasB;
     if (h==0) if (NewHeight > (255/h)) NewHeight = 255/h;
     return true;
}
//---------------------------------------------------------------------------
namespace Cgibutton
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TCGiButton)};
                 RegisterComponents("MesCompo", classes, 0);
        }
}     
/*生活是一種藝術,用心生活才能享受生活*/
發表人 - axsoft 於 2004/04/13 18:13:12
系統時間:2024-11-24 0:51:37
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!