日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

CStatic的透明背景方法

發布時間:2023/12/13 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 CStatic的透明背景方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文鏈接:http://blog.sina.com.cn/s/blog_4a470fcc01000406.html
這篇文章中有些許錯誤,不過思路值得借鑒

如果在一個有顏色的窗體中創建一個CStatic的對象X,而且該X要改變它的文本內容,那么就有一個X背景是默認窗體背景的問題,而不是那個顏色窗體的背景,這是因為,在SetWindowText時會OnEraseBkgnd擦除X原來的界面的背景,然后調用默認的Onpaint畫上window默認的窗體顏色。

解決辦法有兩個:
方法1
接受ON_WM_CTLCOLOR消息,該消息攔截了畫窗體以及窗體上控件的背景顏色的操作
實現如下:

HBRUSH CRDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

//CTLCOLOR_STATIC為 CStatic
//CTLCOLOR_EDIT
//CTLCOLOR_LISTBOX
//CTLCOLOR_BTN
//CTLCOLOR_DLG等等

if ( nCtlColor == CTLCOLOR_STATIC)
{
static HBRUSH hbrStatic = ::CreateSolidBrush(RGB(255, 0, 255));
//COLOR是你想設置的背景顏色此處必須為靜態變量,否則不能實現
pDC->SetBkColor(RGB(255, 0, 255));
return hbrStatic ; //返回該刷
}
return hbr;
}


方法2
思路是:創建一個CSatic的類CStaticEx、,包含一個CDC的成員m_memDC,把窗體的背景作為m_memDC的初始化,在CStaticEx的OnEraseBkgnd內BitBlt m_memDC則,這是X的背景為窗體的背景了

// 在OnPaint內初始化m_memDC
void CStaticEx::OnPaint()
{
static BOOL bFirst = TRUE;

CFont font;
VERIFY(font.CreateFont(
15,// nHeight
0,// nWidth
0,// nEscapement
0,// nOrientation
FW_NORMAL,// nWeight
FALSE,// bItalic
FALSE,// bUnderline
0,// cStrikeOut
DEFAULT_CHARSET,// nCharSet
OUT_DEFAULT_PRECIS,// nOutPrecision
CLIP_DEFAULT_PRECIS,// nClipPrecision
DEFAULT_QUALITY,// nQuality
DEFAULT_PITCH | FF_SWISS,// nPitchAndFamily
"Arial"));// lpszFacename
CPaintDC dc(this); // device context for painting
CRect rcStatic, rcParent;
GetWindowRect(&rcStatic);

if(bFirst)//這里把父窗口所畫的保存起來
{
CRect rcStaticInParent;
bFirst = FALSE;
CWnd* pParent = GetParent();

rcStaticInParent = rcStatic;
pParent->ScreenToClient(&rcStaticInParent);

CDC* pParentDC = pParent->GetDC();
m_memBp.CreateCompatibleBitmap(&dc, rcStaticInParent.Width(), rcStaticInParent.Height());
m_memDC.CreateCompatibleDC(&dc);
m_memDC.SelectObject(m_memBp.GetSafeHandle());
SYS_DEBUG_OUT(Output_Console,"rcStaticInParent.left=%d; rcStaticInParent.top=%d; rc.bottom=%d;rc.right=%d",rcStaticInParent.left,rcStaticInParent.top,rcStaticInParent.bottom, rcStaticInParent.right);

m_memDC.StretchBlt(0, 0, rcStaticInParent.Width(), rcStaticInParent.Height(), pParentDC, rcStaticInParent.left, rcStaticInParent.top, rcStaticInParent.Width(), rcStaticInParent.Height(),SRCCOPY);
GetParent()->ReleaseDC(pParentDC);
}

// 寫CStatic文本
CString strWinText;
GetWindowText(strWinText);
dc.SelectObject(&font);
dc.SetTextColor(RGB(255,255,255));
dc.SetBkMode(TRANSPARENT);
dc.TextOut(0,0,strWinText);
}

BOOL CStaticEx::OnEraseBkgnd(CDC* pDC)
{
CRect rc;
GetClientRect(&rc);
SYS_DEBUG_OUT(Output_Console,"rc.left=%d; rc.top=%d; rc.bottom=%d;rc.right=%d",rc.left,rc.top,rc.bottom, rc.right);
pDC->BitBlt(0, 0, rc.Width(),rc.Height(), &m_memDC,0, 0 ,SRCCOPY);

return FALSE;
}




總結

以上是生活随笔為你收集整理的CStatic的透明背景方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。