Thread: Measuring size

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    34

    Measuring size

    Hi,

    there something I am always curious about for example when I create window I always write the size of the window statically and each time keep trying till I find my right size,but I want to know how can I measure what I want before hand ? because in msdn states that size is in device units which according to screen its in pixels,but it doesn't give any information I have read about pixel measurements etc,but still didn't get into the right direction hopefully someone here can point me to right direction... thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    A little trick when setting up a non-resizeable window is to determine your Client area (the inside of the window) according to controls you are inserting... Once you know how much space the controls need, you can calculate the window size quite easily. It doesn't actually matter what the units of measurement are, so long as you use them consistently (But, you are correct that for a non-dialog window it is Pixels).

    Here's an example:
    Code:
        
         // calculate main window size
        ww = bs * 8;   // width of client area
        wh = (bs * 2) + 28;   // height of client area
        // size display and buttons
        MoveWindow(Wind[1],2,2,ww-4,25,1);
        // relocate toolbars
        MoveWindow(Wind[2],0,28,ww,bs,1);
        MoveWindow(Wind[3],0,bs + 28,ww,bs,1);
        // calculate window rect
        GetWindowRect(Wind[0],&wr);
        wr.right  = ww + (GetSystemMetrics(SM_CXFIXEDFRAME) * 2);
        wr.bottom = wh + (GetSystemMetrics(SM_CYFIXEDFRAME) *2)
                          + GetSystemMetrics(SM_CYCAPTION);
        // resize window
        MoveWindow(Wind[0],wr.left,wr.top,wr.right,wr.bottom,1);
    In the example I've already created all my windows Wind[0] is the main window, Wind[1] is a small message window... Wind[2] and Wind[3] are toolbars. Now I need to wrap my main window around my display and rows of buttons. I know the button sizes so I can easily calculate the width and height I need for my client area and adjust 1, 2, and 3 to suit. From there it's a matter of querying the system for border sizes etc. and then finally adjusting the size of the main window to suit.

    A scheme like this gives you the right size even if the user changes the border width, caption size etc. or switches to "classic view" where the borders are just thin lines.

    I've attached a screen shot of the result...

    Resizeable windows are a whole different animal...
    Last edited by CommonTater; 03-16-2011 at 11:52 AM.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    thanks mate thats good way of measuring size.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use AdjustWindowRect().

    It returns the size you need to create a window if a given style, so that your client area will be the input'ed size.

    EDIT: and works on the current settings of the OS. So large fonts, resolutions or special viewports (ie for people with impared sight) ect do not throw out the calcs

    Have a read about 'twips' if you need to convert pixels to inches/mm.
    Last edited by novacain; 09-12-2010 at 11:01 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by novacain View Post
    I use AdjustWindowRect().

    It returns the size you need to create a window if a given style, so that your client area will be the input'ed size.

    EDIT: and works on the current settings of the OS. So large fonts, resolutions or special viewports (ie for people with impared sight) ect do not throw out the calcs

    Have a read about 'twips' if you need to convert pixels to inches/mm.
    Yep, that works pretty much like the code sample I posted.

    Thing is, I found it's sometimes not accurate, especially if you are using a fixed size window with a toolbar, band or menu bar.

    I kinda fell into my way of doing things over time... I suppose I should revisit AdjustWindowRect and see if they've fixed the problems.
    Last edited by CommonTater; 09-13-2010 at 05:02 AM.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by novacain View Post
    I use AdjustWindowRect().

    It returns the size you need to create a window if a given style, so that your client area will be the input'ed size.

    EDIT: and works on the current settings of the OS. So large fonts, resolutions or special viewports (ie for people with impared sight) ect do not throw out the calcs

    Have a read about 'twips' if you need to convert pixels to inches/mm.
    thanks for your suggestion a swell,also I didn't know abt twips now that I read it...
    it might come handy sometimes while coding gui stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Pointer Size Relative To Integers?
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 04-18-2006, 06:58 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM