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)
{
}
}
}
#####
愿你一寸一寸地攻城略地,一點一點地煥然一新
#####

浙公網安備 33010602011771號