在VC中MSFlexGrid内嵌EDIT、COMBOBOX的实现方法

在VC中MSFlexGrid内嵌EDIT、COMBOBOX的实现方法


Key Words: MSFlexGrid 内嵌控件

在CSDN网络中经常会看到有人问起各种GRID控件内嵌EDIT、COMBOBOX的实现方法,本人在前阶段的开发中也遇到这方面的困难,在网络上找了又找,大多是针对ListView和DBGrid的,而对于MSFlexGrid的实现,则少之又少。在广大网友的支持下,终于本人找到了实现MSFlexGrid内嵌EDIT、COMBOBOX的一种方法,我想本文对于采用MSFlexGrid进行应用开发的朋友一定有相当大的帮助。


总结一些网友以及本人在最初实现MSFlexGrid内嵌控件失败的原因,大多是由两方面造成的:


1、坐标系转换问题,MSFlexGrid采用的坐标系和一般的控件不同,所以在操作时,需要进行转换。
2、控件在创建上的问题,如果你把控件直接创建在主窗口中,那么往往会存在,程序运行时,鼠标一点网格,控件就HIDE掉,所以在创建控件EDIT、COMBOBOX时,要以FlexGrid为父窗口。


    下面,我用一个示例程序来简单的说明一下,同时我们的示例程序还实现了在FlexGrid中按TAB键跳至下一网格[下面提到的网格均指MSFlexGrid中的小单元格]的功能。想要源代码的请登陆http://www.maoyeah.com,“技术文章”专栏找。

首先,在对话框的初始化中调用我们的初始化函数:
void CProg5Dlg::InitControls()
{
<span style="white-space:pre">	</span>//创建各个内嵌控件
<span style="white-space:pre">	</span>m_edit.Create(WS_CHILD,CRect(0,0,0,0),&m_FlexGrid,IDC_EDIT);
<span style="white-space:pre">	</span>m_cmb.Create(WS_CHILD|CBS_DROPDOWNLIST,CRect(0,0,0,0),&m_FlexGrid,IDC_CMB);
<span style="white-space:pre">	</span>//设置为和主窗口相同字体
<span style="white-space:pre">	</span>m_edit.SetFont(GetFont());
<span style="white-space:pre">	</span>m_cmb.SetFont(GetFont());
<span style="white-space:pre">	</span>//用数据填充Grid 和 ComboBox
<span style="white-space:pre">	</span>long lRow ;
<span style="white-space:pre">	</span>long lRowCount = m_FlexGrid.GetRows();
<span style="white-space:pre">	</span>long lCol ;
<span style="white-space:pre">	</span>long lColCount = m_FlexGrid.GetCols();
<span style="white-space:pre">	</span>for (lRow = 1; lRow < lRowCount; lRow++)
<span style="white-space:pre">	</span>{
	<span style="white-space:pre">	</span>m_FlexGrid.SetRow(lRow);
        <span style="white-space:pre">	</span>for(lCol = 1; lCol < lColCount; lCol++)
        <span style="white-space:pre">	</span>{
               <span style="white-space:pre">	</span>m_FlexGrid.SetCol(lCol);
               <span style="white-space:pre">		</span>CString strText;
               <span style="white-space:pre">		</span>strText.Format("%ld-%ld",lRow,lCol);
               <span style="white-space:pre">		</span>//用数据填充Grid
               <span style="white-space:pre">		</span>m_FlexGrid.SetText(strText);
               <span style="white-space:pre">		</span>//用数据填充ComboBox
               <span style="white-space:pre">		</span>m_cmb.AddString(strText);
        <span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>}
} 
其中m_edit、m_cmb是我们声明的类数据成员:
private:
CEdit m_edit;
CComboBox m_cmb;

它们的创建一定要用.Create的方法并以MSFlexGrid为父窗口,要不然,程序运行时,你一点MsflexGrid,你的Edit或ComboBox就不见了 [这是因为,MSFlexGrid和你的Edit或ComboBox同以Dialog为父窗口,你点了MsflexGrid,在Z坐标上,它就盖住了你的内嵌控件] ,因为在创建之后,它们采用的字体可能和你的主窗口风格不一致,所以还要设置一下字体。

接下来就是程序中最重要的一个函数了:

void CProg5Dlg::GridEdit(WORD nKeyAsciiCode, CWnd *p_wnd)

{
	if(p_wnd == NULL)
	{//得到当前编辑的网格的内嵌控件是m_edit or m_cmb
        p_wnd = GetThisCellMaskControl();
	}


	ASSERT(p_wnd != NULL);
	//支持坐标变换
	CDC* pDC = m_FlexGrid.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)
        	{//用来支持汉字输入
              		 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_FlexGrid.GetCellLeft() * nLogX)/1440 - 3, 
     		   (m_FlexGrid.GetCellTop() * nLogY)/1440  - 3,
       		 (m_FlexGrid.GetCellWidth()* nLogX)/1440 , 
       		 (m_FlexGrid.GetCellHeight()* nLogY)/1440 + 100,FALSE);
	}


	else if(p_wnd->IsKindOf(RUNTIME_CLASS(CEdit)))
	{
		p_wnd->MoveWindow(
        	(m_FlexGrid.GetCellLeft() * nLogX)/1440 - 3, 
        	(m_FlexGrid.GetCellTop() * nLogY)/1440  - 3,
        	(m_FlexGrid.GetCellWidth()* nLogX)/1440, 
        	(m_FlexGrid.GetCellHeight()* nLogY)/1440,FALSE);
	}
	else
	{
       		 ASSERT(0);
	}


