MFC的CListCtrl 怎么加入背景位图?100分!!

我想在单文档里用ClistCtrl贴图,但CListCtrl::SetBkImage都需要路径才加载背景位图。我想从资源里加载IDB_BITMAP,应该要重载ClistC... 我想在单文档里用ClistCtrl贴图,但CListCtrl::SetBkImage都需要路径才加载背景位图。
我想从资源里加载IDB_BITMAP,应该要重载ClistCtrl吧,但怎么弄啊???

有实现的源代码和例子最好,有的直接发我邮箱吧Xnazy@126.com
谢谢啦,up有分
展开
 我来答
厚德载物VIP888
2009-12-22 · TA获得超过1965个赞
知道小有建树答主
回答量:1343
采纳率:0%
帮助的人:681万
展开全部
1.创建ClmageList对象,指定图片的宽度和高度

2.将CImageList对象传递给CListCtrl对象

3.ClistCtrl增加item时,将图片ID和文字一起传入

具体代码如下:

ListImage.cpp代码如下

// ListImage.cpp : implementation file
//

#include "stdafx.h"
#include "practise.h"
#include "ListImage.h"
#define THUMWIDTH 75
#define THUMHEIGHT 55

// ListImage dialog

IMPLEMENT_DYNAMIC(ListImage, CDialog)

ListImage::ListImage(CWnd* pParent /*=NULL*/)
: CDialog(ListImage::IDD, pParent)
{

}

ListImage::~ListImage()
{
}

void ListImage::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_Control(pDX, IDC_EDIT2, m_edit2);
}

BEGIN_MESSAGE_MAP(ListImage, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &ListImage::OnBnClickedButton1)
ON_BN_CLICKED(IDOK, &ListImage::OnBnClickedOk)
END_MESSAGE_MAP()

// ListImage message handlers
BOOL ListImage::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void ListImage::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
//文件对话框

CString FilePath;
CFileDialog FileDialog(true, _T(""), _T(""), OFN_EXPLORER,
_T("所有文件(*.*)|*.*||"), this);

//显示对话框
if(FileDialog.DoModal() == IDOK)
{
FilePath = FileDialog.GetPathName();
int g=FilePath.ReverseFind('\\');
FilePath=FilePath.Left(g+1);
m_edit2.SetWindowText(FilePath);

}
else
{
return;
}
//清楚m_ImageList对象和m_List
if (m_ImageList!=NULL)
{
for(int kk=0;kk <m_ImageList.GetImageCount();kk++)
m_ImageList.Remove(kk);
m_List.DeleteAllItems();
}
else
{
m_ImageList.Create(THUMWIDTH,THUMHEIGHT, ILC_COLORDDB|ILC_MASK, 20, 1);
m_List.SetImageList(&m_ImageList, LVSIL_NORMAL);
m_List.SetImageList(&m_ImageList, LVSIL_SMALL);
}
m_List.SetRedraw(false);
CString strDir=FilePath+_T("*.*");
//strDir.Format("%s%s",FilePath,L"*.*");
//_T("D:\\happiness\\风景桌面\\*.*");

int i=0,cn=0,j=0;
SHFILEINFO finfo;
CString fileName;
CString filePath2;
HANDLE hFind;
WIN32_FIND_DATA fd ;
hFind=::FindFirstFile((LPCTSTR)strDir,&fd);
if(hFind==INVALID_HANDLE_VALUE)return;
do
{
if ( !wcscmp(fd.cFileName, _T(".")) || !wcscmp(fd.cFileName, _T("..")))continue;
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)continue;
fileName=fd.cFileName;
filePath2=FilePath+fileName;
i=fileName.Find(_T(".jpg"),0);
//加载jpg,jpeg,gif格式的图片
if (i==-1)
i=fileName.Find(_T(".jpeg"),0);
if (i==-1)
i=fileName.Find(_T(".JPG"),0);
if (i==-1)
i=fileName.Find(_T(".JPEG"),0);
if (i==-1)
i=fileName.Find(_T(".gif"),0);
if (i==-1)
i=fileName.Find(_T(".GIF"),0);
if (i==-1)
i=fileName.Find(_T(".BMP"),0);
if (i==-1)
i=fileName.Find(_T(".bmp"),0);
if (i!=-1)
{
//Creating thumbnail image for the file
HBITMAP bitm=LoadPicture(filePath2);
if (bitm!=NULL)
{

CBitmap* pImage = NULL;
pImage = new CBitmap();
pImage->Attach(bitm);
int imgP=m_ImageList.Add(pImage,RGB(0,0,0));
//Link to the added listview item
m_List.InsertItem(cn,fileName,imgP);
}
}
//加载icon图片
i=fileName.Find(_T(".ico"),0);
if (i==-1)
i=fileName.Find(_T(".ICO"),0);
if (i==-1)
i=fileName.Find(_T(".rc"),0);
if (i==-1)
i=fileName.Find(_T(".rc2"),0);
if (i!=-1)
{
::SHGetFileInfo(filePath2,0,&finfo,sizeof(SHFILEINFO),SHGFI_ICON);
m_ImageList.Add(finfo.hIcon);//加入图标
m_List.InsertItem(cn,fileName,cn);
}
cn++;
}while(::FindNextFile(hFind,&fd));
FindClose(hFind);
m_List.SetRedraw(true);
Invalidate();
UpdateWindow();
}

