WPF无边框窗体怎么移动?C#
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF无边框窗体怎么移动?C#相关的知识,希望对你有一定的参考价值。
我想做一个WPF窗体,没有边框,怎么靠鼠标左键拖动窗体位置?不要API……
是WPF窗体不是WinForm窗体,WPF的MouseEventArgs和WinForm的不一样,求用法
private bool leftFlag = false;
private Point mouseOff;
private void toolStrip2_MouseDown(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Left)
mouseOff = new Point(-e.X, -e.Y); //得到变量的值
leftFlag = true; //点击左键按下时标注为true;
private void toolStrip2_MouseMove(object sender, MouseEventArgs e)
if (leftFlag)
Point mouseSet = Control.MousePosition;
mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
Location = mouseSet;
private void toolStrip2_MouseUp(object sender, MouseEventArgs e)
if (leftFlag)
leftFlag = false;//释放鼠标后标注为false;
ps:这种方法很不好,鼠标移动快就会出问题 参考技术B [DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[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;
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
//拖动窗体
ReleaseCapture();
SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0);
参考技术C private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
this.DragMove();
参考技术D 你是不是WindowStyle="None"?
如果是那就简单多了
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
if (e.LeftButton == MouseButtonState.Pressed)
DragMove();
本回答被提问者采纳
WPF C# 实现鼠标穿透窗体
如题,想在WPF下鼠标穿透窗体,当点击窗体时,可以响应窗体后面的应用程序。
也就是整个桌面被这个WPF应用程序覆盖,这个应用程序是透明的,最顶层的。当在这个应用程序上点击鼠标的时候,把这个鼠标事件传递给下面一层的应用程序。
Window.xmal代码:
<Window x:Class="Test.Class1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Right Click To Close" Height="300" Width="300"
WindowStyle="None" AllowsTransparency="True" MouseRightButtonUp="WindowClicked"
Background="x:Null"
>
<Grid MouseRightButtonDown="WindowClicked">
<TextBlock Text="Binding RelativeSource=RelativeSource AncestorType=x:Type Window, Path=Title"
FontSize="18" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20"
MouseLeftButtonDown="DragWindow" FontFamily="Impact" Foreground="Red"/>
<Viewbox Stretch="Uniform" MouseLeftButtonDown="DragWindow">
<Path Fill="#80D0E0FF" Stroke="Red" StrokeThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center"
Data="M79,3L65,82 17,91 50,138 96,157 104,192 175,154 190,167 218,78 156,76 157,9 111,39z"/>
</Viewbox>
</Grid>
</Window>
Window.xmal.cs 代码:
using System;
using System.Collections.Generic;
using System.Text;
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.Shapes;
namespace Test
public partial class Class1 : System.Windows.Window
public Class1()
InitializeComponent();
public void DragWindow(object sender, MouseButtonEventArgs args)
DragMove();
public void WindowClicked(object sender, MouseButtonEventArgs args)
Close();
本回答被提问者采纳 参考技术B form的试过,wpf的还没玩过 参考技术C FORM的简单 WPF是这东东还没用过 参考技术D 穿透?还是不显示鼠标的光标?
以上是关于WPF无边框窗体怎么移动?C#的主要内容,如果未能解决你的问题,请参考以下文章