<span style="white-space:pre">	</span>//显示我们的控件
<span style="white-space:pre">	</span>p_wnd->ShowWindow(SW_SHOW);
<span style="white-space:pre">	</span>p_wnd->SetFocus();
<span style="white-space:pre">	</span>p_wnd->GetWindowText(sz); 
<span style="white-space:pre">	</span>if(p_wnd->IsKindOf(RUNTIME_CLASS(CEdit)))
<span style="white-space:pre">	</span>{
       <span style="white-space:pre">		</span>((CEdit *)p_wnd)->SetSel(sz.GetLength(), sz.GetLength(), FALSE);
<span style="white-space:pre">	</span>} 
<span style="white-space:pre">	</span>m_FlexGrid.RedrawWindow();
} 


[说明:这个函数部分代码并非原创]


这个函数的作用,主要是支持MSFlexGrid的编辑,并把你的内嵌控件显示出来,当然,如果它是一个EDIT,那么我们有责任把EDIT内的光标置于EDIT中字串的最后。这个函数有两个参数:


WORD nKeyAsciiCode:如果激活编辑FlexGrid的事件是一次按键,那么这个参数当然就和按键的信息有关了,另外在函数中,通过它可以实现支持中文,比如:您把焦点放到一个内嵌为EDIT的网格中[只是让FlexGrid的网格得到焦点,而不让内嵌的EDIT显示出来],直接输入中文,然后就会发现,网格自动进入编辑状态,并且你输入的中文汉字位于字串的最后:


第二个参数:
 CWnd *p_wnd可以指定你想使用的内嵌控件,传入时可以使用&m_edit或&m_cmb。


当然,你可以不指定它而用我们当初设置好的规则[这规则是指MSFlexGrid哪列固定采用哪个内嵌控件],这是用什么实现的呢?


看到函数GridEdit中的

if(p_wnd == NULL)
{
        p_wnd = GetThisCellMaskControl();
}


了吧?

CWnd * CProg5Dlg::GetThisCellMaskControl()
{
	switch(m_FlexGrid.GetCol())
	{
	//第一列,第三列用ComboBox做为内嵌控件
		case 1:
		case 3:
        	return &m_cmb;
        	break;
		//其它的用Edit
		default:
        	return &m_edit;
	}
}
我们可通过这个函数来设定一些基本规则。
那么GridEdit函数是由谁来调用的呢?答案当然是由想实现编辑网格的事件触发的,在这里我设定为鼠标双击和网格有焦点时的按键事件:
//鼠标双击激发
void CProg5Dlg::OnDblClickMsflexgrid() 
{
<span style="white-space:pre">	</span>//第一行和第一列是固定的,我不想编辑它们
<span style="white-space:pre">	</span>if(m_FlexGrid.GetCol() == 0 || m_FlexGrid.GetRow() == 0)
        <span style="white-space:pre">	</span>return;
<span style="white-space:pre">	</span>GridEdit(0,NULL); 
}
//按键事件激发
void CProg5Dlg::OnKeyPressMsflexgrid(short FAR* KeyAscii) 
{
<span style="white-space:pre">	</span>// TODO: Add your control notification handler code here
<span style="white-space:pre">	</span>if(m_FlexGrid.GetCol() == 0 || m_FlexGrid.GetRow() == 0)
        <span style="white-space:pre">	</span>return;
<span style="white-space:pre">	</span>GridEdit(*KeyAscii,NULL);
}


引发这个函数之前,我们最好把ComboBox的当前选择内容和Edit的内容设为要编辑的那个格子的内容:

void CProg5Dlg::OnEnterCellMsflexgrid() 
{
<span style="white-space:pre">	</span>// TODO: Add your control notification handler code here
<span style="white-space:pre">	</span>int nthisRow = m_FlexGrid.GetRow();
<span style="white-space:pre">	</span>if(nthisRow == 0)
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>return;
	}
