So here's is the situation:
I've created a custom button that minimizes my WPF application,
the thing is that it minimizes right away to the taskbar
(unlike random applications that have the Win7 minimize effect).
My question is, how can I put that regular effect on my customized button when it minimizes?
Thank you.
You could P/Invoke AnimateWindow to add the effect you want.
Use this in the Button Click event (the border cause the minimize animation)
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Minimized;
override OnActivate to hide again the border what the window is maximized
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (WindowStyle != WindowStyle.None)
{
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (DispatcherOperationCallback)delegate(object unused)
{
WindowStyle = WindowStyle.None;
return null;
}
, null);
}
}
Related
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 can you program a dotNet Windows (or WPF) Application in order to let it going fullscreen on the secondary monitor?
Extension method to Maximize a window to the secondary monitor (if there is one).
Doesn't assume that the secondary monitor is System.Windows.Forms.Screen.AllScreens[2];
using System.Linq;
using System.Windows;
namespace ExtendedControls
{
static public class WindowExt
{
// NB : Best to call this function from the windows Loaded event or after showing the window
// (otherwise window is just positioned to fill the secondary monitor rather than being maximised).
public static void MaximizeToSecondaryMonitor(this Window window)
{
var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();
if (secondaryScreen != null)
{
if (!window.IsLoaded)
window.WindowStartupLocation = WindowStartupLocation.Manual;
var workingArea = secondaryScreen.WorkingArea;
window.Left = workingArea.Left;
window.Top = workingArea.Top;
window.Width = workingArea.Width;
window.Height = workingArea.Height;
// If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
if ( window.IsLoaded )
window.WindowState = WindowState.Maximized;
}
}
}
}
For WPF apps look at this post. Ultimately it depends on when the WindowState is set to Maximized. If you set it in XAML or in window constructor (i.e. before the window is loaded) it will always be maximized onto primary display. If, on the other hand, you set WindowState to Maximized when the window is loaded - it will maximise on the screen on which it was maximized before.
I notice an answer which advocates setting the position in the Loaded event, but this causes flicker when the window is first shown normal then maximized. If you subscribe to the SourceInitialized event in your constructor and set the position in there it will handle maximizing onto secondary monitors without flicker - I'm assuming WPF here
public MyWindow()
{
SourceInitialized += MyWindow_SourceInitialized;
}
void MyWindow_SourceInitialized(object sender, EventArgs e)
{
Left = 100;
Top = 50;
Width = 800;
Height = 600;
WindowState = WindowState.Maximized;
}
Substitute coords for any on your secondary monitor
See this codeproject article.
The code in there will work, but default to your primary monitor. To change this, you'll need to replace the calls to GetSystemMetrics will calls to GetMonitorInfo. Using GetMonitorInfo, you can get the appropriate RECT to pass to SetWindowPos.
GetMonitorInfo allows you to get the RECT for any monitor.
There is an MSDN Article on Position Apps in Multi-Monitor Setups that might help explain things a bit better.
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = GetSecondaryScreen().Bounds;
}
private Screen GetSecondaryScreen()
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen != Screen.PrimaryScreen)
return screen;
}
return Screen.PrimaryScreen;
}
It's not clear from your question if you are looking for a way to move the window to the secondary monitor and then go fullscreen, or if you are just looking to support fullscreen mode on whatever monitor the window is on (which may be primary or secondary).
If the later, for a WPF window, though not quite the same as fullscreen mode, you can remove the borders when it is maximized and restore the border when not maximized. No need to check for which monitor, etc. The display of the caption/title bar is controlled by the border state.
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == WindowState.Maximized)
{
if (WindowStyle.None != WindowStyle)
WindowStyle = WindowStyle.None;
}
else if (WindowStyle != WindowStyle.SingleBorderWindow)
WindowStyle = WindowStyle.SingleBorderWindow;
base.OnStateChanged(e);
}
Credit goes to Pavel for his Forms-based answer in the current question and to Nir for his answer in this question.
#jay-evans answer did the trick for me. However, I also needed to add the WpfscreenHelper Nuget package to get the screen information while not depending on the old System.Windows.Forms namespace as in #grantnz's answer.
Also note that setting the dimensions only (left, top, width + height) left me with a small border on each side. Only after setting WindowState.Maximized is it that the real full-screen effect was achieved.
Since I have monitors with varying DPI configurations, I also had to use WpfWorkingArea instead of WorkingArea, since the coordinates were coming back incorrectly.
Posting here for completeness, this is using WPF with .NET 7.0 and is confirmed to work with varying DPI configurations:
public MainWindow()
{
InitializeComponent();
SourceInitialized += MainWindow_SourceInitialized;
}
private void MainWindow_SourceInitialized(object? sender, EventArgs e)
{
var screen = WpfScreenHelper.Screen.AllScreens.FirstOrDefault(s => !s.Primary);
if (screen != null)
{
this.WindowState = WindowState.Normal;
this.Left = screen.WpfWorkingArea.Left;
this.Top = screen.WpfWorkingArea.Top;
this.Width = screen.WpfWorkingArea.Width;
this.Height = screen.WpfWorkingArea.Height;
this.WindowState = WindowState.Maximized;
}
}
In WPF: Set the WindowState property in Normal (not Maximixed) and create the event Loaded. In the event write this code:
this.Left = SystemParameters.PrimaryScreenWidth + 100;
this.WindowState = System.Windows.WindowState.Maximized;
I'm making a simple CPU Usage monitor. The application is just an icon in the task bar which uses the Microsoft.WindowsAPICodePack library to display the CPU Usage of a task as an icon progress bar.
This application is currently working fine. For added value, I want to prevent the user from opening the application's form, which is blank. Currently I'm using the following code:
/// <summary>
/// Forces this window to remain minimized.
/// </summary>
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState != FormWindowState.Minimized)
WindowState = FormWindowState.Minimized;
}
This causes the form to flash on the screen for an instant and shrink back to the task bar. I'd prefer for absolutely nothing to visibly happen when the icon is clicked. Is there a way to achieve this?
Attempt 1
Following deathismyfriend's advice, I tried to hide the form. The WindowsAPICodePack throws an exception:
A valid active Window is needed to update the Taskbar.
Attempt 2
Setting this.Opacity = 0 didn't quite work. Funnily enough, the form is transparent... until you minimize it. Then it appears and shrinks to the task bar.
This worked for me. Trap WM_SYSCOMMAND and suppress SC_RESTORE / SC_MAXIMIZE:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Opacity = 0; // prevent ALT-TAB preview
this.WindowState = FormWindowState.Minimized;
}
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_RESTORE = 0xF120;
const int SC_MAXIMIZE = 0xF030;
if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_RESTORE || (int)m.WParam == SC_MAXIMIZE))
{
return;
}
base.WndProc(ref m);
}
}
*Setting Opacity() to 0 (zero) did prevent the Alt-Tab preview window.
You are going against Windows interface standards to do this. That's why it is hard. The system tray is built for this type of application. You still should be able to animate an icon in the system tray to show this sort of information. Here's another question that helps with the system tray:
How can I make a .NET Windows Forms application that only runs in the System Tray?
Still not elegant, but you could set your FormBorderStyle to FormBorderStyle.None to prevent the right-click context menu on the window preview in aero and then move the form off screen:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Location = new Point(int.MinValue, int.MinValue);
}
Set the opacity to 1 and put the window in an offscreen location. The form will still flash, but will be invisible to the user.
Consider the following code:
Window myWindow = new MyWindowSubclass();
myWindow.BringIntoView();
myWindow.Show();
// Code which is effective as pressing the maximize button
Also, how to detect if the window is indeed in maximized state.
In WPF, you can use the WindowState property:
myWindow.WindowState = WindowState.Maximized;
You can of course query that property to obtain the current window state:
if (myWindow.WindowState == WindowState.Maximized) {
// Window is currently maximized.
}
For WinForms, you can use
bool maximized = this.WindowState == System.Windows.Forms.FormWindowState.Maximized;
to test if the window is maximized.
The SizeChanged and Resize events should capture all changes to the window state.
In WinForms, do
// Code which is effective as pressing the maximize button
myWindow.WindowState = FormWindowState.Maximized;
Of course you can test it the same way:
if (myWindow.WindowState == FormWindowState.Maximized) { ... }
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...