简体   繁体   中英

Some questions about Windows Presentation Foundation (C#)

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:

  1. 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 ?
  2. 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 !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM