Search code examples
c#winformsvisual-studio-2012browserfullscreen

How to get out from full-screen mode


I want to display my program (in winforms) in the full-screen-mode. And this is the way how I get in it:

private void Form1_Load(object sender, EventArgs e)
{
    string id_string = System.IO.File.ReadAllText(@"C:\Users\astankic\ID.txt");
    int id_int = Convert.ToInt32(id_string);
    string url = @"http://z0r.de/L/z0r-de_" + id_int + ".swf";
    TopLevel = true;
    Cursor.Hide();
    webBrowser_animation.Url = new Uri();

In the properties of the form, I've set the FormBorderStyle to "None", the WindowState to "Maximiazed" and the TopMost to "true". The webbrowser take also the full size of the screen!

My Problem is: I want to close the program when a Key has been pressed or the mouse has been moved. I already have tried it with this etc:

private void Form_Screensaver_KeyDown(object sender, KeyEventArgs e)
{
    Close();
}

... but it didn't worked? :-(


Solution

  • Try this code. First, implement the interface IMessageFilter:

    public partial class Form1 : Form, IMessageFilter
    {
       ....
    }
    

    Next, implement the required PreFilterMessage method:

    public partial class Form1 : Form, IMessageFilter
    {
        ...
    
        public bool PreFilterMessage(ref Message m)
        {
            // If key is pressed or mouse is moved
            if (m.Msg == 0x0100 || m.Msg == 0x0200)
            {
                Application.RemoveMessageFilter(this);
                Application.Exit();
                return true;
            }
    
            return false;
        }
    }
    

    Next add the filter to your form's constructor and remove it when the form is closed:

        public Form1()
        {
            InitializeComponent();
    
            Application.AddMessageFilter(this);
            FormClosed += Form1_FormClosed;
        }
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.RemoveMessageFilter(this);
        }
    

    See if this works for you!