给 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);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值