如何使用其客户区实现拖动窗口? [英] How do I implement dragging a window using its client area?

查看:190
本文介绍了如何使用其客户区实现拖动窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Win32 HWND,我想允许用户保持控制和鼠标左键拖动窗口在屏幕周围。给定(1),我可以检测到用户保持控制,鼠标左键,并移动鼠标,和(2)我有新的和旧的鼠标位置,如何使用Win32 API和我的HWND更改该窗口的位置?

I have a Win32 HWND and I'd like to allow the user to hold control and the left mouse button to drag the window around the screen. Given (1) that I can detect when the user holds control, the left mouse button, and moves the mouse, and (2) I have the new and the old mouse position, how do I use the Win32 API and my HWND to change the position of the window?

推荐答案

实现WM_NCHITTEST的消息处理程序。调用DefWindowProc()并检查返回值是否为HTCLIENT。返回HTCAPTION,如果是,否则返回DefWindowProc返回值。您现在可以点击客户区域并拖动窗口,就像您通过点击标题拖动窗口。

Implement a message handler for WM_NCHITTEST. Call DefWindowProc() and check if the return value is HTCLIENT. Return HTCAPTION if it is, otherwise return the DefWindowProc return value. You can now click the client area and drag the window, just like you'd drag a window by clicking on the caption.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_NCHITTEST: {
        LRESULT hit = DefWindowProc(hWnd, message, wParam, lParam);
        if (hit == HTCLIENT) hit = HTCAPTION;
        return hit;
    }
    // etc..
}

这篇关于如何使用其客户区实现拖动窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