分享
 
 
 

Getting Toolbars in 256 Colors(翻译)

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Q

I have a problem I cannot find the answer for. Please tell me why the MFC toolbars and views (CListView, for example) do not display the 256-color icons and bitmaps I create in my resources. Instead they are all displayed as 16-color icons or bitmaps. Many Windows-based programs including Explorer, Microsoft® Internet Explorer, and others display full-color toolbars and icons. Please help!

Many Readers

有个问题,俺没找到答案。请告诉俺,怎样在俺的工具栏还有“污油”(譬如“李思特污油”中显示256克拉的图标和位图。很多程序,像是IE等都显示了“蛮色”的工具栏和图示,神啊,救救救我吧!

A

Kind of makes you feel like a second-class citizen, doesn't it? All those other apps have pretty multihued buttons, while your toolbars have a crude, color- challenged toy look. Never fear, it's easy to turn up the volume on your toolbar color dial.

Toolbars and list views both store their images in image lists. An image list is just what it sounds like: a list of images. Only the "list" isn't a linked list, it's a long bitmap that holds all the images, one after another. Say you have seven 20×20 icons. The image list holds them as one 140×20 bitmap (7×20 = 140). The bitmap can have as many color planes as you want; but you have to tell the image list how many colors to use when you create it. By default, you get 16. And when MFC loads your toolbar bitmap, it uses an internal function, AfxLoadSysColorBitmap, that presumes 16 colors. To get around MFC, you have to replace the bitmap; to do that, you have to get at the image list.

你是不是觉得有点像二等公民?哈哈,其他的程序都有好看的多彩的按钮,而你的工具栏却长的像是粗糟的,颜色有限的儿童玩具。别担心,提高你工具栏的颜色很容易。

工具栏和列表视图的图片都存在图像链表中。一条图像链表听起来好像:一个图像的列表。但是链表并不是相互连接的列表,它是一个长形的保存所有图片的位图,一个接一个。比如说你有七个20X20 的图标。图像链表就是一个140X20的位图。位图当然像你像的那样有很多颜色;但你得告诉图像链表你生成它的时候用了多少种颜色。默认情况下,你只会得到个16色的。还有当MFC载入你的工具栏位图之时,它用了一个内部的函数AfxLoadSysColorBitmap,并假定了16种颜色。要绕过MFC,你得换掉那个位图;要换掉那个位图,你得在图像链表中得到它。

Figure 5 Pretty Toolbar

I wrote a little app, TBColor, that shows how. TBColor is a plain, normal, ordinary, boring—did I say pedestrian?—MFC application with a pretty toolbar. Figure 5 tells the story. The buttons don't do much: any one you press brings up the About dialog. But how do the buttons get 256 colors? It all happens in CMainFrame::OnCreate (see Figure 6).

我写了一个小应用,TBColor,来显示如何做到这一切。TBColor是一个平常的,正常的,惯常的,无聊的,我说呆板了吗?带着一个漂亮工具栏的MFC应用程序。图五说明了这件事。按钮只会弹出个About对话。但是按钮是如何具有二五六中颜色的呢?故事发生在CMainFrame::OnCreate中(看图六)。

Figure 7 The Bitmap

The first step is to make sure you load your toolbar bitmap using ::LoadImage. This handy multipurpose function can load cursors, icons, and bitmaps. In the case of bitmaps, it will even (if you ask nicely) map certain colors in your bitmap to the current screen colors. For example, TBColor's toolbar bitmap (see Figure 7) uses RGB(192,192,192) for the background; LoadImage with LR_LOADMAP3DCOLORS maps this to whatever the actual 3D face color (GetSysColor(COLOR_3DFACE)) happens to be on your system.

第一步确信你用::LoadImage载入了toobbar。这个便捷的多用途函数能载入图标,鼠标和位图。对于位图来说哦,它甚至能将(如果你细问)当前的你的位图的颜色映射到当前屏幕的颜色。例如,TBColor's 工具栏位图用RGB(192,192,192)作为背景;在你的系统中LoadImage用LR_LOADMAP3DCOLORS把这映射到无论哪种真正的3D界面颜色(GetSysColor(COLOR_3DFACE))。

HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),

MAKEINTRESOURCE(IDR_MAINFRAME),

IMAGE_BITMAP,

0,0, // cx, cy

LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS );

LoadImage returns a handle to the bitmap (HBITMAP), but MFC likes CBitmaps better, so the next thing is to create one and attach your handle to it.

LoadImage返回一个指向那个位图句柄,但MFC更喜欢CBitmaps,因此下一件事就是要生成一个CBitmap对象并把它挂接到你的句柄上。

CBitmap bm;

bm.Attach(hbm);

Now that you have a CBitmap, you can create a new image list from it.

现在有了一个CBitmap,你能够用它产生一个新的图像链表。

m_ilToolBar.Create(20,20,ILC_COLOR8,4,4);

m_ilToolBar.Add(&bm,(CBitmap*)NULL);

m_ilToolBar is a CMainFrame member and ILC_COLOR8 is the magic flag that says: create an 8-bit paletted bitmap—that is, one with 256 colors. You can even create a bitmap with 24-bit color if you like—but keep in mind LR_LOADMAP3DCOLORS only works with paletted bitmaps, a little secret mommy never told you. In any case, once you've made your image list your way, the final step is to serve it to the toolbar.

M_ilToolBar是一个CMainFrame成员,还有ILC_COLOR8是系统的识别标志:那就是产生一个8位的调色板,拥有256中颜色。你也能够生成一个24位的位图如果你喜欢记住LR_LOADMAP3DCOLORS只和调色板位图一起工作,一旦你做完了图像链表,最后的步骤就是让它为工具栏服务。

m_wndToolBar.GetToolBarCtrl().

SetImageList(&m_ilToolBar);

Pretty simple, once you know the voodoo. I never actually tried it, but the same technique should work with list views and rebars and any other control that uses image lists. Now, go forth and unleash your inner Kandinsky!

小case,只要你知道那个魔法。我从未试验过,但这样的技术应该能够运用在列表视图和rebar还有其他的控件上,现在往前冲吧,释放你内心的Kandinsky(好像是个作画的俄罗斯司机)!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有