How can I find the hWnd or the hDC of an Image in Publisher?

S

schlefty

Here is what I am trying to do. I need to be able to grab a pixel from
an image that I have on the form. In order to do that I am using this
function:

Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal
X As Long, ByVal Y As Long) As Long

But this requires the hDC of the Image. I tried to find that by using
this function:

Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As
Long

But this means that I need the hWnd of the image. I tried to grab that
by using this:

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

But that only works if I use the caption, but Images don't have
captions. So is there any other way I can find either the specific hDC
or hWnd of the image? I can't simply use the hWnd of the form, because
then my GetPixel function will only grab pixels from the form itself
and not from the image. I'm at a loss. Any help would be great. Thanks.
 
D

Dave Miller

Try this code below, I use this whenever I need to mess around with
windows. Run fEnumWindows() with your Immediate window open to see a
list of all open windows.

'************** Start Code***************
Option Compare Database

Private Declare Function apiGetClassName Lib "user32" Alias _
"GetClassNameA" (ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function apiGetDesktopWindow Lib "user32" Alias _
"GetDesktopWindow" () As Long
Private Declare Function apiGetWindow Lib "user32" Alias _
"GetWindow" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function apiGetWindowLong Lib "user32" Alias _
"GetWindowLongA" (ByVal hWnd As Long, ByVal _
nIndex As Long) As Long
Private Declare Function apiGetWindowText Lib "user32" Alias _
"GetWindowTextA" (ByVal hWnd As Long, ByVal _
lpString As String, ByVal aint As Long) As Long
Private Const mcGWCHILD = 5
Private Const mcGWHWNDNEXT = 2
Private Const mcGWLSTYLE = (-16)
Private Const mcWSVISIBLE = &H10000000
Private Const mconMAXLEN = 255

Function fEnumWindows()
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String

lngx = apiGetDesktopWindow()
'Return the first child to Desktop
lngx = apiGetWindow(lngx, mcGWCHILD)

Do While Not lngx = 0
strCaption = fGetCaption(lngx)
If Len(strCaption) > 0 Then
lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
'enum visible windows only
If lngStyle And mcWSVISIBLE Then
Debug.Print "Class = " & fGetClassName(lngx),
Debug.Print "Caption = " & fGetCaption(lngx)
End If
End If
lngx = apiGetWindow(lngx, mcGWHWNDNEXT)
Loop
End Function

Function fEnumWindowsGetClass(sCaption As String) As String
Dim lngx As Long, lngLen As Long
Dim lngStyle As Long, strCaption As String

lngx = apiGetDesktopWindow()
'Return the first child to Desktop
lngx = apiGetWindow(lngx, mcGWCHILD)

Do While Not lngx = 0
strCaption = fGetCaption(lngx)
If Len(strCaption) > 0 Then
lngStyle = apiGetWindowLong(lngx, mcGWLSTYLE)
'enum visible windows only
If lngStyle And mcWSVISIBLE Then
If fGetCaption(lngx) = sCaption Then
fEnumWindowsGetClass = fGetClassName(lngx)
Exit Function
End If
End If
End If
lngx = apiGetWindow(lngx, mcGWHWNDNEXT)
Loop
End Function
Private Function fGetClassName(hWnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetClassName(hWnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetClassName = left$(strBuffer, intCount)
End If
End Function

Private Function fGetCaption(hWnd As Long) As String
Dim strBuffer As String
Dim intCount As Integer

strBuffer = String$(mconMAXLEN - 1, 0)
intCount = apiGetWindowText(hWnd, strBuffer, mconMAXLEN)
If intCount > 0 Then
fGetCaption = left$(strBuffer, intCount)
End If
End Function
'************** Code End ***************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top