-
Nov 13th, 2002, 06:36 PM
#1
Thread Starter
Stuck in the 80s
Visual Basic API FAQs
I figured since this forum didn't have a FAQ part to it, we should make one, since a lot of questions asked are constantly asked. So here are a few things I came up with. Please feel free to add your own API code that you think would benifit someone else.
How to get a windows HWND
Since many tasks through API can be used on other windows, such as changing a caption or getting a caption, it'd
be important to first now how to obtain the HWND (which is used in most API) of another window. There are two methods
explored below:
Get HWND from caption
This example requires that you know the exact caption of the window, such as 'Untitled' with Notepad.
VB Code:
Option Explicit
'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Form_Load()
Dim strCaption As String, lhWnd As Long
'Exact caption of the window:
strCaption = "Untitled - Notepad"
lhWnd = FindWindow(vbNullString, strCaption)
'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
MsgBox "Notepad found: " & lhWnd
End If
End Sub
Get HWND from class name
The other method would be to use the window's class name:
VB Code:
Option Explicit
'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Form_Load()
Dim strClassName As String, lhWnd As Long
'Class Name of the window:
strClassName = "Notepad"
lhWnd = FindWindow(strClassName, vbNullString)
'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
MsgBox "Notepad found: " & lhWnd
End If
End Sub
Bring a window to the top
The following example brings our project form to the top of the Z-order chain. This does NOT make it stay on top. For
Always On Top, see the next example:
VB Code:
Option Explicit
'declare api function:
Private Declare Function BringWindowToTop Lib "user32" _
(ByVal hwnd As Long) As Long
'this code brings our window to the top every half a second:
Private Sub Form_Load()
'setup our timer's interval:
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
'bring our project to the top:
BringWindowToTop Me.hwnd
End Sub
Set window to be top most (always on top)
This code will allow you to toggle your window as being always on top or not always on top, as seen in things like AIM
and HTML Help.
VB Code:
Option Explicit
'declare constants:
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
'declare API:
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 Command1_Click()
'set topmost:
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or _
SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
Private Sub Command2_Click()
'set not topmost:
SetWindowPos Me.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or _
SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
Private Sub Form_Load()
'project requires 2 commandbuttons:
Command1.Caption = "Top Most"
Command2.Caption = "Not Top Most"
End Sub
Get/Set Window Caption AND Get Topmost Window
This code will first allow you to get the HWND of the foreground window, or the window with focus. Then it will capture
the title of the window and reverse it.
VB Code:
Option Explicit
'Declare API:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String) As Long
Private Sub Command1_Click()
Dim lhWnd As Long, strCaption As String
'create our buffer for the caption:
strCaption = String(100, Chr$(0))
'get the topmost window:
lhWnd = GetForegroundWindow()
'get the caption
GetWindowText lhWnd, strCaption, 100
'clear the buffer:
strCaption = Left(strCaption, InStr(strCaption, Chr(0)) - 1)
'reverse the string and set the new caption:
strCaption = StrReverse(strCaption)
SetWindowText lhWnd, strCaption
End Sub
Set Window Parent
This code, although not pretty, makes the VB form the parent of an instance of notepad, thus making notepad "trapped"
into that window.
VB Code:
Option Explicit
'declare API:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private Sub Form_Load()
Dim lhWnd As Long
'Get Notepads HWND:
lhWnd = FindWindow("Notepad", vbNullString)
'if the result is 0, window was not found:
If lhWnd = 0 Then
MsgBox "Could not find Notepad..."
Else
'set the parent:
SetParent lhWnd, Me.hwnd
End If
End Sub
Execute a file in it's default program
This example opens a text file in notepad (if that's the default program)
VB Code:
Option Explicit
'declare constants:
Private Const SW_SHOWNORMAL = 1
'declare API:
Private Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As _
String, ByVal nShowCmd As Long) As Long
Private Sub Form_Load()
Dim lError As Long
'launch C:\movies.txt, given that it exists:
lError = ShellExecute(Me.hwnd, vbNullString, "C:\movies.txt", vbNullString, _
"C:", SW_SHOWNORMAL)
'if returns 2:
If lError = 2 Then
MsgBox "File does not exist!"
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|