在撰寫應用程式的時候後常常會遇到需要將應用程式最大化的需求,尤其是當你的應用程式有特定用途的時候(例如用PDA+GPS去記錄GPS軌跡),這個時候要怎麼做呢?
在撰寫應用程式的時候後常常會遇到需要將應用程式最大化的需求,尤其是當你的應用程式有特定用途的時候(例如用PDA+GPS去記錄GPS軌跡),這個時候要怎麼做呢?
其實在WM系統中要最大化程式不困難,只要將Form給最大化就可以了,像是下面這樣
Me.WindowState = FormWindowState.Maximized
有關WindowState的部分建議這篇一起看一下Form.show?Form.showdialog
一行程式碼就可以搞定最大化的需求;接下來,按了"Window 鍵"之後,"功能表"跳了出來,但是我不想讓功能表可以跳出來,那麼應該怎麼辦呢?這時候就要借助一下Shell API了,有關Shell API可以參考這邊
SHFullScreen
首先需要先宣告API,會像下面這樣
''Reference:http://msdn.microsoft.com/en-us/library/aa457115.aspx
_
Shared Function SHFullScreen(ByVal hwndRequester As IntPtr, ByVal dwState As UInt32) As Int32
End Function
接下來是一些常數的定義
Const SHFS_SHOWTASKBAR As UInt32 = &H1
Const SHFS_HIDETASKBAR As UInt32 = &H2
Const SHFS_SHOWSIPBUTTON As UInt32 = &H4
Const SHFS_HIDESIPBUTTON As UInt32 = &H8
Const SHFS_SHOWSTARTICON As UInt32 = &H10
Const SHFS_HIDESTARTICON As UInt32 = &H20
由常數定義的名稱我們就可以看出一些端倪,要將功能表取消掉,我們就必須用HIDESTARTICON,所以利用下面的方式就可以隱藏"開始功能表"的項目了
SHFullScreen(Me.Handle, SHFS_HIDESTARTICON)
由下面兩張圖我們可以看出服用前跟服用後的效果
這樣就可以讓"Window 鍵"失去作用了,但是可能還有其他的一些硬體按鍵,例如廠商自訂的可以把行事曆或是IE叫出來等等的按鍵,那怎麼辦呢?這時候就要借助HardwareButton的功能了(請特別注意,profression版本的撥打電話跟掛斷電話這個是控制不到的),利用下面的方式可以將硬體按鍵所產生的事件交由應用程式來控制
Imports Microsoft.WindowsCE.Forms Imports System.Runtime.InteropServices Public Class Form1 Const SHFS_SHOWTASKBAR As UInt32 = &H1 Const SHFS_HIDETASKBAR As UInt32 = &H2 Const SHFS_SHOWSIPBUTTON As UInt32 = &H4 Const SHFS_HIDESIPBUTTON As UInt32 = &H8 Const SHFS_SHOWSTARTICON As UInt32 = &H10 Const SHFS_HIDESTARTICON As UInt32 = &H20 ''判斷目前Menu是否顯示中 Dim bolShowMenu As Boolean = True Dim hbtn(5) As HardwareButton ''Reference:http://msdn.microsoft.com/en-us/library/aa457115.aspx
_ Shared Function SHFullScreen(ByVal hwndRequester As IntPtr, ByVal dwState As UInt32) As Int32 End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ''設定hardware button For i As Integer = 0 To 5 hbtn(i) = New HardwareButton hbtn(i).HardwareKey = 193 + i hbtn(i).AssociatedControl = Me Next End Sub ''' ''' 最大化視窗 ''' ''' ''' ''' Private Sub mnuMaxSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMaxSize.Click If mnuMaxSize.Checked Then Me.WindowState = FormWindowState.Normal Else Me.WindowState = FormWindowState.Maximized End If mnuMaxSize.Checked = Not mnuMaxSize.Checked End Sub ''' ''' 顯示或是隱藏輸入法輸入按鈕 ''' ''' ''' ''' Private Sub mnuShowSIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowSIP.Click If mnuShowSIP.Checked Then SHFullScreen(Me.Handle, SHFS_SHOWSIPBUTTON) Else SHFullScreen(Me.Handle, SHFS_HIDESIPBUTTON) End If mnuShowSIP.Checked = Not mnuShowSIP.Checked End Sub ''' ''' 顯示或是隱藏"開始"的圖示 ''' ''' ''' ''' Private Sub mnuShowStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowStart.Click If mnuShowStart.Checked Then SHFullScreen(Me.Handle, SHFS_SHOWSTARTICON) Else SHFullScreen(Me.Handle, SHFS_HIDESTARTICON) End If mnuShowStart.Checked = Not mnuShowStart.Checked End Sub ''' ''' 顯示或是隱藏標題列 ''' ''' ''' ''' Private Sub mnuShowTaskbar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuShowTaskbar.Click If mnuShowTaskbar.Checked Then SHFullScreen(Me.Handle, SHFS_SHOWTASKBAR) Else SHFullScreen(Me.Handle, SHFS_HIDETASKBAR) End If mnuShowTaskbar.Checked = Not mnuShowTaskbar.Checked End Sub ''' ''' 顯示或是隱藏Menu ''' ''' ''' ''' Private Sub btnHideMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHideMenu.Click If bolShowMenu Then Me.Menu = Nothing btnHideMenu.Text = "Show menu" Else Me.Menu = MainMenu1 btnHideMenu.Text = "Hide menu" End If bolShowMenu = Not bolShowMenu End Sub ''' ''' 關閉應用程式 ''' ''' ''' ''' Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click Application.Exit() End Sub Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown TextBox1.Text = e.KeyCode End Sub End Class 完整的測試程式碼也可以在這邊下載。