I wanted to have a completely non-standard window; no border and not rectangular. I looked on the Net for how-to’s on a few WPF issues I was having problems with:
Windows without traditional borders. One reference (http://learnwpf.com/Posts/Post.aspx?postId=e9cb689c-e6af-407a-b28c-d38f2f2f555c) had a good description of this, but one point was omitted that caused me problems until I found it elsewhere. Moving windows that have no borders was something I had to figure out myself.
To remove the traditional Windows border from a window, you must set the WindowStyle to None and the ResizeMode to NoResize within the Window tag.
However if you have an irregularly shaped window (for example if you have rounded corners), you also have to set AllowsTransparency to True and set the Background to Transparent (I had to do this because the background for my dialog is a PhotoShopped image – a rectangle with rounder corners).
So the whole Window tag looks like:
<Window
xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
WindowStyle=“None“ ResizeMode=“NoResize”
AllowsTransparency=“True“ Background=“Transparent“>
I have a Settings window, and wanted to do that all in XAML I found that the AllowsTranspency setting has to be set to true in the code-behind, not in the XAML. This may be because I don’t have a background image, just XAML. So my Window tag looks like:
<Window
xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=“http://schemas.microsoft.com/winfx/2006/xaml”
Background=“Transparent”
WindowStyle=“None“ ResizeMode=“NoResize”
Height=“232“ Width=“240“>
And in the C# code-behind, I have:
Settings settings = new Settings();
settings.Owner = this;
settings.AllowsTransparency = true;
settings.Show();
In both these samples, I have removed things like the class name and, because I created the XAML using Expression Blend, the extra name spaces that involves.How to make such a window movable? I wanted to be able to drag this window to a new place. With no border, Windows does not help you, you have to do that all yourself. I captured two events to achieve this and again in the Window tag I had the line
In both these samples, I have removed things like the class name and, because I created the XAML using Expression Blend, the extra name spaces that involves.
How to make such a window movable? I wanted to be able to drag this window to a new place. With no border, Windows does not help you, you have to do that all youself. I captured two events to achieve this and again in the Window tag I had the line: MouseMove=“onMouseMove“ MouseDown=“onMouseDown“Then in the code-behind I would move the window in the MouseMove handler. In the MouseDown handler, I would capture the place the mouse had been pressed. I found that this would disrupt behaviour of the text boxes and list boxes in the window - basically you can’t actually select text in a text box since that moves the window! so I use the DirectlyOver property of the mouse event to only move the window if I click over background (imageBackground is the image object for my background).private Point m_LastPoint;
private void onMouseDown(object sender, EventArgs e)
{
MouseEventArgs args = (MouseEventArgs)e;
if (args.LeftButton == MouseButtonState.Pressed &&
args.MouseDevice.DirectlyOver == imageBackground)
{
m_lastPoint = args.GetPosition(this);
}
}
private void onMouseMove(object sender, EventArgs e)
{
MouseEventArgs args = (MouseEventArgs)e;
if (args.LeftButton == MouseButtonState.Pressed &&
args.MouseDevice.DirectlyOver == imageBackground)
{
if (m_lastPoint.X >= 0 && m_lastPoint.Y >= 0)
{
this.Top += args.GetPosition(this).Y – m_lastPoint.Y;
this.Left += args.GetPosition(this).X – m_lastPoint.X;
}
}
}
This works fairly well, but even now I have problems if I move the move too quickly. I think it’s because I’ve moved the mouse outside the bounds of the window whis being redrawn slightly lower than I can drag it. For neatness I should tidy that up. Probably it’s got something to do with capturing the mouse, which I remember from my old Win32 days.
