Lightbox style dialogs in the MFC app

Has anyone implemented lightbox-style background highlighting in a modal dialog box in the MFC / non.net application.
I think the procedure should be something like this:

actions:

  • Get parent HWND or CWnd * dialog

  • Get the rectangle of the parent window and draw an overlay with transparency above this window

  • allow the dialog box to do this modal drawing procedure, for example DoModal ()

Are there any existing libraries / frameworks for this, or what is the best way to abandon the translucent overlay in MFC?
edit Here's a mockup of what I'm trying to achieve if you don't know what β€œlightbox style” means. Some application :
alt text

with lightbox dialog
alt text

+4
source share
2 answers

Here's what I did * based on Brian's links
First create a dialog resource with properties:

  • border false
  • 3D view FALSE
  • client edge FALSE
  • Popup Style
  • static edge FALSE
  • Transparent TRUE
  • Header line FALSE

and you should get a dialog box without a frame or anything else, just a gray square. override the Create function to look like this:

BOOL LightBoxDlg::Create(UINT nIDTemplate, CWnd* pParentWnd) { if(!CDialog::Create(nIDTemplate, pParentWnd)) return false; RECT rect; RECT size; GetParent()->GetWindowRect(&rect); size.top = 0; size.left = 0; size.right = rect.right - rect.left; size.bottom = rect.bottom - rect.top; SetWindowPos(m_pParentWnd,rect.left,rect.top,size.right,size.bottom,NULL); HWND hWnd=m_hWnd; SetWindowLong (hWnd , GWL_EXSTYLE ,GetWindowLong (hWnd , GWL_EXSTYLE ) | WS_EX_LAYERED ) ; typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD); PSLWA pSetLayeredWindowAttributes; HMODULE hDLL = LoadLibrary (_T("user32")); pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes"); if (pSetLayeredWindowAttributes != NULL) { /* * Second parameter RGB(255,255,255) sets the colorkey * to white LWA_COLORKEY flag indicates that color key * is valid LWA_ALPHA indicates that ALphablend parameter * is valid - here 100 is used */ pSetLayeredWindowAttributes (hWnd, RGB(255,255,255), 100, LWA_COLORKEY|LWA_ALPHA); } return true; } 

then create a small black raster map in the image editor (say 48x48) and import it as a raster resource (in this example IDB_BITMAP1)
override the WM_ERASEBKGND message with:

 BOOL LightBoxDlg::OnEraseBkgnd(CDC* pDC) { BOOL bRet = CDialog::OnEraseBkgnd(pDC); RECT rect; RECT size; m_pParentWnd->GetWindowRect(&rect); size.top = 0; size.left = 0; size.right = rect.right - rect.left; size.bottom = rect.bottom - rect.top; CBitmap cbmp; cbmp.LoadBitmapW(IDB_BITMAP1); BITMAP bmp; cbmp.GetBitmap(&bmp); CDC memDc; memDc.CreateCompatibleDC(pDC); memDc.SelectObject(&cbmp); pDC->StretchBlt(0,0,size.right,size.bottom,&memDc,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY); return bRet; } 

Create it in DoModal of the desired dialog, create it as a Modal Dialog, that is, on the stack (or, if you want, a bunch), call it Create manually, show it, then create the actual modal dialog above it:

 INT_PTR CAboutDlg::DoModal() { LightBoxDlg Dlg(m_pParentWnd);//make sure to pass in the parent of the new dialog Dlg.Create(LightBoxDlg::IDD); Dlg.ShowWindow(SW_SHOW); BOOL ret = CDialog::DoModal(); Dlg.ShowWindow(SW_HIDE); return ret; } 

and this leads to something exactly like my layout above

* There is still room for improvement, for example, to do this without creating a dialog box for starters and some other general settings.

+4
source

It seems to me that you just need to create a window and set transparency. There is an MFC CGlassDialog sample in CodeProject that can help you. There is also an article on how to do this using the Win32 API.

+2
source

Source: https://habr.com/ru/post/1276608/


All Articles