2014-10-20 77 views
1

我想在全屏模式下顯示我的程序(以winforms形式)。這是我在如何獲得方式:如何從全屏模式退出

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(); 

在窗體的屬性,我已經設置FormBorderStyle爲「無」,將WindowState爲「Maximiazed」和頂端的「真」。網頁瀏覽器也採用屏幕的全尺寸!

我的問題是:我想關閉程序,當一個鍵被按下或鼠標已被移動。我已經試過這個等:

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

...但它沒有奏效? :-(

+0

你試過ALT + F4嗎? – 2014-10-20 15:30:34

+1

我懷疑問題是你正在加載一個Flash文件(SWF)到一個WebBrowser控件中,這個控件是鍵盤焦點。您需要一種將鍵盤焦點返回給應用程序的方法。我們可以假設你的函數「Form_Screensaver_KeyDown」根本不會觸發嗎? – 2014-10-20 15:32:37

+0

@ChrisWalsh是的,我有,它的工作原理,但我想離開它時,鍵被按下或鼠標移動 – user3347346 2014-10-20 15:33:07

回答

1

試試這個代碼首先,實現接口IMessageFilter:。

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

下,實現所需PreFilterMessage方法:

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; 
    } 
} 

接下來的過濾器添加到您的窗體的構造函數,並刪除它時,窗體被關閉:

public Form1() 
    { 
     InitializeComponent(); 

     Application.AddMessageFilter(this); 
     FormClosed += Form1_FormClosed; 
    } 

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) 
    { 
     Application.RemoveMessageFilter(this); 
    } 

看看這對你的作品!

+0

我試了一下,但程序啓動後立即自動關閉。鼠標和鍵沒有被移動或按下! – user3347346 2014-10-20 17:14:38

+0

@ user3347346 - 嘗試使用if(m.Msg == 0x0100)代替。這將只檢測按鍵而不是鼠標移動。 – Icemanind 2014-10-20 17:33:36

+0

是的,它的作品(按鍵)!當只有鼠標移動時,我可以如何關閉它? – user3347346 2014-10-21 12:36:21

0

這是一種直覺,但嘗試再創建一個對象(比如一個Button)作爲webBrowser_animation兄弟並設置焦點對象的Form_Load()部分上。如果按ENTER鍵可以觸發事件Click按鈕,然後你至少知道你可以把焦點從Flash中解放出來,如果這樣做,那麼[a]使Button成爲「屏幕外」,並且[b]將一個通用的Keyboard事件處理程序和Mouse事件處理程序添加到將執行。窗口關閉它的所有有關鍵盤焦點