DrawIconEx

  • Thread starter Thread starter Marshall
  • Start date Start date
M

Marshall

What is the exact syntax for using DrawIconEx
DrawIconEx(System.Windows.Forms.PictureBox1.hdc, _
0, _
0, _
lngmIcon, _
660, _
450, _
0, _
0, _
DI_NORMAL)
as you can see it doesn't work. I tried adding .PictureBox1 (the name
of the control) and it didn't work either.
I'm new to this, so please bear with me.
Marshall
 
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias
"ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String,
lpiIcon As Long) As Long

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal
xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As
Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal
hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As
Long

Why do you want to use unmanaged code? Just curious.
 
vbnetdev said:
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias
"ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String,
lpiIcon As Long) As Long

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal
xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As
Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal
hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As
Long

Why do you want to use unmanaged code? Just curious.
vbnetdev:
I've done all that. I just took my code out of context.
I didn't think that I had to show it all.
It's just that DrawIconEx(System.WIndows.Forms.PictureBox.Picture1.hdc
doesn't work either.
So what is the syntax for System.Forms.....?
Marshall
 
vbnetdev said:
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll"
Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal
lpIconPath As String, lpiIcon As Long) As Long

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long,
ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal
cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long,
ByVal
hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As
Long) As Long


Your code is VB6 code but this is a VB.Net group.

The correct declaration is: (unverified)

Private Declare Function ExtractAssociatedIcon Lib "shell32.dll"
Alias "ExtractAssociatedIconA" (ByVal hInst As intptr, ByVal
lpIconPath As String, byref lpiIcon As Integer) As intptr

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As intptr,
ByVal xLeft As integer, ByVal yTop As integer, ByVal hIcon As intptr, ByVal
cxWidth As integer, ByVal cyWidth As integer, ByVal istepIfAniCur As
integer,
ByVal hbrFlickerFreeDraw As intptr, ByVal diFlags As integer) As boolean

(In VB 2005 you can use the Unsigned Integer type for istepIfAniCur and
diFlags.)

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As
intptr) As boolean


But I agree that we should use the managed version wherever possible. BTW,
in Framework 2.0, there is the Icon.ExtractAssociatedIcon method.


Armin
 
Marshall said:
So what is the syntax for System.Forms.....?


Where do you get the Icon from? Do you have a System.Drawing.Icon object or
did you receive a icon handle somewhere from unmanaged functions?

Wherever possible, use the Graphics object to draw. In a Paint event, you
get it in e.graphics. Outside Paint, you create it by calling
YourControl.Creategraphics. Then call the Graphics object's DrawIcon method.


Armin
 
Gentlemen;
here is the project. All 54 lines of it.
Private Declare Function DestroyIcon Lib "user32" _
(ByVal hIcon As Long) As Long
Private Declare Function DrawIconEx Lib "user32" _
(ByVal hdc As Long, _
ByVal xLeft As Long, _
ByVal yTop As Long, _
ByVal hIcon As Long, _
ByVal cxHeight As Long, _
ByVal cyWidth As Long, _
ByVal istepIfAniCur As Long, _
ByVal hbrFlickerFreeDraw As Long, _
ByVal diFlags As Long) As Long
'
' For Icon extraction and placement in Image control
Private Declare Function ExtractIcon Lib "shell32.dll" _
Alias "ExtractIconA" _
(ByVal hInst As Long, _
ByVal lpszExeFileName As String, _
ByVal nIconIndex As Long) As Long
Private lngmIcon As Long
Const DI_MASK As Long = &H1
Const DI_IMAGE As Long = &H2
Const DI_NORMAL As Long = DI_MASK Xor DI_IMAGE
Const ERROR_SUCCESS = 0

Private Sub frmCCw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Extract_the_Icon()
End Sub

Private Sub Draw_The_Icon()
DrawIconEx(System.Windows.Forms.PictureBox1.hdc, _
0, _
0, _
lngmIcon, _
660, _
450, _
0, _
0, _
DI_NORMAL)
' remove the icon from the memory
DestroyIcon(lngmIcon)
End Sub
Private Sub Extract_the_Icon()
lngmIcon = ExtractIcon("C:\Windows\System32", "Shell32.dll",
84&)
If ERROR_SUCCESS Then
Draw_The_Icon()
End If
End Sub

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles PictureBox2.Click

