Is it possible to create a form that always remains under other applications?
Because the following property exists:
this.TopMost = true;
but there is no property like:
this.BottomMost = true;
Each time the user clicks on the form, it is not positioned at the top level, as normally happens, but remains below other applications. However when the user presses show desktop or Win + D, the desktop is shown but with the form on top.
The form is shown as a kind of Windows Gadget, but it is not a gadget since in Windows 10 they are difficult to activate.
If the form doesn't need to be shown at all times, then just use this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Activated += Form1_Activated;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D && e.Control)
{
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
this.Show();
}
}
private void Form1_Activated(object sender, EventArgs e)
{
this.Hide();
}
}
However, you need to add a COM reference named "Microsoft Shell Controls And Automation"
Also, you will need to change the Form1_KeyDown to something out of this: Global keyboard capture in C# application
Related
I cannot work out where to put nor get the code to trigger when my main form windows are resized (ie minimize button clicked)
I am trying to trigger this code when ANY resize of the DigiDocketMain window is minimized etc, or also how I can specifically code the minimize button to do something - the ideal goal is to get the program - n minimize button click to hide the taskbar icon and show a tray icon.
I have tried placing this is the main code body and the designer code but nothing triggers it. any help would be appreciated.
private void DigiDocketMain_Resize(object sender, System.EventArgs e)
{
MessageBox.Show("You are in the Form.ResizeEnd event.");
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.Hide();
mainTrayIcon.Visible = true;
}
}
In your code behind add the following to the Form_Load Event
this.SizeChanged += Form1_SizeChanged;
Then implement the function, autocomplete may do this for you.
private void Form1_SizeChanged(object sender, EventArgs e)
{
// Add the code that will be called on resize events.
}
According to your description, when clicking the minimize button, you want to hide the
taskbar icon and display the tray icon.
I suggest that you set the Visible of notifyIcon1 to false in the property bar, and select a icon format image as the icon, then try the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Deactivate(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.notifyIcon1.Visible = true;
this.Hide();
this.ShowInTaskbar = false;
}
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}
Is there any way to run the application on the background even if the application/form is closed. All i can do now is just minimize it.
private void Form2_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Normal)
{
Hide();
WindowState = FormWindowState.Minimized;
}
else if (FormWindowState.Normal == this.WindowState)
{
Show();
}
}
If your application (your process) is closed, the notify icon will disappear. There is no way to avoid that.
So what you probably want is to keep your application running even if the user closes your Form. One way to achieve this is to actually not close the form but just hide it.
Therefor you need to subscribe to the Closing event of the Form:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Closing += Form1_Closing;
}
}
and implement the event handler like this:
void Form1_Closing(object sender, FormClosingEventArgs e)
{
Hide();
e.Cancel = true;
}
By setting e.Cancel = true you tell the form not to close. So you are just hiding it.
You'll need to add some code to your notify icon to reopen (Show()) the form again.
What I want
I am creating an application which has two functionalities. Both functionalities have their own form (called FactuurForm and VerhuurForm). I have another Form called Home, which has, among others, two buttons. Depending on which button is clicked, I wish to open one of the two forms, and complete close the Home-form.
What I have
Currently, I have the following code:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Home home = new Home();
home.ShowDialog();
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
home.Close();
factuur.ShowDialog();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
home.Close();
verhuur.ShowDialog();
}
}
}
kiesFactuur and kiesVerhuur are booleans which in my Home class, initialized as false. As soon as I click on of the buttons, the corresponding boolean will flip to true, triggering the if-statements to close the home-form and open the new form.
My question
Altough my current codes works, it seems a bit much for such a simple functionality. I feel like I wouldn't need the booleans and this go all be done easier. So is there an easier/better way to do this?
I've also thought about creating multiple Main functions. Clicking a button would activate the corresponding new Main function and terminate the current Main. Is this even possible and if so, is it a good solution?
I don't exactly understand the need to completely close the home form. I'd just place 2 eventhandlers for each of the buttons and call the following code on them. The first form will be hidden and closed when you close your subform.
private void ShowSubDialog(Form form)
{
this.Hide(); //makes your main form invisible before showing the subform
form.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
ShowSubDialog(new FactuurForm());
Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
ShowSubDialog(new VerhuurForm());
Dispose();
}
private void Factuur_Click(object sender, EventArgs e) {
LoadForm(new FactuurForm());
}
private void Verhuur_Click(object sender, EventArgs e) {
LoadForm(new VerhuurForm());
}
private void LoadForm(Form f) {
this.Hide();
f.ShowDialog();
this.Show();
}
Add this to your Home form, remove everything after home.ShowDialog() from Main, and make Facturr_Click and Verhurr_Click handle their respective button's click events. This will allow Home to hide/show automatically.
You should replace your code like this :
if (home.kiesFactuur)
{
FactuurForm factuur = new FactuurForm();
factuur.Show();
this.Hide();
}
else if (home.kiesVerhuur)
{
VerhuurForm verhuur = new VerhuurForm();
verhuur .Show();
this.Hide();
}
In the VerhuurForm and FactuurForm you may ovveride the event of closure like this :
public VerhuurForm()
{
InitializeComponent();
this.FormClosed += new FormClosedEventHandler(VerhuurForm_FormClosed);
}
void FormClosedEventHandler(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
To be sure that your application is closed if you close the form because the Home still active but hidden.
I'm making a new WPF application and I need to be able to minimize the application and have nice and snug in the system tray, right beside the clock (or in that general area).
This has to work on Windows XP, Vista and 7. I don't have to support older versions of Windows.
What's the simplest way to achieve this if I'm using .NET 4?
Example in MSDN forum
Here's a quick example to show how to minimize to the notification area. You need to add references to the System.Window.Forms and System.Drawing assemblies.
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
ni.Icon = new System.Drawing.Icon("Main.ico");
ni.Visible = true;
ni.DoubleClick +=
delegate(object sender, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == System.Windows.WindowState.Minimized)
this.Hide();
base.OnStateChanged(e);
}
}
I've had success using this free notify-icon implementation in WPF.
http://www.hardcodet.net/projects/wpf-notifyicon
It's pretty simple to setup and the source code is provided. It doesn't rely on Windows Forms, so it's 'pure' WPF and very customizable.
You can find a tutorial on how to use it on CodeProject.
And here is the Nuget Package
Add notifyIcon to your App from Toolbox.
Select your main form >> go to the Properties >> select Events icon >> under FromClosing event type MainForm_FormClosing >> hit enter.
In opened .cs file enter following event action:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
notifyIcon.Visible = true;
ShowInTaskbar = false;
e.Cancel = true;
}
Now your main FORM window will be minimized to the system tray when you click on X button. Next step is to get FORM back to normal state.
Go to the Properties of your notifyIcon >> find DoubleClick event >> type NotifyIcon_DoubleClick and hit enter to get event function created for you.
Put this code to your event:
private void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
notifyIcon.Visible = false;
}
Now, if you want to make the notify icon in fancy style you can add context menu and link it to your notify icon, so you get something like that:
Here is where you link contextMenuStrip to NotifyIcon:
Good luck!
I have a Winforms application, which hosts two PDF Viewers (Unmanaged C++) in its panels.
I want to have application wide hotkeys for project handling (Open,Save,Close,...) which would work even when users focus is on the hosted PDF Viewer.
I managed to achieve that via Winapi and RegisterHotKey, but client doesnt like that those are system wide (e.g. they disable the same MS Word hotkeys)
I did try to disable and enable global hotkeys on Form's Activate/Deactivate events, however those events appear even when i dont leave my own app, for example on top-menu entry or on a dialog-box.
Are there other solutions for application-wide hotkeys, that would work even if the focus is on the hosted application?
SOLUTION:
In the main Form, I handle Activated and Deactivate events like this :
public void PDFPicker_Activated(object sender, EventArgs e)
{
var foregroundHwnd = GUI.winapiwrap.GetForegroundWindow();
if (foregroundHwnd==this.Handle || this.OwnedForms.Any(form => form.Handle==foregroundHwnd))
{
if (!this.hotkeys.Any())
{
registerHotkeys();
this.Text = "Hotkeys just registered";
}
}
}
public void PDFPicker_Deactivate(object sender, EventArgs e)
{
var foregroundHwnd = GUI.winapiwrap.GetForegroundWindow();
if (foregroundHwnd == this.Handle || this.OwnedForms.Any(form => form.Handle == foregroundHwnd))
{
}
else
{
unregisterHotkeys();
this.Text = "Hotkeys off";
}
}
Then in the dialog, which is in the OwnedForms array, i call just this:
private void QuestionPostingPanel_Activated(object sender, EventArgs e)
{
parent.PDFPicker_Activated(sender, e);
}
private void QuestionPostingPanel_Deactivate(object sender, EventArgs e)
{
parent.PDFPicker_Deactivate(sender, e);
}
In case of more such dialogs, I would inherit AbstractDialog : Form, add those 2 event functions and a reference to parent, and then inherit all of them from this AbstractDialog.
While registering/unregistering GlobalHotkeys, it was a problem for me when I tried doing that in parallel - global hot keys were bound to a thread.
So then check on Form Activate/Deactivate events whether the form is a foreground window or not (::GetForegroundWindow()) and then disable/enable hotkeys appropriately.