// TODO: Return a different brush if the default is not desired return hbr; }void CStaticDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default static int i=0; CString str; str.Format("Timer Counter %d",i); i++; i %= 11; m_Static.SetWindowText(str); CDialog::OnTimer(nIDEvent); } 结果:
还是没用。。求助~void CMySubDialog::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//在这里自绘********************************************************************
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);//获取了窗口客户区大小
CDC memDC;//双缓冲内存dc
memDC.CreateCompatibleDC(&dc);
CBitmap bmpBG;//背景图
bmpBG.LoadBitmap(IDB_BITMAP1);//调入资源中的背景图
CBitmap* pOldbmp = (CBitmap*)memDC.SelectObject(&bmpBG);//将背景图缓冲到内存dc
dc.BitBlt(0, 0, rect.right, rect.bottom, &memDC, 0, 0, SRCCOPY);//绘制到窗口屏幕
memDC.SelectObject(pOldbmp);//释放资源
bmpBG.DeleteObject();
//CDialog::OnPaint();
}
}
我这样 自绘 还是有重影,请教哪里错了?void CMySubDialog::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//在这里自绘********************************************************************
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);//获取了窗口客户区大小
CDC memDC;//双缓冲内存dc
memDC.CreateCompatibleDC(&dc);
CBitmap bmpBG;//背景图
bmpBG.LoadBitmap(IDB_BITMAP1);//调入资源中的背景图
CBitmap* pOldbmp = (CBitmap*)memDC.SelectObject(&bmpBG);//将背景图缓冲到内存dc
dc.BitBlt(0, 0, rect.right, rect.bottom, &memDC, 0, 0, SRCCOPY);//绘制到窗口屏幕
memDC.SelectObject(pOldbmp);//释放资源
bmpBG.DeleteObject();
//CDialog::OnPaint();
}
}
我这样写 ,还是有重影 afx_msg LRESULT CMySubDialog::OnSubMsg(WPARAM wParam, LPARAM lParam)
{
CRect rc,rc2;
GetDlgItem(IDC_STATIC1)->GetWindowRect(&rc);
//转换为相对坐标
ScreenToClient(&rc);
//刷新指定区域,注意第2个参数为真,即刷新背景
InvalidateRect(&rc,true); GetDlgItem(IDC_STATIC2)->GetWindowRect(&rc2);
ScreenToClient(&rc2);
InvalidateRect(&rc2,true); // 将 wParam 强制转换为 CString 类型
CString* strMsg = (CString*)wParam;
int len = strMsg->GetLength();
int i = strMsg->Find('@');
CString str1 = strMsg->Left(i);
CString str2 = strMsg->Right(len-1-i);
SetDlgItemText(IDC_STATIC1,str2);
SetDlgItemText(IDC_STATIC2,str1);
return 0;
}
从以上情况来看,可不可以退出这样的结论:1、之前的文字残影并不是在父窗口上【因为我重绘窗口后,还是有 重影
2、InvalidateRect(&rc,true);在这里没有起作用?求救!
而是 “return (HBRUSH)GetStockObject(NULL_BRUSH);//返回一个空画刷”缘故。
{
// TODO: Add your message handler code here and/or call default
static int i=0;
CString str;
str.Format("Timer Counter %d",i);
i++;
CRect rc;
m_Static.GetWindowRect(&rc);
ScreenToClient(&rc);
InvalidateRect(&rc);
SendMessage(WM_PAINT,0,0);
// not : UpdateWindow();
//
m_Static.SetWindowText(str);
// CDialog::OnTimer(nIDEvent);
}
使用 UpdateWindow();
没办法 保证 对话框 重绘一次 ,static 重绘一次。
只好使用:
SendMessage(WM_PAINT,0,0);
你也可以试试。
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (CTLCOLOR_STATIC==nCtlColor)//静态文字控件
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,0,0));
return (HBRUSH)::GetStockObject(NULL_BRUSH);//透明画刷
}
// TODO: Return a different brush if the default is not desired
return hbr;
}
BOOL CStaticDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect rc;
GetClientRect(&rc);
pDC->FillRect(&rc,&m_brushPat);
return TRUE;
return CDialog::OnEraseBkgnd(pDC);
}
对话框 init // TODO: Add extra initialization here
//
LOGFONT lf;
::ZeroMemory(&lf,sizeof(lf));
CDC *pDC=GetDC();
lf.lfHeight = - MulDiv(32, pDC->GetDeviceCaps(LOGPIXELSX),72);
lf.lfCharSet= DEFAULT_CHARSET;// keep chinese !
lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;// fixed
m_Font.CreateFontIndirect(&lf);
m_Static.SetFont(&m_Font);
ReleaseDC(pDC); SetTimer(1,500,0);
return TRUE; // return TRUE unless you set the focus to a control对话框 构造:CStaticDlg::CStaticDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStaticDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStaticDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bitmap.LoadBitmap(IDB_BITMAP1);
m_brushPat.CreatePatternBrush(&m_bitmap);
}
头:
CBitmap m_bitmap;
CBrush m_brushPat;
CFont m_Font;
其实 这个 问题 极简单 , 因为 我的 对话框 是 用 一个 PattenBrush 来 画背景的,所以 把 他 返回给 static 就可以了。HBRUSH CStaticDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if((CTLCOLOR_STATIC==nCtlColor) && (pWnd==&m_Static))
{
pDC->SetBkMode(TRANSPARENT);
pDC->SetTextColor(RGB(255,0,0));
return m_brushPat;// (HBRUSH)::GetStockObject(NULL_BRUSH);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}void CStaticDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int i=0;
CString str;
str.Format("Timer Counter %d",i);
i++;
i %= 11;
m_Static.SetWindowText(str);
CDialog::OnTimer(nIDEvent);
}
结果:
{
// TODO: Add your specialized code here and/or call the base class
static CPoint ptLast;
if(pMsg->message == WM_MOUSEMOVE)
{
CPoint newPt;
if(GetAsyncKeyState(VK_LBUTTON))
{
CRect rc;
m_Static.GetWindowRect(&rc);
m_Static.ScreenToClient(&rc);
m_Static.MapWindowPoints(this,&rc);
newPt=pMsg->pt;
ScreenToClient(&newPt);
if(rc.PtInRect(newPt))
{
rc.OffsetRect(newPt.x-ptLast.x,newPt.y-ptLast.y);
ptLast = newPt;
m_Static.MoveWindow(&rc);
m_Static.RedrawWindow();
}
}
else
{
newPt=pMsg->pt;
ScreenToClient(&newPt);
ptLast = newPt;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
http://download.csdn.net/detail/schlafenhamster/6541179
“一个不常见的 使 对话框 中 控件 透明的 方法。 通过 CreatePatternBrush 产生一个 背景画刷,对话框和 所有透明控件都使用这个 画刷,非常 简单 有效的 方法。”