MFC 透明窗口两种代码对比

两种方法都用到

BOOL SetLayeredWindowAttributes1(
 HWND hwnd,           // handle to the layered window
  COLORREF crKey,      // specifies the color key
 BYTE bAlpha,         // value for the blend function
  DWORD dwFlags        // action
);

第一种:

VideoMark.h

 1 #if !defined(AFX_VIDEOMARK_H__7BDB9F62_B2C1_4634_89E4_1FE739829730__INCLUDED_)
 2 #define AFX_VIDEOMARK_H__7BDB9F62_B2C1_4634_89E4_1FE739829730__INCLUDED_
 3 
 4 #if _MSC_VER > 1000
 5 #pragma once
 6 #endif // _MSC_VER > 1000
 7 // VideoMark.h : header file
 8 //
 9 
10 /////////////////////////////////////////////////////////////////////////////
11 // CVideoMark window
12 #include "resource.h"
13 class CVideoMark : public CDialog
14 {
15     DECLARE_DYNAMIC(CVideoMark)
16 
17 public:
18     CVideoMark(CWnd* pParent = NULL);   // standard constructor
19     virtual ~CVideoMark();
20 
21 // Dialog Data
22     enum { IDD = IDD_MARKDIALOG };
23 
24 protected:
25      CFont      m_font;
26     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
27     void DrawMark(CDC *pDC,CRect rt);
28 
29 
30     DECLARE_MESSAGE_MAP()
31 public:
32     virtual BOOL OnInitDialog();
33     afx_msg void OnPaint();
34     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
35     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
36     afx_msg LRESULT OnUpdateGrData(WPARAM wParam,LPARAM lParam);
37 private:
38     int m_lightval;
39     
40 };
41 /////////////////////////////////////////////////////////////////////////////
42 //{{AFX_INSERT_LOCATION}}
43 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
44 
45 #endif // !defined(AFX_VIDEOMARK_H__7BDB9F62_B2C1_4634_89E4_1FE739829730__INCLUDED_)

VideoMark.cpp

  1 // VideoMark.cpp : implementation file
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "VideoMark.h"
  6 #ifdef _DEBUG
  7 #define new DEBUG_NEW
  8 #undef THIS_FILE
  9 static char THIS_FILE[] = __FILE__;
 10 #endif
 11 
 12 /////////////////////////////////////////////////////////////////////////////
 13 // CVideoMark
 14 #define LWA_COLORKEY            0x00000001
 15 #define LWA_ALPHA               0x00000002
 16 
 17 IMPLEMENT_DYNAMIC(CVideoMark, CDialog)
 18 
 19 CVideoMark::CVideoMark(CWnd* pParent /*=NULL*/)
 20     : CDialog(CVideoMark::IDD, pParent)
 21 {
 22 //    m_HoverWnd=NULL;
 23     m_lightval=-1;
 24 
 25     m_font.CreateFont(16,
 26                        0, 0, 0,
 27                        FW_NORMAL,
 28                        0, 0, 0, 0,
 29                        0, 0, 0, 0,
 30                        _T("幼圆"));
 31 
 32 }
 33 
 34 CVideoMark::~CVideoMark()
 35 {
 36 }
 37 
 38 void CVideoMark::DoDataExchange(CDataExchange* pDX)
 39 {
 40     CDialog::DoDataExchange(pDX);
 41 }
 42 
 43 
 44 BEGIN_MESSAGE_MAP(CVideoMark, CDialog)
 45 
 46     ON_WM_PAINT()
 47     ON_WM_ERASEBKGND()
 48     ON_WM_LBUTTONDOWN()
 49     ON_MESSAGE(WM_UPDATA_GR_DATA,OnUpdateGrData)
 50 END_MESSAGE_MAP()
 51 
 52 
 53 // CVideoMark message handlers
 54 
 55 BOOL CVideoMark::OnInitDialog()
 56 {
 57     CDialog::OnInitDialog();
 58 
 59     // TODO:  Add extra initialization here
 60     //return TRUE;  
 61     ::SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
 62     COLORREF maskColor = RGB(255,255,255);
 63     HINSTANCE hInst=LoadLibrary("User32.DLL");
 64     if(hInst)
 65     {
 66        typedef BOOL     (WINAPI * MYFUNC)(HWND,COLORREF,BYTE,DWORD);
 67        MYFUNC fun=NULL;
 68      //取得SetLayeredWindowAttributes函数指针
 69        fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
 70        if(fun)
 71         fun(this->GetSafeHwnd(),maskColor,255,LWA_COLORKEY);
 72        FreeLibrary(hInst);
 73     } 
 74 
 75     return TRUE;  // return TRUE unless you set the focus to a control
 76     // EXCEPTION: OCX Property Pages should return FALSE
 77 }
 78 
 79 void CVideoMark::OnPaint()
 80 {
 81     CPaintDC dc(this); // device context for painting
 82     CDC *pdc=&dc;
 83      CRect rcBounds;
 84      this->GetClientRect(&rcBounds);
 85 
 86        DrawMark(pdc,rcBounds);
 87 }
 88 
 89 void CVideoMark::DrawMark(CDC *pDC,CRect rt)
 90 {
 91 
 92     int width=rt.Width();
 93     int height=rt.Height();
 94     if(width&&height)
 95     {
 96        CBrush br(RGB(255,255,255));
 97        pDC->FillRect(&rt,&br);
 98        int w=width*0.2;
 99        int h=height*0.2;
100        int x0=(width-w)/2;
101        int x1=(width+w)/2;
102 
103        int y0=(height-h)/2;
104        int y1=(height+h)/2;
105        CRect rt1;
106        int delta=5;
107        CBrush br1(RGB(255,0,0));
108        rt1.SetRect(x0,y0,x1,y0+delta);
109        pDC->FillRect(&rt1,&br1);
110 
111        rt1.SetRect(x0,y1-delta,x1,y1);
112        pDC->FillRect(&rt1,&br1);
113 
114 
115        rt1.SetRect(x0,y0,x0+delta,y1);
116        pDC->FillRect(&rt1,&br1);
117 
118        rt1.SetRect(x1-delta,y0,x1,y1);
119        pDC->FillRect(&rt1,&br1);
120 
121        
122       if(m_lightval>=0)
123       {
124         CString str;
125         str.Format("Current Green value %d",m_lightval);
126         CFont *pOld;
127           
128     
129          pDC->SetBkMode(TRANSPARENT);
130          pOld=pDC->SelectObject(&m_font);
131           pDC->SetTextColor(RGB(0,255,0));
132          pDC->TextOut(x0+25,(y0+y1)/2,str);
133          pDC->SelectObject(pOld);
134       }
135 
136     }
137 
138 
139 }
140 
141 
142 BOOL CVideoMark::OnEraseBkgnd(CDC* pDC)
143 {
144     // TODO: Add your message handler code here and/or call default
145      return TRUE;
146     //return CDialog::OnEraseBkgnd(pDC);
147 }
148 
149 void CVideoMark::OnLButtonDown(UINT nFlags, CPoint point)
150 {
151     // TODO: Add your message handler code here and/or call default
152 
153     CDialog::OnLButtonDown(nFlags, point);
154 }
155 
156 LRESULT CVideoMark::OnUpdateGrData(WPARAM wParam,LPARAM lParam)
157 {
158      m_lightval=wParam;
159      this->Invalidate(1);
160      return 0;
161 }

