给 OnPaint 加个 HDC 参数

MFC的窗口绘制函数是
CWnd::OnPaint  
afx_msg void OnPaint( );


而 WM_PAINT 消息可以是


WM_PAINT 
hdc = (HDC) wParam;


就是说OnPaint可以带个 HDC 的参数.
// in head file
// afx_msg void OnPaint();
afx_msg void OnMyPaint(WPARAM wParam, LPARAM lParam);


// in cpp file
// ON_WM_PAINT()
ON_MESSAGE(WM_PAINT, OnMyPaint)
//
//Replace OnPaint()
void CScrollBitmapDlg::OnMyPaint(WPARAM wParam, LPARAM lParam)
{
    if(wParam)
    {// paint to memDC
         CDC *pDC=CDC::FromHandle((HDC)wParam);
         pDC->BitBlt(m_rcPicture.left,m_rcPicture.top,m_rcPicture.Width(),
m_rcPicture.Height(),&m_dcMem, m_Sourcex, m_Sourcey,SRCCOPY);
     }
...
}
使用方法
void CScrollBitmapDlg::CopyPictures(HWND hWnd,CPoint point) 
{// TODO: Add your message handler code here
UINT ID=::GetDlgCtrlID(hWnd);
// afxDump << ID << " ID\n";
// size
CRect rc;
if(hWnd==0)
{
ID=0;
GetWindowRect(&rc);// the dialog
}
else
{
::GetWindowRect(hWnd,&rc);
}
// create dc
CClientDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
// create empty bmp
HBITMAP bmp=CreateCompatibleBitmap(dc /* not dcMem */, rc.Width(),rc.Height());
dcMem.SelectObject(bmp);
//
CString prompt;
switch (ID)
{
case IDOK:// "Exit"
m_Ok.SendMessage(WM_PAINT,(WPARAM)dcMem.m_hDC,0);
prompt="\"Exit\" button has been copied to clipbroad!";
break;
case IDC_SCROLLBAR1:// "H bar"
m_hBar.SendMessage(WM_PAINT,(WPARAM)dcMem.m_hDC,0);
prompt="\"Horizontal\" scrollbar has been copied to clipbroad!";
break;
case IDC_SCROLLBAR2:// "V bar"
m_vBar.SendMessage(WM_PAINT,(WPARAM)dcMem.m_hDC,0);
prompt="\"Vertical\" scrollbar has been copied to clipbroad!";
break;
case IDC_SHOW:// "Open"
m_Open.SendMessage(WM_PAINT,(WPARAM)dcMem.m_hDC,0);
prompt="\"Open\" button has been copied to clipbroad!";
break;
case IDC_PICTURE:// m_Pic
OnMyPaint((WPARAM)dcMem.m_hDC,0);
prompt="\"CStatic Picture\" has been copied to clipbroad!";
break;
case 0:// the dialog
this->SendMessage(WM_PRINT,(WPARAM)dcMem.m_hDC,
(LPARAM)(PRF_NONCLIENT | PRF_CHILDREN | PRF_CLIENT | 


PRF_ERASEBKGND | PRF_OWNED));
prompt="\"The whole dialog\" has been copied to clipbroad!";
break;
}
// move bmp to clipboard
CBitmap *pbmp=dcMem.GetCurrentBitmap();
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP,pbmp->GetSafeHandle());
    CloseClipboard();
//
DeleteObject(bmp);
//
AfxMessageBox(prompt);
}
阅读更多
文章标签: MFC CDC ONPAINT
个人分类: MFC
上一篇工具条停靠后窗口的扩展
下一篇用GDI+画长方体
想对作者说点什么? 我来说一句

给gpedit.msc加个密码

2009年12月27日 544KB 下载

没有更多推荐了,返回首页

关闭
关闭