Jalaj P. Jha Technical & Miscellaneous Ramblings

26Jan/070

RECT applied to Windows

In my post Windows API functions for Rectangle we saw API functions related to RECT structure. As already discussed the RECT structure is important to Windows as all the Windows are rectangle and the Controls. Lets see here some functions that show relevance of RECT to windows.

The first two functions are GetWindowRect and GetClientRect which gets the WindowRect and ClientRect of a window given the handle to the window.

Now what's the difference between the two?

When you create a Visual Basic project of Application type, you get a default form created there. Now you can select a control from the toolbar and place it anywhere on the form. right? Think again! OK try placing the control over the title bar or overlapping the border, on the minimize-maximize & close button. No you can't.

Check the property window for the Height and Width properties of the window, it may be 3600 and 4800 twips. Now change the BorderStyle property of the form to "0-None" and the Height and Width changes to 3195 and 4680. Why the difference? because the title bar, the maximize minimize button, the borders are all gone leaving behind a form which allows you to place controls on it anywhere! This is what differentiates between a WindowRect and a ClientRect. The client area is the area which is available with the window for placing the controls etc. whereas Window area also includes the area which the various styles i.e. the borders, title bar etc, occupy.

Check the property window more closely you will also find two properties ScaleHeight abd ScaleWidth, which remain the same when border is removed or added. While the Height and Width showed the properties related to Window area, ScaleHeight and ScaleWidth shows those of the Client area.

Now let's get on with the functions.

Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long

Both the functions take handle to window whose properties are to be determined and the pointer to the RECT structure where the function will store the values. The return value is non-zero if the function succeeds otherwise zero.

While the WindowRect function returns the co-ordinates of the window (styles included) with reference to the Top-Left of the screen, ClientRect returns co-ordinates of the Client area with reference to the top-left of it's client area itself. Thus the Top and Left co-ordinates of the ClientRect are always zeros.

When you are developing an application with Windows having styles associated with them, you may not get desired output on all systems if you took Window width and Height in consideration. I may have set display preference to show title bar of width 100 pixels (Yes I may as the Display Properties applet supports my doing so). So how would you decide the width and height of your application windows? I will use AdjustWindowRect function.

Declare Function AdjustWindowRect Lib "user32" (lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long

The AdjustWindowRect takes RECT structure for the first parameter. This should contain the values that you expect to be available for the client area. After the function executes the same parameter will now hold the values that your window should process including the area taken by the styles.

Styles which are desired for the window are passed to the function as the second parameter which includes one or more of below, while the third parameter takes a boolean specifying whether the Window has a menu or not. The function however does not include extra space if the menu bar wraps to two or more rows.

Const WS_BORDER = &H800000
Const WS_CAPTION = &HC00000
Const WS_CHILD = &H40000000
Const WS_CHILDWINDOW = WS_CHILD
Const WS_CLIPCHILDREN = &H2000000
Const WS_CLIPSIBLINGS = &H4000000
Const WS_DISABLED = &H8000000
Const WS_DLGFRAME = &H400000
Const WS_GROUP = &H20000
Const WS_MINIMIZE = &H20000000
Const WS_ICONIC = WS_MINIMIZE
Const WS_MAXIMIZE = &H1000000
Const WS_MAXIMIZEBOX = &H10000
Const WS_MINIMIZEBOX = &H20000
Const WS_POPUP = &H80000000
Const WS_SYSMENU = &H80000
Const WS_POPUPWINDOW = (WS_POPUP Or WS_BORDER Or WS_SYSMENU)
Const WS_THICKFRAME = &H40000
Const WS_SIZEBOX = WS_THICKFRAME
Const WS_TABSTOP = &H10000
Const WS_VISIBLE = &H10000000

AdjustWindowRectEx function extends the above function adding a fouth parameter for extended styles which may be one or more of below.

Declare Function AdjustWindowRectEx Lib "user32" (lpRect As RECT, ByVal dsStyle As Long, _
      ByVal bMenu As Long, ByVal dwEsStyle As Long) As Long

Extended Styles

Const WS_EX_ACCEPTFILES = &H10&
Const WS_EX_APPWINDOW = &H40000
Const WS_EX_CLIENTEDGE = &H200
Const WS_EX_COMPOSITED = &H2000000
Const WS_EX_CONTEXTHELP = &H400
Const WS_EX_CONTROLPARENT = &H10000
Const WS_EX_DLGMODALFRAME = &H1
Const WS_EX_LAYERED = &H80000
Const WS_EX_LAYOUTRTL = &H400000Const
Const WS_EX_LEFT = &H0
Const WS_EX_MDICHILD = &H40
Const WS_EX_NOACTIVATE = &H8000000
Const WS_EX_NOINHERITLAYOUT = &H100000
Const WS_EX_NOPARENTNOTIFY = &H4
Const WS_EX_TOPMOST = &H8
Const WS_EX_TOOLWINDOW = &H80
Const WS_EX_WINDOWEDGE = &H100
Const WS_EX_CLIENTEDGE = &H200
Const WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE)
Const WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST)
Const WS_EX_RIGHT = &H1000
Const WS_EX_RIGHTSCROLLBAR = &H0
Const WS_EX_RTLREADING = &H2000
Const WS_EX_STATICEDGE = &H20000
Const WS_EX_TRANSPARENT = &H20
Const WS_EX_LEFTSCROLLBAR = &H4000
Const WS_EX_LTRREADING = &H0
Comments (0) Trackbacks (2)

Leave a comment

(required)

  • Twitter
  • Buzz
  • Facebook
  • Orkut
  • Picasa
  • YouTube