• 12
• 27
• 9
• 9
• 20

# WinAPI button creation

This topic is 4541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to draw my buttons on my window based on the size of the window and possibly the video resolution (maybe later though). Right now my button sizes are constant: 110 pixels wide and 30 pixels high. I have no idea how to perform the calculations!? I can GetClientRect, but what calculations do I perform on it? I have 4 buttons that I wanted to display on the bottom half of the screen: two centered on the left and two centered on the right. I wanted them centered both vertically and horizontally to each other. If I divide rect.bottom by two, I can chop off the top half. But how do I tell the computer "put these four buttons spaced evenly apart and centered both vertically and horizontally to each other" on the bottom half? I'm using Windows XP and MSVC++ version 6.0. mw

##### Share on other sites
If you divide r.right by 4 you will get the block that each button should occupy. In order to space the buttons out evenly on the window, you should center the button within this space.

For example, let's say your window is 640 pixels wide and your buttons are 100 pixels wide. Dividing 640 by 4 gives 160. So each button's space should be 160 pixels width. To center the button in that space you need to subtract the width of the button from the width of the space and divide the result by 2. This will give you the button's left-offset within that space.

RECT rect;GetClientRect(hwnd, &rect);int spacew = rect.right / 4;int leftoffset = (spacew - button_width) / 2;int button1_x_pos = spacew * 0 + leftoffset;int button2_x_pos = spacew * 1 + leftoffset;int button3_x_pos = spacew * 2 + leftoffset;int button4_x_pos = spacew * 3 + leftoffset;

*Edit, changed GetWindowRect() to GetClientRect(), rect.bottom to rect.right

[Edited by - Colin Jeanne on October 10, 2005 5:36:41 PM]

##### Share on other sites
Uh- in your description you said to divide r.right by 4, but in the code you divide rect.bottom by 4.

I tried playing around with your code, but I still couldn't get it to center correctly.

I forgot to add that I wanted to display the buttons on two lines like this:

Button Button

Button Button

mw