End Sub
End Class
Now everything is fine, I guess, except the line that has
DrawIconEx(System.Windows.Forms.PictureBox1.hdc, _
..
..
Where it reports in the error window,
Error 1 'PictureBox1' is not a member of 'Forms'.
F:\VB Studio Net
Projects\ControlCenter\ControlCenter\CCW.vb 32 20 ControlCenter
So again;
What is the syntax for DrawIconEx?
Marshall
 
Armin said:
Is there a reason why you ignore my posts? The declarations are still wrong.


Armin

I'm looking now on Microsoft.com for just what are the Declarations.
No luck so far.
When I'm wrong I will change the declaration.
WHAT ARE THEY.
God I cant find anything. They're doesn't appear to be any built-in
API Viewer so I can get the declaration right.
AND STILL, what is the syntax fro DrawIconEX?
Marshall
 
Marshall said:
I'm looking now on Microsoft.com for just what are the Declarations.
No luck so far.
When I'm wrong I will change the declaration.
WHAT ARE THEY.
God I cant find anything. They're doesn't appear to be any built-in
API Viewer so I can get the declaration right.
AND STILL, what is the syntax fro DrawIconEX?
Marshall


<quote>
Where do you get the Icon from? Do you have a System.Drawing.Icon object or
did you receive a icon handle somewhere from unmanaged functions?

Wherever possible, use the Graphics object to draw. In a Paint event, you
get it in e.graphics. Outside Paint, you create it by calling
YourControl.Creategraphics. Then call the Graphics object's DrawIcon method.
</quote>


<quote>
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll"
Alias "ExtractAssociatedIconA" (ByVal hInst As intptr, ByVal
lpIconPath As String, byref lpiIcon As Integer) As intptr

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As intptr,
ByVal xLeft As integer, ByVal yTop As integer, ByVal hIcon As intptr, ByVal
cxWidth As integer, ByVal cyWidth As integer, ByVal istepIfAniCur As
integer,
ByVal hbrFlickerFreeDraw As intptr, ByVal diFlags As integer) As boolean

(In VB 2005 you can use the Unsigned Integer type for istepIfAniCur and
diFlags.)

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As
intptr) As boolean


But I agree that we should use the managed version wherever possible. BTW,
in Framework 2.0, there is the Icon.ExtractAssociatedIcon method.
</quote>

I do not have the declaration for DrawIconEx, but you can use
Icon.FromHandle to create an Icon and draw it the managed way as shown
above.


Armin
 
Armin said:
<quote>
Where do you get the Icon from? Do you have a System.Drawing.Icon object or
did you receive a icon handle somewhere from unmanaged functions?

Wherever possible, use the Graphics object to draw. In a Paint event, you
get it in e.graphics. Outside Paint, you create it by calling
YourControl.Creategraphics. Then call the Graphics object's DrawIcon method.
</quote>


<quote>
Private Declare Function ExtractAssociatedIcon Lib "shell32.dll"
Alias "ExtractAssociatedIconA" (ByVal hInst As intptr, ByVal
lpIconPath As String, byref lpiIcon As Integer) As intptr

Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As intptr,
ByVal xLeft As integer, ByVal yTop As integer, ByVal hIcon As intptr, ByVal
cxWidth As integer, ByVal cyWidth As integer, ByVal istepIfAniCur As
integer,
ByVal hbrFlickerFreeDraw As intptr, ByVal diFlags As integer) As boolean

(In VB 2005 you can use the Unsigned Integer type for istepIfAniCur and
diFlags.)

Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As
intptr) As boolean


But I agree that we should use the managed version wherever possible. BTW,
in Framework 2.0, there is the Icon.ExtractAssociatedIcon method.
</quote>

I do not have the declaration for DrawIconEx, but you can use
Icon.FromHandle to create an Icon and draw it the managed way as shown
above.


Armin
Armin,
1. Up above is the method with which I retrieved the Icon.
2. What the heck is 'the managed version'? Never heard of that before.
3. I must be stupid or whatever, because I don't understand a damn
thing you are saying about Icon.handle and create an Icon, etc.
I extracted the Icon, NOW, How do I draw it into a Picture Box.
Step by Step.
If you can't or no one else can, then forget it.
Thanks for all your time and trouble.

Marshall

PS. Now I know why VB6 coders are fighting moving to VB.Net.
It looks similar to Java and C#.
Now who in their right mind wants to go thru all the trouble of the
name.name.name stuff
when in VB6 you could just do it.
I'm trying to learn VB.Net, but as you can see I have a mindset to
overcome.
VB6 is SO MUCH EASIER to understand and to code for; I started with VB
3.0 on WIndows 3.11 and was writing code within a 1/2 hr.
Now I can't even get the program to run unless I do ctrl+F5
VB6 not being an OOP language, as far as I am concerned is a complaint
of the Publishers of magazines, C++ and Java programmers who have a
propensity to favor total OOP languages.
Now that said, I would be willing to wager that 1,000's of VB6 programs
run companies.
So to say it's not a 'real' language is absurd.
But I digress.
Sorry to go on a rant, but I had to say my 2 cents worth. For whatever
that's worth.
 
Marshall said:
Armin,
1. Up above is the method with which I retrieved the Icon.

Today... I did not quote it because I wanted to know how you get the icon
because you posted it meanwhile. I only quoted it because I wrote how you
use the graphics object to paint an icon, and because you wrote you didn't
find the correct declaration. That's why I assumed that you didn't see the
message and I quoted it again.
2. What the heck is 'the managed version'? Never heard of that
before.

