If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 5:07 am
I am on Windows using MSVC++14.0 with wxWidgets 3.1.
What I am currently doing is loading PNG files from a path:
Code: Select all
wxBitmap BitmapSettings("D:\\Settings.png", wxBITMAP_TYPE_PNG);
And setting them on a wxButton and achieving transparency:
Code: Select all
m_settings->SetBitmap(BitmapSettings);
// For transparency
m_settings->SetBackgroundColour(GetBackgroundColour());
So, I want to load PNG from a resource instead of a path. As I recall some years ago we had no support for loading a png file from resource file. Is it possible to easily do this now?
doublemax
Moderator
Posts: 19160 Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2
Post
by doublemax » Tue Sep 11, 2018 5:17 am
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 5:37 am
Hi, I tried:
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("#107"));
Where "#107" is resource ID of PNG.
Is it the same way I load Icons i.e. wxIcon("#101") ?
doublemax
Moderator
Posts: 19160 Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2
Post
by doublemax » Tue Sep 11, 2018 6:57 am
I've never used these macros. Try without the #.
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 7:14 am
I think I tried everything but same error:
This is resource.h file
#define IDI_ICON1 102
#define IDB_PNG1 107
#define IDB_PNG2 108
#define IDB_PNG3 109
This is .rc file
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON "icon1.ico"
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//
IDB_PNG1 PNG "Settings1.png"
IDB_PNG2 PNG "Settings2.png"
IDB_PNG3 PNG "Settings3.png"
So I tried the following:
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("#107"));
or,
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("107"));
or,
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("Settings1.png"));
or,
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("Settings1"));
or,
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG("IDB_PNG1"));
or,
Code: Select all
wxBitmap BitmapSettings(wxBITMAP_PNG(IDB_PNG1));
None of these work and I get this error:
error.png (5.88 KiB) Viewed 5027 times
Insanity.
doublemax
Moderator
Posts: 19160 Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2
Post
by doublemax » Tue Sep 11, 2018 7:47 am
This worked for me:
Code: Select all
// .rc
SOMEPNG RCDATA "toucan.png"
// code
::wxInitAllImageHandlers();
wxBitmap bmp("SOMEPNG", wxBITMAP_TYPE_PNG_RESOURCE);
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 8:04 am
Yes, I also changed "PNG" to "RCDATA":
Code: Select all
IDB_PNG1 RCDATA "Settings1.png"
IDB_PNG2 RCDATA "Settings2.png"
IDB_PNG3 RCDATA "Settings3.png"
and:
Code: Select all
wxInitAllImageHandlers();
wxBitmap BitmapSettings("IDB_PNG1", wxBITMAP_TYPE_PNG_RESOURCE);
I rebuilt the project and it still won't work and ends up with same error.
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 8:07 am
Interestingly, it works only if I add the number ID with '#' with "RCDATA" string.
PB
Part Of The Furniture
Posts: 4204 Joined: Sun Jan 03, 2010 5:45 pm
Post
by PB » Tue Sep 11, 2018 8:18 am
Are you sure it is interesting and not by design, i.e., the difference between ordinal and string resource identifiers?
purplex88
Filthy Rich wx Solver
Posts: 247 Joined: Mon Feb 24, 2014 3:14 pm
Post
by purplex88 » Tue Sep 11, 2018 8:31 am
I am not sure why the method specified by doublemax won't work. I just haven't seen any tutorial where they use # with number identifier from resource.h.
Edit: Okay, I figured that out as well. Doublemax's method will work if I delete the IDs from resource.h file.
Asimov
Earned some good credits
Posts: 146 Joined: Tue Nov 17, 2020 6:43 pm
Post
by Asimov » Sun Dec 13, 2020 12:44 am
Hey I just got my icons to load from a resource file in Codeblocks using the same technique and I haven't even got a resource.h file.
I converted all my pngs to bin using Resource Hacker, saved them out and then just added the following lines to my resource.rc file.
Code: Select all
aaaa ICON "wx/msw/std.ico"
#include "wx/msw/wx.rc"
ARROW-DOWN RCDATA "rc/ARROW-DOWN.bin"
ARROW-UP RCDATA "rc/ARROW-UP.bin"
CREATE RCDATA "rc/CREATE.bin"
DELETE RCDATA "rc/DELETE.bin"
EDIT RCDATA "rc/EDIT.bin"
Now all I gotta do is work out how to load a font from the resource file LOL.
regards
Asimov