GUI window control!

andyinmanc
04-08-2003, 01:06 PM
Hi,

1) How do you keep a window always at the front - so it overlaps everything else?

2) How do you dock a window to an edge of the screen so its out of the way - and make everything else resize so that nothing overlaps?


Cheers,

Andy

Flyguy
04-08-2003, 01:09 PM
2. To dock a form to the edge of the screen

Option Explicit

Private Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uiParam As Long, pvParam As Any, ByVal fWinIni As Long) As Long

Enum DockTypes
DockLeft = 1
DockTop = 2
DockRight = 3
DockBottom = 4
End Enum

Private Type vbRECT ' use this to save old Window Positions
vbLeft As Long
vbTop As Long
vbWidth As Long
vbHeight As Long
End Type

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Const SPIF_SENDWININICHANGE = &H2
Private Const SPI_GETWORKAREA = 48
Private Const SPI_SETWORKAREA = 47

Private vbFormOldRect As vbRECT
Private LastDock As DockTypes
Private DockAmount As Integer

Sub UnDockForm(vbForm As Form)
' This sub does the opposite of DockForm basicly.
Dim Desktop As RECT

' Get the Work space area of the desktop
'
SystemParametersInfo SPI_GETWORKAREA, 0&, Desktop, 0&


With Desktop ' change the values back to normal
Select Case LastDock
Case DockBottom
.Bottom = .Bottom + DockAmount
Case DockLeft
.Left = .Left - DockAmount
Case DockTop
.Top = .Top - DockAmount
Case DockRight
.Right = .Right + DockAmount
Case Else
Exit Sub ' no dock performed
End Select
End With

' Now set the form back to normal

With vbFormOldRect
vbForm.Move .vbLeft, .vbTop, .vbWidth, .vbHeight
End With

' Now, update the SystemParams again..
SystemParametersInfo SPI_SETWORKAREA, 0&, Desktop, SPIF_SENDWININICHANGE
' And clear LastDock
LastDock = 0
' And thats it. Should all be good =]
End Sub

Sub DockForm(vbForm As Form, DockPos As DockTypes)
Dim V As vbRECT
Dim Desktop As RECT

If LastDock <> 0 Then
' form is already docked... you really don't want to dock it somewhere else
' MsgBox "Please don't re-dock without un-docking.", vbOKOnly, "Docking aborted"
Exit Sub
End If

' FIRST, save the RECT of vbForm
With vbFormOldRect
.vbHeight = vbForm.Height
.vbLeft = vbForm.Left
.vbTop = vbForm.Top
.vbWidth = vbForm.Width
End With

'Get the Current Desktop Work Area
SystemParametersInfo SPI_GETWORKAREA, 0&, Desktop, 0&

' Now, resize the form to what we want it to be
V = vbFormOldRect ' (aka current window size)
With V
Select Case DockPos
Case DockLeft
.vbTop = (Desktop.Top * Screen.TwipsPerPixelY)
.vbLeft = (Desktop.Left * Screen.TwipsPerPixelX)
.vbHeight = (Desktop.Bottom * Screen.TwipsPerPixelY) - .vbTop
Case DockRight
.vbTop = (Desktop.Top * Screen.TwipsPerPixelY)
.vbLeft = (Desktop.Right * Screen.TwipsPerPixelX) - .vbWidth
.vbHeight = (Desktop.Bottom * Screen.TwipsPerPixelY) - .vbTop
Case DockBottom
.vbTop = (Desktop.Bottom * Screen.TwipsPerPixelY) - .vbHeight
.vbLeft = (Desktop.Left * Screen.TwipsPerPixelX)
.vbWidth = (Desktop.Right * Screen.TwipsPerPixelX) - .vbLeft
Case DockTop
.vbTop = (Desktop.Top * Screen.TwipsPerPixelY)
.vbLeft = (Desktop.Left * Screen.TwipsPerPixelX)
.vbWidth = (Desktop.Right * Screen.TwipsPerPixelX) - .vbLeft
Case Else
Exit Sub
End Select
End With

' Now, Modify the Desktop values
With Desktop
Select Case DockPos
Case DockBottom
DockAmount = (vbForm.Height / 15)
.Bottom = .Bottom - DockAmount
Case DockRight
DockAmount = (vbForm.Width / 15)
.Right = .Right - DockAmount
Case DockTop
DockAmount = (vbForm.Height / 15)
.Top = .Top + DockAmount
Case DockLeft
DockAmount = (vbForm.Width / 15)
.Left = .Left + DockAmount
End Select
End With

' Now all is needed is to Update the sysParams..
SystemParametersInfo SPI_SETWORKAREA, 0&, Desktop, SPIF_SENDWININICHANGE

With V
vbForm.Move .vbLeft, .vbTop, .vbWidth, .vbHeight
End With
LastDock = DockPos
End Sub

BankCop
04-08-2003, 01:11 PM
Always on top



Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "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)

Private Sub Form_Activate()
'KPD-Team 1998
'URL: <a href=\"http://www.allapi.net/\" target=\"_blank\">http://www.allapi.net/</a>
'E-Mail: KPDTeam@Allapi.net
'Set the window position to topmost
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, _
SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or _
SWP_NOSIZE
End Sub

andyinmanc
04-08-2003, 01:15 PM
wow cool fellas, I'll try all this out.

Another question - not sure if you'll know though..!!!

I'm writing some VB code on an Apple Mac using REALbasic. Do you know how to do the same thing on this platform???


Cheers,

Andy

Flyguy
04-08-2003, 01:18 PM
Will be completly different, because both code sample heavilly rely on the Windows API, which will be VERY different on other platforms.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum