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);
}
給 OnPaint 加個 HDC 參數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
小金魚系列win32-1
Debug_hws
2018-08-27 19:17:41
mfc 圖像類函數定義
tony_0620
2018-08-27 19:07:33
mfc線程同步與資源互斥
tony_0620
2018-08-27 19:06:51
BITMAPFILEHEADER、BITMAPINFOHEADER及BMP結構詳解
tony_0620
2018-08-27 19:06:50
mfc HOOK編程
tony_0620
2018-08-27 19:06:39
靜態文本框的鼠標事件響應
tony_0620
2018-08-27 19:06:39
DoDataExchange函數的作用
tony_0620
2018-08-27 19:06:39
好久沒寫對話框程序了都忘光了總結下
xyygwy
2018-08-27 18:31:16
Unicode環境下寬字符CString與ANSI字符const char* 的轉換
markqian86
2018-08-27 18:31:14
ListCtrl和CTreeCtrl 實際可用 失去焦點 後自定義顏色
wujinzbr
2018-08-27 18:21:51
一份完整的com組件服務端和客戶端教程
wujinzbr
2018-08-27 18:21:51
MFC 設置按鈕背景,點擊後切換背景(不是按下時和鬆開時切換)
Constantine_W
2018-08-27 18:18:32