<span style="white-space:pre">	</span>CString sz;
<span style="white-space:pre">	</span>sz = m_FlexGrid.GetText();
<span style="white-space:pre">	</span>CWnd *pWnd = GetThisCellMaskControl();
<span style="white-space:pre">	</span>if(pWnd->IsKindOf(RUNTIME_CLASS(CComboBox)))
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>((CComboBox *)pWnd)->SelectString(-1,sz);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else if(pWnd->IsKindOf(RUNTIME_CLASS(CEdit)))
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>pWnd->SetWindowText(sz);
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>ASSERT(0);
<span style="white-space:pre">	</span>}
}

你觉得这样就行了吗?当然不行!我们在把焦点移到其它网格时[只是把焦点移走],我们有必要把那个已经显示出来的内嵌控件HIDE掉,并把它的内容传给网格,这也是我们编辑的目的。


void CProg5Dlg::OnLeaveCellMsflexgrid() 
{
<span style="white-space:pre">	</span>// TODO: Add your control notification handler code here
<span style="white-space:pre">	</span>int nthisRow = m_FlexGrid.GetRow();
<span style="white-space:pre">	</span>if(nthisRow == 0)
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>return;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>CString sz;
<span style="white-space:pre">	</span>CWnd * p_ThisWnd = GetThisCellMaskControl();
<span style="white-space:pre">	</span>ASSERT(p_ThisWnd != NULL);
<span style="white-space:pre">	</span>if (p_ThisWnd->IsWindowVisible())
<span style="white-space:pre">	</span>{
       <span style="white-space:pre">		</span>p_ThisWnd->GetWindowText(sz);
        <span style="white-space:pre">	</span>m_FlexGrid.SetText(sz);  
        <span style="white-space:pre">	</span>p_ThisWnd->ShowWindow(SW_HIDE);
<span style="white-space:pre">	</span>}
}

基本上差不多了。

下面简单介绍一下用Tab实现在MSFlexGrid的网格中跳转的问题。

我一看到这种应用,马上想到采用PreTranslateMessage函数,这个函数可是真好用,一般实现什么窗口内焦点的跳转我都用它。
在实现它之前,我们先定义一个跳到一下格子的函数:

void CProg5Dlg::GoToNextCell()
{
<span style="white-space:pre">	</span>if(m_FlexGrid.GetCol() == m_FlexGrid.GetCols() - 1)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>if(m_FlexGrid.GetRow() != m_FlexGrid.GetRows() - 1)
<span style="white-space:pre">		</span>{
               <span style="white-space:pre">		</span>m_FlexGrid.SetRow(m_FlexGrid.GetRow() + 1);
               <span style="white-space:pre">		</span>m_FlexGrid.SetCol(1);
        <span style="white-space:pre">	</span>}
        <span style="white-space:pre">	</span>else
        <span style="white-space:pre">	</span>{
               <span style="white-space:pre">		</span>return;
        <span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else
<span style="white-space:pre">	</span>{
        <span style="white-space:pre">	</span>m_FlexGrid.SetCol(m_FlexGrid.GetCol() + 1);
<span style="white-space:pre">	</span>}
}

在我们的PreTranslateMessage会调用它实现跳到下一网格中:

<pre name="code" class="cpp">
BOOL CProg5Dlg::PreTranslateMessage(MSG* pMsg)  
{ 
<span style="white-space:pre">	</span>// TODO: Add your specialized code here and/or call the base class  
<span style="white-space:pre">	</span>CWnd *pWnd = CWnd::FromHandle(pMsg->hwnd); 
<span style="white-space:pre">	</span>CWnd *pCon = GetThisCellMaskControl();   
<span style="white-space:pre">	</span>if(pMsg->message!=WM_KEYDOWN)   
<span style="white-space:pre">		</span>return CDialog::PreTranslateMessage(pMsg); 
<span style="white-space:pre">	</span>switch(pMsg->wParam)  
<span style="white-space:pre">	</span>{ 
<span style="white-space:pre">	</span>case VK_TAB:  
<span style="white-space:pre">		</span>if(pCon->GetSafeHwnd() == pMsg->hwnd) 
<span style="white-space:pre">		</span>{//如果按TAB时,处于EDIT状态,也会跳到下一格子   
<span style="white-space:pre">			</span>GoToNextCell();   
<span style="white-space:pre">			</span>return TRUE; 
<span style="white-space:pre">		</span>} 
<span style="white-space:pre">		</span>switch(pWnd->GetDlgCtrlID())  
<span style="white-space:pre">		</span>{   
<span style="white-space:pre">		</span>case IDC_MSFLEXGRID:   
<span style="white-space:pre">			</span>GoToNextCell();   
<span style="white-space:pre">			</span>return TRUE;   
<span style="white-space:pre">		</span>}break; 
<span style="white-space:pre">	</span>} 
<span style="white-space:pre">	</span>return CDialog::PreTranslateMessage(pMsg); 
}


 


好了,整个应用就讲完了,我想对于采用MSFlexGrid实现应用的朋友们,这个小东东一定能起到抛砖引玉的作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值