使用CStatic显示图片(bmp、ico、png)

一、显示bmp及ico
 

//h文件

CStatic m_static;


//cpp文件

CBitmap bitmap;
bitmap.LoadBitmapW(IDB_BITMAP); //加载位图
BITMAP bmp;
bitmap.GetBitmap(&bmp); //获取位图参数
CRect rect(10,10,10+bmp.bmWidth,10+bmp.bmHeight); //显示区域
m_static.Create(L"",WS_CHILD | SS_BITMAP | SS_CENTERIMAGE,rect,this,12345); //创建static


//关联控件和位图,其中 IDB_BITMAP 是加载的位图资源ID号
m_static.SetBitmap(::LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_BITMAP)));

m_static.ShowWindow(true);

显示ICO和BMP是一个道理,函数改成LoadIcon 、SetIcon等就行了

二、路径动态加载方式显示PNG
 
 

//h文件

#include "atlimage.h"

CStatic m_static;


//cpp文件

CImage image;
image.Load(_T("res/node.png")); //以相对路径加载PNG图片
HBITMAP hBmp = image.Detach();


CRect rect(50,50,50+image.GetWidth(),50+image.GetHeight());
m_static.Create(L"", SS_BITMAP | SS_CENTERIMAGE,rect,this,12346);
m_static.SetBitmap(hBmp);
m_static.ShowWindow(true);


