【问题标题】:Is moving a window with SetWindowPos the 'normal way' to do?使用 SetWindowPos 移动窗口是“正常方式”吗?
【发布时间】:2011-01-08 01:03:30
【问题描述】:

我想知道是否为了将带有 Win32 API 的 (ms-windows-) 窗口向右移动 20 像素并向下移动 40 像素,以下 函数调用是怎么做的:

SetWindowPos(
  /* hWnd             */  hChildDlg2, 
  /* hWndInsertAfter  */ (HWND) -1,
  /* X                */ 20,
  /* Y                */ 40,
  /* cx               */ -1,
  /* cy               */ -1,
  /* uFlags           */  SWP_NOSIZE |  // Ignore cx, cy
                          SWP_NOZORDER  // and hWndInsertAfter
);

我问是因为在我看来,可能有一个函数只接受 HWNDxy 作为参数。

【问题讨论】:

    标签: windows winapi


    【解决方案1】:

    是的,差不多就是这样。您应该更喜欢使用SetWindowPos(),因为它可以让您对如何移动/调整窗口大小进行相当多的控制。

    我通常这样使用它(我写的一个小框架的一部分):

    // 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);
        }
    }
    

    还有一个MoveWindow() 函数可以做几乎相同的事情。使用SetWindowPos() 功能,it's now more of a convenience function than anything else

    【讨论】:

      【解决方案2】:

      是的,这是正常方式,窗口会收到WM_WINDOWPOSCHANGING 消息(参数已更改)还有较旧的MoveWindow,但灵活性较差,实际上强制您设置大小。

      要正确保存和恢复窗口大小,您应该分别使用GetWindowPlacementSetWindowPlacement

      【讨论】:

      • 看起来 SetWindowPos 强制你设置 Z-Order
      • @mikew 不,使用 SWP_NOZORDER
      【解决方案3】:

      你的意思是MoveWindow

      它需要 hwnd, x, y, width, height,因为没有 SWP_NOSIZE 标志,使用它来移动窗口实际上更复杂,因为您还必须获取大小。

      【讨论】:

        猜你喜欢
        • 2015-12-30
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 2010-12-29
        • 2010-12-24
        • 1970-01-01
        相关资源
        最近更新 更多