Moving a WPF Window Without a Title Bar

Posted Thursday, September 04, 2008 2:42 PM by Nathan Zaugg

here is some great code for moving a window without a title bar. 

Step 1: Make your window transparent

<Window x:Class="TestWpfApp.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    AllowsTransparency="True"
    WindowStyle="None"
    Background="Transparent"
    Title="My WPF Application" Height="300" Width="300">

Step 2: Make a new title bar

<Grid>
    <Rectangle HorizontalAlignment="Stretch"  VerticalAlignment="Top" 
Height="40" MouseDown="move_window" /> </Grid>

Step 3: Add some code

using System.Runtime.InteropServices;
using System.Windows.Interop;

...
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, 
int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

Step 4: Write the event

public void move_window(object sender, MouseButtonEventArgs e) {
    ReleaseCapture();
    SendMessage(new WindowInteropHelper(this).Handle, 
WM_NCLBUTTONDOWN, HT_CAPTION, 0); }

 

Links:
Filed under: , ,

Comments

# re: Moving a WPF Window Without a Title Bar

Friday, August 05, 2011 10:08 AM by Nathan Zaugg

This is probably a better way with newer versions of the WPF framework:

private void LeftMouseDown_Event(object sender, EventArgs e)

{

  this.DragMove();

}