How to create a transparent overlay window for each document of an MDI for MFC?
As regards to a Multiple Document example for the Microsoft Foundation Classes generated by the Visual Studio Professional application wizard, I have made a few modifications to implement a transparent overlay window feature for each document of the MDI.
MFCApplication1.cpp:
// MFCApplication1.cpp : Defines the class behaviors for the application.
//
#include "pch.h"
#include "framework.h"
#include "afxwinappex.h"
#include "afxdialogex.h"
#include "MFCApplication1.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "MFCApplication1Doc.h"
#include "MFCApplication1View.h"
#include "GLView.h"
#include <iostream>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMFCApplication1App
BEGIN_MESSAGE_MAP(CMFCApplication1App, CWinAppEx)
ON_COMMAND(ID_APP_ABOUT, &CMFCApplication1App::OnAppAbout)
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinAppEx::OnFilePrintSetup)
END_MESSAGE_MAP()
// CMFCApplication1App construction
void CMFCApplication1App::PrintOpenDocumentNames()
{
std::cout << "Open Documents:" << std::endl;
for (const auto& doc : openDocuments)
{
if (doc)
{
std::cout << "- " << CStringA(doc->GetTitle()).GetString() << std::endl;
}
}
}
std::map<CString, std::vector<Entity*>> CMFCApplication1App::GetEntitiesByDocument() const
{
std::map<CString, std::vector<Entity*>> entitiesByDoc;
for (const auto* doc : openDocuments) {
if (doc) {
CString title = doc->GetTitle();
const auto& entities = doc->GetEntities();
for (const auto& entity : entities) {
entitiesByDoc[title].push_back(entity.get());
}
}
}
return entitiesByDoc;
}
void CMFCApplication1App::RegisterDocument(CMFCApplication1Doc* doc)
{
openDocuments.push_back(doc);
}
void CMFCApplication1App::UnregisterDocument(CMFCApplication1Doc* doc)
{
auto it = std::find(openDocuments.begin(), openDocuments.end(), doc);
if (it != openDocuments.end())
{
openDocuments.erase(it);
}
}
CMFCApplication1App::CMFCApplication1App() noexcept
{
m_bHiColorIcons = TRUE;
m_nAppLook = 0;
// support Restart Manager
m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_ALL_ASPECTS;
#ifdef _MANAGED
// If the application is built using Common Language Runtime support (/clr):
// 1) This additional setting is needed for Restart Manager support to work properly.
// 2) In your project, you must add a reference to System.Windows.Forms in order to build.
System::Windows::Forms::Application::SetUnhandledExceptionMode(System::Windows::Forms::UnhandledExceptionMode::ThrowException);
#endif
// TODO: replace application ID string below with unique ID string; recommended
// format for string is CompanyName.ProductName.SubProduct.VersionInformation
SetAppID(_T("MFCApplication1.AppID.NoVersion"));
m_pInitialDocument = nullptr;
// TODO: add construction code here,
// Place all significant initialization in InitInstance
AllocConsole();
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
std::cout << "Console allocated successfully!" << std::endl;
}
// The one and only CMFCApplication1App object
CMFCApplication1App theApp;
// CMFCApplication1App initialization
BOOL CMFCApplication1App::InitInstance()
{
std::cout << "CMFCApplication1App::InitInstance()" << std::endl;
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance();
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
EnableTaskbarInteraction();
//AfxInitRichEdit2() is required to use RichEdit control
AfxInitRichEdit2();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
InitContextMenuManager();
InitKeyboardManager();
InitTooltipManager();
CMFCToolTipInfo ttParams;
ttParams.m_bVislManagerTheme = TRUE;
theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_MFCApplication1TYPE,
RUNTIME_CLASS(CMFCApplication1Doc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CGLView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
{
delete pMainFrame;
return FALSE;
}
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Prevent ProcessShellCommand from creating a new document
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
// Assuming a document was created, store it
POSITION pos = GetFirstDocTemplatePosition();
if (pos != nullptr)
{
CDocTemplate* pTemplate = GetNextDocTemplate(pos);
if (pTemplate != nullptr)
{
CDocument* pDoc = pTemplate->OpenDocumentFile(nullptr);
if (pDoc != nullptr)
{
m_pInitialDocument = pDoc; // Store the initial document
pDoc->SetTitle(_T("SAMPLE"));
}
}
}
// Dispatch commands specified on the command line. Will return FALSE if
// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}
int CMFCApplication1App::ExitInstance()
{
//TODO: handle additional resources you may have added
AfxOleTerm(FALSE);
return CWinAppEx::ExitInstance();
}
// CMFCApplication1App message handlers
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg() noexcept;
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// App command to run the dialog
void CMFCApplication1App::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
// CMFCApplication1App customization load/save methods
void CMFCApplication1App::PreLoadState()
{
BOOL bNameValid;
CString strName;
bNameValid = strName.LoadString(IDS_EDIT_MENU);
ASSERT(bNameValid);
GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
bNameValid = strName.LoadString(IDS_EXPLORER);
ASSERT(bNameValid);
GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EXPLORER);
}
void CMFCApplication1App::LoadCustomState()
{
}
void CMFCApplication1App::SaveCustomState()
{
}
// CMFCApplication1App message handlers
// GLView.h
#pragma once
#include <afxext.h>
#include "OverlayWnd.h"
class CGLView : public CView
{
protected:
DECLARE_DYNCREATE(CGLView)
CGLView();
COverlayWnd* m_pOverlayWnd;
public:
HGLRC m_hRC;
CDC* m_pDC;
BOOL InitOpenGL();
virtual void OnDraw(CDC* pDC) override;
virtual BOOL PreCreateWindow(CREATESTRUCT& cs) override
{
cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnWindowPosChanged(WINDOWPOS* lpwndpos); // Add this
afx_msg BOOL OnEraseBkgnd(CDC* pDC) { return TRUE; }
DECLARE_MESSAGE_MAP()
};
// GLView.cpp
#include "pch.h"
#include "GLView.h"
#include <gl/GL.h>
IMPLEMENT_DYNCREATE(CGLView, CView)
BEGIN_MESSAGE_MAP(CGLView, CView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_WINDOWPOSCHANGED() // Add this
END_MESSAGE_MAP()
void CGLView::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
CView::OnWindowPosChanged(lpwndpos);
if (m_pOverlayWnd && m_pOverlayWnd->GetSafeHwnd())
{
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect); // Convert to screen coordinates
m_pOverlayWnd->MoveWindow(rect.left, rect.top, rect.Width(), rect.Height());
}
}
CGLView::CGLView()
: m_hRC(nullptr), m_pDC(nullptr) // Initialize pointers safely
{
// Constructor implementation (if needed)
}
void CGLView::OnDraw(CDC* /pDC/)
{
if (m_hRC && m_pDC)
{
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); // Make the OpenGL context current
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen to the background color
::SwapBuffers(m_pDC->GetSafeHdc()); // Swap buffers to display the result
::wglMakeCurrent(nullptr, nullptr); // Release the context
}
}
BOOL CGLView::InitOpenGL()
{
m_pDC = new CClientDC(this);
if (!m_pDC)
{
AfxMessageBox(_T("Failed to get device context."));
return FALSE;
}
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24, // color depth
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32, // depth buffer
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
int pixelFormat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
if (pixelFormat == 0)
{
AfxMessageBox(_T("Failed to choose pixel format."));
return FALSE;
}
if (!SetPixelFormat(m_pDC->GetSafeHdc(), pixelFormat, &pfd))
{
AfxMessageBox(_T("Failed to set pixel format."));
return FALSE;
}
m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
if (!m_hRC)
{
AfxMessageBox(_T("Failed to create OpenGL context."));
return FALSE;
}
if (!wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC))
{
AfxMessageBox(_T("Failed to activate OpenGL context."));
return FALSE;
}
return TRUE;
}
int CGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
if (!InitOpenGL())
{
AfxMessageBox(_T("Failed to initialize OpenGL."));
return -1;
}
CRect rect;
GetClientRect(&rect);
m_pOverlayWnd = new COverlayWnd();
if (!m_pOverlayWnd)
{
TRACE("Failed to allocate COverlayWnd\n");
return 0;
}
BOOL created = m_pOverlayWnd->CreateEx(
0, // No WS_EX_LAYERED for now
AfxRegisterWndClass(0),
nullptr,
WS_CHILD | WS_VISIBLE,
0, 0, rect.Width(), rect.Height(),
GetSafeHwnd(),
nullptr);
if (!created)
{
TRACE("COverlayWnd::CreateEx failed with error %lu\n", GetLastError());
delete m_pOverlayWnd;
m_pOverlayWnd = nullptr;
return 0;
}
return 0;
}
void CGLView::OnDestroy()
{
if (m_pOverlayWnd)
{
if (m_pOverlayWnd->GetSafeHwnd())
m_pOverlayWnd->DestroyWindow();
delete m_pOverlayWnd;
m_pOverlayWnd = nullptr;
}
if (m_hRC)
{
::wglMakeCurrent(nullptr, nullptr);
::wglDeleteContext(m_hRC);
m_hRC = nullptr;
}
if (m_pDC)
{
delete m_pDC;
m_pDC = nullptr;
}
CView::OnDestroy();
}
void CGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_hRC)
{
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
glViewport(0, 0, cx, cy);
::wglMakeCurrent(nullptr, nullptr);
}
if (m_pOverlayWnd && m_pOverlayWnd->GetSafeHwnd())
{
CRect rect;
GetClientRect(&rect);
ClientToScreen(&rect); // Convert to screen coordinates
m_pOverlayWnd->MoveWindow(rect.left, rect.top, rect.Width(), rect.Height());
}
}
#pragma once
#include <afxwin.h>
class COverlayWnd : public CWnd
{
DECLARE_DYNCREATE(COverlayWnd)
public:
COverlayWnd();
virtual ~COverlayWnd();
static int s_nInstanceCount; // Add static counter
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
void COverlayWnd::OnMove(int x, int y);
DECLARE_MESSAGE_MAP()
};
//OverlayWnd.cpp
#include "pch.h"
#include "OverlayWnd.h"
#include <iostream>
IMPLEMENT_DYNCREATE(COverlayWnd, CWnd)
BEGIN_MESSAGE_MAP(COverlayWnd, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_MOVE()
END_MESSAGE_MAP()
int COverlayWnd::s_nInstanceCount = 0; // Initialize static counter
void COverlayWnd::OnMove(int x, int y)
{
TRACE("COverlayWnd::OnMove called with x=%d, y=%d\n", x, y);
}
COverlayWnd::COverlayWnd()
{
s_nInstanceCount++;
TRACE("COverlayWnd created, total: %d\n", s_nInstanceCount);
}
COverlayWnd::~COverlayWnd()
{
s_nInstanceCount--;
TRACE("COverlayWnd destroyed, total: %d\n", s_nInstanceCount);
}
int COverlayWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
{
TRACE("COverlayWnd::OnCreate failed\n");
return -1;
}
// Set transparency attributes after creation
SetLayeredWindowAttributes(RGB(0, 0, 0), 0, LWA_COLORKEY);
return 0;
}
void COverlayWnd::OnPaint()
{
CPaintDC dc(this);
// Ensure drawing is done entirely within the overlay's client area
CRect overlayRect;
GetClientRect(&overlayRect);
// Fill background with color key for transparency
dc.FillSolidRect(&overlayRect, RGB(0, 0, 0));
// Draw crosshairs within the resized overlay
CPen pen(PS_SOLID, 4, RGB(255, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.MoveTo(overlayRect.left, overlayRect.top);
dc.LineTo(overlayRect.right, overlayRect.bottom);
dc.MoveTo(overlayRect.left, overlayRect.bottom);
dc.LineTo(overlayRect.right, overlayRect.top);
dc.SelectObject(pOldPen);
}
BOOL COverlayWnd::OnEraseBkgnd(CDC* pDC)
{
return TRUE; // Prevent default background erasing
}
BOOL COverlayWnd::PreCreateWindow(CREATESTRUCT& cs)
{
// Set popup style instead of child
cs.style = WS_POPUP | WS_VISIBLE;
cs.dwExStyle |= WS_EX_LAYERED; // Enable layered window for transparency
return CWnd::PreCreateWindow(cs);
}
//void COverlayWnd::OnLButtonDown(UINT nFlags, CPoint point)
//{
// Convert to screen coordinates
//CPoint screenPt = point;
//ClientToScreen(&screenPt);
// Forward to the underlying window (e.g., OpenGL view)
//CWnd* pTarget = AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_PANE_FIRST); // Adjust as needed
//if (pTarget)
//{
//pTarget->ScreenToClient(&screenPt);
//pTarget->PostMessage(WM_LBUTTONDOWN, nFlags, MAKELPARAM(screenPt.x, screenPt.y));
//}
//}
Attached are screenshots showing the show and move window behavior. So far the overlay window doesn't seem to move along with the client area of the document it should be assiciated with. The idea I have is tho associate the overlay window with the client area of each document and have the overlay adjust its size, position and location relative to the client area described. How can that be made to work with MFC?