Solved

Dialog window size

Posted on 1998-07-15
8
872 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:V245
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

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

Author Comment

by:V245
Comment Utility
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
Comment Utility
Well, seems I was able to help after all :-)
0
 
LVL 1

Author Comment

by:V245
Comment Utility
'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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now