这是一个加载文件夹图片略缩图的控件,支持多种图片格式~~用法也比较简单
(1)、源代码

//头文件ListImageCtrl.h

#pragma once
#include <vector>
//note:need GDI+
// ListImageCtrl.h : header file
class CListImageCtrl : public CListCtrl
{
// Construction
public:
CListImageCtrl();
// Attributes
public:
void CreateColumn();
//
BOOL  GetImageFileNames();// gather the image file names
void  DrawThumbnails();// draw the thumbnails in list control
void  Load();         //start load files
void Clear();   //clear list
// Operations
public:
CStringm_strImageDir;
CImageListm_ImageListThumb;// image list holding the thumbnails
std::vector<CString> m_VectorImageNames;// vector holding the image names
intm_nSelectedItem;
BOOL  m_bHorz;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CListImageCtrl)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CListImageCtrl();
// Generated message map functions
protected:
//{{AFX_MSG(CListImageCtrl)
afx_msg void OnDropFiles(HDROP hDropInfo);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
// ListImageCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "Medical.h"
#include "ListImageCtrl.h"
#define THUMBNAIL_WIDTH  90
#define THUMBNAIL_HEIGHT 90
void DoEvents(void);
/
// CListImageCtrl
CListImageCtrl::CListImageCtrl()
{
m_strImageDir = _T("");
m_bHorz = FALSE;
}
CListImageCtrl::~CListImageCtrl()
{
}
BEGIN_MESSAGE_MAP(CListImageCtrl, CListCtrl)
//{{AFX_MSG_MAP(CListImageCtrl)
ON_WM_DROPFILES()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CListImageCtrl message handlers
// This funtion is used to load the Window dropped files into the listview
void CListImageCtrl::OnDropFiles(HDROP hDropInfo) 
{
WORD wNumFilesDropped = DragQueryFile(hDropInfo, -1, NULL, 0);
CString firstFile(_T(""));
int kk=0;
int tTot=(int)wNumFilesDropped;
// show hour glass cursor
BeginWaitCursor();
for (WORD x = 0 ; x < wNumFilesDropped; x++) 
{
kk++;
// Get the number of bytes required by the file's full pathname
WORD wPathnameSize = DragQueryFile(hDropInfo, x, NULL, 0);
TRACE1("wPathnameSize=%d,\n",wPathnameSize);
// Allocate memory to contain full pathname & zero byte
wPathnameSize +=1;
TCHAR * npszFile = (TCHAR *) LocalAlloc(LPTR, sizeof(TCHAR)*wPathnameSize);  //注意分配的内存大小
// If not enough memory, skip this one
if (npszFile == NULL) continue;
DragQueryFile(hDropInfo, x, npszFile, wPathnameSize);
if (firstFile=="") 
firstFile=npszFile;
CString strExt;
CString nFileText;
CString pItemText=npszFile;
TRACE1("%s\n",pItemText);
int i=pItemText.ReverseFind('\\');
nFileText = pItemText.Mid(i+1); 
m_strImageDir = pItemText.Left(i+1);
strExt = pItemText.Right(3);
TRACE1("strExt=%s\n",strExt);
if ( (strExt.CompareNoCase( TEXT("bmp") ) == 0) ||
(strExt.CompareNoCase( TEXT("jpg") ) == 0) ||
(strExt.CompareNoCase( TEXT("gif") ) == 0) ||
(strExt.CompareNoCase( TEXT("tif") ) == 0) ||
(strExt.CompareNoCase( TEXT("png") ) == 0) )
{
// save the image file name, not the path
m_VectorImageNames.push_back(nFileText);  
}
// clean up
LocalFree(npszFile);
}
// Free the memory block containing the dropped-file information
DragFinish(hDropInfo);
if(!m_VectorImageNames.empty())
DrawThumbnails();
SetFocus();
SetItemState(0, LVIS_SELECTED |    LVS_ICON | LVS_AUTOARRANGE, LVIS_SELECTED | LVIS_FOCUSED | LVIS_ACTIVATING); 
EndWaitCursor();
CListCtrl::OnDropFiles(hDropInfo);
}
void CListImageCtrl::CreateColumn()
{
InsertColumn(0,_T("Filename"),LVCFMT_LEFT,125,-1);
InsertColumn(1,_T("Path"),LVCFMT_LEFT,125,-1);
InsertColumn(2,_T("Size"),LVCFMT_LEFT,75,-1);
HIMAGELIST hScreens = ImageList_Create(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, ILC_COLOR32 /*| ILC_MASK*/ , 0, 1);
m_ImageListThumb.Attach(hScreens);
m_nSelectedItem = 0;
// load the starting bitmap ("Loading..." and "Corrupt file")
//  CBitmap dummy;
//  dummy.LoadBitmap(IDB_BITMAP1);
//  m_ImageListThumb.Add(&dummy, RGB(0, 0, 0));
SetImageList(&m_ImageListThumb, LVSIL_NORMAL);
SetImageList(&m_ImageListThumb, LVSIL_SMALL);
}
// this function is used to enable the system messages
// this is mainly used to display the multiple images dropped on the list control
void DoEvents(void)
{
    MSG Symsg;
    
    while(PeekMessage(&Symsg,NULL,0,0,PM_REMOVE))
    {
    TranslateMessage(&Symsg);
    DispatchMessage(&Symsg);
    }
}
BOOL  CListImageCtrl::GetImageFileNames()
{
CStringstrExt;
CStringstrName;
CStringstrPattern;
BOOLbRC = TRUE;
HANDLEhFind = NULL;
WIN32_FIND_DATAFindFileData;
std::vector<CString>VectorImageNames;
if ( m_strImageDir[m_strImageDir.GetLength() - 1] == TCHAR('\\') )
strPattern.Format( TEXT("%s*.*"), m_strImageDir );
else
strPattern.Format( TEXT("%s\\*.*"), m_strImageDir );
hFind = ::FindFirstFile(strPattern, &FindFileData);// strat search
if (hFind == INVALID_HANDLE_VALUE)
{
LPVOID  msg;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
NULL, 
GetLastError(), 
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&msg, 
0, 
NULL);
MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP);
::LocalFree(msg);
return FALSE;
}
// filter off the system files and directories
if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)     &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)     &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY))
{    
// test file extension
strName = FindFileData.cFileName;
strExt = strName.Right(3);
if ( (strExt.CompareNoCase( TEXT("bmp") ) == 0) ||
(strExt.CompareNoCase( TEXT("jpg") ) == 0) ||
(strExt.CompareNoCase( TEXT("gif") ) == 0) ||
(strExt.CompareNoCase( TEXT("tif") ) == 0) ||
(strExt.CompareNoCase( TEXT("png") ) == 0) )
{
// save the image file name
VectorImageNames.push_back(strName);
}
}  
// loop through to add all of them to our vector
while (bRC)
{
bRC = ::FindNextFile(hFind, &FindFileData);
if (bRC)
{
// filter off the system files and directories
if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)     &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)     &&
!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY))
{
// test file extension
strName = FindFileData.cFileName;
strExt = strName.Right(3);
if ( (strExt.CompareNoCase( TEXT("bmp") ) == 0) ||
(strExt.CompareNoCase( TEXT("jpg") ) == 0) ||
(strExt.CompareNoCase( TEXT("gif") ) == 0) ||
(strExt.CompareNoCase( TEXT("tif") ) == 0) ||
(strExt.CompareNoCase( TEXT("png") ) == 0) )
{
// save the image file name
VectorImageNames.push_back(strName);
}
}
}  
else
{
DWORD err = ::GetLastError();
if (err !=  ERROR_NO_MORE_FILES)
{
LPVOID msg;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 
NULL, err, 
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&msg, 0, NULL);
MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP);
::LocalFree(msg);
::FindClose(hFind);
return FALSE;
}
}
} // end of while loop
// close the search handle
::FindClose(hFind);
// update the names, if any
if ( !VectorImageNames.empty() )
{
// reset the image name vector
m_VectorImageNames.clear();
m_VectorImageNames = VectorImageNames;
return TRUE;
}
return FALSE;
}
void  CListImageCtrl::DrawThumbnails()
{
CStringstrPath;
inti;
// no images
if (m_VectorImageNames.empty())
return;
// set the length of the space between thumbnails
// you can also calculate and set it based on the length of your list control
int nGap = 40;
// hold the window update to avoid flicking
SetRedraw(FALSE);
// reset our image list
for (i = 0; i < m_ImageListThumb.GetImageCount(); i++)
m_ImageListThumb.Remove(i);
// remove all items from list view
if (this->GetItemCount() != 0)
this->DeleteAllItems();
// set the size of the image list
m_ImageListThumb.SetImageCount(m_VectorImageNames.size());
i = 0;
// draw the thumbnails
std::vector<CString>::iterator  iter;
for (iter = m_VectorImageNames.begin(); iter != m_VectorImageNames.end(); iter++)
{
HBITMAP hbmReturn = NULL; 
Bitmap  *bmPhoto  = NULL;
CBitmap Bmp1;
// load the bitmap
strPath.Format( TEXT("%s\\%s"), m_strImageDir, *iter );
Bitmap img( strPath.AllocSysString() );
int sourceWidth  = img.GetWidth();
int sourceHeight = img.GetHeight();
int destX, destY, destWidth, destHeight;
const float fRatio=(float)THUMBNAIL_HEIGHT/THUMBNAIL_WIDTH;
const float fImgRatio=(float)sourceHeight/sourceWidth;
if(fImgRatio > fRatio)
{
destWidth=(THUMBNAIL_HEIGHT/fImgRatio);
destX=(THUMBNAIL_WIDTH-destWidth)/2;
destY=0;
destHeight=THUMBNAIL_HEIGHT;
}
else
{
destX=0;
destWidth=THUMBNAIL_WIDTH;
destHeight=(THUMBNAIL_WIDTH*fImgRatio);
destY=(THUMBNAIL_HEIGHT-destHeight)/2;
}
//check out very small image
if ((sourceHeight < THUMBNAIL_HEIGHT) && (sourceWidth < THUMBNAIL_WIDTH))
{
destWidth=sourceWidth;
destHeight=sourceHeight;
destX=(THUMBNAIL_WIDTH-destWidth)/2;
destY=(THUMBNAIL_HEIGHT-destHeight)/2;
}
bmPhoto = new Bitmap( THUMBNAIL_WIDTH, THUMBNAIL_WIDTH , PixelFormat32bppRGB );
bmPhoto->SetResolution( img.GetHorizontalResolution(), img.GetVerticalResolution() );
Graphics *grPhoto = Graphics::FromImage( bmPhoto );
Color colorW(255, 255, 255, 255);
Gdiplus::Pen pen(Color(200,192,192,192));
grPhoto->Clear( colorW );
grPhoto->SetInterpolationMode( InterpolationModeHighQualityBilinear );
grPhoto->DrawImage( &img, Rect(destX, destY, destWidth, destHeight) );
grPhoto->DrawRectangle(&pen,Gdiplus::Rect(0,0,THUMBNAIL_WIDTH-1,THUMBNAIL_HEIGHT-1));  //draw border
bmPhoto->GetHBITMAP( colorW, &hbmReturn );
Bmp1.Attach( hbmReturn );
m_ImageListThumb.Replace( i, &Bmp1, NULL );
//int imgP=m_ImageListThumb.Add(&Bmp1,RGB(0,0,0));
InsertItem(i, m_VectorImageNames[i],i);  //Link to the added listview item 
delete grPhoto;
delete bmPhoto;
Bmp1.Detach();
DeleteObject( hbmReturn );
i++;
}
// let's show the new thumbnails
SetRedraw(); 
}
void CListImageCtrl::Load() 
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
// validate image directory
if (m_strImageDir.IsEmpty())
{
MessageBox(CString((LPCSTR)IDS_DIR_ERROR), CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP);
return;
}
// show hour glass cursor
BeginWaitCursor();
// get the names of bitmap files
 if ( !GetImageFileNames() )
 {
 EndWaitCursor();
 return;
 }
