Winform的Formborder.None情况下,解决不能拖动的问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MoYuForm2
{
<UserControl x:Class="MoYuForm2.TopToolBarPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MoYuForm2"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             Background="Transparent"
             >
    <Grid  Background="Transparent"
           MouseDown="Grid_MouseDown"
           MouseMove="Grid_MouseMove"
           MouseUp="Grid_MouseUp"
           >
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Button Content="JohnYang" Width="100"/>
            <StackPanel Grid.Column="1" 
                       Orientation="Horizontal"
                       HorizontalAlignment="Right"
                       
                        >
                <Button x:Name="opacityBtn" 
                        Click="opacityBtn_Click"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Padding="3"
                        >
                    <Image Source="/Resources/opacity.png" Stretch="None"/>
                </Button>
                <Button 
          Click="pinBtn_Click"
          Background="Transparent"
          BorderBrush="Transparent"
          BorderThickness="0"
                    Padding="3"
          >
                    <Image Source="/Resources/pin-fill.png" Stretch="None"/>
                </Button>
                <Button x:Name="pinBtn"
                        Click="pinBtn_Click"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Padding="3"
                        >
                    <Image Source="/Resources/pin.png" Stretch="None"/>
                </Button>
                <Button x:Name="minBtn" Click="minBtn_Click"
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Padding="3"
                        >
                    <Image Source="/Resources/min.png" Stretch="None"/>
                </Button>
                <Button x:Name="closeBtn" Click="closeBtn_Click" 
                        Background="Transparent"
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Padding="3"
                        >
                    <Image Source="/Resources/close.png" Stretch="None"/>
                </Button>
            </StackPanel>
        </Grid>
        
    </Grid>
</UserControl>

    /// <summary>
    /// TopToolBarPage.xaml 的交互逻辑
    /// </summary>
    public partial class TopToolBarPage : System.Windows.Controls.UserControl
    {
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HTCAPTION = 0x2;
        System.Windows.Point _mouseDownPos;
        System.Drawing.Point _formDownPos;
        bool _ifPressLeft = false;
        MoYuForm2 _form;
        public TopToolBarPage(MoYuForm2 form)
        {
            InitializeComponent();
            _form = form;
            var a = this.closeBtn.Width;
        }

        private void opacityBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void closeBtn_Click(object sender, RoutedEventArgs e)
        {
            _form.Close();

        }

        private void minBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void pinBtn_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                ReleaseCapture();//“当前窗口/控件不要再独占鼠标了,把鼠标输入还给系统。”
                SendMessage(_form.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);//WM_NCLBUTTONDOWN + HTCAPTION 消息时,会认为你点下了窗口标题栏。
            }

        }

        private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
  
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
          
        }
    }
}


posted @ 2025-09-21 23:12  JohnYang819  阅读(10)  评论(0)    收藏  举报