三、静态加载资源方式显示PNG
<wbr style='line-height:28px; color:rgb(55,42,24); font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(225,219,211)'><div style='line-height:28px; color:rgb(55,42,24); font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(225,219,211)'> <pre class="prettyprint" style="line-height:28px; white-space:pre-wrap; padding:2px; border:1px solid rgb(136,136,136)"><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span style="line-height:28px; white-space:pre"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span></span><span class="com" style="line-height:28px; color:rgb(136,0,0)">//h文件</span></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span class="com" style="line-height:28px; color:rgb(136,0,0)"> </span></span><span style="font-family:Hiragino Sans GB W3,Hiragino Sans GB,Arial,Helvetica,simsun,宋体; line-height:28px"><span class="com" style="line-height:28px; color:rgb(136,0,0)">#include "atlimage.h"</span></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span style="line-height:28px"><span class="com" style="line-height:28px; color:rgb(136,0,0)"> </span></span><span class="com" style="line-height:28px; color:rgb(136,0,0)">CStatic m_static;</span></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><br style="line-height:28px"></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span></span><span class="com" style="line-height:28px; color:rgb(136,0,0)">//cpp文件</span></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span style="line-height:28px"><span class="com" style="line-height:28px; color:rgb(136,0,0)"> </span></span><span class="com" style="line-height:28px; color:rgb(136,0,0)">CImage image; </span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">LoadImageFromIDResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(&amp;</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">image</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">IDB_PNG</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">_T</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="str" style="line-height:28px; color:rgb(0,136,0)">"PNG"</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">));</span><span style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span></span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">//自定义函数加载资源</span><br style="line-height:28px"><span style="line-height:28px"><span style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span></span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">HBITMAP hBmp </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> image</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">Detach</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">();</span></span></p><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">CRect</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> rect</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="lit" style="line-height:28px; color:rgb(0,102,102)">50</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="lit" style="line-height:28px; color:rgb(0,102,102)">50</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="lit" style="line-height:28px; color:rgb(0,102,102)">50</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">+</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">image</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GetWidth</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(),</span><span class="lit" style="line-height:28px; color:rgb(0,102,102)">50</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">+</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">image</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GetHeight</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">());</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span class="pln" style="line-height:28px; color:rgb(0,0,0)">m_static</span></span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">Create</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">L</span><span class="str" style="line-height:28px; color:rgb(0,136,0)">""</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> SS_BITMAP </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">|</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> SS_CENTERIMAGE</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">rect</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">this</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="lit" style="line-height:28px; color:rgb(0,102,102)">12346</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span class="pln" style="line-height:28px; color:rgb(0,0,0)">m_static</span></span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">SetBitmap</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hBmp</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span style='line-height:28px; font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,宋体'><span class="pln" style="line-height:28px; color:rgb(0,0,0)">m_static</span></span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">.</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">ShowWindow</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">true</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span></p></pre> </div> <div style='line-height:28px; color:rgb(55,42,24); font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(225,219,211)'> 其中<span style="line-height:28px; font-family:monospace; white-space:pre">LoadImageFromIDResource实现函数如下:</span> </div> <div style='line-height:28px; color:rgb(55,42,24); font-family:"Hiragino Sans GB W3","Hiragino Sans GB",Arial,Helvetica,simsun,u5b8bu4f53; font-size:16px; background-color:rgb(225,219,211)'> <pre class="prettyprint" style="line-height:28px; white-space:pre-wrap; padding:2px; border:1px solid rgb(136,136,136)"><p style="line-height:28px; margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)">BOOL </span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">LoadImageFromIDResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">CImage</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">*</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">pImage</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> UINT nResID</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">LPCTSTR lpTyp</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)</span><br style="line-height:28px"><span class="pun" style="line-height:28px; color:rgb(102,102,0)">{</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> pImage </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">==</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> NULL</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">false</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">;</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> pImage</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">-&gt;</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">Destroy</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">();</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 查找资源</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> HRSRC hRsrc </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">FindResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">AfxGetResourceHandle</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(),</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> MAKEINTRESOURCE</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">nResID</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">),</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> lpTyp</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hRsrc </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">==</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> NULL</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">false</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">;</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 加载资源</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> HGLOBAL hImgData </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">LoadResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">AfxGetResourceHandle</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(),</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> hRsrc</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hImgData </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">==</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> NULL</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">{</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">FreeResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hImgData</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">false</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">;</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">}</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 锁定内存中的指定资源</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> LPVOID lpVoid </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">LockResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hImgData</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> LPSTREAM pStream </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> NULL</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">;</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> DWORD dwSize </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">SizeofResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">AfxGetResourceHandle</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(),</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> hRsrc</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> HGLOBAL hNew </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GlobalAlloc</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">GHND</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> dwSize</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> LPBYTE lpByte </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">LPBYTE</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GlobalLock</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hNew</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">memcpy</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">lpByte</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> lpVoid</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> dwSize</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 解除内存中的指定资源</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GlobalUnlock</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hNew</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 从指定内存创建流对象</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> HRESULT ht </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">CreateStreamOnHGlobal</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hNew</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> TRUE</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">&amp;</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">pStream</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">if</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> ht </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">!=</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> S_OK </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">)</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">{</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GlobalFree</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hNew</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">}</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">else</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">{</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 加载图片</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> pImage</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">-&gt;</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">Load</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">pStream</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">GlobalFree</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hNew</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">}</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="com" style="line-height:28px; color:rgb(136,0,0)">// 释放资源</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">::</span><span class="typ" style="line-height:28px; color:rgb(102,0,102)">FreeResource</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)">hImgData</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">);</span><br style="line-height:28px"><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">return</span><span class="pln" style="line-height:28px; color:rgb(0,0,0)"> </span><span class="kwd" style="line-height:28px; color:rgb(0,0,136)">true</span><span class="pun" style="line-height:28px; color:rgb(102,102,0)">;</span><br style="line-height:28px"><span class="pun" style="line-height:28px; color:rgb(102,102,0)">}</span></p><div><span class="pun" style="line-height:28px; color:rgb(102,102,0)"> </span></div></pre> </div> </wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在MFC应用程序中的CStatic控件上显示BMP图像,可以按照以下步骤操作: 1. 首先,使用LoadBitmap函数加载BMP图像文件。例如,下面的代码将加载名为“myimage.bmp”的图像文件。 ```C++ CBitmap bmp; bmp.LoadBitmap(IDB_MYIMAGE); ``` 其中,IDB_MYIMAGE是资源文件中BMP图像的ID。 2. 创建CDC对象并将其与CStatic控件关联。可以使用以下代码创建CDC对象并从CStatic控件获取设备上下文。 ```C++ CDC* pDC = m_pMyStatic->GetDC(); ``` 其中,m_pMyStatic是指向CStatic控件的指针。 3. 创建CDC对象后,可以使用CDC的BitBlt函数将图像绘制到设备上下文中。例如,下面的代码将图像绘制到CStatic控件中。 ```C++ CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = memDC.SelectObject(&bmp); BITMAP bm; bmp.GetBitmap(&bm); pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0, SRCCOPY); memDC.SelectObject(pOldBitmap); ``` 4. 最后,释放CDC对象并释放资源。 ```C++ ReleaseDC(pDC); bmp.DeleteObject(); ``` 完整的示例代码如下所示: ```C++ // Load BMP image CBitmap bmp; bmp.LoadBitmap(IDB_MYIMAGE); // Get device context of CStatic control CDC* pDC = m_pMyStatic->GetDC(); // Draw image to device context CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap* pOldBitmap = memDC.SelectObject(&bmp); BITMAP bm; bmp.GetBitmap(&bm); pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &memDC, 0, 0, SRCCOPY); memDC.SelectObject(pOldBitmap); // Release resources ReleaseDC(pDC); bmp.DeleteObject(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值