Click here to Skip to main content
15,896,118 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Help in MDI / VC++ needed Pin
2249176-Jan-04 1:50
2249176-Jan-04 1:50 
GeneralCFont class Pin
TolTol5-Jan-04 23:26
TolTol5-Jan-04 23:26 
GeneralRe: CFont class Pin
Monty26-Jan-04 1:11
Monty26-Jan-04 1:11 
GeneralRe: CFont class Pin
TolTol6-Jan-04 4:02
TolTol6-Jan-04 4:02 
GeneralProgress control using GDI+ Pin
Steve Messer5-Jan-04 21:31
Steve Messer5-Jan-04 21:31 
Generalaquiring the size of a dialog resource Pin
luddet5-Jan-04 18:59
luddet5-Jan-04 18:59 
GeneralRe: aquiring the size of a dialog resource Pin
abc8765-Jan-04 20:30
abc8765-Jan-04 20:30 
GeneralRe: aquiring the size of a dialog resource Pin
Monty25-Jan-04 20:50
Monty25-Jan-04 20:50 
I did some searching as i once read such a problem and the solution as well
Lucky You I found it Smile | :)

Below is an Excerpt from C++Q&A (SEPEMBER 1998) by Paul Dilascia

Hope it Helps Wink | ;)

A) This is a typical Windows chicken-and-egg problem, one of those things that seems like it should be easy but isn't. One way to find the size of a dialog is to load the dialog resource into memory—that is, into a DLGTEMPLATE structure—and look at the cx/cy parameters. Unfortunately, these values are in dialog base units, not pixels. So you have to call GetDialogBaseUnits and do some coordinate conversions, which depend on whether your dialog uses the system font or its own font.
Fortunately, there's an easy and surefire way to find out the size of a dialog, even if it is a bit kludgy: just create the dialog, and see how big it is! Of course, you have to create it invisibly if you don't want the user to see what you're doing. But the dialog used for a form-based app is invisible by default, so in this case making the dialog invisible doesn't require extra effort.
To see how it works, I added the automatic sizing feature to the FormSwap program mentioned in the answer to the previous question. The core of the autosize feature is a static function, GetDialogSize, which gets the size of the form. Yes, even in the days of C++, I still write static functions from time to time. But GetDialogSize is implemented using a C++ class, CTempDlg:

class CTempDlg : public CDialog {
 public:
     CRect m_rcDlg;
 protected:
     virtual BOOL OnInitDialog();
 };
 
 BOOL CTempDlg::OnInitDialog()
 {
     GetWindowRect(&m_rcDlg);
     m_rcDlg -= m_rcDlg.TopLeft();
     EndDialog(0);
     return TRUE;
 }


