Search code examples
cwinapibuttonmove

Move the x position of a button during runtime in win32 using C


I haven't been able to find much about this in my win32 book or using google. I want to move the x position of a push button I created in a dialog window. In a nutshell, I have 6 buttons and want to reposition them based on if I have to display 4, 5, or 6 buttons.

The goal is to send a message to a button during runtime and move it's x position. I can find simple ways to update the buttons text field and color during runtime, but not position.

My button is...

#define IDC_PB_BUTTON_A

and created as this in a dialog window...

PUSHBUTTON    "A", IDC_PB_BUTTON_A, 4, 4, 30, 30, BS_MULTILINE

I would like to grab that first 4 value during runtime and move it over 10 units making it... where the x position value has changed from 4 to 14.

PUSHBUTTON    "A", IDC_PB_BUTTON_A, 14, 4, 30, 30, BS_MULTILINE

I was hoping I could do this with some sort of SendDlgItemMessage(), but can't find anything on the subject. Any suggestions?


Solution

  • The API call to move a window is SetWindowPos1. Make sure to pass SWP_NOSIZE so that the controls retain their initial size.


    1 You could use MoveWindow as well, but that requires that you pass in the new window size as well as the position. To retain the window size you'd have to call GetWindowRect first.