VB Tips Form
Come muovere una finestra senza utilizzare la TitleBar
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Declare Function ReleaseCapture Lib "user32"( ) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long ,ByVal wMsg As Long ,ByVal wParam As Long , IParam As Long ) As Long
Sub Form_MouseDown(Button As Integer , Shift As Integer , x As Single , Y As Single)
Dim ReturnVal As Long
If Button = 1 Then

x = ReleaseCapture ( )
ReturnVal = SendMessage ( hwnd , WM_NCLBUTTONDOWN , HTCAPTION , 0 )
End If

End Sub
 

Eliminare i tre tastini da un form MDI

VB4/32, VB5, VB6

Il codice che segue è utile per eliminare i tasti chiusura, ingrandimento, e riduzione ad icona (ossia la x, il - ed i due quadratini) da un form MDI dal momento che tra le proprietà del form non è elencata la controlbox presente invece per tutti gli altri tipi di form.

In un modulo

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 WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const GWL_STYLE = (-16)
Private Const WS_SYSMENU = &H80000

Con le due funzioni API che vedete è possibile ottenere lo stile di una determinata finestra, modificarlo e quindi riassegnarlo alla finestra in questione.

 

Nell' evento MDIForm_Load:

Dim oldStyle As Long
Dim newStyle As Long

oldStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
newStyle = oldStyle Xor WS_MAXIMIZEBOX  Xor WS_MINIMIZEBOX  Xor WS_SYSMENU
Call SetWindowLong(Me.hwnd, GWL_STYLE, newStyle)
Se invece si scrive

newStyle = oldStyle Xor    WS_SYSMENU

 

si elimina lo stesso i tre tasti ma la finestra si massimizza e si ripristina facendo doppio click sulla barra del titolo.
Fare attenzione perchè in questa maniera non è più possibile chiudere la finestra se non da codice.

Massimizzare una finestra durante l'esecuzione del programma
In form_load inserire il seguente codice:
width=screen.width
height=screen.height
left=0:top=0
 

CREARE UNA FORM INDIPENDENTE DALLA RISOLUZIONE

' Da Microsoft Knowledgebase Article ID: Q182070
' 1) Change the video resolution to 800 x 600.
' 2) Start a new project in Visual Basic. Form1 is created by default.
' 3) Add a Label, a CommandButton, and any other types of controls you would like to test.
Copy the following code into the Form's module:
Dim MyForm As FRMSIZE
Dim DesignX As Integer
Dim DesignY As Integer
Private Sub Form_Load()
Dim ScaleFactorX As Single, ScaleFactorY As Single ' Scaling factors
' Size of Form in Pixels at design resolution
DesignX = 800
DesignY = 600
RePosForm = True ' Flag for positioning Form
DoResize = False ' Flag for Resize Event
' Set up the screen values
Xtwips = Screen.TwipsPerPixelX
Ytwips = Screen.TwipsPerPixelY
Ypixels = Screen.Height / Ytwips ' Y Pixel Resolution
Xpixels = Screen.Width / Xtwips ' X Pixel Resolution
' Determine scaling factors
ScaleFactorX = (Xpixels / DesignX)
ScaleFactorY = (Ypixels / DesignY)
ScaleMode = 1 ' twips
'Exit Sub ' uncomment to see how Form1 looks without resizing
Resize_For_Resolution ScaleFactorX, ScaleFactorY, Me
Label1.Caption = "Current resolution is " & Str$(Xpixels) + _
" by " + Str$(Ypixels)
MyForm.Height = Me.Height ' Remember the current size
MyForm.Width = Me.Width
End Sub
Private Sub Form_Resize()
Dim ScaleFactorX As Single, ScaleFactorY As Single
If Not DoResize Then ' To avoid infinite loop
DoResize = True
Exit Sub
End If
RePosForm = False
ScaleFactorX = Me.Width / MyForm.Width ' How much change?
ScaleFactorY = Me.Height / MyForm.Height
Resize_For_Resolution ScaleFactorX, ScaleFactorY, Me
MyForm.Height = Me.Height ' Remember the current size
MyForm.Width = Me.Width
End Sub
Private Sub Command1_Click()
Dim ScaleFactorX As Single, ScaleFactorY As Single
DesignX = Xpixels
DesignY = Ypixels
RePosForm = True
DoResize = False
' Set up the screen values
Xtwips = Screen.TwipsPerPixelX
Ytwips = Screen.TwipsPerPixelY
Ypixels = Screen.Height / Ytwips ' Y Pixel Resolution
Xpixels = Screen.Width / Xtwips ' X Pixel Resolution
' Determine scaling factors
ScaleFactorX = (Xpixels / DesignX)
ScaleFactorY = (Ypixels / DesignY)
Resize_For_Resolution ScaleFactorX, ScaleFactorY, Me
Label1.Caption = "Current resolution is " & Str$(Xpixels) + _
" by " + Str$(Ypixels)
MyForm.Height = Me.Height ' Remember the current size
MyForm.Width = Me.Width
End Sub
' 4) Add a Module from the Project menu and paste in the following code:
Public Xtwips As Integer, Ytwips As Integer
Public Xpixels As Integer, Ypixels As Integer
Type FRMSIZE
Height As Long
Width As Long
End Type
Public RePosForm As Boolean
Public DoResize As Boolean
Sub Resize_For_Resolution(ByVal SFX As Single, _
ByVal SFY As Single, MyForm As Form)
Dim I As Integer
Dim SFFont As Single
SFFont = (SFX + SFY) / 2 ' average scale
' Size the Controls for the new resolution
On Error Resume Next ' for read-only or nonexistent properties
With MyForm
For I = 0 To .Count - 1
If TypeOf .Controls(I) Is ComboBox Then ' cannot change Height
.Controls(I).Left = .Controls(I).Left * SFX
.Controls(I).Top = .Controls(I).Top * SFY
.Controls(I).Width = .Controls(I).Width * SFX
Else
.Controls(I).Move .Controls(I).Left * SFX, _
.Controls(I).Top * SFY, _
.Controls(I).Width * SFX, _
.Controls(I).Height * SFY
End If
.Controls(I).FontSize = .Controls(I).FontSize * SFFont
Next I
If RePosForm Then
' Now size the Form
.Move .Left * SFX, .Top * SFY, .Width * SFX, .Height * SFY
End If
End With