C++ MFC listcontrol简单例子参考



// AutoPlayDlg.cpp : 实现文件
//


#include "stdafx.h"
#include "AutoPlay.h"
#include "AutoPlayDlg.h"
#include "afxdialogex.h"
#include <vector>
#include <fstream>
#include <istream>
#include <string>
using namespace std;


#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();


// 对话框数据
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()




// CAutoPlayDlg 对话框








CAutoPlayDlg::CAutoPlayDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CAutoPlayDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CAutoPlayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST2, m_list);
}


BEGIN_MESSAGE_MAP(CAutoPlayDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON2, &CAutoPlayDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON1, &CAutoPlayDlg::OnBnClickedButton1)
ON_NOTIFY(NM_CLICK, IDC_LIST2, &CAutoPlayDlg::OnNMClickList2)
ON_BN_CLICKED(IDC_BUTTON3, &CAutoPlayDlg::OnBnClickedButton3)
END_MESSAGE_MAP()




int iNum =  0;
CString strAppPath = L"";
// CAutoPlayDlg 消息处理程序


HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
HKEY hKey;
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);


if (nError==ERROR_FILE_NOT_FOUND)
{
nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
}




return hKey;
}


void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));


}


DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data;        DWORD size = sizeof(data);    DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);


if (nError==ERROR_FILE_NOT_FOUND)
data = 0; // The value will be created and set to data next time SetVal() is called.


return data;
}






void NormalMessageVector ( CString str, vector <CString>* m_vecMessage)
{
if (m_vecMessage->size()>0)
{
m_vecMessage->clear();
}


int pos=0;
CString left1;
CString right1;
int charLength=str.GetLength();


for (int j=0;j<charLength;j++)
{
pos = str.Find('|');
if (pos>0)
{
left1 = str.Left(pos);
m_vecMessage->push_back(left1);
right1 = str.Right(str.GetLength()-pos-1);
str = right1;
}
else
break;
}
}




BOOL CAutoPlayDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();


// 将“关于...”菜单项添加到系统菜单中。


// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标


m_list.ModifyStyle( 0, LVS_REPORT );               // 报表模式  
m_list.SetExtendedStyle(m_list.GetExtendedStyle() 
| LVS_EX_GRIDLINES 
| LVS_EX_FULLROWSELECT
|LVS_EX_FLATSB);  
m_list.InsertColumn(0,_T("姓名"),LVCFMT_LEFT,100,0);//设置列
m_list.InsertColumn(1,_T("性别"),LVCFMT_LEFT,50,1);
m_list.InsertColumn(2,_T("检查号"),LVCFMT_LEFT,80,2);
m_list.InsertColumn(3,_T("检查日期"),LVCFMT_LEFT,80,2);
m_list.InsertColumn(4,_T("光盘卷标"),LVCFMT_LEFT,100,2);
TCHAR exepath[MAX_PATH]={0};
::GetModuleFileName(NULL,exepath,MAX_PATH);
(_tcsrchr(exepath, _T('\\')))[1] = 0;
CString directoryPath;
directoryPath.Format(L"%s",exepath);
strAppPath = directoryPath;


USES_CONVERSION;
    vector <CString>* m_vecMessage = NULL;
m_vecMessage = new vector <CString>;
ifstream infile; 
string filePath = W2A(directoryPath);
filePath = filePath + "user.dat";
infile.open(filePath);   //将文件流对象与文件连接起来 
    string sTempVaule;
int i = 0;
while(getline(infile,sTempVaule))
{
CString strValue;
if(sTempVaule == "")
{
continue;
}

NormalMessageVector(A2W(sTempVaule.c_str()),m_vecMessage);
m_list.InsertItem(i,m_vecMessage->at(0));//插入行
m_list.SetItemText(i,1,m_vecMessage->at(1));
m_list.SetItemText(i,2,m_vecMessage->at(2));
m_list.SetItemText(i,3,m_vecMessage->at(3));
m_list.SetItemText(i,4,m_vecMessage->at(4));
i++;
}
infile.close();             //关闭文件输入流 


// TODO: 在此添加额外的初始化代码


return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}


void CAutoPlayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}


// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。


void CAutoPlayDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);


// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}


//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CAutoPlayDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}




#define MAX_FILE_NUM                             10000     //本地打开文件的最大数
void CAutoPlayDlg::OnBnClickedButton2()
{
CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_EXPLORER, 
_T("所有文件(*.*)|*.*|DICOM 格式(*.dcm)|*.dcm;*.dic|BMP 格式(*.bmp)|*.bmp|JPEG 格式(*.jpeg)|*.jpeg;*.jpg|XML 序列格式(*.xml)|*.xml|"), AfxGetMainWnd());//DICOM 格式(*.dcm)|*.dcm;*.dic|JPEG 格式(*.jpeg)|*.jpeg;*.jpg|BMP 格式(*.bmp)|*.bmp|


