在執行時顯示或隱藏窗體的標題欄 (轉)

worldblog發表於2007-12-03
在執行時顯示或隱藏窗體的標題欄 (轉)[@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時,窗體的標題欄將會在隱藏或顯示之間切換。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987584/,如需轉載,請註明出處,否則將追究法律責任。

相關文章