CTempDlg has just one function override: OnInitDialog. OnInitDialog saves the position of the dialog in a data member, then quits. (MFC does a bit of trickery to allow this; in straight C you can't call EndDialog from within WM_INITDIALOG.) GetDialogSize uses CTempDlg to get the dialog's size, which it returns as a CSize.

CSize GetDialogSize(LPCTSTR pszTemplName,
                     CWnd* pParent)
 {
     CTempDlg dlg;
     dlg.Create(pszTemplName, pParent);
                return dlg.m_rcDlg.Size();
 }

Pretty simple. If you try this at home, just remember that the dialog should have no borders and should be invisible (dialogs used in form views always are).
If you want to get the size of a normal dialog, you'll have to do a bit more coding. To first load the dialog template into memory, turn off the WS_VISIBLE and WS_BORDER styles in the DLGTEMPLATE structure and call CDialog:: CreateIndirect instead of Create. If you're already loading the dialog into memory, you may as well just convert the cx and cy values using GetDialogBaseUnits. In passing, I should also mention another function, MapDialogRect, that converts dialog box units to screen pixels. However, this function requires an HDLG, which is an already created dialog.
Once you have the size of the dialog, you've solved the main problem of sizing your frame—but you aren't out of the woods yet. Now the problem is: given that you know how big the dialog is (that is, how big the client view area should be), how big must you create your main window to get a client area exactly that size? This sounds simple but it turns out to be a real pain in the keyboard. There's a virtual function, CWnd::CalcWindowRect, that's supposed to accomplish this feat. Given a certain-sized client rectangle, it calculates the corresponding frame rectangle. Unfortunately, no one in Redmond bothered to implement this function for CFrameWnd. So that leaves it to you.
Implementing CalcWindowRect is an exercise in nit-picking, or should I say pixel-picking. Given a certain-sized client rectangle, you have to add the heights and widths of all the toolbars, splitter borders, menus, window frames, and what-have-you. In the case of FormSwap, there are toolbars, the splitter window, and the left-pane view that must be taken into account. To calculate the height of the toolbars, you can use the MFC function CWnd::RepositionBars. You feed it a rectangle and a magic code, reposQuery—which says you only want to calculate the toolbars, not actually move them—and presto, you have the numbers you need. If you want to understand RepositionBars better, read the documentation. Good luck.
After calculating the heights of the toolbars, the next troublesome task is calculating the height and width of all the splitter window components: the border with which it surrounds each pane and the splitter bar itself. All these magic numbers are contained within data members in CSplitterWnd, but naturally the data is protected, which means you can't access it! Sigh. So what do you do? Simple: just derive a new class with public functions to export the protected data, and use it instead of CSplitterWnd in your main frame.
class CMySplitterWnd : public CSplitterWnd {
public:
    CSize GetBorderSize()
        { return CSize(m_cxBorder,m_cyBorder); }
    CSize GetSplitterSize()
        { return CSize(m_cxSplitter,m_cySplitter);    }
};


If you're lazy (the best programmers are), you might be tempted to just hardwire the border size and other measurements instead of going to all this trouble. But be warned: the values m_cyBorder and its sisters are different under Windows 3.1, Windows 95, and Windows NT®. So if you want the exact values, you must get them from an instantiated CSplitterWnd object.
By now you've probably fallen asleep just thinking of all this tedious work, but once you've calculated how big your main frame should be, the next step is to actually modify the frame's size. Where do you do it? There are oh, so many places. You could override PreCreateWindow and set the size in CREATESTRUCT, implement a WM_GETMINMAXINFO handler, or alter the size in CFrameWnd::OnCreate. In this particular case, however, there's only one option because the magic RepositionBars function works only after the toolbars have been created. This means the winner is door number three: OnCreate.

int CMainFrame::OnCreate(...)
 {
     // normal stuff
 •
 •
 •
     // Make frame fit dialog
     static CSize sz = GetDialogSize(MAKE
         INTRESOURCE(IDD_DIALOG1), this);
     CRect rc(CPoint(0,0), sz);
     CalcWindowRect(&rc);
     SetWindowPos(NULL, 0, 0,rc.Width(), rc.Height(),
                  SWP_NOMOVE|SWP_NOZORDER);
     return 0;
 }


With this and all the other code in place, the window now appears as in Figure 1, just large enough to enclose the form, and not one pixel larger. Whew!
Before signing off, let me spend a few words on WM_GETMINMAXINFO. Windows sends this message just before creating a window, and again whenever the user attempts to move or size it. WM_GETMINMAXINFO is your opportunity to specify the minimum and maximum sizes allowed for your window. If, after going to all that trouble to calculate the window size, you want to prevent the user from decreasing the size of the window, you should handle WM_GETMINMAXINFO. Just follow the directions in your Win32® Owner's Manual. Don't forget that Windows sends the first WM_GETMINMAXINFO before it creates your window, so RepositionBars won't work. You either have to implement CalcWindowRect another way, without using RepositionBars, or ignore the first WM_GETMINMAXINFO message. One way to do this would be to make the dialog size a data member, m_szDlg, which you initialize to zero and then set in OnCreate. Then your WM_GETMINMAXINFO handler would do nothing if m_szDlg is zero.



Live as if your were to die tomorrow. Learn as if you were to live forever.
-Mahatma Gandhi

GeneralRe: aquiring the size of a dialog resource Pin
luddet6-Jan-04 6:32
luddet6-Jan-04 6:32 
GeneralSMS through my app Pin
parths5-Jan-04 17:56
parths5-Jan-04 17:56 
GeneralRe: SMS through my app Pin
misha_grewal5-Jan-04 21:14
misha_grewal5-Jan-04 21:14 
GeneralRe: SMS through my app Pin
parths5-Jan-04 21:28
parths5-Jan-04 21:28 
QuestionHow do I get my IP address when I am on line? Pin
Anonymous5-Jan-04 16:52
Anonymous5-Jan-04 16:52 
AnswerRe: How do I get my IP address when I am on line? Pin
Roger Wright5-Jan-04 17:47
professionalRoger Wright5-Jan-04 17:47 
AnswerRe: How do I get my IP address when I am on line? Pin
kvenkat5-Jan-04 18:21
kvenkat5-Jan-04 18:21 
GeneralTask Bar Icons Pin
User 5838525-Jan-04 16:50
User 5838525-Jan-04 16:50 
GeneralRe: Task Bar Icons Pin
Monty25-Jan-04 21:57
Monty25-Jan-04 21:57 
GeneralLittle Help please Pin
ViTToRi05-Jan-04 15:45
ViTToRi05-Jan-04 15:45 
GeneralRe: Little Help please Pin
Christian Graus5-Jan-04 16:08
protectorChristian Graus5-Jan-04 16:08 
QuestionHow to enumerate all the files in a directory? Pin
George25-Jan-04 15:12
George25-Jan-04 15:12 
AnswerRe: How to enumerate all the files in a directory? Pin
Prakash Nadar5-Jan-04 16:02
Prakash Nadar5-Jan-04 16:02 
GeneralRe: How to enumerate all the files in a directory? Pin
George25-Jan-04 17:30
George25-Jan-04 17:30 
QuestionHow to use! Pin
skylonely5-Jan-04 15:04
skylonely5-Jan-04 15:04 
AnswerRe: How to use! Pin
abc8765-Jan-04 20:07
abc8765-Jan-04 20:07 
QuestionHow can I produce a report ... Pin
Qadddd5-Jan-04 12:13
Qadddd5-Jan-04 12:13 

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.