Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / MFC
Tip/Trick

CResizeWnd, Handle a Dialog Resizing Without Dialog Border

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Dec 2014CPOL 12.9K   529   6  
Use CResizeWnd as an add-on to any dialog

Introduction

Sometimes, I need a custom dialog without the default border or a caption, but the dialog can't be resized without the style "WS_CAPTION" or "DS_MODALFRAME". So I try to use a transparent window to cover on the custom dialog and control the resizing behavior, that's CResizeWnd.

Using the Code

To use CResizeWnd, just declare CResizeWnd as a member in the dialog.

C++
private:
    CResizeWnd    m_WndResize;

Then, create it at the create time, and send pointer this as parent.

C++
int CResizeWndDemoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;

    m_WndResize.Create(this);
    m_WndResize.ShowWindow(SW_SHOW);

    return 0;
}

And remember to fit CResizeWnd to the dialog everytime after it changes size. It will usually be OnInitDialog(), OnSize() and OnMove(). Call SetWindowPos() with the "SWP_NOACTIVATE" to avoid CResizeWnd took dialog focus.

C++
BOOL CResizeWndDemoDlg::OnInitDialog()
{
   ....
   ....
   .... 

   CRect rcWindow;
    GetWindowRect(&rcWindow);
    m_WndResize.SetWindowPos(&wndNoTopMost, rcWindow.left, 
        rcWindow.top, rcWindow.Width(), rcWindow.Height(), SWP_NOACTIVATE);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

void CResizeWndDemoDlg::OnSize(UINT nType, int cx, int cy) 
{
    CRect rcWindow;
    GetWindowRect(&rcWindow);
    m_WndResize.SetWindowPos(&wndNoTopMost, rcWindow.left, 
        rcWindow.top, rcWindow.Width(), rcWindow.Height(), SWP_NOACTIVATE);
}

void CResizeWndDemoDlg::OnMove(int x, int y)
{
    CRect rcWindow;
    GetWindowRect(&rcWindow);
    m_WndResize.SetWindowPos(&wndNoTopMost, rcWindow.left, 
        rcWindow.top, rcWindow.Width(), rcWindow.Height(), SWP_NOACTIVATE);
}

That is very easy to use. And there is a demo in the download list, I left it in a very beginning look from a new MFC project. Hope it will be easier to understand how to make it work.

History

  • 2014-12-22: First published

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Taiwan Taiwan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --