VC界面/窗口的一些小技巧

  • 发表于
  • VC

1、刷新窗口

RedrawWindow(NULL, NULL,RDW_INVALIDATE | RDW_ERASE );

2、设置对话框背景颜色

在InitInstance函数(CChatAPP类中)中的

m_pMainWnd = &dlg;

下面加入

SetDialogBkColor(RGB(240,128,128),RGB(0,0,0));

RGB(240,128,128)为背景颜色,RGB(0,0,0)为文本颜色

3、设置对话框背景图片

void CChatDlg::OnPaint()中
 
//CDialog::OnPaint();
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);
//IDB_BITMAP是你自己的图对应的ID
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,
bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);

4、设置Static Test为透明背景

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

// 改变所有的STATIC为透明,当然也可以将CTLCOLOR_STATIC改为CTLCOLOR_EDIT
if(nCtlColor==CTLCOLOR_STATIC)
{
pDC->SetBkMode(TRANSPARENT);
hbr=(HBRUSH)::GetStockObject(NULL_BRUSH);
 }
// RedrawWindow(NULL, NULL,RDW_INVALIDATE | RDW_ERASE );
 
//改变单个的控件为透明
 
/*
if(nCtlColor == CTLCOLOR_STATIC && pWnd->GetDlgCtrlID() == IDC_STATIC54)
 { 
// IDC_XXX_STATIC 为你想要设为透明的控件ID
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
*/
// TODO: Return a different brush if the default is not desired
return hbr;
}

5、获取时间

CTime t=CTime::GetCurrentTime();
//CString time=t.Format(“%H:%M:%S”);
CString time=t.Format(“%Y-%m-%d %H:%M:%S”);