按鈕載入點陣圖的幾個方法 vc
一,只加載一張點陣圖的方法:
1,
裝入bmp資源,id為IDB_BMP,按鈕的bitmap屬性設為true,icon屬性為false。
2,
CButton *pBtn = (CButton *)GetDlgItem(IDB_BMP);
CBitmap bitMap;
HBITMAP hBit ;
if(bitMap.LoadBitmapW(IDB_NORMAL_BTN))
{
hBit = (HBITMAP)bitMap.Detach();
pBtn->SetBitmap(hBit);
}
缺點:圖片不會自動拉伸。
二,為按鈕的不同狀態載入不同的點陣圖:(使用CBitmapButton類)這種方法還可以。
1,按鈕屬性 Owner Draw選上,按鈕ID: IDC_BUTTON1
為IDC_BUTTON1新增CButton變數m_button,
2,然後手動將CButton m_button改為
CBitmapButton m_button;
3,在初始化裡邊
m_button.LoadBitmaps(IDB_BITMAP1, IDB_BITMAP2);//IDB_BITMAP1:平時;IDB_BITMAP2:按下
m_button.SubclassDlgItem(IDC_BUTTON1, this);
m_button.SizeToContent();
或者用另一種方式,不必為IDC_BUTTON1新增關聯的變數。
直接宣告CBitmapButton m_button;,在初始化裡邊呼叫
m_BitmapBtn.AutoLoad(IDC_BUTTON1_AREA,this);//把按鈕和變數聯絡起來
m_BitmapBtn.LoadBitmaps(IDB_BITMAP1, IDB_BITMAP2);
m_button.SizeToContent();
缺點:1,無法顯示文字,需過載DrawItem函式。
2,無法去掉圖片的背景色,即只能顯示矩形按鈕。
// 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); }
//繼承CBitmapButton的例子
void MyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
//從lpDrawItemStruct獲取控制元件的相關資訊
CRect rect=lpDrawItemStruct-> rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct-> hDC);
int nSaveDC=pDC-> SaveDC();
UINT state = lpDrawItemStruct-> itemState;
TCHAR strText[MAX_PATH + 1];
::GetWindowText(m_hWnd, strText, MAX_PATH);
CBitmapButton::DrawItem(lpDrawItemStruct);
CRect rect1=rect;
rect.SetRect(rect1.left,rect1.top,rect1.left+75,rect1.top+24);
//顯示按鈕的文字
pDC-> SetTextColor(TextColor);
if (strText!=NULL)
{
CFont* hFont = GetFont();
CFont* hOldFont = pDC-> SelectObject(hFont);
CSize szExtent = pDC-> GetTextExtent(strText, lstrlen(strText));
CPoint pt( rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
if (state & ODS_SELECTED)
pt.Offset(1, 1);
int nMode = pDC-> SetBkMode(TRANSPARENT);
if (state & ODS_DISABLED)
pDC-> DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
else
pDC-> DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
pDC-> SelectObject(hOldFont);
pDC-> SetBkMode(nMode);
}
pDC-> RestoreDC(nSaveDC);
}
三,繼承CButton類,重寫OnPaint函式,為Button的不同狀態畫點陣圖(BitBlt,TransparentBlt,StretchBlt ,MaskBlt,
PlgBlt
)。
四,
可以採用CButtonST控制元件,挺好用的,比MFC的那些封裝好用。
假設按鈕ID為IDC_BUTTON1
1.新增成員變數
CButtonST m_btn;
2.新增點陣圖資源,ID設為IDB_BITMAP1
3.在OnInitDialog函式中初始化按鈕m_btn.SubclassDlgItem(IDC_BUTTON1,this);
m_btn.SetBitmaps(IDB_BITMAP1,RGB(0,0,0));
m_btn.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
注: 上面的SetBitmaps函式會將圖片中顏色值為RGB(0,0,0)的點設為透明。
五,
把button按鈕屬性設定為ower draw
然後對映onDrawItem訊息
在ondraw函式內自己繪製就可以了
void CUi6Dlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if(nIDCtl == IDC_HELLO_CFAN)
{
//繪製按鈕框架
UINT uStyle = DFCS_BUTTONPUSH;
//是否按下去了?
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);
//輸出文字
dc.SelectObject(&m_Font);
dc.SetTextColor(RGB(0, 0, 255));
dc.SetBkMode(TRANSPARENT);
CString sText;
m_HelloCFan.GetWindowText(sText);
dc.TextOut(lpDrawItemStruct->rcItem.left + 20, lpDrawItemStruct->rcItem.top + 20, sText);
//是否得到焦點
if(lpDrawItemStruct->itemState & ODS_FOCUS)
{
//畫虛框
CRect rtFocus = lpDrawItemStruct->rcItem;
rtFocus.DeflateRect(3, 3);
dc.DrawFocusRect(&rtFocus);
}
return;
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}