Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

set (change) an icon in a static (picture) control

362 views
Skip to first unread message

Thomas Steinbach

unread,
Jan 29, 2009, 10:38:15 PM1/29/09
to
Hello NG,

I use

---snip---
hIcon = (HICON)LoadImage(hInst, "myicon.ico", IMAGE_ICON,
0, 0, LR_LOADFROMFILE);
---snap---

and I can set this with WM_SETICON to a window, etc.
All is working fine, but:

How can I set (change) this Icon in a
static (picture) control at runtime?

The Icon is defined in a rc file like:

---snip---
ICON "",IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE
---snap---
or at a first inital with an icon from a rc file, like:
---snip---
IDI_ICON_START ICON "mystart.ico"
ICON IDI_ICON_START,IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE
---snap---

Thomas

Christian ASTOR

unread,
Jan 30, 2009, 2:45:42 AM1/30/09
to
Thomas Steinbach wrote:

> How can I set (change) this Icon in a
> static (picture) control at runtime?

STM_SETICON
(or STM_SETIMAGE)

Thomas Steinbach

unread,
Jan 30, 2009, 4:39:04 PM1/30/09
to
Hello Christian,

>> How can I set (change) this Icon in a
>> static (picture) control at runtime?
>
> STM_SETICON
> (or STM_SETIMAGE)

Yes, you're right. Thank you for that tip.
It's working fine with VS 2008 included compiler.
But I have a problem to use this STM message with
MinGW :-(

Do you know if there is a problem to use this
Message with mingw? The Icon won't load. :-(

Description:

hIcon1 = (HICON)LoadImage(GetModuleHandle(0), "myicon.ico", IMAGE_ICON, 0,
0, LR_LOADFROMFILE);
http://msdn.microsoft.com/en-us/library/ms648045(VS.85).aspx

I can't set this Icon with mingw to a static (icon) control with

SendDlgItemMessage(hWnd, IDC_STATIC_ICON, STM_SETICON,
(WPARAM)(HICON)hIcon1, 0);
http://msdn.microsoft.com/en-us/library/cc656592(VS.85).aspx

The control is defined like:

ICON "",IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE

Why? And what do I have to do to get this working?
With VS2008 (SP1) it's working


And this doesn't work no longer: :-(

IDI_ICON_APP ICON "myicon.ico"

ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE

the Icon is not visible after compiling, but the file is accessable
and the hIcon2 (see below) are valid and can be set with WM_SETICON
to the windows small icon if I compile with mingw and do this with:

hIcon2 = LoadIcon(GetModuleHandle(0),
(LPCTSTR)MAKEINTRESOURCE(IDI_ICON_APP));
SendMessage(hWnd, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon2);

http://msdn.microsoft.com/en-us/library/ms648072(VS.85).aspx


It's Fat32 patition where the exe file is running form and
where the sources resides. It shouldn't be a restriction/right
problem.

With the ms compiler of VS2008 it's working fine, but not
with gcc of mingw :-(

Why? And how can I get this running?

The Icons have three icons 1.) 48x48, 2.) 32x32 and 3.) 16x16
all with 256 colors