第二种,用到    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);

MaskDlgPlot.h

 1 #if !defined(AFX_MASKDLGPLOT_H__3B74D89E_AC85_46C7_BE96_597884E58BD9__INCLUDED_)
 2 #define AFX_MASKDLGPLOT_H__3B74D89E_AC85_46C7_BE96_597884E58BD9__INCLUDED_
 3 
 4 #if _MSC_VER > 1000
 5 #pragma once
 6 #endif // _MSC_VER > 1000
 7 // MaskDlgPlot.h : header file
 8 //
 9 
10 
11 /////////////////////////////////////////////////////////////////////////////
12 // CMaskDlgPlot dialog
13 #include "LAKitDlg.h"
14 class CLAKitDlg;
15 class CMaskDlgPlot : public CDialog
16 {
17 // Construction
18 public:
19     CMaskDlgPlot(CWnd* pParent = NULL);   // standard constructor
20     ~CMaskDlgPlot();
21     CLAKitDlg * m_pVideo;
22 // Dialog Data
23     //{{AFX_DATA(CMaskDlgPlot)
24     enum { IDD = IDD_MaskDlgPlot };
25         // NOTE: the ClassWizard will add data members here
26     //}}AFX_DATA
27 
28     CBrush *m_brush;
29 // Overrides
30     // ClassWizard generated virtual function overrides
31     //{{AFX_VIRTUAL(CMaskDlgPlot)
32     protected:
33     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
34     virtual void PostNcDestroy();
35     //}}AFX_VIRTUAL
36 
37 // Implementation
38 protected:
39 
40     // Generated message map functions
41     //{{AFX_MSG(CMaskDlgPlot)
42     afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
43     virtual BOOL OnInitDialog();
44     virtual void OnOK();
45     //}}AFX_MSG
46     DECLARE_MESSAGE_MAP()
47 };
48 
49 //{{AFX_INSERT_LOCATION}}
50 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
51 
52 #endif // !defined(AFX_MASKDLGPLOT_H__3B74D89E_AC85_46C7_BE96_597884E58BD9__INCLUDED_)

