2011-01-10 68 views
3

我需要製作一個winform全屏。這是我在網上找到的。製作Winforms全屏

1. Hook WinProc to catch WM_SYSCOMMAND 

2. Check wParam == SC_MAXIMIZE and then 

3. Set my windiw's attributes 

Me.ResizeMode = ResizeMode.NoResize 

Me.WindowStyle = WindowStyle.None 

Me.WindowState = WindowState.Maximized 

我是相當新的vb.net,不知道該怎麼辦步驟1或2。能有人給我一個片段或點我在正確的方向?

感謝giodamelio

回答

6

訣竅是獲取HwndSource並調用其AddHook()方法。這工作:

Imports System.Windows.Interop 

Class Window1 
    Protected Overrides Sub OnSourceInitialized(ByVal e As System.EventArgs) 
     MyBase.OnSourceInitialized(e) 
     DirectCast(PresentationSource.FromVisual(Me), HwndSource).AddHook(AddressOf WndProc) 
    End Sub 

    Private Const WM_SYSCOMMAND As Integer = &H112 
    Private Const SC_MAXIMIZE As Integer = &HF030 

    Private Function WndProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr, ByRef handled As Boolean) As IntPtr 
     If msg = WM_SYSCOMMAND AndAlso wp.ToInt32() = SC_MAXIMIZE Then 
      Me.ResizeMode = ResizeMode.NoResize 
      Me.WindowStyle = WindowStyle.None 
      Me.WindowState = WindowState.Maximized 
      handled = True 
     End If 
    End Function 

End Class 

爲一個WinForms形式的相同代碼:

Public Class Form1 
    Private Const WM_SYSCOMMAND As Integer = &H112 
    Private Const SC_MAXIMIZE As Integer = &HF030 

    Protected Overrides Sub WndProc(ByRef m As Message) 
     If m.Msg = WM_SYSCOMMAND AndAlso m.WParam.ToInt32() = SC_MAXIMIZE Then 
      Me.FormBorderStyle = FormBorderStyle.None 
      Me.WindowState = FormWindowState.Maximized 
      Return 
     End If 
     MyBase.WndProc(m) 
    End Sub 

    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean 
     '' Restore window when the user presses Escape 
     If Me.WindowState = FormWindowState.Maximized AndAlso keyData = Keys.Escape Then 
      Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable 
      Me.WindowState = FormWindowState.Normal 
     End If 
     Return MyBase.ProcessCmdKey(msg, keyData) 
    End Function 

End Class 
0

很抱歉,這是C#(未VB),但或許它仍然是對你有用:

這裏是我使用的WinForms應用程序,有一個全屏模式的方法:

private void FullScreen(bool Enable) 
    { 
     SizeChanged -= FormMain_SizeChanged; 

     SuspendLayout(); 
     if (Enable) 
     { 
      FormBorderStyle = FormBorderStyle.None; 
      WindowState = FormWindowState.Maximized; 
      if (settings.HideFullScreenCursor) 
       Cursor.Hide(); 
      menuStrip.Visible = false; 
     } 
     else 
     { 
      FormBorderStyle = FormBorderStyle.Sizable; 
      WindowState = FormWindowState.Normal; 
      if (settings.HideFullScreenCursor) 
       Cursor.Show(); 
      menuStrip.Visible = true; 
     } 
     ResumeLayout(); 

     SizeChanged += FormMain_SizeChanged; 
    } 

當然,你可能會想修改它以適應你的需求,但希望它能給你一個起點。

+0

謝謝,這是接近。但是當我運行該代碼時,Windows任務欄仍然存在。 – giodamelio 2011-01-10 18:21:12

+0

任務欄有時會顯示一兩秒鐘,但會消失。 (我不確定是什麼導致它暫時徘徊。) – JYelton 2011-01-10 18:35:54