2

I have MFC SDI application.
It has Ribbon, status bars, properties windows, ...

I need to make client area of a view be x % 16. So I can't adjust entire window, but I need to resize CMyView to be divisible by 16 pixels.

Is there a way to do so?

This code does not work: =(

void CMyView::OnSize(UINT nType, int cx, int cy)
{
    cx -= cx % 16;
    cy -= cy % 16;

    CView::OnSize(nType, cx, cy);

    RECT wr = { 0, 0, cx, cy };
    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
Happy SDE
  • 45
  • 5
  • `AdjustWindowRect` doesn't adjust anything, it merely _calculates the required size of the window rectangle, based on the desired client-rectangle size_ (citation from the [MS documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632665(v=vs.85).aspx)). And calling `CView::OnSize` with a different size doesn't do much (don't remember in which part of the documentation I've read this). – Jabberwocky Jul 07 '16 at 15:27
  • Have you looked at using [MoveWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633534(v=vs.85).aspx)? – Andrew Truckle Jul 07 '16 at 17:50
  • I found some workaround: 1. Create additional child window for a view; 2. On resize align it by 16 pixels and center to the view. – Happy SDE Jul 07 '16 at 20:36

1 Answers1

3

Handling this in WM_SIZE/OnSize is too late because window has already been resized by that time. Use ON_WM_WINDOWPOSCHANGING instead, to monitor changes to window size and apply the following changes:

void CMyWnd::OnWindowPosChanging(WINDOWPOS* wpos)
{
    wpos->cx -= wpos->cx % 16;
    wpos->cy -= wpos->cy % 16;
    __super::OnWindowPosChanging(wpos);
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77