Dave's Brain

Browse - Programming Tips - How do I make a hidden window with MFC?

Date: 2009may12
Framework: MFC
Language: C/C++

Q.  How do I make a hidden window with MFC?

A.  Hidden windows are handy for receiving messages and such.
Here's a class for you:

--- hiddenwnd.h 

class CHiddenWindow : public CWnd { public: CHiddenWindow(HWND hwndParent) { Init(hwndParent); } //{{AFX_DATA(CHiddenWindow) //}}AFX_DATA protected: Init(HWND); //{{AFX_VIRTUAL(CHiddenWindow) protected: //}}AFX_VIRTUAL // Generated message map functions //{{AFX_MSG(CHiddenWindow) afx_msg void OnClose(); //}}AFX_MSG DECLARE_MESSAGE_MAP() };
--- hiddenwnd.cpp
#include "stdafx.h" #include "resource.h" #include "hiddenwnd.h" BOOL CHiddenWindow::Init(const HWND hwndParent) { LPCSTR pszClass; BOOL bResult; pszClass = AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, // Style LoadCursor(NULL, IDC_ARROW), // Cursor (HBRUSH) GetStockObject(WHITE_BRUSH), // Brush LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME))); // Icon if (pszClass == NULL) { printf("CHiddenWindow::Init: could not make class\n"); return FALSE; } bResult = CreateEx(WS_EX_OVERLAPPEDWINDOW, // extended style pszClass, // class name "CHiddenWindow", // title WS_OVERLAPPEDWINDOW, // style CW_USEDEFAULT, // x position CW_USEDEFAULT, // y position CW_USEDEFAULT, // width CW_USEDEFAULT, // height hwndParent, // parent NULL, // menu NULL); // create param if (!bResult) { printf("CHiddenWindow::Init: CreateEx failed\n"); return FALSE; } ShowWindow(SW_HIDE); UpdateWindow(); return bResult; } void CHiddenWindow::OnClose() { // Optionally, do something } BEGIN_MESSAGE_MAP(CHiddenWindow, CWnd) //{{AFX_MSG_MAP(CHiddenWindow) ON_WM_CLOSE() //}}AFX_MSG_MAP END_MESSAGE_MAP()

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.