ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   hWnd of UserForm (https://www.excelbanter.com/excel-programming/290811-hwnd-userform.html)

rsmith

hWnd of UserForm
 
If I activate a UserForm in Excel, how can I access
the hWnd of that UserForm ?



Bob Phillips[_6_]

hWnd of UserForm
 
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Function hWnd() As Long
Dim hWndThis As Long
If Val(Application.Version) 8 Then
hWndThis = FindWindow(lpClassName:="ThunderDFrame",
lpWindowName:=Me.Caption)
Else
hWndThis = FindWindow(lpClassName:="ThunderXFrame",
lpWindowName:=Me.Caption)
End If

hWnd = hWndThis
End Function


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"rsmith" wrote in message
...
If I activate a UserForm in Excel, how can I access
the hWnd of that UserForm ?





Chip Pearson

hWnd of UserForm
 
You need to use the FindWindow API call. For example,

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String)
As Long

Sub AAA()
Dim HWND As Long
If Val(Application.Version) = 9 Then
HWND = FindWindow("ThunderDFrame", UserForm1.Caption)
Else
HWND = FindWindow("ThunderXFrame", UserForm1.Caption)
End If
MsgBox HWND
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"rsmith" wrote in message
...
If I activate a UserForm in Excel, how can I access
the hWnd of that UserForm ?





Tom Ogilvy

hWnd of UserForm
 
From a post by Stephen Bullen:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindowLong Lib "user32" Alias
"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

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 DrawMenuBar Lib "user32" (ByVal hWnd As Long)
As Long

Private Const GWL_STYLE As Long = (-16)
Private Const WS_CAPTION As Long = &HC00000
Private Const WS_SYSMENU As Long = &H80000

Private Sub UserForm_Activate()

Dim iStyle As Long

'Get the userform's window handle
If Val(Application.Version) < 9 Then
hWndForm = FindWindow("ThunderXFrame", Me.Caption) 'XL97
Else
hWndForm = FindWindow("ThunderDFrame", Me.Caption) 'XL2000
End If

iStyle = GetWindowLong(hWndForm, GWL_STYLE)
iStyle = iStyle And Not WS_CAPTION
iStyle = iStyle And Not WS_SYSMENU
SetWindowLong hWndForm, GWL_STYLE, iStyle
DrawMenuBar hWndForm

End Sub


Don't forget to add a way to close the form!


Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.co.uk


ThunderDFrame is for xl2000 and later

--
Regards,
Tom Ogilvy

rsmith wrote in message
...
If I activate a UserForm in Excel, how can I access
the hWnd of that UserForm ?






All times are GMT +1. The time now is 11:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com