⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t_flexgriddlg.cpp

📁 在Visual C++中使用MSFlexGrid控件的几点改进
💻 CPP
字号:
// T_FlexGridDlg.cpp : implementation file
//

#include "stdafx.h"
#include "T_FlexGrid.h"
#include "T_FlexGridDlg.h"

#include "font.h"

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

CString CellStr;

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CT_FlexGridDlg dialog

CT_FlexGridDlg::CT_FlexGridDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CT_FlexGridDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CT_FlexGridDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	//初始化
	m_pEdit=new CMyEdit;
	m_pEdit->m_hWnd=NULL;
	m_pList=new CMyList;
	m_pList->m_hWnd=NULL;
	m_font.m_hObject=NULL;
}

void CT_FlexGridDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CT_FlexGridDlg)
	DDX_Control(pDX, IDOK, m_ok);
	DDX_Control(pDX, IDCANCEL, m_cancel);
	DDX_Control(pDX, IDC_Clear, m_bcle);
	DDX_Control(pDX, IDC_BUTTON4, m_bfo);
	DDX_Control(pDX, IDC_BUTTON5, m_bfiv);
	DDX_Control(pDX, IDC_BUTTON3, m_bt);
	DDX_Control(pDX, IDC_BUTTON2, m_bs);
	DDX_Control(pDX, IDC_BUTTON1, m_bf);
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_flex);
	DDX_Control(pDX, IDC_REMOTEDATACTL1, m_rdc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CT_FlexGridDlg, CDialog)
	//{{AFX_MSG_MAP(CT_FlexGridDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Clear, OnClear)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_MESSAGE(CELL_DATA_CHANGED,OnCellDataChanged)
	ON_MESSAGE(CELL_DB_CLICKED,OnCellDBClicked1)
	ON_MESSAGE(LIST_DATA_CHANGED,OnListDataChanged)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CT_FlexGridDlg message handlers

BOOL CT_FlexGridDlg::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_bcle.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bcle.SetFontColor(RGB(100,100,220));
	m_bf.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bf.SetFontColor(RGB(100,100,220));
	m_bfiv.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bfiv.SetFontColor(RGB(100,100,220));
	m_bs.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bs.SetFontColor(RGB(100,100,220));
	m_bt.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bt.SetFontColor(RGB(100,100,220));
	m_cancel.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_cancel.SetFontColor(RGB(100,100,220));
	m_ok.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_ok.SetFontColor(RGB(100,100,220));
	//m_ok.SetBackColor(RGB(240,180,220));
	m_bfo.LoadBitmaps(IDB_BTNBW,5, 5, 5, 5, 4 );
	m_bfo.SetFontColor(RGB(100,100,220));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CT_FlexGridDlg::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 CT_FlexGridDlg::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 CT_FlexGridDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CT_FlexGridDlg::OnClear() 
{
	m_flex.Clear();
}


void CT_FlexGridDlg::OnButton1() 
{
  long i=m_flex.GetRow();
  CString str;
  str.Format("%d",i);
  MessageBox(str);	
}

void CT_FlexGridDlg::OnButton2() 
{
  long i=m_flex.GetCol();
  CString str;
  str.Format("%d",i);
  MessageBox(str);	
}

void CT_FlexGridDlg::OnButton3() 
{
  CString str;
  long i;
/*  
// first way,too long
//GetTextMatrix为取任一行、任一列的CELL文本
  i=m_flex.GetRow();
  j=m_flex.GetCol();
  str=m_flex.GetTextMatrix(i,j);
*/

/*
  //GetText为取当前选中的CELL文本
  str=m_flex.GetText();
*/

  //GetTextArray是把当前所有的CELL(包括列标题)文本当成是一个数组,然后取文本
  str="";
  for (i=0;i<m_flex.GetCols()*m_flex.GetRows()-1;i++)
  {   
	  if (i%3==0)
		  str+="\n";
	  str+="  "+m_flex.GetTextArray(i);  
  }  
 MessageBox(str);
}

void CT_FlexGridDlg::OnButton4() 
{
  CString str;
  str.Format("行高:%d\n 列宽:%d",m_flex.GetRowHeight(1),m_flex.GetColWidth(1));
  MessageBox(str);
}

void CT_FlexGridDlg::OnButton5() 
{
  //GetRowPos获得参数所指定的行的左上角的值
  int i=m_flex.GetRowPos(2);
  CString str;
  str.Format("%d",i);
  MessageBox(str);	

  int j=m_flex.GetColPos(2);
  str.Format("%d",j);
  MessageBox(str);	
}

LONG CT_FlexGridDlg::OnCellDataChanged(UINT wParam,LONG lParam)
{
  CString str;
  m_pEdit->GetWindowText(str);
  m_flex.SetTextMatrix(m_OldRow,m_OldCol,str);
  return 1L;
}

