Home All Groups Group Topic Archive Search About

Icon.FromHandle(..) not working?

Author
25 Jul 2006 4:19 PM
iwdu15
hi, im attempting to get an applications Icon by using the method
Icon.FromHandle() and passing in a windows handle, yet nothing works ive
tried two different things and neither work. Ive been googleing this to no
avail....code:


    Protected Overrides Sub WndProc(ByRef m As Message)

        MyBase.WndProc(m)

        If m.Msg = WM_SYSCOMMAND Then

            Select Case m.WParam.ToInt32

                Case 1000

                    Dim tray As New NotifyIcon(Me.components)


                     tray.Icon = Icon.FromHandle(m.HWnd)

                    ''tried using Me.Handle to see if that would work but no
                    'tray.Icon = Icon.FromHandle(Me.Handle)
                    tray.Visible = True

                    AddHandler tray.DoubleClick, AddressOf
NotifyIcon1_DoubleClick

                    SendMessage(m.HWnd.ToInt32, 0, Nothing, Nothing)

            End Select

        End If

    End Sub


i set a breakpoint and step through, the method returns something, but
apparently not an Icon like the MSDN documentation says...any idea how i can
get this working? thanks

--
-iwdu15

Author
25 Jul 2006 4:28 PM
Herfried K. Wagner [MVP]
"iwdu15" <jmmgoalsteratyahoodotcom> schrieb:
> hi, im attempting to get an applications Icon by using the method
> Icon.FromHandle() and passing in a windows handle

You will have to pass in an icon handle ('HICON').

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
25 Jul 2006 4:41 PM
iwdu15
so then if i get a windows handle, how can i get either a)its icon handle or
b) its icon? thanks for your help
--
-iwdu15
Author
25 Jul 2006 4:53 PM
Herfried K. Wagner [MVP]
"iwdu15" <jmmgoalsteratyahoodotcom> schrieb:
> so then if i get a windows handle, how can i get either a)its icon handle
> or
> b) its icon?

PInvoke with 'SendMessage' + 'WM_GETICON' (see documentation).

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
25 Jul 2006 5:04 PM
iwdu15
Great, thanks!
--
-iwdu15
Author
25 Jul 2006 6:03 PM
iwdu15
i cant seem to figre this out....google hasnt helped either....heres what i
have

Protected Overrides Sub WndProc(ByRef m As Message)

        MyBase.WndProc(m)

        If m.Msg = WM_SYSCOMMAND Then

            Select Case m.WParam.ToInt32

                Case 1000

                    SendMessage(m.HWnd, WM_GETICON, Nothing, Nothing)

                    Dim tray As New NotifyIcon

                    Dim x As IntPtr = DefWindowProcA(m.HWnd, WM_GETICON,
Nothing, Nothing)

                    tray.Icon = Icon.FromHandle(x)
                    tray.Visible = True

                    AddHandler tray.DoubleClick, AddressOf
NotifyIcon1_DoubleClick

                    SendMessage(m.HWnd, 0, Nothing, Nothing)

            End Select

        End If

    End Sub


yet the DefWindowProcA doesnt return a windows handle....i couldnt find any
documentation of how to get a specific message on MSDN or AllApi etc....thanks

--
-iwdu15
Author
25 Jul 2006 7:12 PM
Herfried K. Wagner [MVP]
"iwdu15" <jmmgoalsteratyahoodotcom> schrieb:
>i cant seem to figre this out....google hasnt helped either....heres what i
> have

'SendMessage''s return value is the icon handle you can pass to
'Icon.FromHandle'.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
25 Jul 2006 7:50 PM
iwdu15
SendMessage() was returning 0, so i found a way around it. I iterated through
each process comparing that handle to the handle i already have, if theyre
the same i get the main module file name and extract the icon....thanks
though Herfried!
--
-iwdu15