Jump to content
  • Advertisement
Sign in to follow this  
Lionmane

WinAPI button creation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!