[comp.windows.ms.programmer] Creating windows with a specified client size

bcw@rti.rti.org (Bruce Wright) (11/09/90)

In article <18647@haddock.ima.isc.com>, andya@haddock.ima.isc.com (Andy Adler) writes:
> Is there a way to create a window by specifying the window's
> client area?  Is there a way to query windows about the
> size of the decorations it will add to a window?  I want to 
> be able to create a window and know what the client area will
> be before showing it.

It depends on what toolbox you are using.  It's quite possible to
create a window and specify a specific size and placement - those
are parameters to the CreateWindow function call in the Microsoft
SDK.  The problem is that these parameters are given in terms of
the total window size, including the title bar, menu bar, border,
scroll bars, etc.  What you have to do is to call the GetSystemMetrics
function to find the size(s) of the other object(s) created with
the window (depending on what type of window it is), and add their
sizes to the size of the client area that you want to create.

You can also use SetWindowPos to set the position and size of
a window that has already been created.

Note that the dimensions of both the sizes reported by the
GetSystemMetrics function and the sizes/coordinates required by
the CreateWindow function are in screen coordinates (pixels), which
are not necessarily the same dimensions that you will want to use
when drawing on the client area.  You may find that MM_LOENGLISH
(for example) is a better mapping mode for what you want to paint
in the client area;  then you would have to use the DPtoLP and
LPtoDP functions to convert between the different mapping modes
to get the "size of the created window"/"size of the window to be
created" (depending on exactly what you're trying to do).  

You do run into a minor problem when you are trying to create a 
window with a specific size in (say) inches rather than pixels:  
the LPtoDP function requires a Device Context (which you can't get
until you create the window).  In that case you either have to
make the computation manually from information returned by
GetSystemMetrics and GetDeviceCaps, or use SetWindowPos after the 
window has been created (presumably without being displayed) and 
you have called LPtoDP, or play games such as getting the desktop 
window with GetDesktopWindow and using it for a Device Context.  
You may find the MulDiv routine useful for some of this, since it 
eliminates the need for somewhat elaborate coercions to avoid 
overflow.  To my mind, all of these are ugly in varying degrees, 
but I may be missing a clever way to do this.

						Bruce C. Wright

miked@banyan.UUCP (Mike Deem@Eng@Banyan) (11/09/90)

In article <18647@haddock.ima.isc.com>, andya@haddock.ima.isc.com (Andy Adler) writes:
> Is there a way to create a window by specifying the window's
> client area?  Is there a way to query windows about the
> size of the decorations it will add to a window?  I want to 
> be able to create a window and know what the client area will
> be before showing it.

You want the AdjustWindowRect function. You give it a RECT structure with
your desired client size, the style of your frame rectangle, and a flag 
indicating if you have a menu and it will adjust the RECT structure to 
include those frame controls.

Hope this helps.

Mike Deem

jls@hsv3.UUCP (James Seidman) (11/10/90)

In article <1990Nov8.180215.10952@rti.rti.org> bcw@rti.rti.org (Bruce Wright) writes:
>You do run into a minor problem when you are trying to create a 
>window with a specific size in (say) inches rather than pixels:  
>the LPtoDP function requires a Device Context (which you can't get
>until you create the window).  In that case you either have to
>make the computation manually from information returned by
>GetSystemMetrics and GetDeviceCaps, or use SetWindowPos after the 
>window has been created (presumably without being displayed) and 
>you have called LPtoDP, or play games such as getting the desktop 
>window with GetDesktopWindow and using it for a Device Context.  

I don't have the SDK reference in front of me, so I might be wrong here,
but I think that you can call LPtoDP with an *Information Context* as
opposed to a Device Context.  ICs are designed to be used in just these
sorts of cases where you run into a Catch-22 on needing information on
a DC before you create it.  More information should be under the command
"CreateIC()" in the reference manual.  I believe Petzold also gives
examples of using ICs for such things.

-- 
Jim Seidman (Drax), the accidental engineer.
"It doesn't have to work... they'll be paralyzed just from laughing at me."
							- Dr. Who, _Shada_
UUCP: ames!vsi1!hsv3!jls	         INTERNET: hsv3.UUCP!jls@apple.com