// draw thumbnail images in list control
DrawThumbnails();
// draw the selected image in its full size
//DrawSelectedImage();
// if this was a shortcut, we need to expand it to the target path
SetItemState(0, LVIS_SELECTED |    LVS_ICON | LVS_AUTOARRANGE, LVIS_SELECTED | LVIS_FOCUSED | LVIS_ACTIVATING); 
SetFocus();
RedrawWindow(NULL,NULL);
EndWaitCursor();
}
void CListImageCtrl::Clear()
{
// hold the window update to avoid flicking
SetRedraw(FALSE);
// reset our image list
for (int i = 0; i < m_ImageListThumb.GetImageCount(); i++)
m_ImageListThumb.Remove(i);
// remove all items from list view
if (this->GetItemCount() != 0)
this->DeleteAllItems();
m_strImageDir = _T("");
m_VectorImageNames.clear();
SetRedraw(); 
}
(2)、用法
1、在界面上放一个ListCtrl控件,设置View属性为ICON,Accept files属性TRUE,然后关联一个变量,如CListImageCtrl  m_lstImg;
2、在 BOOL CXXXDlg::OnInitDialog()初始化控件,一个语句就可以了m_lstImg.CreateColumn();
3、加载略缩图方法可以拖拽图片到控件,也可以这样加载
                        //strPath 是一个CString变量,文件夹路径
