Questions similar to this appear on the Bentley Discussion Groups. This problem appeared in the VBA discussion group. Note that there is a separate discussion group for each major version of MicroStation.
Q
How do I find the native Window handle (a Win32 HWND) of a MicroStation view or dialog?
A MicroStation is a Windows application. MicroStation itself is a window, and each view and dialog within MicroStation is also a window. MicroStation VBA doesn't have a way to get a view or dialog window handle. However, you can get a window handle using MDL functions that are part of MicroStation. This article tells you how to use MDL functions to obtain a window handle.
Each Windows' window has a unique ID, assigned by Windows.
That unique ID is the Win32 HWND handle.
A Win32 HWND is useful to a programmer,
because once you have that handle, you can do things with a window or extract information about that window.
For more information about Windows programming, visit the Microsoft Developer Network web site.
There is no way to obtain a Win32 HWND through VBA, but we can borrow the necessary function from
the MicroStation Development Library (MDL).
To use an MDL function, you must first Declare it.
The declaration informs the VBA compiler of the function's name and the library (DLL file) where it can be found.
External function declarations should be placed near the top of your VBA module, before any implementation code …
Declare Function mdlWindow_nativeWindowHandleGet Lib "stdmdlbltin.dll" (ByRef nativeHandleP As Long, ByVal windowP As Long, ByVal type_ As Long) As Long
For more information about the Declare statement, see the VBA documentation.
It's common to use this technique to call Microsoft Win32 functions.
This code is provided by LA Solutions Ltd as an example. LA Solutions Ltd does not warrant the code fit for any particular purpose.
The example uses two MDL functions: mdlWindow_viewWindowGet and mdlWindow_nativeWindowHandleGet.
mdlWindow_viewWindowGet converts a zero-based MicroStation view index to an MDL window pointer.
mdlWindow_nativeWindowHandleGet converts the MDL window pointer to a Win32 HWND.
The declaration of mdlWindow_nativeWindowHandleGet corrects the documented MDL declaration.
If you use the MDL Declare statement your code will cause MicroStation to exit spontaneously.
Option Explicit' --------------------------------------------------------------------- ' MDL function declarations ' --------------------------------------------------------------------- ' The declaration below, taken from the MDL docs, is incorrect: ' Declare Function mdlWindow_nativeWindowHandleGet Lib "stdmdlbltin.dll" (ByVal nativeHandleP As Long, ByVal windowP As Long, ByVal type_ As Long) As Long ' This is the correct declaration:Declare Function mdlWindow_nativeWindowHandleGet Lib "stdmdlbltin.dll" (ByRef nativeHandleP As Long, ByVal windowP As Long, ByVal type_ As Long) As Long Declare Function mdlWindow_viewWindowGet Lib "stdmdlbltin.dll" (ByVal viewNum As Long) As Long ' Returns a pointer to a structure' --------------------------------------------------------------------- ' GetViewWindowHandle ' Gets a Win32 HWND handle to a MicroStation view window ' Note that VBA (and a MicroStation user) numbers views starting a 1, ' but the MDL functions used here are zero-based ' ---------------------------------------------------------------------Function GetViewWindowHandle(viewNum As Integer) As Long GetViewWindowHandle = 0' MDL view number must be between 0 and 7Debug.Assert viewNum < 8 And 0 <= viewNum Const HANDLETYPE_HWND As Long = 0 Dim window As Long window = mdlWindow_viewWindowGet(viewNum) Debug.Print "Window address " & CStr(window) Dim hwnd As Long mdlWindow_nativeWindowHandleGet hwnd, window, HANDLETYPE_HWND Debug.Print "HWND " & CStr(hwnd) GetViewWindowHandle = hwnd End Function' --------------------------------------------------------------------- ' Main entry point: start your code here ' ---------------------------------------------------------------------Sub Main() Dim hwnd As Long Const viewNum As Integer = 0' Zero-based: 0-7hwnd = GetViewWindowHandle(viewNum) Debug.Print "View [" & CStr(viewNum) & "] handle " & CStr(hwnd) End Sub