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

BCB VCL 漸層 Button -switch when pressed mode

尚未結案
allenx_wu
一般會員


發表:4
回覆:1
積分:1
註冊:2004-05-02

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-19 14:24:56 IP:61.216.xxx.xxx 未訂閱
BCB VCL 漸層 Button -switch when pressed mode 其我是用以Delphi K.Top討論區下載的ColorButton改寫的... 但似乎還有一個問題..... 新增.... Value這個屬性... 用以記載當前是按鈕按住或放開..... 如圖所示 目前最大的問題就是當fouce在其它元件時 假若使用者點選ColorButton 其Value屬性...不會變換 除非使用者再點選一次 方能有狀態改變...........     //ColorButton.c
 //---------------------------------------------------------------------------    #include     #pragma hdrstop    #include "ColorButton.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//    static inline void ValidCtrCheck(TColorButton *)
{
        new TColorButton(NULL);
}
//---------------------------------------------------------------------------
 __fastcall TColorButton::TColorButton(TComponent* Owner)
         : TButton(Owner), ColorLo_(clBtnShadow), ColorHi_(clBtnHighlight), Value_(false),
          Canvas_(new TCanvas()),draw_as_default_(false), GFlag_(true)
{    } // end the declaration
//---------------------------------------------------------------------------
namespace Colorbutton
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TColorButton)};
                 RegisterComponents("Standard", classes, 0);
        }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::CreateParams(TCreateParams& Params)
{
  TButton::CreateParams(Params);
  Params.Style |= BS_OWNERDRAW;
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::SetButtonStyle(bool ADefault)
{
  if (draw_as_default_ != ADefault)
  {
    draw_as_default_ = ADefault;
    InvalidateRect(Handle, NULL, FALSE);
  }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::CNDrawItem(TMessage& Msg)
{
  // grab pointer to the DRAWITEMSTRUCT
  const DRAWITEMSTRUCT* pDrawItem =
    reinterpret_cast
      (Msg.LParam);      // store the current state of
  // the target DC
  SaveDC(pDrawItem->hDC);
  // bind Canvas_ to the target DC
  Canvas_->Handle = pDrawItem->hDC;
  try
  {
    // extract the state flags...
    TOwnerDrawState state;        // if the button has keyboard focus
    if (pDrawItem->itemState & ODS_FOCUS)
    {
      state = state << odFocused;        }
    
    // if the button is pushed
    if (pDrawItem->itemState &
        ODS_SELECTED)
    {
      state = state << odSelected;
      
      if (Value_){
        Value_=false;
        GFlag_=true;
      }
      else{
        Value_=true;
        GFlag_=true;
      }        }        // if the button is disabled
    if (pDrawItem->itemState &
        ODS_DISABLED)
    {
      state = state << odDisabled;
    }        // draw the button's face
    DoDrawButtonFace(state);        // draw the button's text
    DoDrawButtonText(state);
  }
  catch (...)
  {
    // clean up
    Canvas_->Handle = NULL;
    RestoreDC(pDrawItem->hDC, -1);
  }
  // clean up
  Canvas_->Handle = NULL;
  RestoreDC(pDrawItem->hDC, -1);      // reply TRUE
  Msg.Result = TRUE;
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::DoDrawButtonFace(const TOwnerDrawState& state)
{
  int i,intR_Par,intG_Par,intB_Par;      // draw a colored button...
  //Button上繪入漸層圖像
  //Canvas_->Brush = Brush;      intR_Par=(227-173)/Height;
  intG_Par=(236-177)/Height;
  intB_Par=(234-204)/Height;      for (i=0;iPen->Color=TColor(RGB((227-intR_Par*i),(236-intG_Par*i),(234-intB_Par*i)));
    Canvas_->MoveTo(2,i+2);
    Canvas_->LineTo(Width-2,i+2);
  }          TRect RClient = ClientRect;      // if the button is the default button
  // or has keyboard focus...      if (draw_as_default_ ||
      state.Contains(odFocused))
  {
      Canvas_->Pen->Color=clBtnHighlight;
      Canvas_->MoveTo(Width,0);
      Canvas_->LineTo(0,0);
      Canvas_->LineTo(0,Height-1);
      Canvas_->Pen->Color=cl3DLight;
      Canvas_->MoveTo(Width-1,1);
      Canvas_->LineTo(1,1);
      Canvas_->LineTo(1,Height-2);          Canvas_->Pen->Color=cl3DDkShadow;
      Canvas_->MoveTo(0,Height-1);
      Canvas_->LineTo(Width-1,Height-1);
      Canvas_->LineTo(Width-1,0);
      Canvas_->Pen->Color=clBtnShadow;
      Canvas_->MoveTo(Width-2,2);
      Canvas_->LineTo(Width-2,Height-2);
      Canvas_->LineTo(0,Height-2);
    InflateRect(
       reinterpret_cast
(&RClient),
       -1, -1
       );
   
  }      if (!(state.Contains(odSelected)))
  {
  // if the button isn't pushed...
    if (Value_){
      Canvas_->Pen->Color=cl3DDkShadow;
      Canvas_->MoveTo(Width,0);
      Canvas_->LineTo(0,0);
      Canvas_->LineTo(0,Height-1);
      Canvas_->Pen->Color=clBtnShadow;
      Canvas_->MoveTo(Width-1,1);
      Canvas_->LineTo(1,1);
      Canvas_->LineTo(1,Height-2);          Canvas_->Pen->Color=clBtnHighlight;
      Canvas_->MoveTo(0,Height-1);
      Canvas_->LineTo(Width-1,Height-1);
      Canvas_->LineTo(Width-1,0);
      Canvas_->Pen->Color=cl3DLight;
      Canvas_->MoveTo(Width-2,2);
      Canvas_->LineTo(Width-2,Height-2);
      Canvas_->LineTo(0,Height-2);
        }
    else{
      Canvas_->Pen->Color=clBtnHighlight;
      Canvas_->MoveTo(Width,0);
      Canvas_->LineTo(0,0);
      Canvas_->LineTo(0,Height-1);
      Canvas_->Pen->Color=cl3DLight;
      Canvas_->MoveTo(Width-1,1);
      Canvas_->LineTo(1,1);
      Canvas_->LineTo(1,Height-2);          Canvas_->Pen->Color=cl3DDkShadow;
      Canvas_->MoveTo(0,Height-1);
      Canvas_->LineTo(Width-1,Height-1);
      Canvas_->LineTo(Width-1,0);
      Canvas_->Pen->Color=clBtnShadow;
      Canvas_->MoveTo(Width-2,2);
      Canvas_->LineTo(Width-2,Height-2);
      Canvas_->LineTo(0,Height-2);
    }
  }    }
//---------------------------------------------------------------------------
void __fastcall TColorButton::DoDrawButtonText(const TOwnerDrawState& state)
{
  int i,intR_Par,intG_Par,intB_Par;      if (Caption.Length() == 0) return;      RECT RText = {0, 0, Width, Height};
  Canvas_->Font = Font;      // draw a colored button...
  //Button上繪入漸層圖像
  //Canvas_->Brush = Brush;
  intR_Par=(227-173)/Height;
  intG_Par=(236-177)/Height;
  intB_Par=(234-204)/Height;      for (i=0;iPen->Color=TColor(RGB((227-intR_Par*i),(236-intG_Par*i),(234-intB_Par*i)));
  Canvas_->MoveTo(2,i+2);
  Canvas_->LineTo(Width-2,i+2);
  }      SetBkMode(
    Canvas_->Handle, TRANSPARENT
    );      // if the button is pushed...
  if (state.Contains(odSelected))
  {
     // offset the caption
     OffsetRect(&RText, 1, 1);
  }      // if the button is disabled...
  if (!Enabled ||
      state.Contains(odDisabled))
  {        // render the caption
    // in a disabled fashion
    OffsetRect(&RText, 1, 1);
    Canvas_->Font->Color = ColorHi_;
    DrawText(
      Canvas_->Handle, Caption.c_str(),
      -1, &RText, DT_CENTER |
      DT_VCENTER | DT_SINGLELINE
      );
    OffsetRect(&RText, -1, -1);
    Canvas_->Font->Color = ColorLo_;
  }      // render the caption
  DrawText(
    Canvas_->Handle, Caption.c_str(), -1,
    &RText, DT_CENTER | DT_VCENTER |
    DT_SINGLELINE
    );      // if the button has keyboard focus...
  if (state.Contains(odFocused))
  {
    // render the selection rectangle
    TRect RFocus = ClientRect;
    InflateRect(
      reinterpret_cast
(&RFocus),
      -4, -4
      );
    Canvas_->DrawFocusRect(RFocus);
  }
}
//---------------------------------------------------------------------------
TColor __fastcall TColorButton::GetColor()
{
  return Brush->Color;
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::SetColor(TColor Value)
{
  if (Brush->Color != Value)
  {
    Brush->Color = Value;
    InvalidateRect(Handle, NULL, TRUE);
  }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::SetColorLo(TColor Value)
{
  if (ColorLo_ != Value)
  {
    ColorLo_ = Value;
    InvalidateRect(Handle, NULL, TRUE);
  }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::SetColorHi(TColor Value)
{
  if (ColorHi_ != Value)
  {
    ColorHi_ = Value;
    InvalidateRect(Handle, NULL, TRUE);
  }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::SetValue(bool Value)
{
  if (Value_ != Value)
  {
    Value_ = Value;
    InvalidateRect(Handle, NULL, TRUE);
  }
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::CMFontChanged(TMessage& Msg)
{
  TButton::Dispatch(&Msg);
  InvalidateRect(Handle, NULL, TRUE);
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::CMEnabledChanged(TMessage& Msg)
{
  TButton::Dispatch(&Msg);
  InvalidateRect(Handle, NULL, TRUE);
}
//---------------------------------------------------------------------------
void __fastcall TColorButton::WMLButtonDblClk(TMessage& Msg)
{
  SNDMSG(
    Handle, WM_LBUTTONDOWN,
    Msg.WParam, Msg.LParam
    );
}
//ColorButton.h
 //---------------------------------------------------------------------------    #ifndef ColorButtonH
#define ColorButtonH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include 
#include     //---------------------------------------------------------------------------
class PACKAGE TColorButton : public TButton
{
private:
  TColor ColorLo_;
  TColor ColorHi_;
  std::auto_ptr Canvas_;
  bool draw_as_default_,Value_,GFlag_;      TColor __fastcall GetColor();      void __fastcall SetValue(bool Value);      void __fastcall SetColor(TColor Value);
  void __fastcall SetColorLo(TColor Value);
  void __fastcall SetColorHi(TColor Value);      MESSAGE void __fastcall CNDrawItem(TMessage& Msg);
  MESSAGE void __fastcall WMLButtonDblClk(TMessage& Msg);
  MESSAGE void __fastcall CMFontChanged(TMessage& Msg);
  MESSAGE void __fastcall CMEnabledChanged(TMessage& Msg);    protected:
  // inherited member functions
  virtual void __fastcall CreateParams(TCreateParams& Params);
  virtual void __fastcall SetButtonStyle(bool ADefault);
  // introduced member functions
  virtual void __fastcall DoDrawButtonFace(const TOwnerDrawState& state);
  virtual void __fastcall DoDrawButtonText(const TOwnerDrawState& state);    public:
  BEGIN_MESSAGE_MAP
  MESSAGE_HANDLER(CN_DRAWITEM, TMessage, CNDrawItem)
  MESSAGE_HANDLER(WM_LBUTTONDBLCLK, TMessage, WMLButtonDblClk)
  MESSAGE_HANDLER(CM_FONTCHANGED, TMessage, CMFontChanged)
  MESSAGE_HANDLER(CM_ENABLEDCHANGED, TMessage, CMEnabledChanged)
  END_MESSAGE_MAP(TButton)            __fastcall TColorButton(TComponent* Owner);
__published:
   __property TColor Color = {read = GetColor, write = SetColor};
  __property TColor ColorLo = {read = ColorLo_, write = SetColorLo,default = clBtnShadow};
  __property TColor ColorHi = {read = ColorHi_, write = SetColorHi,default = clBtnHighlight};
  __property bool Value = {read = Value_, write = SetValue,default = false};
};
//---------------------------------------------------------------------------
#endif
allenx_wu
一般會員


發表:4
回覆:1
積分:1
註冊:2004-05-02

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-27 15:44:25 IP:211.20.xxx.xxx 未訂閱
有需要該程式碼的...可至如下下載 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=78963
系統時間:2024-05-05 19:18:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!