m_lstImg.m_strImageDir = strPath;
m_lstImg.Load();

过年回家,走之前留一个用GDI+实现的略缩图控件相关推荐

  1. 一个很好用的gif动态图控件:GifImageView

    首先,导入依赖: implementation 'com.facebook.fresco:animated-gif:1.11.0' //gif依赖 然后就可以直接在布局中使用了, <pl.dro ...

  2. 发布一个用于WinCE的矢量图控件

    发布一个在wince操作系统下,采用.net compact framework 1.0 ( c#)开发的矢量图控件,我于2007年3月份集中一个月的经历完成了它.当然,它的前身是2005年12月我写 ...

  3. android tv 开发布局,Android TV开发总结(七)构建一个TV app中的剧集列表控件

    前言:剧集类控件,在TV app中非常常见,今天将介绍构建一个TV app中的剧集列表控件,此控件上传到我的Github:https://github.com/hejunlin2013/Episode ...

  4. 100% .NET Control_使用CurrencyManager 创建一个导航条来控制DatagGrid的XNavBar控件(VB.NET)....

    Begin MSDN: 要使 Windows 窗体数据绑定成为可能,必须有数据提供程序和使用者. 从提供程序这一方最容易接近 Windows 窗体数据绑定的结构. 绑定一个 Windows 窗体及其控 ...

  5. android 分组柱状图_整理了一个 android 上的波形图及柱状图绘制控件

    SimpleWaveform [说明:以前画过波形图,最近又需要画,略不同,但还得重复写.在网上搜了一下,只找到一个复杂的框架,而我们往往只需要画简单的波形或柱状图.所以我整理提取了过去的代码,有了这 ...

  6. 一个有意思的CStatic和combobox以及Cedit控件结合使用

    如图..源代码下载地址 http://download.csdn.net/detail/hemmingway/4187082 这是编辑字符串... 这是用组合框选择字符串.... 使用方法是托一个St ...

  7. 一个Demo学会用Android兼容包新控件

    2019独角兽企业重金招聘Python工程师标准>>> 前言 伟大的Google为Android推出了一系列的兼容包,最新的就是Design Support Library了,这里我 ...

  8. 一个简单的自定义多附件上传控件

    在网上提供的都是生成多个上传控件的代码,但大家可以看看21cn,163那里的多附件上传是不是比那些只是生成多个上传控件的界面美观很多呢??      现在就开始做一个类似21CN那样的上传的.先看看我 ...

  9. android组合控件的焦点,撸一个简单的TV版焦点控制的日历控件

    1.效果 最近需求要一个遥控控制的日历控件,找了半天没找到轮子,就自己撸一个,先看效果图: 效果图.gif 2.XML属性,所有属性默认为效果图 calender_textSize:星期和日期的字体大 ...

最新文章

  1. java培训教程分享:Java中怎样将数据对象序列化和反序列化?
  2. IE6、 IE7、IE8、Firefox兼容性问题
  3. python笔记基础-Python入门基础知识学习笔记之一
  4. Linux中增加软路由的两种方法,Linux中增加软路由的三种方法
  5. VC6.0显示代码行号
  6. Hive引擎改为Tez笔记
  7. 【招聘(深圳)】轻岁 诚聘.NET Core开发
  8. 非常漂亮的后台登录页面
  9. 官方下载weka,亲测可用!
  10. leetcode:买卖股票最佳时机含手续费
  11. Java编程基础之Set和Map的简单使用
  12. word/论文版本管理方案
  13. 《Android 开发入门与实战(第二版)》——6.10节本章小结
  14. HTML常用标签的基本介绍
  15. 夜访北京互联网公司:「码农」十点下班很正常,加班成纠纷焦点
  16. powershell 激活WIN10
  17. 小程序+阿里矢量图标图iconfont
  18. Android ios
  19. 路痴福利!新一代GPS芯片来了,精准度可到厘米
  20. 传感器在未来领域的应用与畅想

热门文章

  1. 陶哲轩实分析习题8.5.15
  2. 关闭系统进程,以及如何调用cmd并执行命令
  3. 各linux版本比较
  4. mysql索引 聚集索引_MySql数据库索引-聚集索引和辅助索引
  5. 代码与html混合,自定义的标签与html的标签混合应用_css
  6. 大数据在各个行业中的应用_三维设计广泛应用各个行业
  7. c#窗口科学计算机,c#窗口科学计算器连等如何实现,大神帮忙一下好么?
  8. ifix如何设画面大小_天涯明月刀手游研发揭秘:如何做出有“豪华感”的国风MMO大世界?...
  9. 模拟退火总结+洛谷模板题(P1337 [JSOI2004]平衡点 / 吊打XXX)
  10. little w and Segment Coverage(差分)