VC++の3 Dテキストを持つボタンコントロール
上記の目的をOwnerDrawスタイルで実現します.
1.AppWizardを実行してダイアログベースのtestプロジェクトを生成し、ダイアログボックスにCButtonコントロールを追加します.CButtonコントロールのGeneralプロパティページでコントロールのIDをIDC_に変更3 DTEXTBTN、Captionを「誰が狂っているか」に変更し、コントロールStylesプロパティページでOwnerDrawを選択し、残りの設定はデフォルトのままにします.
2.classwizardで新しいクラスを作成します:C 3 dTextButton、ベースクラスはCButtonです.
3.C 3 dTextButtonクラスにprotectedの関数void Draw(CDC*pDC,const CRect&rect,UINT state)を追加します.次のようにコードを記述します.
4.C 3 dTextButtonクラスのDrawItem関数をclasswizardで再ロードします.記述コードは次のとおりです.
5.classwizardをIDC_とする3 DTEXTBTN C 3 dTextButtonコントロール変数m_を作成3dTextButton1.
6.「3 dTextButton.h」をtestDlgヘッダファイルに追加します.
7.アプリケーションをコンパイルしてテストします.さあ、労働の成果を鑑賞することができます.
1.AppWizardを実行してダイアログベースのtestプロジェクトを生成し、ダイアログボックスにCButtonコントロールを追加します.CButtonコントロールのGeneralプロパティページでコントロールのIDをIDC_に変更3 DTEXTBTN、Captionを「誰が狂っているか」に変更し、コントロールStylesプロパティページでOwnerDrawを選択し、残りの設定はデフォルトのままにします.
2.classwizardで新しいクラスを作成します:C 3 dTextButton、ベースクラスはCButtonです.
3.C 3 dTextButtonクラスにprotectedの関数void Draw(CDC*pDC,const CRect&rect,UINT state)を追加します.次のようにコードを記述します.
void C3dTextButton::Draw(CDC *pDC, const CRect &rect, UINT state)
{
CString text; GetWindowText(text);
int l=text.GetLength();
CRect rectClient=rect;
//
CFont* pFont=GetFont();
//
LOGFONT logfont;
pFont->GetObject(sizeof(LOGFONT),&logfont);
if(logfont.lfHeight==0)logfont.lfHeight=20;
logfont.lfWidth=0;// 0,
logfont.lfWeight=1000;
logfont.lfEscapement=logfont.lfOrientation=0;
CFont tryfont; VERIFY(tryfont.CreateFontIndirect(&logfont));
CFont* pFontOld=pDC->SelectObject(&tryfont);
// , ,
CSize textSizeClient=pDC->GetTextExtent(text,l);
if(rectClient.Width()*textSizeClient.cy>rectClient.Height()*textSizeClient.c
x){
logfont.lfHeight=::MulDiv(logfont.lfHeight,rectClient.Height(),textSizeC
lient.cy);
}
else{
logfont.lfHeight = ::MulDiv(logfont.lfHeight,rectClient.Width(),textSize
Client.cx);
}
//
CFont font; font.CreateFontIndirect(&logfont);
pDC->SelectObject(&font);
textSizeClient=pDC->GetTextExtent(text,l);
// minx,miny
int minx=rectClient.left+(rectClient.Width()-textSizeClient.cx)/2;
int miny=rectClient.top+(rectClient.Height()-textSizeClient.cy)/2;
int oldBkMode=pDC->SetBkMode(TRANSPARENT);
COLORREF textcol=::GetSysColor(COLOR_BTNTEXT);
COLORREF oldTextColor=pDC->SetTextColor(textcol);
int cx = minx;
int cy = miny;
int s=(state&ODS_SELECTED)?-1:+1;
cx+= 3; cy+= 3;
// 3D
pDC->SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
pDC->TextOut(cx-s*2,cy+s*2,text);
pDC->TextOut(cx+s*2,cy-s*2,text);
pDC->TextOut(cx+s*2,cy+s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
pDC->TextOut(cx+s*1,cy-s*2,text);
pDC->TextOut(cx-s*2,cy+s*1,text);
pDC->TextOut(cx-s*2,cy-s*2,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
pDC->TextOut(cx-s*1,cy+s*1,text);
pDC->TextOut(cx+s*1,cy-s*1,text);
pDC->TextOut(cx+s*1,cy+s*1,text);
pDC->SetTextColor(::GetSysColor(COLOR_3DLIGHT));
pDC->TextOut(cx,cy-s*1,text);
pDC->TextOut(cx-s*1,cy,text);
pDC->TextOut(cx-s*1,cy-s*1,text);
pDC->SetTextColor(textcol);
//
pDC->TextOut(cx,cy,text);
//
pDC->SetTextColor(oldTextColor);
pDC->SetBkMode(oldBkMode);
pDC->SelectObject(pFontOld);
}
4.C 3 dTextButtonクラスのDrawItem関数をclasswizardで再ロードします.記述コードは次のとおりです.
void C3dTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
ASSERT_VALID(pDC);
CRect rectClient=lpDrawItemStruct->rcItem;
Draw(pDC,rectClient,lpDrawItemStruct->itemState);
}
5.classwizardをIDC_とする3 DTEXTBTN C 3 dTextButtonコントロール変数m_を作成3dTextButton1.
6.「3 dTextButton.h」をtestDlgヘッダファイルに追加します.
7.アプリケーションをコンパイルしてテストします.さあ、労働の成果を鑑賞することができます.