MaskDlgPlot.cpp

  1 // MaskDlgPlot.cpp : implementation file
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "MaskDlgPlot.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 // CMaskDlgPlot dialog
 15 
 16 
 17 CMaskDlgPlot::CMaskDlgPlot(CWnd* pParent /*=NULL*/)
 18     : CDialog(CMaskDlgPlot::IDD, pParent)
 19 {
 20     //{{AFX_DATA_INIT(CMaskDlgPlot)
 21         // NOTE: the ClassWizard will add member initialization here
 22     //}}AFX_DATA_INIT
 23     m_pVideo=(CLAKitDlg*)pParent;
 24     m_brush=NULL;
 25 }
 26 
 27 CMaskDlgPlot::~CMaskDlgPlot()
 28 {
 29      TRACE("~CMaskDlgPlot()\n");
 30      if(m_brush) delete m_brush;m_brush=NULL;
 31 }
 32 void CMaskDlgPlot::DoDataExchange(CDataExchange* pDX)
 33 {
 34     CDialog::DoDataExchange(pDX);
 35     //{{AFX_DATA_MAP(CMaskDlgPlot)
 36         // NOTE: the ClassWizard will add DDX and DDV calls here
 37     //}}AFX_DATA_MAP
 38 }
 39 
 40 
 41 BEGIN_MESSAGE_MAP(CMaskDlgPlot, CDialog)
 42     //{{AFX_MSG_MAP(CMaskDlgPlot)
 43     ON_WM_CTLCOLOR()
 44     //}}AFX_MSG_MAP
 45 END_MESSAGE_MAP()
 46 
 47 /////////////////////////////////////////////////////////////////////////////
 48 // CMaskDlgPlot message handlers
 49 
 50 HBRUSH CMaskDlgPlot::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
 51 {
 52     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
 53     
 54     // TODO: Change any attributes of the DC here
 55     if(nCtlColor == CTLCOLOR_DLG)   //此处设置为窗体透明,CTLCOLOR_DLG表示对话框
 56     {
 57         TRACE("brush = new CBrush(RGB(255,255,255));\n");
 58         if(m_brush==NULL)
 59         {
 60             m_brush = new CBrush(RGB(255,255,255));
 61         }
 62         return (HBRUSH)(m_brush->m_hObject);
 63     }
 64     /*
 65     switch(pWnd->GetDlgCtrlID())   //此处为设置Static Text文本透明。
 66     {
 67     case IDC_STATIC1:
 68         pDC->SetBkMode(TRANSPARENT);
 69         pDC->SetTextColor(RGB(255,0,0));
 70         return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
 71     default: break;
 72     } 
 73     */
 74 
 75     // TODO: Return a different brush if the default is not desired
 76     return hbr;
 77 }
 78 
 79 BOOL CMaskDlgPlot::OnInitDialog() 
 80 {
 81     CDialog::OnInitDialog();
 82     
 83     // TODO: Add extra initialization here
 84     RECT myVideoRect;
 85     m_pVideo->GetClientRect(&myVideoRect);
 86     m_pVideo->ClientToScreen(&myVideoRect);
 87     RECT myShowVideoRect;
 88     m_pVideo->GetDlgItem(IDC_VIDEO)->GetClientRect(&myShowVideoRect);
 89     m_pVideo->GetDlgItem(IDC_VIDEO)->ClientToScreen(&myShowVideoRect);
 90     
 91     
 92     int myInfoCx=myVideoRect.right-myVideoRect.left;
 93     int  myinfoStartx=myVideoRect.left;
 94     
 95     int myinfoStarty=myShowVideoRect.top;
 96     int myinfoCy=myShowVideoRect.bottom;
 97     
 98     
 99     this->SetWindowPos(NULL, myinfoStartx, myinfoStarty, myInfoCx, myinfoCy, SWP_NOZORDER | SWP_SHOWWINDOW);
100 
101 
102 //    ::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);//TopMost
103     
104     SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
105         GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE) | 0x80000);
106     HINSTANCE hInst = LoadLibrary("User32.DLL"); 
107     if(hInst) 
108     {            
109         typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);          
110         MYFUNC fun = NULL;
111         
112         COLORREF maskColor = RGB(255,255,255);   //掩码颜色
113         //取得SetLayeredWindowAttributes函数指针     
114         fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
115         if(fun)fun(this->GetSafeHwnd(),maskColor,255,1);     
116         FreeLibrary(hInst); 
117     }
118     return TRUE;  // return TRUE unless you set the focus to a control
119                   // EXCEPTION: OCX Property Pages should return FALSE
120 }
121 
122 void CMaskDlgPlot::OnOK() 
123 {
124     // TODO: Add extra validation here
125     
126     CDialog::OnOK();
127 }
128 
129 void CMaskDlgPlot::PostNcDestroy() 
130 {
131     // TODO: Add your specialized code here and/or call the base class
132     delete this;
133     CDialog::PostNcDestroy();
134 }

完。

posted @ 2015-03-23 10:02  boyang987  阅读(817)  评论(0编辑  收藏  举报