How to make a Close button in a form disabled [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to make the Close button disabled in a windows Form using C# coding
I want to disable Close button in a form can any one help me in this.

The following should help:
[DllImport("user32.dll")]
private static extern int GetSystemMenu(int hwnd, int revert);
[DllImport("user32.dll")]
private static extern int GetMenuItemCount(int menu);
[DllImport("user32.dll")]
private static extern int RemoveMenu(int menu, int position, int flags);
private void DisableCloseButton()
{
int menu = GetSystemMenu(Handle.ToInt32(), 0);
int count = GetMenuItemCount(menu);
RemoveMenu(menu, count - 1, MF_DISABLED | MF_BYPOSITION);
}

Related

WPF C# hide Console application Show Icon in Hidden task bar [duplicate]

This question already has answers here:
.Net Console Application in System tray
(6 answers)
Closed 5 years ago.
I´m using C#, I want to hide the console application but I want to show the icon in "hidden taskbar" (I don´t know if it is the correct name), something similar like Thunderbird, Team Viewer....
I´ve tried this:
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOW = 5;
const int SW_MINIMIZE = 6;
private const string AppGuid = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9";
// ==============================
// ==============================
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE);
But If I hide the console it is not shown in the "Hidden Task Bar". Is it posible to hide the console and show in the "Hidden Task Bar"?
the keyword which you must search for is "notification icon", then you will find something like How to show a message with icon in the notification area
i can not explain it more precise because i don't know whether you are using wpf or a console application

Hide minimize, maximize, close buttons from a window and show the icon

I'm trying to hide the minimize, maximize and close buttons from the top of my window and still display my icon.
I have tried a couple different things but can't get the icon to stay. This is the code I am working with:
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x00080000;
[DllImport("user32.dll")]
private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hwnd, int index);
public Window()
{
SourceInitialized += MainWindow_SourceInitialized;
InitializeComponent();
Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);
}
void MainWindow_SourceInitialized(object sender, EventArgs e)
{
WindowInteropHelper wih = new WindowInteropHelper(this);
int style = GetWindowLong(wih.Handle, GWL_STYLE);
SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);
}
Any help will be greatly appreciated!
Thanks!
You can set the WindowStyle property of the WPF window in XAML to None.
i.e.
WindowStyle="None"
Using code you can do the same thing as follows:-
WindowName.WindowStyle = WindowStyle.None;
It must work to hide all the three buttons.
This is code I have used to enable and disable the close button in winforms. I realize that's different than what you want in 3 ways
1) It only deals with the close button (although, if Oscar is correct, it's the only one you need to worry about)
2) it doesn't hide it, it just disables/greys it out (though you may be able to change a parameter to completely hide it instead)
3) It is for winforms, not wpf
Despite these differences, perhaps looking at the code will help you figure out what you are missing. If it you do figure it out, I'd be interested in you posting your solution :)
#region Enable / Disable Close Button
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
private const int SC_CLOSE = 0xF060;
private const int MF_BYCOMMAND = 0x0000;
private const int MF_ENABLED = 0x0000;
private const int MF_GRAYED = 0x0001;
protected void DisableCloseButton()
{
try
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
this.CloseButtonIsDisabled = true;
}
catch{}
}
protected void EnableCloseButton()
{
try
{
EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
this.CloseButtonIsDisabled = false;
}
catch{}
}
protected override void OnSizeChanged(EventArgs e)
{
if (this.CloseButtonIsDisabled)
this.DisableCloseButton();
base.OnSizeChanged(e);
}
#endregion
Note that some window styles can not be changed after window creation but I don't know whether this applies to these flags or not... As far as I know if your titlebar is painted by the system you either have both an icon and a close button or none of these because both of them are controlled by the WS_SYSMENU window style.
In the Form properties, for example in a WPF application, you can only hide the minimize and mazimize buttons.
There is a property called ResizeMode, and if you put to NoResize, this two button will be hidden. ;)

Size option disable from the system menu in c# application

I have a c# windows base application.Now I want that the in the system menu the size option should be disable.
To add the option in system menu I am using user32.dll.
I am using windows form.
If you have a dialog box (you haven't specified that) ...
... and if you're using Winforms (you haven't specified that, either) ...
then you can disable the ability to resize by specifying a Fixed border type; and y7ou can disable the ability to minimize or maximize by setting the respective form properties to "false".
For example:
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
form1.MaximizeBox = false;
form1.MinimizeBox = false;
Otherwise, please specify what you're doing, and how you're trying to do it. Sample code is always helpful :)
Now I found the solution,
private const int WM_SYSCOMMAND = 0x112;
private const int MF_BYCOMMAND = 0x00000000;
private const int SC_SIZE = 0xF000 ;
[DllImport("user32.dll")]
private static extern int GetSystemMenu(int hwnd, int bRevert);
[DllImport("user32.dll")]
private static extern bool DeleteMenu(int hMenu, int uPosition, int uFlags);
int menu = GetSystemMenu(this.Handle.ToInt32(), 0);
DeleteMenu(menu, SC_SIZE, MF_BYCOMMAND);

Can a windows form display the min and max buttons without the close button?

Is there any way (in C#) to display a form with just the minimise and maximise buttons? Without the close button?
The only way of removing the close button (that I'm aware of) is:
form.ControlBox = false;
But this also gets rid of both the other buttons.
I wrote a function to do this once
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
if (EnableMenuItem(GetSystemMenu(this.Handle, 0), SC_CLOSE, MF_GRAYED) == -1)
throw new Win32Exception("The message box did not exist to gray out its X");
}
private const int SC_CLOSE = 0xF060;
private const int MF_GRAYED = 0x1;
[DllImport("USER32")]
internal static extern int EnableMenuItem(IntPtr WindowHandle, int uIDEnableItem, int uEnable);
[DllImport("USER32")]
internal static extern IntPtr GetSystemMenu(IntPtr WindowHandle, int bReset);
}
Note alt-f4 still works and right click "close this window" when you are looking at it from the task bar. (tested in windows 7)
There's an article here showing how to do that. It requires using the unmanaged User32.dll

Sending keys to inactive application in C#/.NET [duplicate]

This question already has answers here:
Simulating Key Press C#
(8 answers)
Closed 4 years ago.
I have an application with combobox that contains names of currently running applications. As I understood from msdn library, SendKeys method can send keys only to active application. Is it somehow possible in .NET, to send keys also to inactive app? Or at least in WinAPI ?
You can use the SendMessage() API function to send keystrokes to an inactive window.
With C# <3 is everthing possible :D
No need to be active window, as u wished.
Also here a usefull list of Virtual Key Codes
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
const int WM_SYSKEYDOWN = 0x0104;
const int VK_KEY_A = 0x41;
IntPtr WindowToFind = FindWindow(null, "Window Name");
//In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy
PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0);
//PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
}

Categories