blah blah blah is here! blah blah » Close

up0down
link

not sure whether it is correct place here to pose WPF Questions.

(a) in WPF Application, how do I disable the Right Top Close (x) button, Maximize and Minimize buttons?

(b) in WPF Application, how do I ensure that the application is not movable?

(c) in WPF Application, how do I fix the Window startup location to certain position?

any code samples is greatly appreciated.

last answered one year ago

14 answers

link

hello. in the window class try WindowStyle="None" WindowStartupLocation="CenterScreen" (as elements of the window tag) something like this:

<Window x:Class="Test.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowStyle="None" WindowStartupLocation="CenterScreen">
<Grid>

</Grid>
</Window>

WindowStyle will remove the border and then you won't have a problem with the border and the minimize/max/close at all. beside this the startup location is set to the center of the screen by WindowStartupLocation="CenterScreen">.

hope this helps

up0down
link

The only way I can think of to make the application "not movable" is to handle the form's moved event and the size changed event. On the move, get the current x and y positions and when the user drags the window to move it, just set the form's location to the current x and y you captured. Same thing for size changed, get the current width and height, on size change reset width and height to the values you just captured.

This will make it so if the user moves the window or resizes the window it will seemingly snap back to its original place and size.

Good luck!

up0down
link

yes, your suggestion of WindowStyle="None" works, but it hides my title. I would want my title to be visible. Also, I would want to disable the user from resizing the windows.
thanks.

up0down
link

concerning the title, i usually make a custom title and a custom drag bar to move the form. its found on the net and you can do it easily. and since the windowstyle is none the user won't be able to to resize the form.

up0down
link

how do we change Icon in the WPF application?

up0down
link

use this in the window class tag as its shown in this link:

http://msdn.microsoft.com/en-us/library/system.windows.window.icon.aspx

up0down
link

how about preventing the user from re-sizing the WPF form?

up0down
link

You can prevent resizing by setting the Window's ResizeMode property to ResizeMode.NoResize.

Notice that this also gets rid of the Minimize and Maximize buttons but not the Close button.

EDIT - in response to comment below

To get rid of the Close button as well but without removing the title bar, you'll have to call the Win32 function SetWindowLong() with the appropriate arguments in the window's Loaded eventhandler. There's some sample code in this thread.

You'll need to add the following using directives in order to use this code:

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

Needless to say, you'll need to have another 'escape route' for closing the form if you don't want to user to end up having to kill the process from the TaskBar.

To set the window's startup location to a particular position, first set its WindowStartupLocation property to WindowStartupLocation.Manual.Then set its Left and Top properties to the position you want on the desktop in units of 1/96th of an inch.

up0down
link

thanks. is it possible to get rid of all those 3 buttons (Minimize, Maximize and Close) at the same time, without removing the title? also, possible to control the windows startup position at certain location?

vulpes
17279

Please see my edit.

up0down
link

hi Vulpes, thanks for the good answers. yes, I would provide another 'escape route' for closing the form, meaning another form will close the first form, when it is appropriate. part(a) and part(c) of my problem is solved. how about part (b): in WPF Application, how do I ensure that the application is not movable?

up0down
link

The only easy way to make a WPF window immovable without removing the title bar is to do something similar to what didittoday suggested, namely to handle the window's LocationChanged event and set the Left and Top properties to what they were originally.

The code to do this would be something like the following:

public partial class Window1 : Window
{
// private variables for storing initial location
private double startLeft, startTop;

public Window1()
{
InitializeComponent();
// store initial location
startLeft = this.Left;
startTop = this.Top;
}

// add handler from designer
private void Window_LocationChanged(object sender, EventArgs e)
{
if (this.Left != startLeft || this.Top != startTop)
{
this.Hide();
this.Left = startLeft;
this.Top = startTop;
this.Show();
}
}

// other window code
}

As this is capturing the event after the window has been moved and then moving it back, it's a bit jumpy but it's the best I can come up with.

Unfortunately, WPF doesn't expose WndProc and so it's difficult to prevent the window from moving by intercepting the appropriate Win32 messages. It is possible to 'manufacture' WndProc by hooking into HWndSource but I wouldn't recommend doing that unless you're really desperate.

up0down
link

thanks Vulpes, that is a very intelligent approach to make the form immovable. Yes, it is jumpy. How complicated would it be if I want to use the "intercepting the appropriate Win32 messages" approach? Would you mind try? I would need this, and I believe this would be useful for other people's needs too. thanks.

up0down
link

it seems possible to implement "handle WndProc messages in WPF" from the below 2 links.
http://stackoverflow.com/questions/624367/how-to-handle-wndproc-messages-in-wpf
http://www.steverands.com/2009/03/19/custom-wndproc-wpf-apps/

but I am not sure in details how to implement it for purpose such as to make the form immovable.

up0down
link

Hurray, I got to make the WPF form immovable from the above 2 useful links.

private const int WM_SYSCOMMAND = 0x0112;
private const int SC_MOVE = 0xF010;

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle messages...
if (msg == WM_SYSCOMMAND)
{
if ((wParam.ToInt32() & 0xFFFF0) == SC_MOVE) handled = true; ;
}
return IntPtr.Zero;
}

vulpes
17279

Yes, hooking into HWndSource is the technique I had in mind. The reason I was nervous about recommending this is that the WPF designers no doubt had good reasons for not exposing WndProc directly as they do with Windows Forms but, if it works OK, what the heck :)

Feedback