Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to resize window accurately to get desired client area ?
[ All Languages » VB »  Windows]

Total Hit ( 7630)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Sometimes you might get unexpected window size when you resize or assign different height or width. To resize or change window height/width accurately based on required client area you can use AdjustWindowRect or AdjustWindowRectEx api. You can pass required client area as a RECT parameter to AdjustWindowRect or AdjustWindowRectEx api and also you can specify dwStyle and bMenu parameters. dwStyle is standard windows styles (i.e. WS_CAPTION = window with caption, WS_THICKFRAME = Window with sizing border...and many more). bMenu specifies whether the window has a menu.

To actually resize the window based on required client area required 4 steps

- First call AdjustWindowRect or AdjustWindowRectEx with RECT parameter which contains desired client area.
- Call BeginDeferWindowPos function which allocates memory for a multiple-window- position structure and returns the handle to the structure.
- Call DeferWindowPos function which updates the specified multiple-window – position structure for the specified window.
- Call EndDeferWindowPos function which simultaneously updates the position and size of one or more windows in a single screen-refreshing cycle. This will actually resize the window so its important to call EndDeferWindowPos otherwise you wont see any change.

If you dont want to use BeginDeferWindowPos and EndDeferWindowPos then you can use MoveWindow API

Step-By-Step Example

Click here to copy the following block
Const WS_BORDER = &H800000
Const WS_DLGFRAME = &H400000
Const WS_THICKFRAME = &H40000
Const WS_CAPTION = &HC00000 WS_BORDER Or WS_DLGFRAME

'//Some Extended Styles (can be used with AdjustWindowRectEx)
Const WS_EX_CLIENTEDGE = &H200
Const WS_EX_LEFT = &H0&

Const HWND_BOTTOM = 1
Const HWND_TOP = 0
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_SHOWWINDOW = &H40

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type
'/////////////////////////////////////////////////////////
Private Declare Function AdjustWindowRect Lib "user32" ( _
  lpRect As RECT, ByVal dwStyle As Long, ByVal bMenu As Long) As Long
  
Private Declare Function AdjustWindowRectEx Lib "user32" ( _
  lpRect As RECT, ByVal dsStyle As Long, ByVal bMenu As Long, _
  ByVal dwEsStyle As Long) As Long

'/////////////////////////////////////////////////////////
Private Declare Function BeginDeferWindowPos Lib "user32" ( _
  ByVal nNumWindows As Long) As Long
  
Private Declare Function DeferWindowPos Lib "user32" ( _
  ByVal hWinPosInfo As Long, ByVal hwnd As Long, _
  ByVal hWndInsertAfter As Long, ByVal x As Long, _
  ByVal y As Long, ByVal cx As Long, ByVal cy As Long, _
  ByVal wFlags As Long) As Long
  
Private Declare Function EndDeferWindowPos Lib "user32" ( _
  ByVal hWinPosInfo As Long) As Long
'Or
Private Declare Function MoveWindow Lib "user32" ( _
  ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, _
  ByVal nWidth As Long, ByVal nHeight As Long, _
  ByVal bRepaint As Long) As Long
'/////////////////////////////////////////////////////////

Private Sub Form_Load()

  Dim R As RECT, hDWP As Long
  R.Left = 100
  R.Top = 100
  R.Bottom = 200
  R.Right = 300

  '//Before calling AdjustWindowRect
  Debug.Print "BEFORE Adjust Window: (L=" & R.Left & " T=" & R.Top & ")" & _
    "(R=" & R.Right & " B=" & R.Bottom & ")"

  '//This will modify RECT to get required client area
  AdjustWindowRect R, WS_THICKFRAME Or WS_CAPTION, False

  '//Or (if you want to specify extended styles then call AdjustWindowRectEx)

  'AdjustWindowRectEx R, WS_THICKFRAME Or WS_CAPTION, False, WS_EX_CLIENTEDGE

  '//After calling AdjustWindowRect
  Debug.Print "AFTER Adjust Window: (L=" & R.Left & " T=" & R.Top & ")" & _
    "(R=" & R.Right & " B=" & R.Bottom & ")"

  '////////////////////////////
  'MoveWindow Me.hwnd, _
   '  R.Left, _
   '  R.Top, _
   '  R.Right - R.Left, _
   '  R.Bottom - R.Top, _
   '  False
  '////////////////////////////
  'Or
  '////////////////////////////
  hDWP = BeginDeferWindowPos(1)

  DeferWindowPos hDWP, _
      Me.hwnd, _
      HWND_TOP, _
      R.Left, _
      R.Top, _
      R.Right - R.Left, _
      R.Bottom - R.Top, _
      SWP_SHOWWINDOW

  '//This will actually resize the window
  EndDeferWindowPos hDWP
  '/////////////////////////////
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.