Windows programming

Moderators: None (Apply to moderate this forum)
Number of threads: 3670
Number of posts: 9122

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Converting to dialog units Posted by RX7 on 19 Jul 2001 at 1:33 PM
Lets say I have a dialog box with a button on it. What I'm trying to do is change the buttons position and/or size, relative to its previous position and size.

I use GetWindowRect() to retrieve the RECT of the button. I guess the unit I get is pixels. Then I use GetDialogBaseUnits() and convert from pixels to dialog untis as stated in the Win32 Programmer's Reference under GetDialogBaseUnits(). Finally I use MoveWindow to move and/or resize the button.

Here is where I have trouble. When I try to "move" it to the same position as it allready is (set in the resource file), it should stay in place, but it moves around. What am I doing wrong?

Sorry, I'm not exactly the king when it comes to explaining stuff.

Report
Re: Converting to dialog units Posted by AsmGuru62 on 19 Jul 2001 at 1:55 PM
: Lets say I have a dialog box with a button on it. What I'm trying to do is change the buttons position and/or size, relative to its previous position and size.
:
: I use GetWindowRect() to retrieve the RECT of the button. I guess the unit I get is pixels. Then I use GetDialogBaseUnits() and convert from pixels to dialog untis as stated in the Win32 Programmer's Reference under GetDialogBaseUnits(). Finally I use MoveWindow to move and/or resize the button.
:
: Here is where I have trouble. When I try to "move" it to the same position as it allready is (set in the resource file), it should stay in place, but it moves around. What am I doing wrong?
:
: Sorry, I'm not exactly the king when it comes to explaining stuff.
:

'MoveWindow()' uses the same pixels as 'GetWindowRect()' so you do not have to convert anything...



Report
Thanks, but still got problems Posted by RX7 on 21 Jul 2001 at 10:14 AM
Thanks for your reply.

Ok, GetWindowRect() and MoveWindow() both use pixels. Then the following piece of code should make the button stay in place, right?

RECT rcWnd;

GetWindowRect(GetDlgItem(GetActiveWindow(), IDBUTTON), &rcWnd);
MoveWindow(GetDlgItem(GetActiveWindow(), IDBUTTON), rcWnd.left, rcWnd.top, rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top, TRUE);


But it doesn't, why is that?

Thanks again!

Report
Re: Thanks, but still got problems Posted by Sephiroth on 21 Jul 2001 at 10:40 AM
: Thanks for your reply.
:
: Ok, GetWindowRect() and MoveWindow() both use pixels. Then the following piece of code should make the button stay in place, right?
:
:
: RECT rcWnd;
: 
: GetWindowRect(GetDlgItem(GetActiveWindow(), IDBUTTON), &rcWnd);
: MoveWindow(GetDlgItem(GetActiveWindow(), IDBUTTON), rcWnd.left, rcWnd.top, rcWnd.right - rcWnd.left, rcWnd.bottom - rcWnd.top, TRUE);
: 

:
: But it doesn't, why is that?
:
: Thanks again!
:

Try this:
MoveWindow(GetDlgItem(GetActiveWindow(), IDBUTTON), rcWnd.left, rcWnd.top, <width>, <height>, TRUE);


-Sephiroth


Report
Re: Thanks, but still got problems Posted by AsmGuru62 on 21 Jul 2001 at 5:11 PM
That happens, because 'GetWindowRect()' returns pixels relative to the whole screen, but when you 'MoveWindow()' a button - you use coordinates, relative to a dialog, because button is a child in the dialog, the correct code is this:

RECT rect;
HWND hButton = GetDlgItem (hDlg, IDC_BUTTON1);

GetWindowRect (hButton, &rect);
ScreenToClient (hDlg, (POINT*) &(rect.left));
ScreenToClient (hDlg, (POINT*) &(rect.right));
OffsetRect (&rect, 8, 0); // Shift it 8 pixels to the right
MoveWindow (hButton, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, TRUE);

Cheers!


Report
Thanks again...have one more quesiton Posted by RX7 on 22 Jul 2001 at 5:45 AM
Is there a better way of getting the dialog handle, than using GetActiveWindow()?

Thanks again for your help, I'm grateful!

Report
Re: Thanks again...have one more quesiton Posted by AsmGuru62 on 23 Jul 2001 at 8:12 AM
: Is there a better way of getting the dialog handle, than using GetActiveWindow()?
:
: Thanks again for your help, I'm grateful!
:

