dialog_flexgrid.cpp

来自「库存管理,有MSHflexgrid的使用和网络数据库存储」· C++ 代码 · 共 364 行

CPP
364
字号
// Dialog_Flexgrid.cpp : implementation file
//

#include "stdafx.h"
#include "query.h"
#include "Dialog_Flexgrid.h"
#include "ADOconn.h"
#include "Dlg_Edit.h"
#include "CSpreadSheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDialog_Flexgrid dialog


CDialog_Flexgrid::CDialog_Flexgrid(CWnd* pParent /*=NULL*/)
	: CDialog(CDialog_Flexgrid::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDialog_Flexgrid)
	m_col = _T("");
	m_one = _T("");
	m_row = _T("");
	m_thi = _T("");
	m_two = _T("");
	m_value = _T("");
	m_strlist = _T("");
	//}}AFX_DATA_INIT
}


void CDialog_Flexgrid::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDialog_Flexgrid)
	DDX_Control(pDX, IDC_TREE1, m_tree);
	DDX_Control(pDX, IDC_MSHFLEXGRID1, m_flexgrid2);
	DDX_Text(pDX, IDC_STATIC_COL, m_col);
	DDX_Text(pDX, IDC_STATIC_ONE, m_one);
	DDX_Text(pDX, IDC_STATIC_ROW, m_row);
	DDX_Text(pDX, IDC_STATIC_THI, m_thi);
	DDX_Text(pDX, IDC_STATIC_TWO, m_two);
	DDX_Text(pDX, IDC_STATIC_VALUE, m_value);
	DDX_Text(pDX, IDC_EDIT1, m_strlist);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDialog_Flexgrid, CDialog)
	//{{AFX_MSG_MAP(CDialog_Flexgrid)
	ON_BN_CLICKED(IDC_BUTTON_OUTEXCEL, OnButtonOutexcel)
	ON_BN_CLICKED(IDC_BUTTON_INEXCEL, OnButtonInexcel)
	ON_NOTIFY(NM_CLICK, IDC_TREE1, OnClickTree1)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_CXPTOTXT, OnCxptotxt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialog_Flexgrid message handlers

BOOL CDialog_Flexgrid::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here		
    //tree set
	/*HICON m_hIcon;
	CFont m_font;
	CImageList m_imageList;
	HICON hicon[8];
    m_imageList.Create(16,16,0,8,8);
    hicon[0]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[1]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[2]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[3]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[4]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[5]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[6]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    hicon[7]=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    for(int n=0;n<8;n++)
        m_imageList.Add(hicon[n]);

    m_tree.SetImageList(&m_imageList,LVSIL_SMALL); 
    m_font.CreateFont(14, 0,0,0,FW_NORMAL, 0,0,0,
		DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");	
    m_tree.SetFont(&m_font);*/
	TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("United States");

HTREEITEM hCountry = m_tree.InsertItem(&tvInsert);

