无法更改按钮的位置
Can't change position of button
在C++应用程序中,我使用CreateWindowEx创建了一个按钮,然后尝试使用SetWindowPos更改其位置,但该按钮没有出现在我想要的位置。
有趣的是,当我调整窗口大小(使用鼠标,而不是以编程方式)时,我可以在一瞬间看到一个空白的轮廓,与按钮应该出现的按钮大小相同。这一定是因为我还调用 SetWindowPos 来响应窗口大小调整事件。但是,实际按钮会保留在同一位置。我会发布屏幕截图,但由于某种原因,剪影从未出现在屏幕截图中。
这是更改 X 位置的代码(更改 Y 位置的代码几乎相同):
HRESULT Control::put_Left(float left)
{
RECT windowRect;
::GetWindowRect(m_hWnd, &windowRect);
if (m_isTopLevel)
{
BOOL bResult = ::SetWindowPos(
m_hWnd,
nullptr,
static_cast<int>(DesktopDpi::GetInstance().DipsToPixelsX(left)),
windowRect.top,
0,
0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION
);
if (!bResult)
return HRESULT_FROM_WIN32(::GetLastError());
}
else
{
// NOTE: for a button this is the code that will execute, because a
// button is not a top-level window
HWND hWndParent = ::GetParent(m_hWnd);
if (hWndParent == nullptr)
return HRESULT_FROM_WIN32(::GetLastError());
POINT parentPos = {0, 0};
if (!::ClientToScreen(hWndParent, &parentPos))
return E_FAIL;
BOOL bResult = ::SetWindowPos(
m_hWnd,
nullptr,
static_cast<int>(DesktopDpi::GetInstance().DipsToPixelsX(left)),
windowRect.top - parentPos.y,
0,
0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION
);
if (!bResult)
return HRESULT_FROM_WIN32(::GetLastError());
}
return S_OK;
}
您确定按钮的父级在处理WM_SIZE
时没有将其移回旧位置吗? 从你的描述中听起来确实很像。
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 问:如何使用C++中的按钮从窗口打开窗口
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- QML按钮点击功能执行顺序
- 当用户超过按钮点击限制时报告
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 函数计算用户按下按钮的频率
- 非常量变量只读位置的赋值
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 如何定义更改car类中car位置的方法
- 设置按钮位置
- 如何获取资源文件中对话框的按钮控件的大小和位置
- 在随机位置生成QPush按钮的计时器
- 无法更改按钮的位置
- 单击按钮时,将当前位置/订单保存在QTabWidget中 - 用户设置