C# Custom Close, Minimize and Maximixe buttons - c#

I've been trying to create my own program, with custom close maximize and minimize buttons (like in Visual Studio, or Word 2013 etc...(my border style is set to "None")) So what I've been trying to do is creating three buttons. One with the closing option, (works fine) one with the minimize option, (also works fine) and one with the maximize button. alone the maximize button works fine, but i want to have it like standard windows buttons, so that when the form is maximized, it will restore the forms previous state (Normal) which i know can be done with
this.WindowState = FormWindowState.Normal;
But it should be with one button if you understand what i mean. What i've tried is making a bool, which's value is set to true when the form is maximized (with an "if" statement) and set to false when the form isn't maximized (else function). Now when the maximize button is clicked the form will maximize and therefore the boolean will be set to true, but when i click again, nothing happens! The other functions like close and minimize works just fine, i even made a "Restore" button, which works just fine!
Any help appreciated, this is my code:
bool restore;
private void set_Restore()
{
{
if (this.WindowState == FormWindowState.Maximized) //Here the "is" functions is
{
restore = true; //Sets the bool "restore" to true when the windows maximized
}
else
{
restore = false; //Sets the bool "restore" to false when the windows isn't maximized
}
}
}
private void MaximizeButton_Click(object sender, EventArgs e)
{
{
if (restore == true)
{
this.WindowState = FormWindowState.Normal; //Restore the forms state
}
else
{
this.WindowState = FormWindowState.Maximized; //Maximizes the form
}
}
}
Well, i've got three warnings and this is the one i thinks is wrong:
Field 'WindowsFormsApplication2.Form1.restore' is never assigned to, and will always have its default value false.
I think it says that the bool "restore" is never used and will always have it's default value FALSE, which it shouldn't because of my set_Restore when it is maximized.
The other two warnings are:
The variable 'restore' is assigned but its value is never used
The variable 'restore' is assigned but its value is never used
Thank you in advance.

You are creating a new local restore variable in your set_Restore() method:
bool restore = true;
Try changing it to just:
restore = true;
I don't even think the variable is needed though. I think you can just do this:
private void MaximizeButton_Click(object sender, EventArgs e) {
if (this.WindowState == FormWindowState.Maximized) {
this.WindowState = FormWindowState.Normal;
} else {
this.WindowState = FormWindowState.Maximized;
}
}

Related

Windows Forms Hidden Panel with buttons is still active

First, I have to use windows forms I know it is bad for making games, but its for school.
I have designed a pause menu for my game using a panel with buttons on it. Here is the code for When I press escape to open the pause menu.
private void Game_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
if (paused == true)
{
paused = false;
pnlPaused.Visible = false;
tmrAnimations.Start();
tmrGame.Start();
tmrJump.Start();
tmrPlayerMovement.Start();
tmrLeftMovement.Start();
tmrRightMovement.Start();
}
else
{
pnlPaused.BringToFront();
pnlPaused.Visible = true;
pnlPaused.BackColor = Color.FromArgb(80, 79, 249, 249);
paused = true;
tmrAnimations.Stop();
tmrGame.Stop();
tmrJump.Stop();
tmrPlayerMovement.Stop();
tmrLeftMovement.Stop();
tmrRightMovement.Stop();
}
}
}
This code works fine until I click one of the buttons. When any button is clicked, even the ones with no code, it is impossible to press escape to unpause again. With this resume button the timers all start and the game starts working again but the form doesn't register any key clicks.
private void btnResume_Click(object sender, EventArgs e)
{
paused = false;
pnlPaused.Visible = false;
tmrAnimations.Start();
tmrGame.Start();
tmrJump.Start();
tmrPlayerMovement.Start();
tmrLeftMovement.Start();
tmrRightMovement.Start();
}
I have tried adding this code into the button click code
pnlPaused.Enabled = false;
This code works in re-enabling user input but every time a key is pressed the windows error sound plays, the one when you have to dismiss an alert etc but try clicking off (hopefully that makes sense). Does anyone know another way to fix the panel still being active after being hidden or preventing the windows sound?
I have realized all I needed to do was unfocus the button with
this.Focus();

Minimize to tray make form unvisible

I am using NotifyIcon to make my form minimize to tray to work at background.
However below code doesn't show app icon at all. Form goes totally invisible. I have to kill that from task manager.
private void Button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
if (FormWindowState.Minimized == this.WindowState)
{
Hide();
this.ShowInTaskbar = false;
notifyIcon1.Visible = true;
}
}
What could be the reason? I want to see my app-icon to re-open the form.
You need to assign an Icon to NotifyIcon to show it in system tray. Also you need to set Visible to true.
You can set properties using property grid at design time or you can set them by code. For example, you can use such code:
this.notifyIcon1.Icon = this.Icon;
this.notifyIcon1.Visible = true;
If you don't set the Icon or if the visible is not true, it will not show the icon.

