6.1. Windows

Microsoft Visual C++/Microsoft Foundation Classes


6.1. Windows

6.1.1. How can I use a custom icon for a window?

The Microsoft Foundation Class Library stores icons for the main frame window and the MDI frame window as resources. The icon with resource ID AFX_IDI_STD_MDIFRAME is the icon for the MDI frame window, and the icon with resource ID AFX_IDI_STD_FRAME is the icon for the main frame window. To replace these icons in your application, add an icon to your resources file with the appropriate ID.

The application specifies the icon for a view in an MDI child window when it creates the template. The application uses the icon with the specified resource ID when the user minimizes the MDI child window that contains the corresponding view.

This technique allows you to specify one icon for the application to associate with these windows. Windows also supports dynamically painting a minimized window. To do this with MFC, use AfxRegisterWndClass() to register a window class with a NULL icon handle. Override the PreCreateWindow() function in the window class for the dynamically painted icon and copy the name returned by AfxRegisterWndClass() into the lpszClassName member of the CREATESTRUCT. This creates the window using the class that has a NULL icon. When the user minimizes this window, the icon receives WM_PAINT messages that it can process to display information appropriately. To do so, override the OnPaint() message handler and call the IsIconic() function to see if the window is minimized. If so, create a CPaintDC object and use it to draw on the icon. If the window is not minimized, call the base class version of OnPaint() to update the window normally.

MSVC Knowledge Base 6/4/94

6.1.2. How do I change the styles for a window that's created by MFC?

To change the default window attributes used by a framework application created in AppWizard, override the window's PreCreateWindow() virtual member function. PreCreateWindow() allows an application to access the creation process normally processed internally by the CDocTemplate class. The framework calls PreCreateWindow() just prior to creating the window. By modifying the CREATESTRUCT structure parameter to PreCreateWindow(), your application can change the attributes used to create the window.

The CTRLBARS sample application, provided with the Microsoft Foundation Class Library version 2.0, demonstrates this technique to change window attributes. Note that depending on what your application changes in PreCreateWindow(), it may be necessary to call the base class implementation. For more information, see MSVC knowledge base article Q99847.

MSVC Knowledge Base 6/7/95

6.1.3. How do I get the minimal size of a window using MFC?

Write a handler for WM_GETMINMAXINFO.

[email protected], Mike Blaszczak, 6/12/95 via programmer.misc

6.1.4. How do I change a Window's title?

AfxGetApp()->m_pMainWnd->SetWindowText("My Window Title");
                            -or-
AfxGetMainWnd()->SetWindowText ( "My Own Title" ) ;

[email protected], mfc-l, 7/9/95

6.1.5. How do I get rid of 'Untitled' in my main window caption?

Override the PreCreateWindow() function in your MainFrame class and do the following in it..

cs.style &= ~FWS_ADDTOTITLE ;        

You can also set the initial window position (cs.x, cs.y, cs.cx, cs.cy) this way and change your class (cs.lpszClass) this way! Remember to call CFrameWnd::PreCreateWindow at the end...

[email protected], programmer.misc, 7/29/95

6.1.6. How do I maximize my MDI child?

void CMainFrame::ActivateFrame(int nCmdShow)
{
    if (!m_bActivated)
    {
        m_bActivated = TRUE;
        nCmdShow = SW_SHOWMAXIMIZED;
    }
    CFrameWnd::ActivateFrame(nCmdShow);
}

where m_bActivated is a member variable of your frame object.

[email protected], programmer.win32, 8/3/95

6.1.7. Why does focus go nutso with a CSplitterWnd?

Whenever I move the splitter bar, the I-beam cursor in my edit control goes away. I have to click again in the edit control to get back the cursor.

The following Knowledge Base Article (GO MSKB on CIS) explains the focus problem associated with splitter windows and a couple of work-arounds to the problem. This may be of help to you.

ID: Q108434

FIX: CSplitterWnd Class Does Not Handle All Focus Cases

Ramesh, NetQuest., MSMFC, 8/3/95

6.1.8. How do I make my first MDI child window start out maximized?

Here is a solution which works for me:

class CChildFrame : public CMDIChildWnd
{
    // .. stuff deleted ...
   
    // This makes the MDI child maximized.
    virtual void ActivateFrame(int nCmdShow)
    {
        // if another window is open, use default
        if(GetMDIFrame()->MDIGetActive())
            CMDIChildWnd::ActivateFrame(nCmdShow);
        else // else open maximized.
            CMDIChildWnd::ActivateFrame(SW_SHOWMAXIMIZED);
    }
   
    // ... stuff deleted ...
};

- Stephen Bade, [email protected]

6.1.9. How do I make a window stay on top of the others?

SetWindowPos(&wndTopMost, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE)

(like DBWIN does)

[email protected], via mfc-l, 1/19/95

6.1.10. How do I make a window move in front of another?

Call either:

SetWindowPos(&wndTop, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE)

Or

BringWindowToFront();

[email protected], via mfc-l, 1/19/95

6.1.11. How do I implement docking windows like DevStudio has?

MFC does not easily let you do this. The problem is that the dockbar/control bar architecture is built for basic toolbars, not windows. We have solved the problem in our Objective Toolkit product - http://www.stingray.com has demos and white papers - check it out!

NEW!! 6.1.12.  Why is the GetSafeHwnd() function needed, when would a HWND may be unsafe?

If you see the MFC source code in afxwin2.inl in msdev\mfc\include directory, you will realize why you need GetSafeHwnd() function. It's implementation goes something like this :

_AFXWIN_INLINE HWND CWnd::GetSafeHwnd() const
{ return this == NULL ? NULL : m_hWnd; }

which does nothing other than checking for a CWnd pointer and if it is not null returning the m_hWnd member variable of it. There is a possibility of CWnd is null and m_hWnd is junk so GetSafeHwnd() is a real useful function. It does the checking for you about window pointers, which are the part of Windows PtrToHandle maps. Just check about handlemaps which are permanent and temporary and then you will realize the importance of that function.

Chandra Sekhar Rentachinthala, [email protected], mfc-l, 6/28/98