Click here to Skip to main content
15,885,876 members
Articles / Desktop Programming / MFC
Article

Provide Your Custom Class Name to your MFC Application

Rate me:
Please Sign up or sign in to vote.
4.76/5 (20 votes)
14 May 2011CPOL4 min read 59.5K   1.5K   32   17
Provide your customized class name to MFC application SDI/MDI and dialog based
Image 1

Introduction

In some cases, it is useful to create an MFC application where the main window uses a specific window class.

This is especially useful when I want to provide some easy and simple inter process communication (IPC). The easiest way to communicate from one process to another process is to send a message. The only problem is to determine and identify the target window easily.

One way is to iterate over all top level windows and to determine classname or window name. Or to broadcast a registered windows message to all windows. But this is some overhead and there is an easy way to solve the problem to find a window directly.

Windows provides the function FindWindow, and is easy to find a window with a specific name or class name.

Window titles usually change with the opened file, and might change if the users changes the language for the application (if there is support for multiple languages). But if it is possible to define your own class name, you have something really unique to search for.

But in MFC programs, the class names are internally defined and used by the MFC framework. And for a dialog based application, it is the fixed system class #32770.

Background

The problem is how to change the MFC application to use a class name that is provided by the developer.

The function we need to change is CMainFrame::PreCreateWindow that is normally already created by the application wizards for MFC SDI and MDI applications.

For a dialog based application, we have to force the system to load our dialog resource template with our defined class name.

Using the Code for SDI and MDI Applications

If you create a SDI/MDI application with the MFC application wizard, you find a function CMainFrame::PreCreateWindow. This function is called several times and is used to provide the information for the window classes and styles that are used to create the window.

The current code just overrides to normal implementation, registers a new window class with a name provided by us and passes the class name to the resulting structure.

You have to remember that CMainFrame::PreCreateWindow is called at least twice. Also the MFC routines check the icon that is defined in the window class struct if it matches the one used and defined by CWinApp::LoadFrame. If the MFC routine (CFrameWnd::GetIconWndClass) that is calling PreCreateWindow finds a different hIcon set in the struct, it creates its own class using the icon that MFC thinks is the correct one. Usually the icon has the ID IDR_MAINFRAME.

The code below just shows what you have to change in your mainfrm.cpp:

C++
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;

    // Just clear the styles we don't want.
    cs.dwExStyle &= ~WS_EX_CLIENTEDGE;

    // Check if our class is already defined
    LPCTSTR pszClassName = _T("OwnClassName");
    WNDCLASS wndcls;
    if (!::GetClassInfo(AfxGetInstanceHandle(), pszClassName, &wndcls))
    {
        // Get the current requested window class
        VERIFY(GetClassInfo(AfxGetInstanceHandle(), cs.lpszClass, &wndcls));

        // We want to register this info with our name
        wndcls.lpszClassName = pszClassName;

        // Need to preset the icon otherwise the function GetIconWndClass
        // calling us will overwrite our class.
        LPCTSTR pszIcon = MAKEINTRESOURCE(IDR_MAINFRAME);
        HINSTANCE hInst = AfxFindResourceHandle(pszIcon, ATL_RT_GROUP_ICON);
        _ASSERTE(hInst!=NULL);
        wndcls.hIcon =     ::LoadIcon(hInst, pszIcon);

        // Register our class now and check the outcome
        if (!::RegisterClass(&wndcls))
        {
            _ASSERTE(!__FUNCTION__ "RegisterClass failed");
            return FALSE;
        }
    }

    // Now use our class 
    cs.lpszClass = pszClassName;
    return TRUE;
}

Using the Code for Dialog Based Applications

In a dialog based application, we have to do two things.

The class name used for a normal dialog is "#32770". When we want to use a different class, we need to tell the dialog template that it should use this special self defined class name, because we don't control the creation of the dialog. There is a property in the resource editor for a dialog to specify a user defined class name but VS-2008 and VS-2010 have a bug here and don't allow the user to enter some data here. The property is grayed and so disabled. Even a changed class name isn't shown in the property window.

