MFC listcontrol显示图片有关问题
MFC listcontrol显示图片问题
代码如下:
// PicDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Test.h"
#include "PicDlg.h"
//#include "PicView.h"
//#include "PicDoc.h"
// PicDlg 对话框
IMPLEMENT_DYNAMIC(PicDlg, CDialog)
PicDlg::PicDlg(CWnd* pParent /*=NULL*/)
: CDialog(PicDlg::IDD, pParent)
{
/*CCreateContext pContext;
CWnd* pFrameWnd = this;
pContext.m_pCurrentDoc = new PicDoc;
pContext.m_pNewViewClass = RUNTIME_CLASS(PicView);
PicView* pView = (PicView*)((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
ASSERT(pView);
pView->m_nMapMode = MM_TEXT;
pView->ShowWindow(SW_NORMAL);
CRect rect;
GetWindowRect(rect);
rect.top -= 40;
rect.bottom -= 40;
pView->MoveWindow(rect);*/
}
PicDlg::~PicDlg()
{
}
void PicDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//DDX_Control(pDX,IDC_LIST1,m_list);
}
BEGIN_MESSAGE_MAP(PicDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &PicDlg::OnBnClickedButton1)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &PicDlg::OnLvnItemchangedList1)
END_MESSAGE_MAP()
// PicDlg 消息处理程序
void PicDlg::PicCreate()
{
i = 0;
i_list = new CImageList();
bool a= i_list->Create(24,24,ILC_COLOR24,0,200);
}
void PicDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString PicPathName = NULL;
m_list = (CListCtrl*)GetDlgItem(IDC_LIST1);
m_list->SetExtendedStyle(m_list->GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_SUBITEMIMAGES|LVS_EX_FULLROWSELECT);
edit = (CEdit*)GetDlgItem(IDC_EDIT1);
CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(LPCTSTR)_TEXT("BMP Files(*bmp)|*.bmp||"),NULL);
if(dlg.DoModal()==IDOK)
{
PicPathName = dlg.GetPathName();
edit->SetWindowTextA(PicPathName);
if(PicPathName) //PicPathName为读取的bmp文件路径
{ //NULL换成AfxGetInstanceHandle()也不行
HBITMAP hbitmap = (HBITMAP)::LoadImage(N
ULL,_T(PicPathName),IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_CREATEDIBSECTION|LR_LOADFROMFILE);
CBitmap m_Bitmap;
if(hbitmap>0)
{
m_Bitmap.Attach(hbitmap);
}
i_list->Add(&m_Bitmap,RGB(0,0,0));
int a = i_list->GetImageCount(); ->此处getcount为什么执行一次就是42???
m_list->SetImageList(i_list,LVSIL_SMALL);
m_list->InsertItem(0,_T("图片"),i);//i的初始值为0
}
i++;
}
else
{
return;
}
}
void PicDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
//m_list.InsertColumn(0,_T("图片"));
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
}
------解决思路----------------------
请检查每个函数的返回值。
用TRACE辅助调试MFC程序。
TRACE
TRACE( exp )
Parameters
exp
Specifies a variable number of arguments that are used in exactly the same way that a variable number of arguments are used in the run-time function printf.
Remarks
Provides similar functionality to the printf function by sending a formatted string to a dump device such as a file or debug monitor. Like printf for C programs under MS-DOS, the TRACE macro is a convenient way to track the value of variables as your program executes. In the Debug environment, the TRACE macro output goes to afxDump. In the Release environment, it does nothing.
TRACE is limited to sending a total of 512 characters at a time. If you call TRACE with formatting commands, the total string length after the formatting commands have been expanded cannot be more than 512 characters, including the terminating NULL. Exceeding this limit causes an ASSERT.
Note This macro is available only in the debug version of MFC.
For more information, seeMFC Debugging Support in Visual C++ Programmer’s Guide.
Example
// example for TRACE
int i = 1;
char sz[] = "one";
TRACE( "Integer = %d, String = %s\n", i, sz );
// Output: 'Integer = 1, String = one'
See Also TRACE0, TRACE1, TRACE2, TRACE3, AfxDump, afxTraceEnabled
代码如下:
// PicDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Test.h"
#include "PicDlg.h"
//#include "PicView.h"
//#include "PicDoc.h"
// PicDlg 对话框
IMPLEMENT_DYNAMIC(PicDlg, CDialog)
PicDlg::PicDlg(CWnd* pParent /*=NULL*/)
: CDialog(PicDlg::IDD, pParent)
{
/*CCreateContext pContext;
CWnd* pFrameWnd = this;
pContext.m_pCurrentDoc = new PicDoc;
pContext.m_pNewViewClass = RUNTIME_CLASS(PicView);
PicView* pView = (PicView*)((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
ASSERT(pView);
pView->m_nMapMode = MM_TEXT;
pView->ShowWindow(SW_NORMAL);
CRect rect;
GetWindowRect(rect);
rect.top -= 40;
rect.bottom -= 40;
pView->MoveWindow(rect);*/
}
PicDlg::~PicDlg()
{
}
void PicDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//DDX_Control(pDX,IDC_LIST1,m_list);
}
BEGIN_MESSAGE_MAP(PicDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &PicDlg::OnBnClickedButton1)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, &PicDlg::OnLvnItemchangedList1)
END_MESSAGE_MAP()
// PicDlg 消息处理程序
void PicDlg::PicCreate()
{
i = 0;
i_list = new CImageList();
bool a= i_list->Create(24,24,ILC_COLOR24,0,200);
}
void PicDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString PicPathName = NULL;
m_list = (CListCtrl*)GetDlgItem(IDC_LIST1);
m_list->SetExtendedStyle(m_list->GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_SUBITEMIMAGES|LVS_EX_FULLROWSELECT);
edit = (CEdit*)GetDlgItem(IDC_EDIT1);
CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(LPCTSTR)_TEXT("BMP Files(*bmp)|*.bmp||"),NULL);
if(dlg.DoModal()==IDOK)
{
PicPathName = dlg.GetPathName();
edit->SetWindowTextA(PicPathName);
if(PicPathName) //PicPathName为读取的bmp文件路径
{ //NULL换成AfxGetInstanceHandle()也不行
HBITMAP hbitmap = (HBITMAP)::LoadImage(N
CBitmap m_Bitmap;
if(hbitmap>0)
{
m_Bitmap.Attach(hbitmap);
}
i_list->Add(&m_Bitmap,RGB(0,0,0));
int a = i_list->GetImageCount(); ->此处getcount为什么执行一次就是42???
m_list->SetImageList(i_list,LVSIL_SMALL);
m_list->InsertItem(0,_T("图片"),i);//i的初始值为0
}
i++;
}
else
{
return;
}
}
void PicDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
//m_list.InsertColumn(0,_T("图片"));
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
}
------解决思路----------------------
请检查每个函数的返回值。
用TRACE辅助调试MFC程序。
TRACE
TRACE( exp )
Parameters
exp
Specifies a variable number of arguments that are used in exactly the same way that a variable number of arguments are used in the run-time function printf.
Remarks
Provides similar functionality to the printf function by sending a formatted string to a dump device such as a file or debug monitor. Like printf for C programs under MS-DOS, the TRACE macro is a convenient way to track the value of variables as your program executes. In the Debug environment, the TRACE macro output goes to afxDump. In the Release environment, it does nothing.
TRACE is limited to sending a total of 512 characters at a time. If you call TRACE with formatting commands, the total string length after the formatting commands have been expanded cannot be more than 512 characters, including the terminating NULL. Exceeding this limit causes an ASSERT.
Note This macro is available only in the debug version of MFC.
For more information, seeMFC Debugging Support in Visual C++ Programmer’s Guide.
Example
// example for TRACE
int i = 1;
char sz[] = "one";
TRACE( "Integer = %d, String = %s\n", i, sz );
// Output: 'Integer = 1, String = one'
See Also TRACE0, TRACE1, TRACE2, TRACE3, AfxDump, afxTraceEnabled