tempprojdlg.cpp

来自「一个FlexGrid应用的好程序」· C++ 代码 · 共 542 行

CPP
542
字号
// TempProjDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TempProj.h"
#include "TempProjDlg.h"
#include <afxdlgs.h>
#include <afx.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempProjDlg dialog

CTempProjDlg::CTempProjDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTempProjDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTempProjDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTempProjDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTempProjDlg)
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_Grid);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTempProjDlg, CDialog)
	//{{AFX_MSG_MAP(CTempProjDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(ID_ADD, OnAdd)
	ON_COMMAND(ID_MU_OPENFILE, OnMuOpenfile)
	ON_COMMAND(ID_MU_SAVE, OnMuSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTempProjDlg message handlers

BOOL CTempProjDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Count=0;
	m_Grid.SetRows(100);
	//设置一个浮动的文本框,必须以m_Grid为父窗口,IDC_EDIT是view->Resource symbol中加的
	m_Edit.Create(WS_CHILD,CRect(0,0,0,0),&m_Grid,IDC_EDIT);

    //设置为和主窗口相同字体
    m_Edit.SetFont(GetFont());
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTempProjDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTempProjDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTempProjDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	
	CDialog::OnOK();
}

void CTempProjDlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	if(m_Input.DoModal()==IDOK)
		m_StudentInfo[m_Count].m_Name=m_Input.m_Name;
	    m_StudentInfo[m_Count].m_Sex=m_Input.m_Sex;
		m_StudentInfo[m_Count].m_Age=m_Input.m_Age;
	    m_StudentInfo[m_Count].m_Class=m_Input.m_Class;
		m_StudentInfo[m_Count].m_Score=m_Input.m_Score;
		m_Count++;
		if(m_Count>9)
			m_Grid.SetRow(m_Count+1);
		 LoadData();





	
}

void CTempProjDlg::LoadData()
{
		CString str;
	
for(int i=0;i<m_Count;i++)
{
	//第i行的第1-5列数据
	m_Grid.SetTextArray(GenID(i+1,1),m_StudentInfo[i].m_Name);//姓名
	m_Grid.SetTextArray(GenID(i+1,2),m_StudentInfo[i].m_Sex);    //性别
    str.Format("%d",m_StudentInfo[i].m_Age);                      //年龄

	m_Grid.SetTextArray(GenID(i+1,3),str);
	m_Grid.SetTextArray(GenID(i+1,4),m_StudentInfo[i].m_Class); //部门

	str.Format("%.2f",m_StudentInfo[i].m_Score);
	m_Grid.SetTextArray(GenID(i+1,5),str);                 //月薪
}
  

}

int CTempProjDlg::GenID(int m_Row, int m_Col)
{
	int cols=m_Grid.GetCols();
    //int i=cols;
    return m_Col+cols*m_Row;

}

void CTempProjDlg::OnMuOpenfile() 
{
	// TODO: Add your command handler code here
	CFileDialog fileDlg(TRUE);
	if(fileDlg.DoModal()==IDOK)
	m_FilePathName=fileDlg.GetPathName();
	else return;

	CFile file;
	file.Open(m_FilePathName,CFile::modeRead);
	

	CArchive ar(&file,CArchive::load);
	ar>>m_Count;
	if(m_Count>49)
	{
	  file.Close();
	  m_FilePathName.Empty();
	  return;   //大于最大个数,错误
	}
	for(int i=0;i<m_Count;i++)
	  m_StudentInfo[i].ReadStudentInfo(ar);
	
	ar.Close();
    
	
	LoadData();
	
}

void CTempProjDlg::OnMuSave() 
{
	// TODO: Add your command handler code here
	if(m_FilePathName.IsEmpty())
	{
		CFileDialog fileDlg(FALSE);
		fileDlg.m_ofn.lpstrTitle="保存为:";
		if(fileDlg.DoModal()==IDOK)
    	 	m_FilePathName=fileDlg.GetPathName();
		else return;
	}

	CFile file;
	file.Open(m_FilePathName,CFile::modeCreate|CFile::modeWrite);
	CArchive ar(&file,CArchive::store);
	ar<<m_Count;
	 for(int i=0;i<m_Count;i++)
	  m_StudentInfo[i].SaveStudentInfo(ar);
	ar.Close();
	
}

