CButton::DrawItem

更新 : 2007 年 11 月

オーナー描画ボタンの表示上の外観が変化すると、フレームワークが呼び出します。

virtual void DrawItem(
   LPDRAWITEMSTRUCT lpDrawItemStruct 
);

パラメータ

  • lpDrawItemStruct
    DRAWITEMSTRUCT 構造体への long ポインタ。この構造体には、描画する項目や描画種別に関する情報を指定します。

解説

オーナー描画ボタンには、BS_OWNERDRAW スタイルがあります。オーナー描画の CButton オブジェクトの描画を実装するには、このメンバ関数をオーバーライドします。アプリケーションでは、メンバ関数が終了する前に、lpDrawItemStruct で指定されたディスプレイ コンテキストに対して選択されているすべてのグラフィック デバイス インターフェイス (GDI) オブジェクトを元に戻す必要があります。

BS_ スタイル値も参照してください。

使用例

// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"), 
//      WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, 
//      CRect(10,10,100,30), pParentWnd, 1);
//

// This example implements the DrawItem method for a CButton-derived 
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
   UINT uStyle = DFCS_BUTTONPUSH;

   // This code only works with buttons.
   ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);

   // If drawing selected, add the pushed style to DrawFrameControl.
   if (lpDrawItemStruct->itemState & ODS_SELECTED)
      uStyle |= DFCS_PUSHED;

   // Draw the button frame.
   ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
      DFC_BUTTON, uStyle);

   // Get the button's text.
   CString strText;
   GetWindowText(strText);

   // Draw the button text using the text color red.
   COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
   ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
      &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

必要条件

ヘッダー : afxwin.h

参照

参照

CButton クラス

階層図

CButton::SetButtonStyle

WM_DRAWITEM

その他の技術情報

CButton のメンバ