设置对话框的图片背景,控件的字体及透明等,字体重叠等

用MFC ClassWizard添加WM_CTLCOLOR消息:
HBRUSH CSerialTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

HBRUSH hbr= CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 static CBrush gBr;
 static bool isInited = false;


// TODO: Change any attributes of the DC here



// TODO: Return a different brush if the default is not desired



switch(nCtlColor)//注意nCtlColor的值,可以再msdn看的到,所以,我们在不同的case,也就是不同的控件中设置各个控件的字体,颜色,及透明等

{

case CTLCOLOR_STATIC: //对所有静态文本控件的设置

{

pDC->SetBkMode(TRANSPARENT);//设置背景为透明

pDC->SetTextColor(RGB(0,0,0)); //设置字体颜色

return HBRUSH(GetStockObject(HOLLOW_BRUSH)); // 必须

}

case CTLCOLOR_DLG: //对所有静态文本控件的设置

{

pDC->SetBkMode(TRANSPARENT);

return hbr; //返回画刷句柄

}

default:

return CDialog::OnCtlColor(pDC,pWnd, nCtlColor);

}

 if(!isInited) 
 {
  
  CBitmap bitmap;
  
  bitmap.LoadBitmap(IDB_BITMAP3);
  
  gBr.CreatePatternBrush(&bitmap);
  
  COLORREF clearColor = -1;
  
  bitmap.DeleteObject();
  
  isInited = true;
 }
 if(pWnd==this) 
 {
  pDC->SetBkMode(TRANSPARENT);
  
  return gBr; //主窗口背景使用这个背景刷
 }

return hbr;

}

msdn:

 

CWnd::OnCtlColor

afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );

Return Value

OnCtlColor must return a handle to the brush that is to be used for painting the control background.

Parameters

pDC

Contains a pointer to the display context for the child window. May be temporary.

pWnd

Contains a pointer to the control asking for the color. May be temporary.

nCtlColor

Contains one of the following values, specifying the type of control:

  • CTLCOLOR_BTN Button control

  • CTLCOLOR_DLG Dialog box

  • CTLCOLOR_EDIT Edit control

  • CTLCOLOR_LISTBOX List-box control

  • CTLCOLOR_MSGBOX Message box

  • CTLCOLOR_SCROLLBAR Scroll-bar control

  • CTLCOLOR_STATIC Static control

 

,,在说下,将文本框或者其他控件设置为透明时,当每次显示时字体的改变,会使该区域字体叠加问题。

其实问题所在就是你将窗口重绘下,就ok,在你每次显示字的代码前边加代码:

  CRect rect;
  GetWindowRect(&rect);
  CWnd* pParent = GetParent();
  if (pParent)
  {
   pParent->ScreenToClient(&rect);
   pParent->InvalidateRect(&rect);
  }
  UpdateWindow();

亲测,ok

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值