BEGIN_EVENTSINK_MAP(CTempProjDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CTempProjDlg)
	ON_EVENT(CTempProjDlg, IDC_MSFLEXGRID1, -601 /* DblClick */, OnDblClickMsflexgrid1, VTS_NONE)
	ON_EVENT(CTempProjDlg, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
	ON_EVENT(CTempProjDlg, IDC_MSFLEXGRID1, 71 /* EnterCell */, OnEnterCellMsflexgrid1, VTS_NONE)
	ON_EVENT(CTempProjDlg, IDC_MSFLEXGRID1, 72 /* LeaveCell */, OnLeaveCellMsflexgrid1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CTempProjDlg::OnDblClickMsflexgrid1() 
{
	// TODO: Add your control notification handler code here
	if(m_Grid.GetCol() == 0 || m_Grid.GetRow() == 0)

        return;


     GridEdit(0,NULL); 
	
}

void CTempProjDlg::GridEdit(WORD nKeyAsciiCode, CWnd *p_wnd)
{
	if(p_wnd == NULL)

{//得到当前编辑的网格的内嵌控件是m_edit or m_cmb

        p_wnd = &m_Edit;

}

 

ASSERT(p_wnd != NULL);


//支持坐标变换

CDC* pDC = m_Grid.GetDC();
//获取有关给定显示设备功能的指定类型指定设备信息
//沿显示宽度方向,每一逻辑单位的像素数

int nLogX = pDC->GetDeviceCaps(LOGPIXELSX);
//沿显示高度方向,每一逻辑单位的像素数
int nLogY = pDC->GetDeviceCaps(LOGPIXELSY);

ReleaseDC(pDC);


CString sz=' ';

 

//当有文字输入时,如果当前控件是Edit,那么光标到末尾

if (nKeyAsciiCode >= 0 && nKeyAsciiCode < ' ')

{

        if(p_wnd->IsKindOf(RUNTIME_CLASS(CEdit)))//测试该对象是否与指定类相关联

        {

               ((CEdit *)p_wnd)->SetSel(-1, -1);

        }

}

//else

{

        CString Input = "  ";

        p_wnd->GetWindowText(sz);

 

        

        if (nKeyAsciiCode > 0x100)

        {//用来支持汉字输入nKeyAsciiCode(二进制)右移8位

               Input.SetAt(0, nKeyAsciiCode >> 8);//设置给定位置上的字符

               Input.SetAt(1, nKeyAsciiCode & 0xff);

        }

        else

        {//非汉字

               Input = (char)nKeyAsciiCode;
                          
        }

 

        sz += Input;

        p_wnd->SetWindowText(sz); 

}

 

/*if(p_wnd->IsKindOf(RUNTIME_CLASS(CComboBox)))

{

        p_wnd->MoveWindow(

        (m_Grid.GetCellLeft() * nLogX)/1440 - 3, 

        (m_Grid.GetCellTop() * nLogY)/1440  - 3,

        (m_Grid.GetCellWidth()* nLogX)/1440 , 

        (m_Grid.GetCellHeight()* nLogY)/1440 + 100,FALSE);

}*/

//else if(p_wnd->IsKindOf(RUNTIME_CLASS(CEdit)))

//{

p_wnd->MoveWindow(

        (m_Grid.GetCellLeft() * nLogX)/1440 - 3, 

        (m_Grid.GetCellTop() * nLogY)/1440  - 3,

        (m_Grid.GetCellWidth()* nLogX)/1440, 

        (m_Grid.GetCellHeight()* nLogY)/1440,FALSE);

//}

//else

//{

      //  ASSERT(0);

//}

//显示我们的控件

p_wnd->ShowWindow(SW_SHOW);

p_wnd->SetFocus();

p_wnd->GetWindowText(sz);

 

if(p_wnd->IsKindOf(RUNTIME_CLASS(CEdit)))

{

       ((CEdit *)p_wnd)->SetSel(sz.GetLength(), sz.GetLength(), FALSE);

}

 

m_Grid.RedrawWindow();//重画

}

/*CWnd * CTempProjDlg::GetThisCellMaskControl()
{
	switch(m_Grid.GetCol())

{

//第一列,第三列用ComboBox做为内嵌控件

case 1:

case 3:

       // return &m_cmb;

       // break;

//其它的用Edit

default:

        return &m_Edit;

}
}*/

void CTempProjDlg::OnClickMsflexgrid1() 
{
	// TODO: Add your control notification handler code here
	
}

void CTempProjDlg::OnEnterCellMsflexgrid1() 
{
	// TODO: Add your control notification handler code here
	int nthisRow = m_Grid.GetRow();

if(nthisRow == 0)
    return;

CString sz;
sz = m_Grid.GetText();

CWnd *pWnd = &m_Edit;//GetThisCellMaskControl();

 if(pWnd->IsKindOf(RUNTIME_CLASS(CEdit)))
{
        pWnd->SetWindowText(sz);
}
else

{

        ASSERT(0);

}
	
}

void CTempProjDlg::OnLeaveCellMsflexgrid1() 
{
	// TODO: Add your control notification handler code here
	int nthisRow = m_Grid.GetRow();
if(nthisRow == 0)       return;
CString sz;
CWnd * p_ThisWnd =&m_Edit;// GetThisCellMaskControl();
ASSERT(p_ThisWnd != NULL);

if (p_ThisWnd->IsWindowVisible())
{
        p_ThisWnd->GetWindowText(sz);
        m_Grid.SetText(sz);  
        p_ThisWnd->ShowWindow(SW_HIDE);
}
	
}

⌨️ 快捷键说明

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