Mike
Mike

Reputation: 819

Center Group Box

I'm creating my first application in Visual C++ and for the life of me I cannot figure out to have the groupbox automatically center when the window is maximized. Right now the groupbox will only align to the left. How would I center it?

Upvotes: 0

Views: 462

Answers (2)

pineconesundae
pineconesundae

Reputation: 353

Note: I'm assuming you are using a Visual C++ Windows Forms Application.

You will have to manually align it by adjusting the Left and Top properties of your control(s). You can override the Resize event of the form so it will trigger when the size of the form is manipulated. You can then use the width and height of the form to position your control in the center appropriately:

void myForm_Resize(System::Object^ sender, System::EventArgs^ e)
{
    myControl->Left = (myForm->Width - myControl->Width) / 2;
    myControl->Top = (myForm->Height - myControl->Height) / 2;
}

Upvotes: 0

Jonathan Potter
Jonathan Potter

Reputation: 37162

Windows doesn't support automatic resizing / centering / justification, etc. for controls. You need to add a WM_SIZE handler for your dialog/window, and manually reposition the control whenever the parent client area changes.

Upvotes: 1

Related Questions