LONG CT_FlexGridDlg::OnListDataChanged(UINT wParam,LONG lParam)
{
	CString str;
	int sel=m_pList->GetCurSel();
	m_pList->GetText(sel,str);
	m_pEdit->SetWindowText(str);
	m_flex.SetTextMatrix(m_OldRow,m_OldCol,str);
    return 1L;
}
void CT_FlexGridDlg::OnCellDBClicked1(UINT wParam,LONG lParam)
{
	//确保只有一个列表框
   	if (m_pList->GetSafeHwnd()!=NULL)
	  m_pList->DestroyWindow();

	//设置列表框矩形rect,其中m_CellRect为网格所占的矩形,
	//FlexRect为MSFlexGrid控件所占的矩形,用以调整列表框矩形rect的位置
	CRect rect,FlexRect;
	rect.left=m_CellRect.left;
	rect.right=m_CellRect.right;
	rect.top=m_CellRect.top+m_CellRect.Height();
	rect.bottom=m_CellRect.bottom+m_CellRect.Height()*3;
	m_flex.GetClientRect(&FlexRect);
	//调整矩形
	if (FlexRect.bottom<rect.bottom-m_CellRect.Height()*2)
	{
		rect.top=m_CellRect.top-m_CellRect.Height()*2;
		rect.bottom=m_CellRect.bottom;
	}
	//动态创建列表框
	m_pList=new CMyList;
	m_pList->Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|WS_HSCROLL, rect,
					m_flex.FromHandle(m_flex.GetSafeHwnd()),ID_CELL_LIST);
	//根据不同的列弹出不同的列表选择
	switch (m_selectedCol)
	{
	case 0:
			m_pList->ShowWindow(SW_HIDE);   //列表框变为隐藏,双击第0列不弹出列表选择
			break;
	case 1:
			m_pList->AddString(_T("张三"));
			m_pList->AddString(_T("李四"));
			m_pList->AddString(_T("王五"));
			break;
	case 2:
			m_pList->AddString(_T("95"));
			m_pList->AddString(_T("96"));
			break;
	}
	m_pList->BringWindowToTop();
	//保持字体的一致
	m_pList->SetFont(&m_font);
	m_pList->SetFocus();
}
BEGIN_EVENTSINK_MAP(CT_FlexGridDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CT_FlexGridDlg)
	ON_EVENT(CT_FlexGridDlg, IDC_MSFLEXGRID1, 71 /* EnterCell */, OnEnterCellMsflexgrid1, VTS_NONE)
	ON_EVENT(CT_FlexGridDlg, IDC_MSFLEXGRID1, 73 /* Scroll */, OnScrollMsflexgrid1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CT_FlexGridDlg::OnEnterCellMsflexgrid1() 
{
  //确保只有一个EDIT 
  if (m_pEdit->GetSafeHwnd()!=NULL)
	  m_pEdit->DestroyWindow();

  if (m_font.m_hObject!=NULL)
	  m_font.DeleteObject();

  int col=m_flex.GetCol();
  int row=m_flex.GetRow();

  if (row==0) return;

  m_selectedRow=row;
  m_selectedCol=col;

  //获得CELL的包围矩形
  CClientDC dc(this);
  int x_pixels=dc.GetDeviceCaps(LOGPIXELSX);
  int y_pixels=dc.GetDeviceCaps(LOGPIXELSY);
  long R_pos=m_flex.GetRowPos(m_selectedRow);
  long C_pos=m_flex.GetColPos(m_selectedCol);
  long R_height=m_flex.GetRowHeight(m_selectedRow);
  long C_width=m_flex.GetColWidth(m_selectedCol);
  //CRect rect;
  m_CellRect.top=(int)((R_pos/1440.0)*y_pixels);
  m_CellRect.left=(int)((C_pos/1440.0)*x_pixels);
  m_CellRect.bottom=(int)(((R_pos+R_height)/1440.0)*y_pixels);
  m_CellRect.right=(int)(((C_pos+C_width)/1440.0)*x_pixels);
  
  m_OldRow=m_selectedRow;
  m_OldCol=m_selectedCol;

  //动态创建控件
  m_pEdit=new CMyEdit;
  m_pEdit->Create(ES_AUTOHSCROLL | WS_VISIBLE | WS_BORDER | LVS_NOLABELWRAP,
	              m_CellRect,m_flex.FromHandle(m_flex.GetSafeHwnd()),ID_CELL_EDIT);
  
  //获得当前MSFlexGrid控件使用的字体
  COleFont OleFt=m_flex.GetFont();
  CY size=OleFt.GetSize();
  CString FontName=OleFt.GetName();
  int i=size.Lo/1000;
  m_font.CreatePointFont(i,FontName);
  //将编辑框的字体设置为和MSFlexGrid控件相同的字体
  m_pEdit->SetFont(&m_font);
 
  //设置输入焦点
  m_pEdit->SetFocus();
  CellStr=m_flex.GetTextMatrix(m_selectedRow,m_selectedCol);
  //MessageBox(str);
  m_pEdit->SetWindowText(CellStr);
  if (m_pList->GetSafeHwnd()!=NULL)
	 m_pList->DestroyWindow();
}

HBRUSH CT_FlexGridDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	//if ((GetDlgCtrlID()==ID_CELL_EDIT)| (nCtlColor==CTLCOLOR_EDIT))
//nu	if (nCtlColor==CTLCOLOR_EDIT)
//nu		pDC->SetTextColor(RGB(255,0,0));
	// TODO: Return a different brush if the default is not desired
	return hbr;
}

void CT_FlexGridDlg::OnScrollMsflexgrid1() 
{
	//销毁编辑框窗口
	if (m_pEdit->GetSafeHwnd()!=NULL)
	  m_pEdit->DestroyWindow();
	//销毁列表框窗口
	if (m_pList->GetSafeHwnd()!=NULL)			
		m_pList->DestroyWindow();
	Invalidate();
}

⌨️ 快捷键说明

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