Solved

Dialog window size

Posted on 1998-07-15
8
879 Views
Last Modified: 2013-12-03
I create a dialog window by using CreateIndirect(). And I have a problem.
I want to create the window with a certain size, for example with a client
rectangle size of a parent window. I can get wanted size by using GetClientRect().
But the values I get are measured in pixels. At the same time I have to set
x, y, cx and cy parameters of DLGTEMPLATE in dialog box units, not in pixels.
I make the following:

...
CRect MainWndClientRect;
pParentWnd->GetClientRect(&MainWndClientRect);
...
LONG  DlgBaseUnit = GetDialogBaseUnits();
UINT  DlgBaseUnitX = (UINT)( ( DlgBaseUnit << 16 ) >> 16 );
UINT  DlgBaseUnitY = (UINT)( DlgBaseUnit >> 16 );

DLGTEMPLATE  dlgTempl;
...
dlgTempl.x  = (short)( ( MainWndClientRect.left     * 4 ) / DlgBaseUnitX );
dlgTempl.y  = (short)( ( MainWndClientRect.top      * 8 ) / DlgBaseUnitY );
dlgTempl.cx = (short)( ( MainWndClientRect.Width()  * 4 ) / DlgBaseUnitX );
dlgTempl.cy = (short)( ( MainWndClientRect.Height() * 8 ) / DlgBaseUnitY );
...
CreateIndirect( &dlgTempl, &pParentWnd );
...

As a result I have the dialog window with a wrong size. Why?
0
Comment
Question by:V245
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1410262
I'm not sure why that doesn't work.  But I can suggest an improvement.  (Yes that's right a change that will make it not work even faster.)

instead of

UINT  DlgBaseUnitX = (UINT)( ( DlgBaseUnit << 16 ) >> 16 );
UINT  DlgBaseUnitY = (UINT)( DlgBaseUnit >> 16 );

use

UINT  DlgBaseUnitX = (UINT)( DlgBaseUnit && 0x0000FFFF );
UINT  DlgBaseUnitY = (UINT)( DlgBaseUnit >> 16 );

How far are you off by?  like could this be a rounding thing?  
0
 
LVL 2

Accepted Solution

by:
duneram earned 70 total points
ID: 1410263
Why not catch the WM_INITDIALOG message in that dialog and then use the SetWindowPos() API?

Then you could just use pixel values directly.

In mfc its OnInitDialog
In Owl its SetupWindow
0
 
LVL 11

Expert Comment

by:alexo
ID: 1410264
The calculation you make is only valid if you're using the system font.
There is a workaround.  See KB articles Q145994 and Q74280 for details.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:V245
ID: 1410265
I'm using system font. So I accept the Duneram answer also at common I don't like such solutions. To my mind it isn't a problem solution but it is a trick for go around the problem. In any case it works.
Thanks for Nietod and Alexo for your advices.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1410266
I'm still curious, how far off was the positioning using your formulas?  was it close?
0
 
LVL 1

Author Comment

by:V245
ID: 1410267
Sorry for my delay, I was in leave. Well, at the time I know that   GetDialogBaseUnits() is cause of my problem. This function returns wrong value ( DlgBaseUnitX=10 and  DlgBaseUnitY=20 instead of 8 and 16 ). I tried 'Method Two' from article Q145994 - it works correct.
Thanks for all.
Vladimir
0
 
LVL 11

Expert Comment

by:alexo
ID: 1410268
Well, seems I was able to help after all :-)
0
 
LVL 1

Author Comment

by:V245
ID: 1410269
'Method Two' can be used for calculating dialog units instead GetDialogBaseUnits(). But in my case I can't use it because I need the dialog units values before the window is created. So I can't call MapDialogRect(). In my program I implemented Duneram's solution: I create the window with wrong size and then call SetWindowPos() in OnInitDialog(). I guess His Majesty Bill Gates didn't let me any choice ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question