无标题窗口拖动

15 篇文章 0 订阅

功能:拖动无标题窗口

环境:vs2010,wpf


方法1:


前台代码:

<Window x:Class="DragWnd.DragWnd"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="120" Width="280" WindowStyle="None" ResizeMode="NoResize" Background="#FFB3E8B3" MouseLeftButtonDown="Window_MouseLeftButtonDown">
    <Grid>
        <Label Content="按下鼠标左键拖拽" Height="23" HorizontalAlignment="Left" Margin="44,30,0,0" Name="label1" VerticalAlignment="Top" Width="144" />
    </Grid>
</Window>


后台代码:

using System;
using System.Windows;
using System.Windows.Input;
using System.Runtime.InteropServices;


namespace DragWnd
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DragWnd : Window
    {
        public DragWnd()
        {
            InitializeComponent();
        }



        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        Point pos = new Point();

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pos = e.GetPosition(null);
            IntPtr hWnd = GetForegroundWindow();
            SendMessage(hWnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
}



方法2:

添加事件:MouseDown  MouseLeftButtonDown

        /// <summary>
        /// 点击按钮 拖动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wxWidgets是一个跨平台的GUI库,可以在Windows、Linux、macOS等系统上开发应用程序。有时候我们需要在应用程序中创建一个无标题窗口,常见的场景是实现自定义的窗口样式或者全屏窗口等。 但是,有时候我们会发现无标题窗口无法拖动,这是因为窗口拖动的操作通常是在标题栏上进行的,但是该窗口并没有标题栏。 为了解决这个问题,我们可以通过鼠标的移动事件来实现窗口拖动。具体来说,我们需要在窗口类中重载鼠标按下、鼠标松开和鼠标移动等事件函数。当鼠标按下时记录鼠标的位置,当鼠标移动时计算相对于鼠标按下时的位置偏移量,并根据偏移量来移动窗口的位置。当鼠标松开时结束拖动操作。 代码实现如下: ``` class MyFrame : public wxFrame { public: MyFrame(wxWindow* parent, const wxString& title) : wxFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxFRAME_NO_TASKBAR | wxNO_BORDER | wxFRAME_FLOAT_ON_PARENT) { // 设置背景色为白色 SetBackgroundColour(wxColour(255, 255, 255)); } protected: wxPoint m_dragPos; void OnMouseDown(wxMouseEvent& event) { if (event.LeftIsDown()) { m_dragPos = event.GetPosition(); } } void OnMouseUp(wxMouseEvent& event) { if (event.LeftIsUp()) { wxPoint pos = GetPosition(); pos.x += event.GetPosition().x - m_dragPos.x; pos.y += event.GetPosition().y - m_dragPos.y; SetPosition(pos); } } void OnMouseMove(wxMouseEvent& event) { if (event.Dragging() && event.LeftIsDown()) { wxPoint pos = GetPosition(); pos.x += event.GetPosition().x - m_dragPos.x; pos.y += event.GetPosition().y - m_dragPos.y; SetPosition(pos); } } wxDECLARE_EVENT_TABLE(); }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_LEFT_DOWN(MyFrame::OnMouseDown) EVT_LEFT_UP(MyFrame::OnMouseUp) EVT_MOTION(MyFrame::OnMouseMove) wxEND_EVENT_TABLE() ``` 在这个例子中,我们创建了一个名为MyFrame的无标题窗口,通过重载OnMouseDown、OnMouseUp和OnMouseMove等事件函数来实现窗口拖动。其中,m_dragPos变量用于保存鼠标按下时的位置,OnMouseDown函数记录该位置,OnMouseUp函数计算偏移量并移动窗口的位置,OnMouseMove函数实时更新鼠标的位置并调整窗口的位置。 最后,我们还需要在类定义中通过wxDECLARE_EVENT_TABLE宏来声明该类所处理的事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值