vacuumhead
vacuumhead

Reputation: 509

Windows API SetWindowSize() seems to have no effect

I have what should be an extremely simple goal yet it seems intractable. I have to get some precisely-sized screen pix for documentation so I thought it would make sense to add a control to size my app's window to the exact dimensions I want.

I'm working with VS2010.

Here's the relevant code from my wizard-generated dialog:

DlgSetWindowSize::DlgSetWindowSize(CWnd* pParent /*=NULL*/)
    : CDialogEx(DlgSetWindowSize::IDD, pParent)
{
    m_width = 0;
    m_height = 0;
    m_parent = pParent;
}

void DlgSetWindowSize::OnBnClickedOk()
{
    CDialogEx::OnOK();
    SetWindowPos(m_parent, 0, 0, m_width, m_height, SWP_NOMOVE|SWP_NOZORDER|SWP_NOOWNERZORDER);
}

and here is the call to it:

void CMyAppView::OnWindowSize()
{
    DlgSetWindowSize d(this);
    if (IDOK == d.DoModal())
    {
        Invalidate();
    }
}

SetWindowPos returns a nonzero value, which indicates success according to the documentation (click here). Of course when running the dialog, I enter the pixel values I want for width and height. The debugger verifies that the expected values are being passed to SetWindowPos, and also that SetWindowPos gives a nonzero return. The bit switches in the last parameter to SetWindowPos are set to ignore the position parameters and only express the size parameters in that call, so that the window should be sized as I want without changing position.

Everything appears to be in order, but the window size doesn't change. I have applied this logic to my app's document window, and when that didn't work, I also applied it to the application's MainFrame window. Zero action.

Am I missing something here, or is there some completely different approach I should be using?

Upvotes: 3

Views: 1902

Answers (1)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

Judging by your use of CDialogEx and the naming convention, I guess you are using MFC, right?

Your call to SetWindowPos() operates on the dialog window itself, as this is a class method.

If you want to call parent's SetWindowPos(), you could do:

m_parent->SetWindowPos(0, 0, 0, m_width, m_height,
 SWP_NOMOVE|SWP_NOZORDER|SWP_NOOWNERZORDER);

Also please note that in MFC Document-View architecture, the document doesn't have a window.

Alternatively, you could use Win API call:

::SetWindowPos(m_parent->GetSafeHwnd(), 0, 0, 0, m_width, m_height,
 SWP_NOMOVE|SWP_NOZORDER|SWP_NOOWNERZORDER);

Upvotes: 3

Related Questions