can i hide a control without using the ShowWindow() api function?
can i hide a control without using the ShowWindow() api function?
I assume you're already aware of the "Visible" property for Visual Basic controls. Here is some code that hides windows without using the ShowWindow API function:
People could help you better if you explained why you don't want to use the ShowWindow API function.Code:Option Explicit Private Const GWL_STYLE As Long = -16& Private Const WS_VISIBLE As Long = &H10000000 Private Declare Function GetParent Lib "User32.dll" (ByVal hwnd As Long) As Long Private Declare Function GetWindowLongA Lib "User32.dll" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLongA Lib "User32.dll" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function UpdateWindow Lib "User32.dll" (ByVal lhwnd As Long) As Long Private Sub HideWindow(WindowH As Long) Dim Styles As Long Styles = GetWindowLongA(WindowH, GWL_STYLE) SetWindowLongA WindowH, GWL_STYLE, (Styles Xor WS_VISIBLE) RefreshWindow WindowH End Sub Public Sub Main() Dim WindowH As Long WindowH = CLng(Val(InputBox$("Window handle:", , "0"))) If WindowH = 0 Then Exit Sub HideWindow WindowH End Sub Public Sub RefreshWindow(ByVal WindowH As Long) Do UpdateWindow WindowH WindowH = GetParent(WindowH) DoEvents Loop Until WindowH = 0 End Sub
Last edited by Peter Swinkels; Nov 21st, 2012 at 07:33 AM.
SetWindowPos can also hide/show a control:
Code:Private Const SWP_NOSIZE As Long = &H1 'Retains the current size (ignores the cx and cy parameters) Private Const SWP_NOMOVE As Long = &H2 'Retains the current position (ignores X and Y parameters) Private Const SWP_NOZORDER As Long = &H4 'Retains the current Z order (ignores the hWndInsertAfter parameter) Private Const SWP_SHOWWINDOW As Long = &H40 'Displays the window Private Const SWP_HIDEWINDOW As Long = &H80 'Hides the window Private Declare Function SetWindowPos Lib "user32.dll" ( _ ByVal hWnd As Long, _ Optional ByVal hWndInsertAfter As Long, _ Optional ByVal X As Long, Optional ByVal Y As Long, _ Optional ByVal cx As Long, Optional ByVal cy As Long, _ Optional ByVal uFlags As Long _ ) As Long Public Sub HideOrShowControl(ByRef Ctrl As Control, Optional ByVal Show As Boolean) Const SWP_FLAGS = SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOZORDER On Error Resume Next If Show Then SetWindowPos Ctrl.hWnd, uFlags:=SWP_FLAGS Or SWP_SHOWWINDOW Else SetWindowPos Ctrl.hWnd, uFlags:=SWP_FLAGS Or SWP_HIDEWINDOW End If End Sub
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)