HBITMAP ListImage::LoadPicture(CString mFile)
{
CString pFSize;
WCHAR wpath[MAX_PATH];
// MultiByteToWideChar(CP_ACP, 0,lpszBuf, -1, wpath, MAX_PATH);
IPicture* pPic;
OleLoadPicturePath(T2OLE((LPTSTR)(LPCTSTR)mFile), NULL, NULL, NULL, IID_IPicture,(LPVOID*)&pPic);

if (pPic==NULL) return NULL;
HBITMAP hPic = NULL;
pPic->get_Handle((UINT*)&hPic);

long nWidth=THUMWIDTH;
long nHeight=THUMHEIGHT;

long mWid,mHei;
pPic->get_Height(&mHei);
pPic->get_Width(&mWid);

HBITMAP hPicRet = (HBITMAP)CopyImage(hPic, IMAGE_BITMAP, nWidth, nHeight , LR_COPYDELETEORG);

// Create Brushes for Border and BackGround
HBRUSH hBrushBorder=::CreateSolidBrush(RGB(192, 192, 192));
HBRUSH hBrushBk=::CreateSolidBrush(RGB(255, 255, 255));

// Border Size
RECT rcBorder;
rcBorder.left=rcBorder.top=0;
rcBorder.right=THUMWIDTH;
rcBorder.bottom=THUMHEIGHT;

const float fRatio=(float)THUMHEIGHT/THUMWIDTH;

int XDest, YDest, nDestWidth, nDestHeight;
// Calculate Rect to fit to canvas
const float fImgRatio=(float)mHei/mWid;
if(fImgRatio > fRatio)
{
nDestWidth=(THUMHEIGHT/fImgRatio);
XDest=(THUMWIDTH-nDestWidth)/2;
YDest=0;
nDestHeight=THUMHEIGHT;
}
else
{
XDest=0;
nDestWidth=THUMWIDTH;
nDestHeight=(THUMWIDTH*fImgRatio);
YDest=(THUMHEIGHT-nDestHeight)/2;
}

CClientDC cdc(this);
HDC hDC=::CreateCompatibleDC(cdc.m_hDC);
HBITMAP bm = CreateCompatibleBitmap(cdc.m_hDC, THUMWIDTH, THUMHEIGHT);
HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(hDC,bm);
// Draw Background
::FillRect(hDC, &rcBorder, hBrushBk);

// Draw Border
::FrameRect(hDC, &rcBorder, hBrushBorder);

HBITMAP hBmReturn= (HBITMAP)::SelectObject(hDC, pOldBitmapImage);

CDC hdcSrc, hdcDst;

hdcSrc.CreateCompatibleDC(NULL);
hdcDst.CreateCompatibleDC(NULL);

// Load the bitmaps into memory DC
CBitmap* hbmSrcT = (CBitmap*) hdcSrc.SelectObject(hPicRet);
CBitmap* hbmDstT = (CBitmap*) hdcDst.SelectObject(hBmReturn);

// This call sets up the mask bitmap.
hdcDst.BitBlt(XDest,YDest,nDestWidth, nDestHeight, &hdcSrc,0,0,SRCCOPY);
//hdcDst.StretchBlt(XDest,YDest,nDestWidth, nDestHeight, &hdcSrc,0,0,48,48,SRCCOPY);

pOldBitmapImage = (HBITMAP)SelectObject(hdcDst.m_hDC,bm);

// Release used DC and Object
DeleteDC(hDC);
DeleteObject(hBrushBorder);
DeleteObject(hBrushBk);

return pOldBitmapImage;

}

