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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 ;
}