在VC下使用资源通常都是先在resource.h中定义个整数比如:
# IDI_LIGHTNING_R 200 // 图标
然后在resource.rc中定义这个图标:
IDI_LIGHTNING_R ICON "icons\\lightning_r.ico"
读取图标时候则用:
::LoadIcon(h, MAKEINTRESOURCE(IDI_LIGHTNING_R));
这样形式用wxWidgets也想当然地这样做了结果用
pMainWnd->SetIcon(wxICON(IDI_LIGHTNING_R));
无论如何不起作用
看了下wxWidgetes代码:
# wxICON(X) wxIcon(wxT(#X))
直接将IDI_LIGHTNING_R转换成了个串wxIcon构造
wxIcon::wxIcon(const wxString& iconfile,
long flags,
desiredWidth,
desiredHeight)
{
LoadFile(iconfile, flags, desiredWidth, desiredHeight);
}
往下看LoadFile:
bool wxIcon::LoadFile(const wxString& filename,
long type,
desiredWidth, desiredHeight)
{
UnRef;
wxGDIImageHandler *handler = FindHandler(type);
( !handler )
{
// load via wxBitmap which, in turn, uses wxImage allowing us to
// support more formats
wxBitmap bmp;
( !bmp.LoadFile(filename, type) )
false;
CopyFromBitmap(bmp);
true;
}
handler->Load(this, filename, type, desiredWidth, desiredHeight);
}
嗯查找读取图标Handler然后用它来完成实际操作图标Handler由wxICOResourceHandler这个类来完成看其Load思路方法:
virtual bool Load(wxGDIImage *image,
const wxString& name,
long flags,
desiredWidth, desiredHeight)
{
wxIcon *icon = wxDynamicCast(image, wxIcon);
wxCHECK_MSG( icon, false, _T("wxIconHandler _disibledevent=>
wxASSERT_MSG( !hasSize || (desiredWidth != -1 && desiredHeight != -1),
_T("width and height should be either both -1 or not") );
// try to load the icon from this program first to allow overriding the
// standard icons (although why _disibledevent=>
desiredWidth, desiredHeight,
LR_DEFAULTCOLOR);
}
{
hicon = ::LoadIcon(wxGetInstance, name);
}
…………………..
wxSize size = wxGetHiconSize(hicon);
icon->SetSize(size.x, size.y);
icon->SetHICON((WXHICON)hicon);
icon->Ok;
}
最终读取操作同样将由LoadIcon这个windows api完成它必须接受个串做为参数在wxWidgets中从开始传递进来就是”IDI_LIGHTNING_R”这样个串它并没有使用MAKEINTRESOURCE转换得到串因而自然无法正常读取资源
比较wxWidgets自己resource.rc文件它并没有使用resource.h自然也没有将这个资源标志定义为整数呵呵原来如此
在resource.rc中修改图标定义:
lightning_r ICON "icons\\lightning_r.ico"
这里lightning_r是个未定义符号这样vc资源编译将把它做为个串处理并以此为这个图标标识最后读取图标时候用:
pMainWnd->SetIcon(wxICON(lightning_r));
切OK!
最新评论