Can the size of the first Icon (48x48) be a problem?
But it was working before I tried to set an another Icon
which I load from a file... :-(

Hope that somebody can help me or have an idea what is the
problem

Thomas

Norman Bullen

unread,
Jan 30, 2009, 7:41:28 PM1/30/09
to
See answers in line below.

Thomas Steinbach wrote:
> Hello Christian,
>
>>> How can I set (change) this Icon in a
>>> static (picture) control at runtime?
>>
>>
>> STM_SETICON
>> (or STM_SETIMAGE)
>
>
> Yes, you're right. Thank you for that tip.
> It's working fine with VS 2008 included compiler.
> But I have a problem to use this STM message with
> MinGW :-(
>
> Do you know if there is a problem to use this
> Message with mingw? The Icon won't load. :-(
>
> Description:
>
> hIcon1 = (HICON)LoadImage(GetModuleHandle(0), "myicon.ico", IMAGE_ICON, 0,
> 0, LR_LOADFROMFILE);

This loads an icon from a file which must be in the working directory of
the application. The relationship between the application's working
directory and the directory into which the compiler builds the .EXE file
may be different between MSVC and mingw.

Add some code to check the value of hIcon1 after the function returns;
I'll bet it is NULL when you're running the version compiled by mingw.

Add some code to display the working directory path (see
GetCurrentDirectory() ) and compare the results with the MSVC version
and the mingw version.


>
> I can't set this Icon with mingw to a static (icon) control with
>
> SendDlgItemMessage(hWnd, IDC_STATIC_ICON, STM_SETICON,
> (WPARAM)(HICON)hIcon1, 0);

If LoadImage() failed (see above) you can't expect this to work.


>
> The control is defined like:
>
> ICON "",IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE
>
> Why? And what do I have to do to get this working?
> With VS2008 (SP1) it's working
>
>
> And this doesn't work no longer: :-(
>
> IDI_ICON_APP ICON "myicon.ico"
>
> ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE
>
> the Icon is not visible after compiling, but the file is accessable
> and the hIcon2 (see below) are valid and can be set with WM_SETICON
> to the windows small icon if I compile with mingw and do this with:

The above ought to work (perhaps you need to remove the extra space
inside the quotes) if IDI_ICON_APP is NOT defined in your resource.h file.


>
> hIcon2 = LoadIcon(GetModuleHandle(0),
> (LPCTSTR)MAKEINTRESOURCE(IDI_ICON_APP));
> SendMessage(hWnd, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon2);
>

But when you use MAKEINTRESOURCE() like this it implies that
IDI_ICON_APP must be defined in your resource.h file. Which one is it?
You need to be consistent.


>
>
> It's Fat32 patition where the exe file is running form and
> where the sources resides. It shouldn't be a restriction/right
> problem.
>
> With the ms compiler of VS2008 it's working fine, but not
> with gcc of mingw :-(
>
> Why? And how can I get this running?
>
> The Icons have three icons 1.) 48x48, 2.) 32x32 and 3.) 16x16
> all with 256 colors
>
> Can the size of the first Icon (48x48) be a problem?
> But it was working before I tried to set an another Icon
> which I load from a file... :-(
>
> Hope that somebody can help me or have an idea what is the
> problem
>
> Thomas


--
Norm

To reply, change domain to an adult feline.

Thomas Steinbach

unread,
Jan 31, 2009, 1:46:29 PM1/31/09
to
Hello Norman,

>> [...]


>> Do you know if there is a problem to use this
>> Message with mingw? The Icon won't load. :-(
>>
>> Description:
>>
>> hIcon1 = (HICON)LoadImage(GetModuleHandle(0), "myicon.ico", IMAGE_ICON,
>> 0,
>> 0, LR_LOADFROMFILE);
>
> This loads an icon from a file which must be in the working directory of
> the application. The relationship between the application's working
> directory and the directory into which the compiler builds the .EXE file
> may be different between MSVC and mingw.

Yes, I load the file with the path given by GetFullPathName()

> Add some code to check the value of hIcon1 after the function returns;
> I'll bet it is NULL when you're running the version compiled by mingw.
>
> Add some code to display the working directory path (see
> GetCurrentDirectory() ) and compare the results with the MSVC version and
> the mingw version.

I have done all this checking code, but still can't
find the error or what is the problem.
I reduced the code to a small sample project and it can be
downloaded at:

http://www.failure.bravehost.com/prog/win32/loadicon/

or direct link:

http://www.failure.bravehost.com/prog/win32/loadicon/sample02.zip


It is a vs2008 solution, with a vcproj file in os\win32
and a batch to compile with mingw. Just fix the paths.
Please don't blame me that it's not good coded, it's just
an example. exe files are included too. And
there is definitely no virus inside.

I hope that somebody can find the problem. And as you can see
the compiled mingw build (in bin) doesn't load the icon from
resource and doesn't load from file...

>> [...]


> The above ought to work (perhaps you need to remove the extra space inside
> the quotes) if IDI_ICON_APP is NOT defined in your resource.h file.

What "extra spaces" do you mean?

>> [...]


> But when you use MAKEINTRESOURCE() like this it implies that IDI_ICON_APP
> must be defined in your resource.h file. Which one is it? You need to be
> consistent.

Yes, it is defined. See example project.


Thomas

Fred

unread,
Jan 31, 2009, 3:08:30 PM1/31/09
to
"Thomas Steinbach" <stei...@gmx-topmail.de> wrote in message
news:gm26eq$rjc$01$1...@news.t-online.com...

> I reduced the code to a small sample project and it can be
> downloaded at:
>
> http://www.failure.bravehost.com/prog/win32/loadicon/
>
> or direct link:
>
> http://www.failure.bravehost.com/prog/win32/loadicon/sample02.zip
>

Have you tried removing your manifest ?
(the excutable doesn't work on a PC where MSVC90 is not present...)


Thomas Steinbach

unread,
Jan 31, 2009, 4:52:15 PM1/31/09
to
Hello Fred,

>> I reduced the code to a small sample project and it can be
>> downloaded at:
>>
>> http://www.failure.bravehost.com/prog/win32/loadicon/
>>
>> or direct link:
>>
>> http://www.failure.bravehost.com/prog/win32/loadicon/sample02.zip
>>
>
> Have you tried removing your manifest ?
> (the excutable doesn't work on a PC where MSVC90 is not present...)

I tried to remove the manifest, but the problem still exist.
And it wouldn't be a good idea, because the final exe wouldn't
use the visual styles. But it seems that this is not the problem.
Do you have tested the MinGW version and tested to compile
the MinGW version by yourself? What was the behavior of
the executables?

Thomas


PS: If anybody need the runtimes for builds of VS 2008 (sp1)
you can download them at:

VS2008 SP1 x86

http://www.microsoft.com/downloads/details.aspx?familyid=A5C84275-3B97-4AB7-A40D-3802B2AF5FC2&displaylang=en


VS2008 x86

http://www.microsoft.com/downloads/details.aspx?FamilyID=9b2da534-3e03-4391-8a4d-074b9f2bc1bf&displaylang=en


amd64 and x64 binaries are also available...

Norman Bullen

unread,
Jan 31, 2009, 7:32:05 PM1/31/09
to
Again, see below.

Thomas Steinbach wrote:
> Hello Norman,
>
>>> [...]
>>> Do you know if there is a problem to use this
>>> Message with mingw? The Icon won't load. :-(
>>>
>>> Description:
>>>
>>> hIcon1 = (HICON)LoadImage(GetModuleHandle(0), "myicon.ico", IMAGE_ICON,
>>> 0,
>>> 0, LR_LOADFROMFILE);
>>
>>
>> This loads an icon from a file which must be in the working directory of
>> the application. The relationship between the application's working
>> directory and the directory into which the compiler builds the .EXE file
>> may be different between MSVC and mingw.
>
>
> Yes, I load the file with the path given by GetFullPathName()

GetFullPathName() is unnecessary. LoadImage() can load an image from a
file that is named with a partial or no path, as long as the partial
path to the file exists.

Did GetFullPathName() return a valid path to the icon file in those
cases where the icon failed to appear? Did you check?


>
>> Add some code to check the value of hIcon1 after the function returns;
>> I'll bet it is NULL when you're running the version compiled by mingw.
>>
>> Add some code to display the working directory path (see
>> GetCurrentDirectory() ) and compare the results with the MSVC version and
>> the mingw version.

And what did you find when you checked the value of hIcon1? And the
value path returned by GetCurrentDirectory()? Was hIcon1 non-NULL? Did
GetCurrentDirectory() give you a path to the directory containing your
icon file?


>
>
> I have done all this checking code, but still can't
> find the error or what is the problem.
> I reduced the code to a small sample project and it can be
> downloaded at:
>
> http://www.failure.bravehost.com/prog/win32/loadicon/
>
> or direct link:
>
> http://www.failure.bravehost.com/prog/win32/loadicon/sample02.zip
>
>
> It is a vs2008 solution, with a vcproj file in os\win32
> and a batch to compile with mingw. Just fix the paths.
> Please don't blame me that it's not good coded, it's just
> an example. exe files are included too. And
> there is definitely no virus inside.
>
> I hope that somebody can find the problem. And as you can see
> the compiled mingw build (in bin) doesn't load the icon from
> resource and doesn't load from file...
>
>>> [...]
>>
>> The above ought to work (perhaps you need to remove the extra space
>> inside
>> the quotes) if IDI_ICON_APP is NOT defined in your resource.h file.
>
>
> What "extra spaces" do you mean?

Extra "space" - just one. Your posting on 1/30/09 contained this line:
ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE
Do you see the space inside the quotes? It's possible that it works with
a space at the end of the string; it's also possible that the space
causes the failure that you are experiencing.


>
>>> [...]
>>
>> But when you use MAKEINTRESOURCE() like this it implies that IDI_ICON_APP
>> must be defined in your resource.h file. Which one is it? You need to be
>> consistent.
>
>
> Yes, it is defined. See example project.
>

You can't have it both ways. If IDI_ICON_APP is defined (by virtue of
being defined in resource.h, the statement
IDI_ICON_APP ICON "myicon.ico"
creates a NUMBERED resource; it has a number instead of a name. This
statement
ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE
is probably looking for a NAMED resource which it fails to find. And
this statement loads a NUMBERED resource.
hIcon2 = LoadIcon(GetModuleHandle(0),
(LPCTSTR)MAKEINTRESOURCE(IDI_ICON_APP));
(By the way, you don't need "(LPCTSTR)". MAKEINTRESOURCE() does that for
you.

Thomas Steinbach

unread,
Jan 31, 2009, 10:15:44 PM1/31/09
to
Hello Norman,

> Again, see below.
;-)

> [...]


>>>> hIcon1 = (HICON)LoadImage(GetModuleHandle(0), "myicon.ico", IMAGE_ICON,
>>>> 0,
>>>> 0, LR_LOADFROMFILE);
>>>
>>> This loads an icon from a file which must be in the working directory of
>>> the application. The relationship between the application's working
>>> directory and the directory into which the compiler builds the .EXE file
>>> may be different between MSVC and mingw.
>>
>> Yes, I load the file with the path given by GetFullPathName()
>
> GetFullPathName() is unnecessary. LoadImage() can load an image from a
> file that is named with a partial or no path, as long as the partial path
> to the file exists.

That is not the point. I want to say. "The file ist accessable
and at the right place/dir on the disk"

> Did GetFullPathName() return a valid path to the icon file in those cases
> where the icon failed to appear? Did you check?

yes, please see the sample project.

>> [...]


> And what did you find when you checked the value of hIcon1? And the value
> path returned by GetCurrentDirectory()? Was hIcon1 non-NULL? Did
> GetCurrentDirectory() give you a path to the directory containing your
> icon file?

An integer (long) which schould represent the HICON
and definitely non-NULL

GetCurrentDirectory give me the path to the dir containing
my icon file. Please have a look at the sample project.

>> I have done all this checking code, but still can't
>> find the error or what is the problem.
>> I reduced the code to a small sample project and it can be
>> downloaded at:
>>
>> http://www.failure.bravehost.com/prog/win32/loadicon/
>>
>> or direct link:
>>
>> http://www.failure.bravehost.com/prog/win32/loadicon/sample02.zip
>>

> [...]

>>> The above ought to work (perhaps you need to remove the extra space
>>> inside
>>> the quotes) if IDI_ICON_APP is NOT defined in your resource.h file.
>>
>> What "extra spaces" do you mean?
>
> Extra "space" - just one. Your posting on 1/30/09 contained this line:
> ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE
> Do you see the space inside the quotes? It's possible that it works with

ok, that wasn't me intention. Must came from the
copy & paste action and in case of edit...

> a space at the end of the string; it's also possible that the space causes
> the failure that you are experiencing.

Please have a look at the sample project. You will
not found any spaces

>>>> [...]
>>> But when you use MAKEINTRESOURCE() like this it implies that
>>> IDI_ICON_APP
>>> must be defined in your resource.h file. Which one is it? You need to be
>>> consistent.

it is defined and available.
BOTH. ONE is compiled in as a resource.
and ONE is loaded from the disk.

> You can't have it both ways. If IDI_ICON_APP is defined (by virtue of
> being defined in resource.h, the statement
> IDI_ICON_APP ICON "myicon.ico"
> creates a NUMBERED resource; it has a number instead of a name. This
> statement
> ICON "IDI_ICON_APP ",IDC_STATIC,14,14,20,20,SS_CENTERIMAGE
> is probably looking for a NAMED resource which it fails to find. And this
> statement loads a NUMBERED resource.

Yes I know. Think you didn't understand me. There are two
_DIFFRENT_ icons. One is loaded from the compiled in rsource
and the other is loaded by filename from the disk.

Please take a look at the sample project and you
will understand me and my intention.


Thomas

Christian ASTOR

unread,
Feb 1, 2009, 6:32:39 AM2/1/09
to
Thomas Steinbach wrote:

> The Icon is defined in a rc file like:
>
> ---snip---
> ICON "",IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE

Try removing SS_CENTERIMAGE...

Thomas Steinbach

unread,
Feb 1, 2009, 10:10:30 AM2/1/09
to
Hello Christian,

>> The Icon is defined in a rc file like:
>>
>> ---snip---
>> ICON "",IDC_STATIC_ICON,14,14,20,20,SS_CENTERIMAGE
>
> Try removing SS_CENTERIMAGE...

Yes, that works. Strange. I never expected that.
Thanks for that tip. Now it's working great...

Thomas

0 new messages