?
在這個項目中用到了幾個GDI的函數,包括CFont ?CPen ?CBrush等,一般要和設備上下文DC結合起來使用。
并且創建GDI對象使用完后一定要釋放,否則可能會造成資源泄漏
對于CPen CFont CBrush用構造函數定義的GDI對象 和 用 CreateXXX獲得的對象在釋放時要調用DeleteObject
對于GetXXX獲得的對象在釋放時要使用ReleaseObject。
1 CDC *pDC0 = GetDlgItem(IDC_VIDEO0)-> GetDC(); 2 CFont font; // GDI對象 3 font.CreatePointFont( 200 , _T( " 宋體 " )); // CreateXXX 4 CFont *oldFont = pDC0->SelectObject(& font); 5 pDC0->TextOut( 20 , 20 , CString(_T( " 1 " ))); 6 pDC0-> SelectObject(oldFont); 7 8 GetDlgItem(IDC_VIDEO0)-> ReleaseDC(pDC0); 9 10 font.DeleteObject(); // 釋放GDI對象資源
?
下面的例子是畫一個矩形的邊框,矩形是一個Picture控件,當點擊該控件時給這個控件的邊緣畫顏色
1 CRect rect; 2 CDC* pDC = GetDlgItem(m_nCurrentPicture)-> GetDC(); 3 GetDlgItem(m_nCurrentPicture)->GetClientRect(& rect); 4 5 CPen pen(PS_SOLID, 10 , RGB( 0x99 , 0x32 , 0xcc )); 6 CPen *oldPen = NULL; 7 oldPen = pDC->SelectObject(& pen); 8 9 // 上邊框; 10 pDC-> MoveTo(rect.TopLeft()); 11 pDC->LineTo(CPoint(rect.TopLeft().x + rect.Width(), // x 12 rect.TopLeft().y // y 13 ) ); 14 15 // 右邊框; 16 pDC->MoveTo(CPoint(rect.TopLeft().x + rect.Width(), // x 17 rect.TopLeft().y // y 18 )); 19 pDC-> LineTo(rect.BottomRight()); 20 21 // 下邊框; 22 pDC-> MoveTo(rect.BottomRight()); 23 pDC->LineTo(rect.TopLeft().x, // x 24 rect.BottomRight().y // y 25 ); 26 27 // 左邊框; 28 pDC->MoveTo(rect.TopLeft().x, // x 29 rect.BottomRight().y // y 30 ); 31 pDC-> LineTo(rect.TopLeft()); 32 33 34 pDC-> SelectObject(oldPen); 35 36 int ret = GetDlgItem(m_nCurrentPicture)-> ReleaseDC(pDC); 37 pDC = NULL; 38 39 pen.DeleteObject();
?
?多個Picture控件,每個Picture控件都在控件中間位置寫上1 2 3 4等數字標識是哪個攝像頭
1 void CCameraMonitorView::OnDraw(CDC* /* pDC */ ) 2 { 3 // TODO: 在此添加專用代碼和/或調用基類; 4 5 if ( 0 != m_nCurrentPicture) 6 { 7 ChoosePicture(m_nCurrentPicture); 8 } 9 10 int index = 0 ; 11 for (index = 0 ; index < MAX_CAMERAS_NUM; ++ index) 12 { 13 if (videoPicturesCtrl[index] != 0 ) 14 { 15 CDC *pDC = GetDlgItem(videoPicturesCtrl[index])-> GetDC(); 16 CFont font; 17 font.CreatePointFont( 800 , _T( " 宋體 " )); 18 CFont *oldFont = pDC->SelectObject(& font); 19 pDC-> SetBkMode(TRANSPARENT); 20 CRect rect; 21 GetDlgItem(videoPicturesCtrl[index])->GetClientRect(& rect); 22 23 int x = (rect.Width() / 2 ) - 20 ; 24 int y = (rect.Height() / 2 ) - 40 ; 25 char numArr[ 5 ] = { 0 }; 26 _itoa_s(index + 1 , numArr, RADIX_10); 27 CString strDisplayNum(numArr); 28 29 pDC-> TextOut(x, y, strDisplayNum); 30 pDC-> SelectObject(oldFont); 31 32 GetDlgItem(IDC_VIDEO1)-> ReleaseDC(pDC); 33 34 font.DeleteObject(); 35 36 } 37 } 38 39 }
?
?
?
從窗口指針獲得DC
CDC *pDC = pWnd->GetDC();
?
?
根據DC獲得HDC
HDC hDC = pDC->GetSafeHdc();
?
?
根據句柄獲得窗口指針
CWnd *pWnd = FromHandle(hWnd);
?
void CJietu::OnPaint() { CPaintDC dc( this ); // device context for painting // TODO: 在此處添加消息處理程序代碼 // 不為繪圖消息調用 CDialog::OnPaint() HWND hWnd = m_hWnd; CWnd *pWnd = FromHandle(hWnd); CDC *pDC = pWnd-> GetDC(); HDC hDC = pDC-> GetSafeHdc(); StretchBlt(hDC, 0 , 0 , m_nCx, m_nCy, m_hDCGlobal, 0 , 0 , m_nCx, m_nCy, SRCCOPY); pWnd -> ReleaseDC(pDC); }
?
?
?另外一個例子
1 bool DrawPicToHDC(IplImage *img, HWND hWnd, bool bIsShowInfo) 2 { 3 CWnd *pWnd = CWnd::FromHandle(hWnd); 4 if (NULL == pWnd || FALSE == ::IsWindow(pWnd-> m_hWnd)) 5 { 6 AfxMessageBox(_T( " DrawPicToHDC error 2 " )); 7 return false ; 8 } 9 10 CDC *pDC = pWnd-> GetDC(); 11 HDC hDC = pDC-> GetSafeHdc(); 12 CRect rect; 13 pWnd->GetClientRect(& rect); 14 CvvImage cimg; 15 cimg.CopyOf(img); 16 cimg.DrawToHDC(hDC, & rect); 17 18 pDC->SetBkMode(TRANSPARENT); // 文字透明; 19 CTime time = CTime::GetCurrentTime(); // 獲得當前時間; 20 CString strTime; 21 strTime = time.Format( " %Y-%m-%d %H:%M:%S " ); 22 23 CFont font; 24 font.CreatePointFont( 200 , _T( " 宋體 " ), NULL); 25 CFont *oldFont = pDC->SelectObject(& font); 26 pDC->SetTextColor(RGB( 255 , 0 , 0 )); // 文字顏色 27 28 pDC->TextOut( 10 , 10 , strTime); 29 30 if ( true == bIsShowInfo) 31 { 32 pDC->TextOut( 10 , 35 , CString( " 正在錄像... " )); 33 } 34 35 pDC-> SelectObject(oldFont); 36 37 ReleaseDC(hWnd,hDC); // 釋放DC 38 font.DeleteObject(); // 釋放GDI對象 39 40 return true ; 41 }
?
?
?
?
?
?
?
?
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
