Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable MFC writing workspace settings to registry?

Tags:

c++

registry

mfc

By default, a basic MFC C++ project in Visual Studio 2010 will store all its workspace settings in the HKCU registry hive under a user-configurable key name. This includes last window size/position, ribbon settings, status bar, etc.

How can you disable this feature completely so as to not write to the registry at all?

I tried not setting SetRegistryKey(), which gave me a debug assertion from the framework on first read/write to registry. SetRegistryKey((LPCTSTR)NULL) gave the same results. SetRegistryBase() seems to have no effect. No other methods in CWinApp/CWinAppEx seem to help.

like image 692
spoulson Avatar asked Oct 26 '10 16:10

spoulson


2 Answers

Edit: My original answer was wrong. I have edited the answer.


You can tell MFC to store settings in an .ini file instead of the registry. See this previous answer. (Update: That only works if you are not using CWinAppEx.)

If you want to prevent MFC from saving some of the state of the menus and toolbars altogether, add the following to your app’s constructor:

m_bSaveState = FALSE;

The m_bSaveState member is only defined if your app is derived from CWinAppEx.

Alternatively, you can override CWinAppEx::SaveState and CWinAppEx::LoadState.


To eliminate the WindowPlacement registry key, override CWinAppEx::StoreWindowPlacement.

You may still get other registry entries being written. A complete solution would involve subclassing CSettingsStore and then, in your application, calling CSettingsStoreSP::SetRuntimeClass. (See this for more information.) This is fairly difficult because there are a whole bunch of virtual functions you will have to override in your custom CSettingsStore class.

like image 137
Nate Avatar answered Nov 02 '22 22:11

Nate


I know this is late for many but I found that CFrameWndEx -- it will be in your CMainFrame class -- uses WM_CLOSE window to SAVE your applications default location. I do not know what loads the location. However, if you override WM_CLOSE, then that window state is never saved when you exit the program. It will attempt to re-load your last window state, but since none was ever save to begin with, there's nothing to worry about.

A GOTCHA SITUATION: Since the framework DOES still call some sort of WM_INIT function to LOAD the last position, if you compiled your code as normal, maximize the application window, closed the program with the X, WM_CLOSE would have saved the application state as MAXIMIZED. If you recompile the app by overriding WM_CLOSE as mentioned above, re-launch the application, you'll notice it starts out maximized! Obvious not what you intended. Thus, you must re-activate (comment out WM_CLOSE), let the program save the application state by re-launch the program, when closing as a normal window. Allow the overridden WM_CLOSE to work again and you'll see that normal window reappears.

CODE:

In your CMainFrame.h

public: afx_msg void OnClose();

In your CMainFrame.cpp ... Extend your MESSAGE MAP

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)



*ON_WM_CLOSE() // <<< I ADDED THIS*

.... END_MESSAGE_MAP()

void CMainFrame::OnClose() {

PostQuitMessage(0);
//CFrameWndEx::OnClose(); << WE DO NOT WANT TO HAVE THIS CALLED!

}

like image 25
Mubeen Avatar answered Nov 02 '22 23:11

Mubeen