How to check/uncheck menu item and keep windows on top

I am writing a C# web application where I have a menu strip with an option to keep windows always on top. The name of it is alwaysOnTopToolStripMenuItem and during page load I keep it checked because I start with the form being on top:
private void frmMain_Load(object sender, EventArgs e)
{
alwaysOnTopToolStripMenuItem.Checked = true;
}
I then have the following code to allow the user to select and change it based on the condition of the menu item:
private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
{
if (alwaysOnTopToolStripMenuItem.Checked == true)
{
MessageBox.Show("TRUE - SETTING TO FALSE");
alwaysOnTopToolStripMenuItem.Checked = false;
this.TopMost = false;
}
if (alwaysOnTopToolStripMenuItem.Checked == false)
{
MessageBox.Show("FALSE - SETTING TO TRUE");
alwaysOnTopToolStripMenuItem.Checked = true;
this.TopMost = true;
}
}
When it's commented out, If I go through the menu option, clicking it doesn't uncheck it and then clicking it again doesn't check it. It always remains checked.
The MessageBox displays both prompt no matter what I choose.
How can I fix the following:
Change check to uncheck and uncheck to check based on the current state?
Also, make the form TopMost based on the selection?
Change your second if statement to an else if, because now it will change it to Checked = true, then in the second check (which equals to true), it'll switch it back to Checked = false

c# make a program minimize to task bar

I am trying to make my application minimize to task bar/tray
Here is my code so far that I have pulled together from other SO posts and other people seem to have it working but my app minimizes to the tray but when I click it in the tray it doesn't reopen.
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
just to explain again the problem is the application minimizes to the tray but when I click on the icon it doesn't restore the app to normal. Instead it does nothing.
I found my problem.
what I didn't do was this
Set the NotifyIcon's visible property to false in the property editor. Now go to the property editor of Form1, click on the little lightning symbol to access the events, and double click on the Resize event, and change the code to:
and I also didn't do this
Finally we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer,
I found this information here
Dreamincode
This is what I have used in the past but I fund a copy online at the link below
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
found here: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray

How to start WinForm app minimized to tray?

I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:
Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...
WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:
this.Visible = false;
this.ShowInTaskbar = false;
When the NotifyIcon is double-clicked I have the following:
this.WindowState = FormWindowState.Normal;
this.Visible = true;
this.ShowInTaskbar = true;
Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!
The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) {
allowVisible = true;
Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
allowClose = true;
Application.Exit();
}
}
Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.
I'm reading all the answers and see hacks and black magic... (no offense, mates)
No hacks needed. You don't even have to set "ShowInTaskbar=false" and other stuff. Just do this:
//"Form Shown" event handler
private void Form_Shown(object sender, EventArgs e)
{
//to minimize window
this.WindowState = FormWindowState.Minimized;
//to hide from taskbar
this.Hide();
}
NOTE: I strongly recommend NOT TOUCHING the "ShowInTaskbar" property. For example, if your application registers system-wide hotkeys or other similar stuff (hooks, etc) - setting ShowInTaskBar=false and minimizing your app will prevent Windows from sending some messages to your window... And your hooks/hotkeys/etc will stop working.
In the constructor, remove these two lines:
this.Visible = false;
this.ShowInTaskbar = false;
and add after InitializeComponent();:
this.WindowState = FormWindowState.Minimized;
In designer, set ShowInTaskbar to false & FormWindowState to Normal.
EDIT:
If you post the same in Load event, the window does get minimized but still shows minimized on the desktop. I think this is a bug.
When minimizing an application and you want to hide it from Alt+Tab:
You also need to set the Opacity to stop the titlebar showing near the Start Menu when you set the Border Style to a Tool Window.
On Minimize Event:
this.Visible = false;
this.Opacity = 0;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ShowInTaskbar = false;
On Normalize Event:
this.Visible = true;
this.Opacity = 100;
this.FormBorderStyle = FormBorderStyle.FixedSingle; //or whatever it was previously set to
this.ShowInTaskbar = true;
Move the following code from the Form's constructor to Form_Main_Load(). With the same setup on Notification_Icon when Form_Resize().
// Hide the Form to System Tray
this.WindowState = FormWindowState.Minimized;
This "quick and dirty fix" worked for me:
$form1.FormBorderStyle = "fixedtoolwindow"
$form1.top = -1000000
$form1.Left = -1000000
$form1.Width = 10
$form1.Height = 10
$form1.WindowState = "normal"
$form1.ShowInTaskbar = $False
$form1.Opacity = 0
$form1.Hide()
Hope it helps someone else...

Categories

Resources