Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groupbox resizing issue with radio buttons on top

I'm not sure what I'm doing wrong here. I'm trying to implement a resizing dialog window using MFC. The code is pretty straightforward. I override the following sizing notification:

void CMyDlg::OnSize(UINT nType, int cx, int cy)
{
    CDialog::OnSize(nType, cx, cy);

    // TODO: Add your message handler code here


    //...

    //First move the groupbox, pGroupbox is of type CWnd
    pGroupbox->MoveWindow(rcGroupbox);

    //And then move all radio buttons in it
    //Each is moved the exact same way
    //pEachRadioButton is of type CWnd
    pEachRadioButton->MoveWindow(rcEachRadioButton);

}

But what I get as a result is this.

First here's the initial groupbox:

enter image description here

It happens only when I start dragging the bottom of the main window frame down. I get this artifact:

enter image description here

Note that the radio button positions themselves are correct. If I move the mouse over either of them, it redraws itself correctly (like this "shut-down" button):

enter image description here

Here's the layout of the dialog itself:

IDD_MY_DIALOG DIALOGEX 0, 0, 437, 190
STYLE DS_SETFONT | DS_FIXEDSYS | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
EXSTYLE WS_EX_APPWINDOW
CAPTION "My dialog"
MENU IDR_MENU_MAIN
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
    PUSHBUTTON      "&Cancel",IDCANCEL,381,169,50,14
    GROUPBOX        "When Tasks Are Completed",IDC_STATIC_WHEN_COMPLETED,7,113,423,36
    CONTROL         "Close the pro&gram",IDC_RADIO_CLOSE_PROGRAM,"Button",BS_AUTORADIOBUTTON | WS_GROUP,26,129,73,8
    CONTROL         "Put computer to sleep",IDC_RADIO_SLEEP,"Button",BS_AUTORADIOBUTTON,122,129,84,10
    CONTROL         "Hibernate computer",IDC_RADIO_HIBERNATE,"Button",BS_AUTORADIOBUTTON,229,129,78,10
    CONTROL         "Shut down computer",IDC_RADIO_SHUT_DOWN,"Button",BS_AUTORADIOBUTTON,330,129,81,10
    DEFPUSHBUTTON   "&OK",IDC_BUTTON_SET,311,161,67,22
END

I did some search and found this article, but unfortunately setting those styles did not fix the bug.

Any idea how to fix this?

PS. I'm testing it on Windows Vista, 7, or 8 with visual themes enabled.

like image 713
c00000fd Avatar asked Mar 20 '23 15:03

c00000fd


1 Answers

When you move a window, the window manager will move the current image of the window as it exists. Unfortunately because you moved the frame first, all those windows got clipped. Flipping them around wouldn't help, because then the tops would get clipped.

The easy way to fix it would be to call InvalidateRect on each control after moving it.

The better way would be to call BeginDeferWindowPos before you start moving anything, then EndDeferWindowPos when you're done so that all the windows move together.

P.S. Windows prefers for the group box to come after the radio buttons in the tab order, that might make a difference too.

like image 119
Mark Ransom Avatar answered Mar 27 '23 20:03

Mark Ransom