• 创建任意形状的窗口 .


    1、 新建一个MFC AppWizard项目,选择选基于文档或者对话框都可以。

    2、 添加两个图片,如下,第二张图片把第一张图片的中间部分镂空,也即删除该部分区域,让背景透明

           

    3、 添加位图资源,导入上面的那两张图片。

        

    4、 新建一个基类为CWnd的类TransparentWnd,代码如下:

    头文件:

    1. //TransparentWnd.H   
    2. #if !defined(AFX_TRANSPARENTWND_H__INCLUDED_)   
    3. #define AFX_TRANSPARENTWND_H__INCLUDED_   
    4. #if _MSC_VER > 1000   
    5. #pragma once   
    6. #endif   
    7.   
    8.   
    9. class CTransparentWnd : public CWnd  
    10. {  
    11. public:  
    12.   
    13. CTransparentWnd();   
    14. void CreateTransparent(LPCTSTR pTitle, RECT &rect, unsigned short MaskID, unsigned short BitmapID);  
    15. void SetupRegion(CDC *pDC, unsigned short MaskID);  
    16.   
    17. public:  
    18. // Overrides   
    19. // ClassWizard generated virtual function overrides   
    20. //{{AFX_VIRTUAL(CTransparentWnd)   
    21. //}}AFX_VIRTUAL   
    22. // Implementation   
    23.   
    24. public:  
    25. virtual ~CTransparentWnd();  
    26.   
    27. protected:  
    28. unsigned short m_BitmapID;  
    29. // Generated message map functions   
    30.   
    31. protected:  
    32. //{{AFX_MSG(CTransparentWnd)   
    33. afx_msg void OnPaint();  
    34. afx_msg BOOL OnEraseBkgnd(CDC* pDC);  
    35. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);  
    36. //}}AFX_MSG   
    37.   
    38. DECLARE_MESSAGE_MAP()  
    39.   
    40. };  
    41.   
    42. //{{AFX_INSERT_LOCATION}}   
    43.   
    44. // Microsoft Visual C++ will insert additional declarations immediately before the previous line.   
    45.   
    46. #endif  

     

     实现文件:

    1. //TransparentWnd.CPP   
    2. #include "stdafx.h"   
    3. #include "TransparentWnd.h"   
    4.   
    5. #include <assert.h>   
    6.   
    7. #ifdef _DEBUG   
    8. #define new DEBUG_NEW   
    9. #undef THIS_FILE   
    10. static char THIS_FILE[] = __FILE__;  
    11. #endif   
    12.   
    13.   
    14. //********************************************************************************   
    15. //* Constructor   
    16. //********************************************************************************   
    17.   
    18. CTransparentWnd::CTransparentWnd()  
    19. {  
    20. }  
    21.   
    22.   
    23. //********************************************************************************   
    24. //* Destructor   
    25. //********************************************************************************   
    26.   
    27. CTransparentWnd::~CTransparentWnd()  
    28. {  
    29. }  
    30.   
    31.   
    32. BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)  
    33.     //{{AFX_MSG_MAP(CTransparentWnd)   
    34.     ON_WM_PAINT()  
    35.     ON_WM_ERASEBKGND()  
    36.     ON_WM_LBUTTONDOWN()  
    37.     //}}AFX_MSG_MAP   
    38. END_MESSAGE_MAP()  
    39.   
    40.   
    41. //**************************************************************************   
    42. //*创建一个背景透明的窗体   
    43. //**************************************************************************   
    44.   
    45. void CTransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect, unsigned short MaskID, unsigned short BitmapID)  
    46. {  
    47.     CreateEx(   0,  
    48.                         AfxRegisterWndClass(0),  
    49.                         pTitle,  
    50.                         WS_POPUP | WS_SYSMENU,  
    51.                         rect,  
    52.                         NULL,  
    53.                         NULL,  
    54.                         NULL );  
    55.     SetupRegion(GetWindowDC(), MaskID);  
    56.     m_BitmapID = BitmapID;  
    57. }  
    58.   
    59.   
    60. //********************************************************************************   
    61. //*创建一个背景透明的窗体   
    62. //********************************************************************************   
    63.   
    64. void CTransparentWnd::SetupRegion(CDC *pDC, unsigned short MaskID)  
    65. {  
    66.     CDC                 memDC;  
    67.     CBitmap         cBitmap;  
    68.     CBitmap*        pOldMemBmp = NULL;  
    69.     COLORREF        col;  
    70.     CRect               cRect;  
    71.     int                 x, y;  
    72.     CRgn                wndRgn, rgnTemp;  
    73.   
    74.     GetWindowRect(&cRect);  
    75.   
    76.     cBitmap.LoadBitmap(MaskID);  
    77.     memDC.CreateCompatibleDC(pDC);  
    78.     pOldMemBmp = memDC.SelectObject(&cBitmap);  
    79.   
    80.     wndRgn.CreateRectRgn(0, 0, cRect.Width(), cRect.Height());  
    81.     for(x=0; x<=cRect.Width(); x++)  
    82.     {  
    83.         for(y=0; y<=cRect.Height(); y++)  
    84.         {  
    85.             col = memDC.GetPixel(x, y);  
    86.               
    87.             if(col == 0)  
    88.             {  
    89.                 rgnTemp.CreateRectRgn(x, y, x+1, y+1);  
    90.                 wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);  
    91.                 rgnTemp.DeleteObject();   
    92.             }  
    93.               
    94.         }  
    95.     }  
    96.     if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);  
    97.     SetWindowRgn((HRGN)wndRgn, TRUE);  
    98. }  
    99.   
    100. //**************************************************************************   
    101. //*响应鼠标左键点击,让窗口可移动   
    102. //**************************************************************************   
    103.   
    104.   
    105. void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point)   
    106. {  
    107.     CWnd::OnLButtonDown(nFlags, point);  
    108.     PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));  
    109. }  
    110. //**************************************************************************   
    111. //*加载图片1作为背景   
    112. //**************************************************************************   
    113.   
    114.   
    115. BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC)   
    116. {  
    117.     CRect   rect;  
    118.     GetWindowRect(&rect);  
    119.   
    120.     CDC memDC;  
    121.     CBitmap         cBitmap;  
    122.     CBitmap*        pOldMemBmp = NULL;  
    123.   
    124.     cBitmap.LoadBitmap(m_BitmapID);  
    125.     memDC.CreateCompatibleDC(pDC);  
    126.     pOldMemBmp = memDC.SelectObject(&cBitmap);  
    127.     pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);  
    128.   
    129.     if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );  
    130.       
    131.     return TRUE;  
    132. }  
    133.   
    134. void CTransparentWnd::OnPaint()  
    135. {  
    136.     CPaintDC dc(this);  
    137.   
    138.     // Add your drawing code here!   
    139. }  

    5、 修改项目中的×××App.cpp文件的InitInstance()函数,如下: 

    1. BOOL ×××App::InitInstance()  
    2. {  
    3.     SetRegistryKey(_T("Local AppWizard-Generated Applications"));  
    4.     CTransparentWnd* pFrame = new CTransparentWnd;  
    5.     m_pMainWnd = pFrame;  
    6.     CRect rect(0, 0, 200, 250);  
    7.     pFrame->CreateTransparent("Transparent Test", rect, IDB_BITMAP2, IDB_BITMAP1);  
    8.     pFrame->ShowWindow(SW_SHOW);  
    9.     pFrame->UpdateWindow();  
    10.   
    11.     return TRUE;  
    12. }  

    6、 运行看结果:

        

    7、 Try it yourself

    8、 扩展:

    我们可以制作一组连续的图片构成动画,然后按照上面的方法动态更改窗口背景图片,可以作出更加好看的动画窗口,试试。

    from:http://blog.csdn.net/sllins/article/details/5557285

  • 相关阅读:
    Matlab中如何将(自定义)函数作为参数传递给另一个函数
    字幕文件 WebVTT 与 srt 之间的互相转化
    MathType 常用快捷键
    如何解决mathpage.dll或MathType.dll文件找不到问题
    Accelerating Matlab
    VR 相关专业词汇
    Computer Graphics Research Software
    C 和 C++ 混合代码 cmath编译出错
    CG&CAD resource
    Python 面向对象编程——初见
  • 原文地址:https://www.cnblogs.com/lidabo/p/2783292.html
Copyright © 2020-2023  润新知