Well, if you pass an HWND which came into your dialog procedure in every function you write then you get your HDLG everywhere without calling 'GetActiveWindow()' - it can cause problems when you do some background processing in your dialog, but in some moment you switch to another task - it will give you the HWND of the window in that task instead of your dialog...

void InitControls (HWND hDlg)
{
}

void WmCommand (HWND hDlg, WORD wControlID, WORD wNotification)
{
}

BOOL CALLBACK MyDlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
{
  switch (msg) {
    case WM_INITDIALOG:
      InitControls (hDlg);
      break;
    case WM_COMMAND:
      WmCommand (hDlg, LOWORD (wp), HIWORD (wp));
      break;
    default:
      return FALSE;
  }
  return TRUE;
}




Another way is to make it global for only this source file when it comes in WM_INITDIALOG:

static HWND st_hDlg;

BOOL CALLBACK MyDlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
{
...
case WM_INITDIALOG:
  st_hDlg = hDlg;
  break;
...
}



But it can cause problems in case you have two of these dialogs running as modeless...


Report
...and another one Posted by RX7 on 22 Jul 2001 at 11:09 AM
I have another question, hope you don't mind.

I seem to get it to work without using OffsetRect. But I have another problem. I want to convert the button's posistion and size, from pixels to dialog units, to display them in a MessageBox() (or whatever). If I haven't moved it after I start the program, I should, when I have converted to dialog units, get the same left, top, width, and height as I set in the resource file right? I try to convert using the following code:
typedef struct DLGRECT {
   LONG left;
   LONG top;
   LONG width;
   LONG height;
}      

RECT rcWnd;
DLGRECT rcDlg;
LONG DlgBaseUnits, BaseUnitX, BaseUnitY;

DlgBaseUnits = GetDialogBaseUnits();
BaseUnitX = LOWORD(DlgBaseUnits);
BaseUnitY = HIWORD(DlgBaseUnits);

GetWindowRect(GetDlgItem(GetActiveWindow(), IDBUTTON), &rcWnd);

rcDlg.left   = (rcWnd.left                 * 4) / BaseUnitX;
rcDlg.width  = ((rcWnd.right - rcWnd.left) * 4) / BaseUnitX;   
rcDlg.top    = (rcWnd.top                  * 8) / BaseUnitY;
rcDlg.height = ((rcWnd.bottom - rcWnd.top) * 8) / BaseUnitY;

But it doesn't work! I get the wrong dialog units. Can you help?

Thanks again!


Report
Re: ...and another one Posted by AsmGuru62 on 23 Jul 2001 at 8:02 AM
: I have another question, hope you don't mind.
:
: I seem to get it to work without using OffsetRect. But I have another problem. I want to convert the button's posistion and size, from pixels to dialog units, to display them in a MessageBox() (or whatever). If I haven't moved it after I start the program, I should, when I have converted to dialog units, get the same left, top, width, and height as I set in the resource file right? I try to convert using the following code:
:
: typedef struct DLGRECT {
:    LONG left;
:    LONG top;
:    LONG width;
:    LONG height;
: }      
: 
: RECT rcWnd;
: DLGRECT rcDlg;
: LONG DlgBaseUnits, BaseUnitX, BaseUnitY;
: 
: DlgBaseUnits = GetDialogBaseUnits();
: BaseUnitX = LOWORD(DlgBaseUnits);
: BaseUnitY = HIWORD(DlgBaseUnits);
: 
: GetWindowRect(GetDlgItem(GetActiveWindow(), IDBUTTON), &rcWnd);
: 
: rcDlg.left   = (rcWnd.left                 * 4) / BaseUnitX;
: rcDlg.width  = ((rcWnd.right - rcWnd.left) * 4) / BaseUnitX;   
: rcDlg.top    = (rcWnd.top                  * 8) / BaseUnitY;
: rcDlg.height = ((rcWnd.bottom - rcWnd.top) * 8) / BaseUnitY;
: 

: But it doesn't work! I get the wrong dialog units. Can you help?
:
: Thanks again!
:
:

Good link for you:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hh/winui/dlgboxes_7wfn.asp





Report
Re: ...and another one Posted by AsmGuru62 on 23 Jul 2001 at 8:16 AM
I just realized something... if you are trying to resize a button to take the whole sufrace of a dialog you do not need all that:


void ResizeButton (HWND hDlg, int nBtnID)
{
  RECT dlgRect;
  HWND hButton = GetDlgItem (hDlg, nBtnID);

  GetClientRect (hDlg, &dlgRect);
  MoveWindow (hButton, 0, 0, dlgRect.right, dlgRect.bottom, TRUE);
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.