Calling API functions (also called pInvoke (=plattform invoke)) is the
"native" way to draw an icon, wheras using the classes in the
..Net Framework is called the "managed" way. It is called that
because the Framework provides the "managed excution" of applications
that are based on the framework.
3. I must be stupid or whatever, because I don't understand
a damn
thing you are saying about Icon.handle and create an Icon, etc.

An Icon is an object. See class System.Drawing.Icon. The method
Icon.FromHandle is a method that can be used to create an Icon object by
passing an icon handle. Pass the icon handle that you got from the
Extracticon function.
I extracted the Icon, NOW, How do I draw it into a Picture Box.
Step by Step.


1. get the icon handle using your api function
2. call icon.fromhandle to create an Icon
3. see first quote above
If you can't or no one else can, then forget it.
Thanks for all your time and trouble.

Marshall

PS. Now I know why VB6 coders are fighting moving to VB.Net.
It looks similar to Java and C#.
Now who in their right mind wants to go thru all the trouble of the
name.name.name stuff
when in VB6 you could just do it.

Do you have all your files in C:\ or do you create sub folders? I love
(VB).Net
for being able to organize classes in namespaces. Would be very chaotic if
thousands of them were thrown in one big pot, including the problem of
conflicting names.
I'm trying to learn VB.Net, but as you can see I have a mindset to
overcome.
VB6 is SO MUCH EASIER to understand and to code for; I started with
VB 3.0 on WIndows 3.11 and was writing code within a 1/2 hr.
Now I can't even get the program to run unless I do ctrl+F5
VB6 not being an OOP language, as far as I am concerned is a
complaint of the Publishers of magazines, C++ and Java programmers
who have a propensity to favor total OOP languages.
Now that said, I would be willing to wager that 1,000's of VB6
programs run companies.
So to say it's not a 'real' language is absurd.
But I digress.
Sorry to go on a rant, but I had to say my 2 cents worth. For
whatever that's worth.

Well, it takes some time to get accustomed to it because it is so different,
but once done, you'll love it. Things are often much simpler to do (not
always, of course).


Armin
 
Armin said:
Today... I did not quote it because I wanted to know how you get the icon
because you posted it meanwhile. I only quoted it because I wrote how you
use the graphics object to paint an icon, and because you wrote you didn't
find the correct declaration. That's why I assumed that you didn't see the
message and I quoted it again.


Calling API functions (also called pInvoke (=plattform invoke)) is the
"native" way to draw an icon, wheras using the classes in the
.Net Framework is called the "managed" way. It is called that
because the Framework provides the "managed excution" of applications
that are based on the framework.


An Icon is an object. See class System.Drawing.Icon. The method
Icon.FromHandle is a method that can be used to create an Icon object by
passing an icon handle. Pass the icon handle that you got from the
Extracticon function.



1. get the icon handle using your api function
2. call icon.fromhandle to create an Icon
3. see first quote above


Do you have all your files in C:\ or do you create sub folders? I love
(VB).Net
for being able to organize classes in namespaces. Would be very chaotic if
thousands of them were thrown in one big pot, including the problem of
conflicting names.


Well, it takes some time to get accustomed to it because it is so different,
but once done, you'll love it. Things are often much simpler to do (not
always, of course).


Armin
Armin;
Thank you for your time and efforts.
I will give it a try.
Hopefully I can get it to work.
As an aside; Who knew that Native was not as good as Managed.
Native sounds like it's built in, and Managed sounds like it run by a
committee. LOL
Marshall
 
Marshall,

I agree with Armin. I've been coding for some time but I just started
coding in .NET a couple of years ago. It takes about 3-6 months before you
"get it" (OOP) but once you do you don't want to go back. VB6 uses objects
it just conceals them from you and consequently you don't get the benefit of
being able to use them yourself for the most part.

The basic idea of "managed code" is that your code runs in it's own process
space and the framework "manages" it. The framework keeps an application
from interfering with other applications running on the system and keeps the
application from interfering with the system itself. There are a number of
advantages to "managed code", including cleaning up objects after they're no
longer needed, but the most significant is probably that in the even that
your program crashes the vast majority of the time it won't affect other
running applications or crash the system. If you use "unmanaged code" and
you make a simple programming error you can crash the entire system.

I'm sure that some of the other posters could give you a better synopsis of
the advantages but that should give you a good start.

I've read quite a few books on OOP and .NET and if you'd like to keep the
length of time it takes to "get it" down to 3 months get a copy of John
Connell's book Coding Techniques for Microsoft(r) Visual Basic(r) .NET.

Stan

--
Stan Smith
ADS Programming Services
Birmingham, AL
205-222-1661
(e-mail address removed)
 
Back
Top