how to create transparent dialog ( not invisible)

2019-06-14 21:53发布

how to create transparent dialog but image or text drawn on it is visible in MFC. I searched many articles but didn't get exactly what i want. I want to deploy this in my project. help can be appreciated please help.

标签: mfc
1条回答
男人必须洒脱
2楼-- · 2019-06-14 22:39

In OnInitDialog you put this:

SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd,GWL_EXSTYLE) ^ WS_EX_LAYERED);
SetLayeredWindowAttributes(m_hWnd, RGB(255,0,255), 0, LWA_COLORKEY);

RGB(255,0,255) is the COLORREF for magenta. We suppose here that you don't use the color magenta anywhere in your dialog. With that all magenta pixels of your dialog will be transparent. As you want only the background to be transparent, we will paint the background of the dialog in magenta. This is done via the WM_ERASEBKGND message:

In the message map of your dialog add ON_WM_ERASEBKGND(), so your message map should look like this :

BEGIN_MESSAGE_MAP(CYourDialogDlg, CDialog)
//{{AFX_MSG_MAP(CYourDialogDlg)
...
//}}AFX_MSG_MAP
  ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

In the header file of your dialog you should have afx_msg BOOL OnEraseBkgnd(CDC* pDC);

In the .cpp file of your dialog put this :

BOOL CYourDialogDlg::OnEraseBkgnd(CDC *pDC)
{
  CRect clientRect ;

  GetClientRect(&clientRect) ;
  pDC->FillSolidRect(clientRect, RGB(255,0,255)) ;  // paint background in magenta

  return FALSE  ;
}

Sample dialog as visible in the dialog editor and as visible once executed

查看更多
登录 后发表回答