CString strTitle = L"请选择要打开的图像"; //设置对话框标题
dlg.m_ofn.lpstrTitle = strTitle;


dlg.m_ofn.nMaxFile = MAX_FILE_NUM * MAX_PATH;
dlg.m_ofn.lpstrFile = new TCHAR[dlg.m_ofn.nMaxFile];
memset(dlg.m_ofn.lpstrFile, 0, sizeof(TCHAR) * dlg.m_ofn.nMaxFile);


vector<CString> vecDcmFile;
vector<CString> vecDcmImgPath;




CString strDcmFile;
if(dlg.DoModal() != IDOK )
{
return ;
}
else
{


}
 
}




void CAutoPlayDlg::OnBnClickedButton1()
{
CString strStyNum = _T("");
CString strDiskNum = _T("");
if(iNum >=0 && iNum < m_list.GetItemCount())
{
strStyNum = m_list.GetItemText(iNum,2);
strDiskNum = m_list.GetItemText(iNum,4);
}
else
{
strStyNum = m_list.GetItemText(0,2);
strDiskNum = m_list.GetItemText(0,4);
}


CString strPath = strAppPath + strDiskNum + "\\" + strStyNum;
//CFile file(_T("c:\\test.txt"),CFile::modeCreate|CFile::modeWrite);
//file.Write(strPath.GetBuffer(), strPath.GetLength() *sizeof(TCHAR));
//file.Close();
//HKEY key = NULL;
//HKEY key1 = NULL;
//if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\ZBSOFT\\MedBackUp"),0,KEY_WRITE|KEY_READ, &key))
//{
// int dateCount = 11;
// RegCreateKey(HKEY_LOCAL_MACHINE, _T("Software\\ZBSOFT\\MedBackUp"), &key);
//}
//RegSetValueEx(key,_T("diskNum"),NULL,REG_SZ,(LPBYTE)&strDiskNum, sizeof(strDiskNum));
//RegSetValueEx(key,_T("styNum"),NULL,REG_SZ,(LPBYTE)&strStyNum, sizeof(strStyNum));
//RegCloseKey(key);


//::ShellExecute(NULL, "open", pszPName, "参数1 参数2 参数3", NULL, SW_SHOW); 
::ShellExecute(this->GetSafeHwnd(), _T("open"), strAppPath + L"IFC\\" + _T("LiteMedViewer.exe"), strPath, NULL, SW_SHOWNORMAL); 








//Sleep(200);


//USES_CONVERSION;
//CString v1,v2;
//CString callBackStr;
//callBackStr = strAppPath + strDiskNum  + "\\" + strStyNum;
//CWnd * m_pWndReport=  CWnd::FindWindow(_T("IFCTEST1"),  NULL);


//if   (m_pWndReport!=NULL)
//{
// //AfxGetMainWnd()->PostMessage(WM_ShowInfo,0/*(WPARAM)LOG_ERROR*/,(LPARAM)(LPCTSTR)L"findwindow 'TMAINFORM',ok!");
// COPYDATASTRUCT   cpd;
// cpd.dwData =   0;
// cpd.cbData =  2*callBackStr.GetLength() + 1;
// cpd.lpData =   (void*)callBackStr.GetBuffer();
// m_pWndReport->SendMessage(WM_COPYDATA,  0, (LPARAM)&cpd);


// //((CIFCCtrl*)GetParent())->CallBack(callBackStr);


// //AfxMessageBox(callBackStr);
// callBackStr.ReleaseBuffer();
//}
}


void CAutoPlayDlg::OnNMClickList2(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;


DWORD dwPos = GetMessagePos();
CPoint point( LOWORD(dwPos), HIWORD(dwPos) );


m_list.ScreenToClient(&point);


LVHITTESTINFO lvinfo;
lvinfo.pt = point;
lvinfo.flags = LVHT_ABOVE;


int nItem = m_list.SubItemHitTest(&lvinfo);
if(nItem != -1)
{
CString strtemp;
strtemp.Format(L"单击的是第%d行第%d列", lvinfo.iItem, lvinfo.iSubItem);
}
iNum = lvinfo.iItem;
}




void CAutoPlayDlg::OnBnClickedButton3()
{
exit(0);
// TODO: 在此添加控件通知处理程序代码
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值