But you can change the class name with the editor, and this information is not deleted by the resource editor when we may change the dialog later. So we open the RC file with our preferred editor, search the entry for the dialog template we want to change and add a CLASSNAME statement as shown here:

C++
IDD_OWNCLASSNAMEDLG_DIALOG DIALOGEX 0, 0, 199, 28
STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | 
WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "OwnClassNameDlg"
CLASS "OwnClassNameDlg"
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
...

The only thing left now is to register a new class based on the system class #32770 with our class name that we set in the dialog template before we call dlg.DoModal();

The code you have to add is shown here and it isn't great magic:

C++
...
    // Just get default class for the dialogs
    WNDCLASS wndcls;
    ::GetClassInfo(NULL,MAKEINTRESOURCE(32770),&wndcls);
    
    // Set our own class name
    wndcls.lpszClassName = _T("OwnClassNameDlg");

    // Just register the class
    if (!::RegisterClass(&wndcls))
    {
        _ASSERTE(! __FUNCTION__ " Failed to register window class");
        return FALSE;
    }
    
    COwnClassNameDlgDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
...

BTW: In fact, "#32770" is not a class name. It is just a class name registered with an integer ID where MAKEINTRESOURCE(32770) is used as a class name. It is also ok to use the string "#32770".

Have fun...

History

  • 10 May, 2011 -- Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
MVP for C++ in Germany from 2000 up to 2015.
Developer since 1979, working with C/C++ since 1982
Started with Windows development in 1990

Love my bicycles (specially my recumbent) and geocaching
http://blog.m-ri.de/index.php/category/real-life/fahrrad/

Comments and Discussions

 
QuestionGet an Error with Doc/View in VS2010 Pin
drzhf25-May-12 11:40
drzhf25-May-12 11:40 
Questionone error Pin
prudnik6-Apr-12 4:23
prudnik6-Apr-12 4:23 
AnswerRe: one error Pin
Martin Richter [rMVP C++]6-Apr-12 7:52
Martin Richter [rMVP C++]6-Apr-12 7:52 
Questionmodify dialog class name directly Pin
wocow320-Jun-11 5:46
wocow320-Jun-11 5:46 
GeneralMy vote of 4 Pin
xiayingang17-May-11 4:40
xiayingang17-May-11 4:40 
GeneralMy vote of 5 Pin
code_junkie16-May-11 15:02
code_junkie16-May-11 15:02 
QuestionWraping the Code in Functions Pin
.dan.g.16-May-11 13:52
professional.dan.g.16-May-11 13:52 
AnswerRe: Wraping the Code in Functions Pin
Martin Richter [rMVP C++]16-May-11 20:47
Martin Richter [rMVP C++]16-May-11 20:47 
GeneralMy vote of 2 Pin
xiayingang16-May-11 4:37
xiayingang16-May-11 4:37 
GeneralRe: My vote of 2 Pin
Martin Richter [rMVP C++]16-May-11 11:16
Martin Richter [rMVP C++]16-May-11 11:16 
GeneralRe: My vote of 2 Pin
xiayingang16-May-11 15:06
xiayingang16-May-11 15:06 
GeneralRe: My vote of 2 Pin
Martin Richter [rMVP C++]16-May-11 20:37
Martin Richter [rMVP C++]16-May-11 20:37 
GeneralRe: My vote of 4 Pin
xiayingang17-May-11 4:43
xiayingang17-May-11 4:43 
GeneralRe: My vote of 4 Pin
Martin Richter [rMVP C++]17-May-11 5:01
Martin Richter [rMVP C++]17-May-11 5:01 
Generalgood ,just for me Pin
xiayingang16-May-11 4:28
xiayingang16-May-11 4:28 
GeneralMissing Points Pin
trotwa14-May-11 22:15
trotwa14-May-11 22:15 
GeneralRe: Missing Points Pin
Martin Richter [rMVP C++]15-May-11 0:15
Martin Richter [rMVP C++]15-May-11 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.