void ListImage::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
OnOK();
}
----------------------------------------------------------------------------------------------------

ListImage.h代码如下

#pragma once
#include "afxcmn.h"
#include "afxwin.h"

// ListImage dialog

class ListImage : public CDialog
{
DECLARE_DYNAMIC(ListImage)

public:
ListImage(CWnd* pParent = NULL); // standard constructor
virtual ~ListImage();
CImageList m_ImageList;
HBITMAP ListImage::LoadPicture(CString mFile);

// Dialog Data
enum { IDD = IDD_DIALOG1 };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
CListCtrl m_List;
CEdit m_edit2;
afx_msg void OnBnClickedOk();
};

或者 在CXXXApp::InitInstance()下
使用ASSERT(AfxOleInit() != FALSE ); //Initialize COM libraries
m_List.SetBkImage("c:\\background2.bmp");
m_List.SetTextBkColor(CLR_NONE); //设置背景透明

也有人说 LoadBitmap然后CListCtrl::SetBkImage,不需要位图文件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
xiaohuh421
2009-12-22 · TA获得超过904个赞
知道小有建树答主
回答量:897
采纳率:0%
帮助的人:377万
展开全部
看下这个方法行不:
CBitmap bitmap;
bitmap.LoadBitmap(ID);
HBITMAP hBit=(HBITMAP)bitmap.GetSafeHandle();
***.SetBkImage(hBit);
用的是下面这个重载
//BOOL SetBkImage( HBITMAP hbm, BOOL fTile = TRUE, int xOffsetPercent = 0, int yOffsetPercent = 0);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
GIS炮
2009-12-28 · TA获得超过869个赞
知道小有建树答主
回答量:561
采纳率:0%
帮助的人:404万
展开全部
if (bEnable)
{
/*
There are two ways to include a background image:
(1) Specify a valid filepath and load the bitmap handle from there
(2) Specify a valid URL using the res:protocol (preferred).

(2) Is preferable because you don't need to include a separate file
with your application. You can embed the BMP in the resource fork
and use the RES: protocol to have the ListControl load it. See
http://www.microsoft.com/mind/0199/cutting/cutting0199.asp for more
information about the RES: syntax. This idea was suggested by Nick Hodapp
(http://www.codetools.com/script/Articles/list_articles.asp?userid=162)

Both methods are included for completeness, but the sample
application uses the embedded BMP. Note that specifying a HBITMAP
in the struct is not currently supported.
*/

// Load the resource and apply it to the background
TCHAR szBuffer[_MAX_PATH];
VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuffer, _MAX_PATH));
CString sPath;
sPath.Format(_T("res://%s/#2/#142"),szBuffer);

LVBKIMAGE bki;
bki.ulFlags = LVBKIF_STYLE_TILE | LVBKIF_SOURCE_URL ;
bki.pszImage = sPath.GetBuffer(sPath.GetLength());
bki.cchImageMax = sPath.GetLength();
VERIFY(m_cListCtrl.SetBkImage( &bki));

/*
Construct the path to the BMP. Although the CListCtrl::SetBkImage docs
indicate that you can use an HBITMAP, the latest docs on the LVBKIMAGE
underlying struct indicates that the LVBKIMAGE.hbm member is not used.
*/
/*
TCHAR szBuffer[_MAX_PATH];
VERIFY(::GetModuleFileName(AfxGetInstanceHandle(), szBuffer, _MAX_PATH));
CString sPath = (CString)szBuffer;
sPath = sPath.Left(sPath.ReverseFind('\\') + 1);
sPath += "bk.bmp";
m_cListCtrl.SetBkImage( sPath.GetBuffer(sPath.GetLength()), TRUE);
sPath.ReleaseBuffer();
*/

// Whichever one you choose, log the path used
CString str;
str.Format(_T("Set background image: %s"), sPath.GetBuffer(sPath.GetLength()));
m_Log.AppendString(str);
sPath.ReleaseBuffer();

}
else
{
m_cListCtrl.SetBkImage( HBITMAP(0));
m_Log.AppendString(_T("Cleared background image."));

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式