I have an application in WPF C# where other than the purpose of what it's done, I customized the close and minimize buttons. The problem is that when I minimize, all is good, I cycle around all other apps, but, when I want to go back to the application, I click in the window in the taskbar, and the window pops up... But when it pops up, the window pursues the mouse pointer throughout the screen...
The code i've implemented the the simplest it can be...
private void Minimize_LeftMouseDown(object sender, MouseButtonEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
Can you point me some directions?
Thanks
It's possible that you've somehow captured the mouse at that point, and the minimize state is preventing WPF's normal release from occurring. If your control is named "Minimize", try adding:
private void Minimize_LeftMouseDown(object sender, MouseButtonEventArgs e)
{
// Make sure we're not capturing the mouse anymore
Mouse.Capture(null);
this.WindowState = WindowState.Minimized;
}
Use the LeftMouseUp event instead of LeftMouseDown. You want to minimize the window when the mouse is released, not when it is pressed down.
Related
I have a winforms application with a parent panel contains PictureBox and Label.
The application has an event on mouse enter on the parent panel to do some animation that hides the picturebox and shows the label.
It also has another event on mouse leave to handle the animation that hides the label and shows picture box.
What's happening is when mouse enters panel, animation kicks in and does what it does, but when the mouse enters label it practically leaves panel and the reverse animation kicks in.
My question is -
how can I prevent mouse leave event when my mouse enters child control of the panel?
Check if the mouse is still in the panel
private void Panel1_MouseLeave(object sender, EventArgs e)
{
if (!(sender as Panel).ClientRectangle.Contains(PointToClient(Control.MousePosition)))
{
//do animation
}
}
I am working on a program that contains (among other things) a WPF window for which I am using the next code to maximize it at a MouseDoubleClick Event:
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
Now, what I want to do is that when the window is maximized and the mouse exits the screen (goes to the bottom of the screen until it exits the screen) a new window to appear at the bottom of the screen(WPF or WindowsForm) that will contains several things (buttons, a scrollBars, etc) and that will be active only as long as the mouse is over it (just like in BSplayer). My question is how to do that ? I'm really a starter with WPF, I don't know XAML and I would prefer to do as much as I can using C# code.
So: how do I know when the mouse leaves the screen and how do I make that window to appear on bottom of the screen (without minimizing or doing anything else with the original window) ?
I tried using this.MouseLeave but it doesn't work when the window is maximized.
And if I am asking this question here, I will use my chance to also ask two other things:
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
When the WPF window is not maximized, I want that the border of the screen to be very small, almost invisible (no minimize, close or other button). I am using this.WindowStyle = System.Windows.WindowStyle.ToolWindow but it still leaves the exit/close button there; If I use this.WindowStyle = System.Windows.WindowStyle.None it looks perfect, but then I can't move the window. I there anyway to make the window movable with WindowStyle.None ? Preferably, when I keep the mouse pressed on the interior of the screen, I want to be able to drag the WPF window around on my screen.
Really need help to these problems. It's a pretty important project that I am working on.
Answer to this question
When the WPF window is maximized and if the mouse hasn't been moved for more than 5 seconds, than I want the mouse to be hidden and to become visible again only when the mouse moves. How do I do this ?
This can be achieved by using the timer with an interval of 5 seconds. When timer elapse set mouse cursor to None to hide it and when mouse moves, reset the mouse cursor to original one.
Put below code in the constructor:
this.MouseMove += new MouseEventHandler(MainWindow_MouseMove);
tm = new System.Timers.Timer();
tm.Interval = 5000;
tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed);
tm.Start();
Below are event defination:
void MainWindow_MouseMove(object sender, MouseEventArgs e)
{
tm.Stop();
tm.Start();
// Reseting the time back to original. Here I have assumed that original one is Arrow.
this.Dispatcher.Invoke(new Action(() =>
{
Mouse.OverrideCursor = Cursors.Arrow;
}));
}
void tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.Invoke(new Action(() =>
{
if (Mouse.OverrideCursor != Cursors.None)
{
Mouse.OverrideCursor = Cursors.None;
currentCursor = Mouse.OverrideCursor;
}
}));
}
Hope this helps !!
I have a very typical hackish solution for minimizing to system tray in C#:
private void MainFormResize(object sender, EventArgs e) {
if (FormWindowState.Minimized == this.WindowState) {
this.Hide();
systemTrayIcon.Visible = true;
}
}
private void systemTrayIconMouseDoubleClick(object sender, MouseEventArgs e) {
systemTrayIcon.Visible = false;
this.Show();
this.WindowState = FormWindowState.Normal;
}
Ideally, I want my application to disappear/reappear when minimizing to or reopening from the system tray. Minimizing to the system tray works as expected -- the window disappears with no delay and there appears a new tray icon.
Double-clicking on the icon, however, has some very strange effects. The window undergoes a resize animation to its position -- the window appears to fly in from a completely random corner of the screen.
I don't want that. I just want Minimize > -Poof- Disappear and Double-click > -Poof- Appear with no animations or delays or anything of that sort.
Why does this code have an animation? If I call Form.Show() in any other context, the window automatically appears like I want, but when called from a NotifyIcon, it acts strangely. I thought it might be the WindowState = FormWindowState.Normal line, but if I remove that, the window isn't brought to the foreground.
Edit: This problem seems to be OS and theme dependent. The problem doesn't appear to exist in Windows XP, but it's hard to tell because my virtual machine is a little laggy. In Windows 7 Aero, the abitrary-offscreen position problem occurs. In Windows 7 Basic/Classic, it minimizes to the task bar, and reappears from its old position in the taskbar (as if it was actually minimized to the task bar, not the system tray). I haven't tested on Vista. Any tips?
Did you try reordering to put WindowState = FormWindowState.Normal before Show()? I believe the animation you are seeing is the standard window restore animation. Since you are calling Show() before restoring your window, it gets an off-screen position.
Edit: I see your issue now - I looked at it for a second or so, and even tried an IMessageFilter, but for some reason couldn't trap WM_SYSCOMMAND when minimizing (although it fires on restoring).
The one easy thing you could do is live with the minimize animation, though - in your resize handler, just before the Hide() call, set WindowState to Normal. You'll see the minimize animation, but not the maximize (which on most platforms is much less noticeable).
If you need to hide the window when the program runs, your best bet is to create a class that derives from ApplicationContext and shows the NotifyIcon. You then use this class instead of form in the Application.Run.
class TaskTray : ApplicationContext
{
private NotifyIcon _Icon;
public TaskTray()
{
_Icon = new NotifyIcon();
//...
)
}
static void Main()
{
Application.Run(new TaskTray());
}
At least it is possible to have the animation originate from where it should - you have to move the minimized window near the tray notification area: see my hack here
Well, this is an old question, but it's the first result I hit when googling, so I'll share my findings, maybe someone could find this useful.
My application starts with no window showing on the desktop, only a tray icon. The main window can be shown by double-clicking on the tray icon, and closing the window will hide it to the tray icon again.
When my application is hidden, the taskbar icon is hidden as well. However, when it's shown, I have the taskbar icon shown as well, so it can be switched between other windows.
It works as intended. However, I can't help but noticing the animation when showing the window feels very strange and jerky, and it's bugging me a lot. After some digging around, I found the animation behavior is affected by ShowInTaskbar property of the form.
private void ShowMainWindow(object sender, EventArgs e)
{
//ShowInTaskbar = true; // smooth animation
Show();
//ShowInTaskbar = true; // jerky animation
}
Without the ShowInTaskbar = true; line, the window would appear instantly without any animation. Putting the line below Show(); results in the "jerky" animation. Putting the line above Show() gives a smooth fading-in animation and that's the one I choose in the end.
I have Windows Forms application and I need to capture the mouse movements outside my window. My simplified code in my window class is:
private void ButtonOnClick(object sender, EventArgs e)
{
Capture = true;
MouseMove += OnMouseMove;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
Console.Out.Write("!");
}
As you see, when user presses a button, my program should start tracking mouse (even if it is outside of window - this is a key featue!) But I get really strange behavior. If I move mouse inside the window, everything is great, ! are written to console. But when I move mouse outside of the window, only OnMouseMove is called only once (and the point is really outside). Then if I move mouse anywhere outside of the window it is not called anymore. If I return mouse back to window, everything is perfect. Move away - 1 message, move in the window - OK.
Can anybody help?
You would need a global mouse hook for that. I recommend that you first read something about hooks, eg. on MSDN. An example implementation in C# can be found at the CodeProject.
Hope that helps a bit.
I would like to disable displaying of the content of the window when resizing, is it possible? The problem is that when I'm resizing my window the controls redraw on correct positions but it doesn't look good because it's not done fluently.
EDIT: I would like a code that would manage the following scenario:
I click on the corner of window
Now only the border of window is visible - the middle part is transparent
I set the size of the window by mouse
I release the mouse button and the middle part of the window will appear
EDIT II:
I've got the MDI application and it doesn't support transparency for child windows
An idea is to put all the controls in a panel and set it's visibility to false on the resize event of the form.
Edit: this will make the form transparent while resizing.
private void Form1_ResizeBegin(object sender, EventArgs e)
{
panel1.Visible = false;
Form1.ActiveForm.TransparencyKey = Color.Transparent;
}
private void Form1_ResizeEnd(object sender, EventArgs e)
{
panel1.Visible = true;
Form1.ActiveForm.TransparencyKey = Color.Gray; // or whatever color your form was
}