透明窗体

矫情吗;* 2021-09-22 08:48 568阅读 0赞

透明窗体:

主要代码:

  1. // TestDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Test.h"
  5. #include "TestDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{
  19. {AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{
  24. {AFX_VIRTUAL(CAboutDlg)
  25. protected:
  26. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  27. //}}AFX_VIRTUAL
  28. // Implementation
  29. protected:
  30. //{
  31. {AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{
  38. {AFX_DATA_INIT(CAboutDlg)
  39. //}}AFX_DATA_INIT
  40. }
  41. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43. CDialog::DoDataExchange(pDX);
  44. //{
  45. {AFX_DATA_MAP(CAboutDlg)
  46. //}}AFX_DATA_MAP
  47. }
  48. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  49. //{
  50. {AFX_MSG_MAP(CAboutDlg)
  51. // No message handlers
  52. //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54. /
  55. // CTestDlg dialog
  56. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
  57. : CDialog(CTestDlg::IDD, pParent)
  58. {
  59. //{
  60. {AFX_DATA_INIT(CTestDlg)
  61. // NOTE: the ClassWizard will add member initialization here
  62. //}}AFX_DATA_INIT
  63. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  64. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  65. }
  66. void CTestDlg::DoDataExchange(CDataExchange* pDX)
  67. {
  68. CDialog::DoDataExchange(pDX);
  69. //{
  70. {AFX_DATA_MAP(CTestDlg)
  71. // NOTE: the ClassWizard will add DDX and DDV calls here
  72. //}}AFX_DATA_MAP
  73. }
  74. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
  75. //{
  76. {AFX_MSG_MAP(CTestDlg)
  77. ON_WM_SYSCOMMAND()
  78. ON_WM_PAINT()
  79. ON_WM_QUERYDRAGICON()
  80. ON_WM_MOVE()
  81. ON_WM_CREATE()
  82. //}}AFX_MSG_MAP
  83. END_MESSAGE_MAP()
  84. /
  85. // CTestDlg message handlers
  86. BOOL CTestDlg::OnInitDialog()
  87. {
  88. CDialog::OnInitDialog();
  89. // Add "About..." menu item to system menu.
  90. // IDM_ABOUTBOX must be in the system command range.
  91. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  92. ASSERT(IDM_ABOUTBOX < 0xF000);
  93. CMenu* pSysMenu = GetSystemMenu(FALSE);
  94. if (pSysMenu != NULL)
  95. {
  96. CString strAboutMenu;
  97. strAboutMenu.LoadString(IDS_ABOUTBOX);
  98. if (!strAboutMenu.IsEmpty())
  99. {
  100. pSysMenu->AppendMenu(MF_SEPARATOR);
  101. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  102. }
  103. }
  104. // Set the icon for this dialog. The framework does this automatically
  105. // when the application's main window is not a dialog
  106. SetIcon(m_hIcon, TRUE); // Set big icon
  107. SetIcon(m_hIcon, FALSE); // Set small icon
  108. // TODO: Add extra initialization here
  109. //初始化GDI+.
  110. m_Blend.BlendOp=0; //theonlyBlendOpdefinedinWindows2000
  111. m_Blend.BlendFlags=0; //nothingelseisspecial...
  112. m_Blend.AlphaFormat=1; //...
  113. m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA
  114. //窗体样式为0x80000为层级窗体
  115. DWORD dwExStyle=GetWindowLong(m_hWnd,GWL_EXSTYLE);
  116. SetWindowLong(m_hWnd,GWL_EXSTYLE,dwExStyle^0x80000);
  117. //绘制内存位图
  118. HDC hdcTemp=GetDC()->m_hDC;
  119. m_hdcMemory=CreateCompatibleDC(hdcTemp);
  120. HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp,500,500);
  121. SelectObject(m_hdcMemory,hBitMap);
  122. //使用GDI+载入PNG图片
  123. HDC hdcScreen=::GetDC (m_hWnd);
  124. RECT rct;
  125. GetWindowRect(&rct);
  126. POINT ptWinPos={rct.left,rct.top};
  127. Graphics graph(m_hdcMemory); //GDI+中的类
  128. Image image(L"bk.png",TRUE); //GDI+中的类
  129. graph.DrawImage(&image,0,0,267,154); //后面两个参数要设置成跟图片一样大小,否则会失真
  130. //使用UpdateLayerWindow进行窗口透明处理
  131. HMODULE hFuncInst=LoadLibrary("User32.DLL");
  132. typedef BOOL (WINAPI *MYFUNC)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
  133. MYFUNC UpdateLayeredWindow;
  134. UpdateLayeredWindow=(MYFUNC)GetProcAddress(hFuncInst,"UpdateLayeredWindow");
  135. SIZE sizeWindow={267,154};
  136. POINT ptSrc={0,0};
  137. UpdateLayeredWindow( m_hWnd,hdcScreen,&ptWinPos,&sizeWindow,m_hdcMemory,&ptSrc,0,&m_Blend,2);
  138. return TRUE; // return TRUE unless you set the focus to a control
  139. }
  140. void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  141. {
  142. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  143. {
  144. CAboutDlg dlgAbout;
  145. dlgAbout.DoModal();
  146. }
  147. else
  148. {
  149. CDialog::OnSysCommand(nID, lParam);
  150. }
  151. }
  152. // If you add a minimize button to your dialog, you will need the code below
  153. // to draw the icon. For MFC applications using the document/view model,
  154. // this is automatically done for you by the framework.
  155. void CTestDlg::OnPaint()
  156. {
  157. if (IsIconic())
  158. {
  159. CPaintDC dc(this); // device context for painting
  160. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  161. // Center icon in client rectangle
  162. int cxIcon = GetSystemMetrics(SM_CXICON);
  163. int cyIcon = GetSystemMetrics(SM_CYICON);
  164. CRect rect;
  165. GetClientRect(&rect);
  166. int x = (rect.Width() - cxIcon + 1) / 2;
  167. int y = (rect.Height() - cyIcon + 1) / 2;
  168. // Draw the icon
  169. dc.DrawIcon(x, y, m_hIcon);
  170. }
  171. else
  172. {
  173. CDialog::OnPaint();
  174. }
  175. }
  176. // The system calls this to obtain the cursor to display while the user drags
  177. // the minimized window.
  178. HCURSOR CTestDlg::OnQueryDragIcon()
  179. {
  180. return (HCURSOR) m_hIcon;
  181. }
  182. void CTestDlg::OnMove(int x, int y)
  183. {
  184. CDialog::OnMove(x, y);
  185. // TODO: Add your message handler code here
  186. CRect rcWindow; // 使用MoveWindow函数的示例
  187. GetWindowRect(rcWindow);
  188. rcWindow.bottom-=10;
  189. rcWindow.left+=10;
  190. rcWindow.right-=10;
  191. rcWindow.top+=20;
  192. pChildWnd->MoveWindow(&rcWindow);
  193. }
  194. int CTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
  195. {
  196. if (CDialog::OnCreate(lpCreateStruct) == -1)
  197. return -1;
  198. // TODO: Add your specialized creation code here
  199. //创建子窗体
  200. pChildWnd=new COnWindow(this);
  201. pChildWnd->Create(IDD_ONWINDOW_DIALOG);
  202. pChildWnd->ShowWindow(SW_SHOW);
  203. return 0;
  204. }

效果图:

20171009205006108

程序和源代码链接:

http://download.csdn.net/download/zhangxiaoyu_sy/9961970

发表评论

表情:
评论列表 (有 0 条评论,568人围观)

还没有评论,来说两句吧...

相关阅读

    相关 libVLC 在视频上叠加透明

    ![format_png][] 在做多媒体开发时,很多时候需要在视频中添加一些“额外的装饰”,像图片、文字、或者一些其他图形形状,这使得显示效果更加丰富。 可能你觉得很简单

    相关 QT将变为顶层

    QT将窗体变为顶层窗体 在windows上通过鼠标双击某应用程序图标,该应用程序往往会以顶层窗口的形式呈现在我们面前,但是对于一个已经打开的非顶层窗口,我们怎么将其激活为