开发者

Is moving a window with SetWindowPos the 'normal way' to do?

开发者 https://www.devze.com 2023-02-03 08:02 出处:网络
I am wondering if, in order to move a (ms-windows-)window with the Win32 API 20 px to the right and 40 px downwards, the following

I am wondering if, in order to move a (ms-windows-)window with the Win32 API 20 px to the right and 40 px downwards, the following function call would be how to do it:

SetWindowPos(
  /* hWnd             */  hChildDlg2, 
  /* hWndInsertAfter  */ (HWND) -1,
  /* X        开发者_高级运维        */ 20,
  /* Y                */ 40,
  /* cx               */ -1,
  /* cy               */ -1,
  /* uFlags           */  SWP_NOSIZE |  // Ignore cx, cy
                          SWP_NOZORDER  // and hWndInsertAfter
);

I ask because it seems to me that there could be a function only taking a HWND and an x and y as parameters.


Yes, that's pretty much how it's done. You should prefer to use SetWindowPos() since it gives you quite a bit of control over how the window should be moved/resized.

I typically use it like this (part of a small framework I wrote):

// Window member function
void Window::Move(int x, int y)
{
    if(hwnd != 0)
    {
        ::SetWindowPos(hwnd, 0, x, y, 0, 0, 
            SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
    }
}

There's also a MoveWindow() function that does pretty much the same thing. With the SetWindowPos() function available, it's now more of a convenience function than anything else.


Yes, this is the normal way and the window will get a WM_WINDOWPOSCHANGING message (with the parameters that changed) There is also the older MoveWindow but it is less flexible and actually forces you to set the size.

To properly save and restore window sizes you should use GetWindowPlacement and SetWindowPlacement respectively.


You mean like MoveWindow?

It takes hwnd, x, y, width, height, since there's no SWP_NOSIZE flag, it's actually more complicated to use it to just move the window, since you also have to fetch the size.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号