Thunderdonkey
Thunderdonkey

Reputation: 39

How can I remove the border on a Dialog Window with Dynamic Layout controls?

I have a WIN32 application that uses a main dialog window as background and several alternative dialogs that can appear in front of the main window. These overlay dialogs should not have any border because they need to appear to be part of the main window. Everything was working well until I activated Dynamic Layout on the controls in an overlay dialog. It then acquired a thin border with drop shadow, a thin top bar that was sometimes windows top bar color and sometimes white, and the dialog became independently resizable. I don't want this, I want the overlay dialog to resize only with the main dialog window. How can I force the dialog to have No Border?

Upvotes: 2

Views: 1365

Answers (2)

Thunderdonkey
Thunderdonkey

Reputation: 39

So the answer to my original question is to put the following code in the overloaded OnInitDialog() after the call to the base class.
LONG_PTR wStyle = GetWindowLongPtr(m_hWnd, GWL_STYLE); // Get the current style
wStyle &= ~WS_SIZEBOX; // Mask out the style bit(s) we don't want
SetWindowLongPtr(m_hWnd, GWL_STYLE, wStyle); // And set the new style

Upvotes: 0

Adrian Mole
Adrian Mole

Reputation: 51815

You can modify the style of a dialog window in your override of the OnInitDialog() member function, if that window is created using a custom class derived from CDialog or CDialogEx, or something similar. (If not, you'll need to somehow 'intercept' the window's creation process.)

Assuming you have overridden OnInitDialog(), the process will be along these lines:

BOOL MyDialog::OnInitDialog()
{
    BOOL answer = CDialog::OnInitDialog();                 // Call base class stuff
    LONG_PTR wStyle = GetWindowLongPtr(m_hWnd, GWL_STYLE); // Get the current style
    wStyle &= ~WS_BORDER;   // Here, we mask out the style bit(s) we want to remove
    SetWindowLongPtr(m_hWnd, GWL_STYLE, wStyle);           // And set the new style

    // ... other code as required ...

    return answer;
}

Note: It is important to call the base class OnInitDialog() before you attempt to modify the window's style; otherwise, the window may not be in a 'completed' state, and any changes you make may be reverted.


As mentioned in the comment by IInspectable, it may be possible (or even better) to modify the style (taking out the WS_BORDER attribute) in an override of the PreCreateWindow() function:

BOOL MyDialog::PreCreateWindow(CREATESTRUCT &cs)
{
    if (!CDialog::PreCreateWindow(cs)) return FALSE;
    cs.style &= ~WS_BORDER;
    return TRUE;
}

Again, as shown here, you should call the base class member before modifying the style.

Upvotes: 2

Related Questions