在執行時顯示或隱藏窗體的標題欄 (轉)
在執行時顯示或隱藏窗體的標題欄 (轉)[@more@]這則程式碼告訴你如何在執行時顯示或隱藏窗體的標題欄。要使一個視窗的標題欄消失,你必須去掉control box、最大化按鈕和最小化按鈕,並且將caption設為空。不幸的是,VB中窗體的ControlBox、MinButton和MaxButton屬性在執行期是隻讀的,因此,你只能在設計時做這些事。其實,只要能熟練操作關於視窗式樣的,你同樣能在執行時辦到這一點。
新建一個專案,把以下程式碼寫入窗體:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000 ' WS_BORDER 或 WS_DLGFRAME
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000
Private Declare Function SetWindowPLib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Enum ESetWindowPosStyles
SWP_SHOWWINDOW = &H40
SWP_HWINDOW = &H80
SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_DRAWFRAME = SWP_FRAMECHANGED
HWND_NOTOPMOST = -2
End Enum
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Function ShowTitleBar(ByVal bState As Boolean)
Dim lStyle As Long
Dim tR As RECT
' 獲取視窗的位置:
GetWindowRect Me.hwnd, tR
' 調整標題欄是否可見:
lStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
If (bState) Then
Me.Caption = Me.Tag
If Me.ControlBox Then
lStyle = lStyle Or WS_SYSMENU
End If
If Me.MaxButton Then
lStyle = lStyle Or WS_MAXIMIZEBOX
End If
If Me.MinButton Then
lStyle = lStyle Or WS_MINIMIZEBOX
End If
If Me.Caption <> "" Then
lStyle = lStyle Or WS_CAPTION
End If
Else
Me.Tag = Me.Caption
Me.Caption = ""
lStyle = lStyle And Not WS_SYSMENU
lStyle = lStyle And Not WS_MAXIMIZEBOX
lStyle = lStyle And Not WS_MINIMIZEBOX
lStyle = lStyle And Not WS_CAPTION
End If
SetWindowLong Me.hwnd, GWL_STYLE, lStyle
' 重新設定視窗:
SetWindowPos Me.hwnd, 0, tR.Left, tR.Top, tR.Right - tR.Left, tR.Bottom - tR.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED
Me.Refresh
' 你可能需要在Form_Resize中加一點程式碼,因為客戶區的大小已經改變:
'Form_Resize
End Function
為了試驗一下程式碼,在窗體上放一個CheckBox,將它的Value屬性設為1 (Checked)。然後寫入以下程式碼:
Private Sub Check1_Click()
If (Check1.Value = Checked) Then
ShowTitleBar True
Else
ShowTitleBar False
End If
End Sub
執行,當你點選這個CheckBox時,窗體的標題欄將會在隱藏或顯示之間切換。
新建一個專案,把以下程式碼寫入窗體:
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_CAPTION = &HC00000 ' WS_BORDER 或 WS_DLGFRAME
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const WS_SYSMENU = &H80000
Private Declare Function SetWindowPLib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Enum ESetWindowPosStyles
SWP_SHOWWINDOW = &H40
SWP_HWINDOW = &H80
SWP_FRAMECHANGED = &H20 ' The frame changed: send WM_NCCALCSIZE
SWP_NOACTIVATE = &H10
SWP_NOCOPYBITS = &H100
SWP_NOMOVE = &H2
SWP_NOOWNERZORDER = &H200 ' Don't do owner Z ordering
SWP_NOREDRAW = &H8
SWP_NOREPOSITION = SWP_NOOWNERZORDER
SWP_NOSIZE = &H1
SWP_NOZORDER = &H4
SWP_DRAWFRAME = SWP_FRAMECHANGED
HWND_NOTOPMOST = -2
End Enum
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Function ShowTitleBar(ByVal bState As Boolean)
Dim lStyle As Long
Dim tR As RECT
' 獲取視窗的位置:
GetWindowRect Me.hwnd, tR
' 調整標題欄是否可見:
lStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
If (bState) Then
Me.Caption = Me.Tag
If Me.ControlBox Then
lStyle = lStyle Or WS_SYSMENU
End If
If Me.MaxButton Then
lStyle = lStyle Or WS_MAXIMIZEBOX
End If
If Me.MinButton Then
lStyle = lStyle Or WS_MINIMIZEBOX
End If
If Me.Caption <> "" Then
lStyle = lStyle Or WS_CAPTION
End If
Else
Me.Tag = Me.Caption
Me.Caption = ""
lStyle = lStyle And Not WS_SYSMENU
lStyle = lStyle And Not WS_MAXIMIZEBOX
lStyle = lStyle And Not WS_MINIMIZEBOX
lStyle = lStyle And Not WS_CAPTION
End If
SetWindowLong Me.hwnd, GWL_STYLE, lStyle
' 重新設定視窗:
SetWindowPos Me.hwnd, 0, tR.Left, tR.Top, tR.Right - tR.Left, tR.Bottom - tR.Top, SWP_NOREPOSITION Or SWP_NOZORDER Or SWP_FRAMECHANGED
Me.Refresh
' 你可能需要在Form_Resize中加一點程式碼,因為客戶區的大小已經改變:
'Form_Resize
End Function
為了試驗一下程式碼,在窗體上放一個CheckBox,將它的Value屬性設為1 (Checked)。然後寫入以下程式碼:
Private Sub Check1_Click()
If (Check1.Value = Checked) Then
ShowTitleBar True
Else
ShowTitleBar False
End If
End Sub
執行,當你點選這個CheckBox時,窗體的標題欄將會在隱藏或顯示之間切換。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987584/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Qt 子視窗 隱藏標題欄的圖示,隱藏在工作列上的圖示QT
- fltk-rs 隱藏標題欄但顯示工作列圖示
- 如何在 macOS Monterey 或更早版本中隱藏或顯示Dock欄Mac
- Qt隱藏標題欄QT
- 在 Laravel 中動態 隱藏 / 顯示 API 欄位LaravelAPI
- JavaScript側邊欄顯示和隱藏JavaScript
- AppCompatActivity隱藏標題欄APP
- 滾動cell 顯示隱藏導航欄
- Qt隱藏系統標題欄,使用自定義標題欄QT
- ElementUI側邊欄導航選單隱藏顯示問題UI
- Android studio隱藏標題欄Android
- jQuery側邊欄隱藏和顯示導航jQuery
- mac顯示隱藏檔案,取消顯示隱藏檔案Mac
- C# 顯示、隱藏視窗對應的工作列C#
- 由顯示/隱藏引出的CSSbug(轉)CSS
- 由顯示/隱藏引出的CSS Bug(轉)CSS
- Windows 任務計劃隱藏顯示黑視窗Windows
- jQuery table表格tr行顯示隱藏jQuery
- iOS 解決導航欄隱藏,顯示,頁面跳轉閃一下的問題iOS
- view的隱藏和顯示View
- iOS 隱藏&顯示tabBariOStabBar
- MacOS X隱藏和顯示隱藏檔案Mac
- Mac顯示和隱藏“隱藏檔案”命令Mac
- javascript動態隱藏顯示技術(轉)JavaScript
- Sqlplus查詢時不能顯示標題欄SQL
- MAC如何顯示隱藏檔案和隱藏隱藏檔案的命令Mac
- Mac下顯示和隱藏隱藏檔案的命令Mac
- Android 顯示、隱藏狀態列和導航欄Android
- 區塊的顯示和隱藏
- winform之在主窗體中不顯示子窗體的選單欄ORM
- 通過Behavior在RecycleView中隱藏顯示FABView
- steam隱藏的遊戲怎麼顯示出來 steam隱藏顯示遊戲方法介紹遊戲
- excel隱藏的部分如何顯示出來 excel裡面怎麼顯示隱藏部分Excel
- 直播平臺原始碼,隱藏app圖示並不在最近執行中顯示原始碼APP
- OSX中隱藏和顯示[隱藏檔案]的命令列命令列
- jQuery 效果 – 隱藏和顯示jQuery
- mac隱藏檔案顯示Mac
- Mac 顯示隱藏檔案Mac