HTREEITEM hPA = m_tree.InsertItem(TVIF_TEXT,
   _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL);

	//HTREEITEM hroot=m_tree.InsertItem("FEA");
	HTREEITEM hroot_fea=m_tree.InsertItem("root cod",hCountry, NULL);//插入fea
	HTREEITEM hroot_ele=m_tree.InsertItem("ELEMENT",hCountry, NULL);
	HTREEITEM hroot_ra=m_tree.InsertItem("RA",hCountry, NULL);
	HTREEITEM hroot_dim=m_tree.InsertItem("DIMENSION",hCountry, NULL);
	HTREEITEM hroot_tol=m_tree.InsertItem("TOLERANCE",hCountry, NULL);
    m_tree.Expand(hCountry,TVE_EXPAND);
	m_tree.Expand(hroot_fea,TVE_EXPAND);
    //m_tree.SetBkColor(RGB(0,200,255));
	//m_tree.SetTextColor(RGB(100,0,0));

	//mshflexgrid set

	ADOconn adocon; 
    adocon.OnInitADOconn();	
    CString str[3];    
    CString sqlstr,vt_str;	
    sqlstr.Format("SELECT * FROM query_data");
    _RecordsetPtr rs;
	rs=adocon.GetRecordSet((_bstr_t)(LPCTSTR)sqlstr);	
    /*long num=1;
	rs->MoveFirst();
	while (!rs->adoEOF)
	{
		num++;
		rs->MoveNext();
	}*/
   //可通过两种方式填充mshflexgrid:
	//1.如上通过循环获得记录集的数目num,后设置SetRows(num),再循环SetCol/Row/Text即可
	//2.不用设置SetRows(2),默认即为2。循环取数的过程中AddItem,注意由于默认为2,需少AddItem一次
	m_flexgrid2.Clear();   
    m_flexgrid2.ClearStructure(); //此句可不写,清除grid顺序及列名
    m_flexgrid2.SetRows(2);//此句不能少
	m_flexgrid2.Refresh();
	last_row=1;
	last_col=0;
	CString sHeading;
    sHeading = _T("   序号|   用户名称|   登录口令");
    m_flexgrid2.SetFormatString  ( sHeading);
    //if(num>=1) m_flexgrid2.SetRows(num);//要绑定数据库的话行必须>=2,为1的话表头就没了
    //else  
	//m_flexgrid2.SetRows(2);
	m_flexgrid2.SetCols(0,3);//列
	//m_flexgrid2.SetCols()
	m_flexgrid2.SetCellAlignment(1);    
	//m_flexgrid2.SetColHeaderCaption(0,0,"列1");
	//m_flexgrid2.SetColHeaderCaption(1,1,"列2");
	//m_flexgrid2.SetColHeaderCaption(2,2,"列3");
    bk_color=m_flexgrid2.GetBackColor();
	
    _variant_t vt;
	rs->MoveFirst();
	int j=1;
	while (!rs->adoEOF)
	{	
		CString item;		
		item.Format("%d",j);
		if(j!=1) m_flexgrid2.AddItem("1",_variant_t(item));//第二个参数为数字转换的字符,
		//表在第item+1行添加新行,旧行往下挤。默认极为2,需少AddItem一次:
		//可以如上加if(j!=1)或者不加if(j!=1)而在后面加m_flexgrid2.RemoveItem(j)
		//m_flexgrid2.AddItem("2",_variant_t("1"));
		//m_flexgrid2.AddItem("3",_variant_t("1"));
		//m_flexgrid2.AddItem("4",_variant_t("1"));
		for (int i=0;i<3;i++)
		{
			if (i==0)  vt=rs->GetCollect("name");
			if (i==1)  vt=rs->GetCollect("code");
			if (i==2)  vt=rs->GetCollect("state");
			vt_str=(LPCTSTR)(_bstr_t)vt;
			m_flexgrid2.SetCol(i);
			m_flexgrid2.SetRow(j);
			m_flexgrid2.SetText(vt_str);			
		}
		j++;
		rs->MoveNext();		
	}
	//m_flexgrid2.RemoveItem(2);//删除最后一行。前面加if就无需删了
	if (j==1) m_flexgrid2.SetRows(1);//如无数据则只显示表头,加了RemoveItem,此行就不需要了
	//如果是空表,则是表头加一行空白行,此空白行为fixedrow,删除不了
	UpdateData(false);
    //m_flexgrid.SetAllowUserResizing(1);
    //m_flexgrid2.SetRefDataSource(rs.Detach());
	adocon.ExitConnect();        
	//m_flexgrid.SetDataSource (rs.Detach());
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BEGIN_EVENTSINK_MAP(CDialog_Flexgrid, CDialog)
    //{{AFX_EVENTSINK_MAP(CDialog_Flexgrid)
	ON_EVENT(CDialog_Flexgrid, IDC_MSHFLEXGRID1, -600 /* Click */, OnClickMshflexgrid1, VTS_NONE)
	ON_EVENT(CDialog_Flexgrid, IDC_MSHFLEXGRID1, -601 /* DblClick */, OnDblClickMshflexgrid1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDialog_Flexgrid::OnClickMshflexgrid1() 
{
	// TODO: Add your control notification handler code here	
	//m_flexgrid2.SetGridColor(m_flexgrid2.GetGridColor());
	int row_num,col_num;
	row_num=m_flexgrid2.GetRow();	
	col_num=m_flexgrid2.GetCol();
	m_row.Format("%d",row_num);
	m_col.Format("%d",m_flexgrid2.GetCol()+1);
	m_value=m_flexgrid2.GetText();
	m_flexgrid2.SetRow(last_row);
	m_flexgrid2.SetCol(last_col);
	m_flexgrid2.SetCellBackColor(bk_color);
	m_flexgrid2.SetRow(row_num);
	m_flexgrid2.SetCol(col_num);
	m_flexgrid2.SetCellBackColor(RGB(100,150,100));
	last_row=row_num;
	last_col=col_num;
    //m_flexgrid2.SetCellBackColor(RGB(100,150,20));
	//m_flexgrid2.SetBackColorSel(RGB(100,150,20));
	m_one=m_flexgrid2.GetTextMatrix(row_num,0);
	m_two=m_flexgrid2.GetTextMatrix(row_num,1);
	m_thi=m_flexgrid2.GetTextMatrix(row_num,2);
    UpdateData(false);
}

void CDialog_Flexgrid::OnDblClickMshflexgrid1() 
{
	// TODO: Add your control notification handler code here
	CDlg_Edit dlg_edit;
	int row_num=m_flexgrid2.GetRow();	
	dlg_edit.edit_str[0]=m_flexgrid2.GetTextMatrix(row_num,0);
	dlg_edit.edit_str[1]=m_flexgrid2.GetTextMatrix(row_num,1);
	dlg_edit.edit_str[2]=m_flexgrid2.GetTextMatrix(row_num,2);
	dlg_edit.m_name=dlg_edit.edit_str[0];
    dlg_edit.m_code=dlg_edit.edit_str[1];
	dlg_edit.m_state=dlg_edit.edit_str[2];
    dlg_edit.m_hide="EDIT";
    //dlg_edit.SetWindowText("修改记录");
	if (dlg_edit.DoModal()==IDOK){OnInitDialog();}
}

void CDialog_Flexgrid::OnButtonOutexcel() 
{
	// TODO: Add your control notification handler code here| OFN_OVERWRITEPROMPT
	CFileDialog dlg(FALSE,"txt",NULL,OFN_HIDEREADONLY,"Excel file(*.xls)|*.xls|All Files (*.*)|*.*||",NULL); 
	dlg.m_ofn.lpstrInitialDir="F:\\系统开发\\库存管理";
	dlg.m_ofn.lpstrTitle="批量导出";
	//"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|
	//Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";
	if (dlg.DoModal()==IDOK)
	{
    CString filename,pathname,path;
	filename=dlg.GetFileName();
	pathname=dlg.GetPathName();
    //path=pathname.Left(pathname.GetLength()-filename.GetLength());//get path
    //pathname.Format("")
	CFileFind ff;	
	BOOL sign;
	if (ff.FindFile(pathname))
	{
		if(AfxMessageBox("要保存的文件已经存在,要覆盖吗?",MB_OKCANCEL,0)==IDOK)
		{
			CFile::Remove(pathname);
            sign=true;
		}else sign=false;
	} else sign=true;
	if (sign)
	{
	CSpreadSheet SS("F:\\系统开发\\库存管理\\Test.xls", "TestSheet");
	CStringArray sampleArray, testRow;
	
	SS.BeginTransaction();
	
	// 加入标题
	sampleArray.RemoveAll();
	sampleArray.Add("姓名");
	sampleArray.Add("年龄");
	SS.AddHeaders(sampleArray);
	
	// 加入数据
	CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鹏"};
	CString strAge[]  = {"27","23","28","27","26"};
	for(int i = 0; i < sizeof(strName)/sizeof(CString); i++)
	{
		sampleArray.RemoveAll();
		sampleArray.Add(strName[i]);
		sampleArray.Add(strAge[i]);
		SS.AddRow(sampleArray);
	}
	
	// 初始化测试行数据,进行添加、插入及替换数据操作演示
	for (int k = 1; k <= 2; k++)
	{
		testRow.Add("Test");
	}
	
	SS.AddRow(testRow);				// 添加到尾部
	SS.AddRow(testRow, 2);			// 插入新行到第二行
	SS.AddRow(testRow, 6, true);	// 替换原第四行来新的内容
	
	SS.Commit();		
	
	//SS.Convert(";");            // 将原Excel文件转换为用分号分隔的文本,并另存为同名文本文件

	AfxMessageBox("文件写入成功!");
	}
	}
}

void CDialog_Flexgrid::OnButtonInexcel() 
{
	// TODO: Add your control notification handler code here
	
}

void CDialog_Flexgrid::OnClickTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CDialog_Flexgrid::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here   
	*pResult = 0;
}

void CDialog_Flexgrid::OnButton1() 
{
	// TODO: Add your control notification handler code here
	HTREEITEM root=m_tree.GetFirstVisibleItem();
	HTREEITEM hNextItem;
    HTREEITEM hChildItem = m_tree.GetChildItem(root);
    CString strlist="";
   while (hChildItem != NULL)
   {
      hNextItem = m_tree.GetNextItem(hChildItem, TVGN_NEXT);   
	  if (m_tree.GetCheck(hNextItem)) strlist+="1,";
	  else strlist+="0,";
      hChildItem = hNextItem;
   }
   m_strlist=strlist;
   UpdateData(false);
}

void CDialog_Flexgrid::OnCxptotxt() 
{
	// TODO: Add your control notification handler code here
	CFile   filename;   
  CString   str;   
  if(filename.Open("C:\\myfile.txt",CFile::modeRead))   
  {   
  char   cBuf[512];   
  UINT   uBytesRead;       
  while(uBytesRead=filename.Read(cBuf,sizeof(cBuf)-1))   
  {   
  cBuf[uBytesRead]=NULL;   
  str+=CString(cBuf);   
  }   
  filename.Close();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?