How To Close The Skype Window
Table of Contents
- Introduction
- ResizeMode
- P/Invoke
- Sample code
- See besides
Introduction
There may exist situations when yous want to hide or disable the minimize, maximize, or close push of a window without having to modify the mode of the window in whatsoever other way. In WPF yous tin can indeed set the WindowStyle property of a Window to System.Windows.WindowStyle.ToolWindow to get rid of the minimize and maximize buttons completely, just the window will then look slightly different compared to when the property is set to its default value of SingleBorderWindow.
ResizeMode
You can in fact as well disable the minimize push button by setting the ResizeMode property of the window to CanMinimize only this will preclude the user from being able to resize the window using the mouse as a side-effect. And what if you for instance want to go rid of the minimize and maximize buttons and maybe disable the close button? At that place are in fact no API:s bachelor in the .Internet Framework that lets you practise this. The adept news is that you can achieve this pretty easily anyway past making utilize of native methods in Windows.
P/Invoke
The easiest fashion to call unmanaged code (C++ in this instance) from managed (.NET) code is to apply the Platform Invocation Services, frequently also referred to as P/Invoke. Y'all simply provide the compiler with a declaration of the unmanaged function and phone call it like you would call any other managed method. In that location is an unmanaged SetWindowLong method that can be used to alter an attribute of a specified window. To be able to call this method from your WPF window class using P/Invoke, you lot only add together the post-obit declaration to the window class:
[DllImport( "user32.dll" )]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
The DllImport attribute specifies the name of the DLL that contains the method and the extern keyword tells the C# compiler that the method is implemented externally and that it won't find any implementation or method torso for it when compiling the awarding. The beginning statement to be passed to the SetWindowLong method is a handle for the window for which you desire to disable whatsoever of the mentioned buttons. You can get handle for a WPF window past creating an example of the managed WindowInteropHelper class and access its Handle holding in an event handler for the window'south SourceInitialized event. This event is raised when the handle has been completely created. The second statement of the SetWindowLong method specifies the aspect or value of the window to be set, expressed as a constant integer value. When you want to change the window manner, you should laissez passer the GWL_STYLE (= -sixteen) constant every bit the 2d argument to the method.
private const int GWL_STYLE = -16;
Finally the third argument specifies the the replacement value. There are a set of constants that y'all could use hither:
individual const int WS_MAXIMIZEBOX = 0x10000; //maximize push
private const int WS_MINIMIZEBOX = 0x20000; //minimize button
Notation still that since you are supposed to laissez passer in a DWORD that specifies the consummate value for the "property" specified by the second statement, i.east. the window style in this case, you cannot simply pass any of these constants past themselves equally the 3rd argument to the method. There is another GetWindowLong method that retrieves the current value of a specific property – again the GWL_STYLE in this case – and you tin then use bitwise operators to get the correct value of the third parameter to laissez passer to the SetWindowLong method. Below is a consummate code sample of how you for example could disable the minimize push for a window in WPF:
public fractional form MainWindow : Window
{
[DllImport( "user32.dll" )]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport( "user32.dll" )]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private const int GWL_STYLE = -16;
private const int WS_MAXIMIZEBOX = 0x10000; //maximize button
private const int WS_MINIMIZEBOX = 0x20000; //minimize push button
public MainWindow() {
InitializeComponent();
this .SourceInitialized += MainWindow_SourceInitialized;
}
private IntPtr _windowHandle;
individual void MainWindow_SourceInitialized( object sender, EventArgs e) {
_windowHandle = new WindowInteropHelper( this ).Handle;
//disable minimize button
DisableMinimizeButton();
}
protected void DisableMinimizeButton() {
if (_windowHandle == IntPtr.Zero)
throw new InvalidOperationException( "The window has not yet been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) & ~WS_MINIMIZEBOX);
}
}
Disabling the maximize push is and so but a matter of replacing the WS_MINIMIZEBOX constant with the WS_MAXIMIZEBOX constant in the phone call to the SetWindowLong method:
individual void MainWindow_SourceInitialized( object sender, EventArgs eastward) {
_windowHandle = new WindowInteropHelper( this ).Handle;
//disable the maximize button
DisableMaximizeButton();
}
protected void DisableMaximizeButton() {
if (_windowHandle == aught )
throw new InvalidOperationException( "The window has not withal been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) & ~WS_MAXIMIZEBOX);
}
If you want to hibernate both the minimize and the maximize buttons (disabling both these buttons volition make them invisible) you could utilise the beneath method:
protected void HideMinimizeAndMaximizeButtons() {
if (_windowHandle == zilch )
throw new InvalidOperationException( "The window has not nevertheless been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
}
When it comes to the close button you cannot use the SetWindowLong method to disable information technology just there is an EnableMenuItem method that works similarly. The reason why you cannot utilize the SetWindowLong to disable the close button has to do with the fact that there was no close button earlier the Windows 95. The window explanation simply had the the minimize and maximize buttons in the upper right corner and they were controlled with a window style. The EnableMenuItem method takes a handle to a carte particular, an unsigned integer that specifies the detail menu detail to be disabled or enabled and another unsigned integer argument that controls interpretation of the second argument and indicates whether the menu item should be enabled or disabled:
[DllImport( "user32.dll" )]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
The handle to the menu detail tin be obtained by using the GetSystemMenu method:
[DllImport( "user32.dll" )]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport( "user32.dll" )]
individual static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
[DllImport( "user32.dll" )]
private static extern IntPtr DestroyMenu(IntPtr hWnd);
individual const uint MF_BYCOMMAND = 0x00000000;
individual const uint MF_GRAYED = 0x00000001;
private const uint SC_CLOSE = 0xF060;
IntPtr menuHandle;
protected void DisableCloseButton() {
if (_windowHandle == null )
throw new InvalidOperationException( "The window has not however been completely initialized" );
menuHandle = GetSystemMenu(_windowHandle, false );
if (menuHandle != IntPtr.Zero) {
EnableMenuItem(menuHandle, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
}
}
When passing fake as the second statement to the GetSystemMenu method, the function will render a handle to the copy of the window menu currently in use. For y'all to be able to enable the close button again later on you lot have disabled it, yous need to relieve the reference to this handle (menuHandle in the sample lawmaking above). You can then simply utilize the MF_ENABLED constant to enable it again:
private const uint MF_ENABLED = 0x00000000;
protected void EnableCloseButton() {
if (_windowHandle == null )
throw new InvalidOperationException( "The window has non yet been completely initialized" );
if (menuHandle != IntPtr.Zilch) {
EnableMenuItem(menuHandle, SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
}
}
Also note that you should call a DestroyMenu function to release the managed retention associated with the handle once you lot are washed using it in club to prevent memory leaks. You could for example do this when the window is endmost down by overriding the OnClosing method of the Window class:
[DllImport( "user32.dll" )]
private static extern IntPtr DestroyMenu(IntPtr hWnd);
protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
base .OnClosing(e);
if (menuHandle != IntPtr.Zero)
DestroyMenu(menuHandle);
}
To hide the maximize, minimize and close buttons altogether yous tin simply use yet another constant (WS_SYSMENU) like you employ the WS_MAXIMIZEBOX and WS_MINIMIZEBOX constants and call the SetWindowLong method every bit before:
private const int WS_SYSMENU = 0x80000;
protected void HideAllButtons() {
if (_windowHandle == aught )
throw new InvalidOperationException( "The window has non yet been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) & ~WS_SYSMENU);
}
Enabling the buttons after you have disabled them is but a matter of changing the operator from a logical AND and XOR to a logical OR and and apply the same constants:
protected void EnableMinimizeButton() {
if (_windowHandle == IntPtr.Nix)
throw new InvalidOperationException( "The window has not yet been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) | WS_MINIMIZEBOX);
}
protected void EnableMaximizeButton() {
if (_windowHandle == null )
throw new InvalidOperationException( "The window has not still been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) | WS_MAXIMIZEBOX);
}
protected void ShowMinimizeAndMaximizeButtons() {
if (_windowHandle == null )
throw new InvalidOperationException( "The window has not nonetheless been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) | WS_MAXIMIZEBOX | WS_MINIMIZEBOX);
}
protected void ShowAllButtons() {
if (_windowHandle == null )
throw new InvalidOperationException( "The window has not yet been completely initialized" );
SetWindowLong(_windowHandle, GWL_STYLE, GetWindowLong(_windowHandle, GWL_STYLE) | WS_SYSMENU);
}
Sample code
I take put together a sample solution that consists of a class library with a CustomWindow class that contains all the lawmaking from above and a sample WPF awarding containing merely a window of this type with buttons and event handlers that telephone call the different methods in the custom base window course. You tin can downloaded it from the MSDN Samples Code Gallery here.
See too
GetWindowLong function: http://msdn.microsoft.com/en-united states/library/windows/desktop/ms633584(v=vs.85).aspx
SetWindowLong function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx
EnableMenuItem function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647636(five=vs.85).aspx
GetSystemMenu role: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647985(five=vs.85).aspx
DestroyMenu function: http://msdn.microsoft.com/en-us/library/windows/desktop/ms647631(five=vs.85).aspx
Source: https://social.technet.microsoft.com/wiki/contents/articles/29183.wpf-disabling-or-hiding-the-minimize-maximize-or-close-button-of-a-window.aspx
Posted by: smithcappirms.blogspot.com

0 Response to "How To Close The Skype Window"
Post a Comment