MFC SDI: How do I create transparent SDI views

 

MFC SDI: How do I create transparent SDI views?

Q: How do I create transparent SDI views?

A: One can make a the view of an MFC application transparent by handling 'CView::OnEraseBkgnd()' function and returning 'FALSE' instead of the default 'CView::OnEraseBkgnd()' call.


Code:
   
   
BOOL CTransparentView::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default return FALSE; // return CView::OnEraseBkgnd(pDC); }

But this will not give a real transparent window, since the original view background will be dragged along while moving the application window. To overcome this, I have handled 'OnMove()' and 'OnSize()' functions of 'CMainFrame' class to update the view to reflect the real background, by repainting the view each time the application window is moved / sized.


Code:
   
   
void CMainFrame::OnMove( int x, int y) { CFrameWnd::OnMove(x, y); // TODO: Add your message handler code here RepaintView(); } void CMainFrame::OnSize(UINT nType, int cx, int cy) { CFrameWnd::OnSize(nType, cx, cy); // TODO: Add your message handler code here RepaintView(); }

I have defined a function 'RepaintWindow()' as part of the 'CMainFrame' class and implemented to repaint the view with correct background information.


Code:
   
   
void CMainFrame::RepaintView() { // Gets current view CView* pView = GetActiveView(); if (pView) // Valid view { // Hides view to get background ShowWindow(SW_HIDE); // Gets desktop device context HDC hDC = ::GetDC(NULL); if (hDC) // Valid device context handle { // Get clients rectangle in screen coordinates CRect Rect; pView->GetClientRect(Rect); pView->ClientToScreen(Rect); // Gets device context for current view CClientDC DC(pView); // Pastes background to view ::BitBlt(DC.m_hDC, 0, 0, Rect.Width(), Rect.Height(), hDC, Rect.left, Rect.top, SRCCOPY); // Releases desktop device context ::ReleaseDC(NULL, hDC); } // Shows view with right background info ShowWindow(SW_SHOW); } }

At present, this is implemented for SDI application. With little modification (for updating all views), this methodology can be extended for MDI applications as well.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值