MS Flex Grid控件的使用(续二)

类别:VC语言 点击:0 评论:0 推荐:

下面我把CGrid的内容和对CGrid的使用转贴一下,该文的原文件来自于
www.vchelp.net上的flex_grid_src.zip文件。对CGrid的使用进行了一下修改,
由于不知道原作者,望见量。

其中CGrid.h和CGrid.cpp是对CFlexGrid的封装,CDlgUserManger是使用CGrid

/****************CGrid.h**************************/

// Grid.h: interface for the CGrid class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_GRID_H__91679B43_6C53_11D3_BEF7_00C04F755C52__INCLUDED_)
#define AFX_GRID_H__91679B43_6C53_11D3_BEF7_00C04F755C52__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "msflexgrid.h"


class CGrid : public CMSFlexGrid
{
public:
 CGrid();
 virtual ~CGrid();
  
public :
 
 BOOL GoRight();
 BOOL GoLeft();
 BOOL GoDown();
 BOOL GoUp();
 OnKeyPressGrid(short FAR* KeyAscii) ;
 OnClickGrid();
 void OnEnterCell();
private :
 CString m_GridData;
 BOOL bEditMode;
 BOOL TransferValue (BOOL ToGrid);
};

#endif // !defined(AFX_GRID_H__91679B43_6C53_11D3_BEF7_00C04F755C52__INCLUDED_)


/****************CGrid.cpp**********************/
#include "stdafx.h"
#include "grid.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGrid ::CGrid()
{
 
 
}


CGrid ::~CGrid()
{

}


BOOL CGrid ::GoUp()
 
{
 
 TransferValue(TRUE);
 if (GetRow () > 1 ) {
  int CurrentRow = GetRow();
  CurrentRow --;
  SetRow (CurrentRow );
  TransferValue (FALSE);
  if (! GetRowIsVisible (CurrentRow))
            SetTopRow (CurrentRow  );

 }
 return TRUE;


}


BOOL CGrid ::GoDown()
{
 
 TransferValue(TRUE);
 if ( GetRow () < GetRows () -1) {
  int CurrentRow = GetRow();
  CurrentRow ++;
  SetRow ( CurrentRow);
  TransferValue (FALSE);
  if (! GetRowIsVisible (CurrentRow))
            SetTopRow (CurrentRow );
 }


 return TRUE;

}


BOOL CGrid ::GoLeft()
{
 
 TransferValue(TRUE);
 if ( GetCol () > 1 ) {
  int CurrentCol = GetCol();
  CurrentCol --;
  SetCol (CurrentCol);
  TransferValue (FALSE);
  if (!GetColIsVisible (CurrentCol))
    SetLeftCol (CurrentCol);
 }
 return TRUE;


}


BOOL CGrid ::GoRight()
{
 
 TransferValue(TRUE);
 if ( GetCol () < GetCols() - 1 ) {
  int CurrentCol = GetCol ();
  CurrentCol++;
  SetCol (CurrentCol);
  TransferValue (FALSE);
  if (!GetColIsVisible (CurrentCol))
    SetLeftCol (CurrentCol);
  
 }
 
 return TRUE;
}

 

BOOL CGrid ::TransferValue(BOOL ToGrid)
{
 CString sText;

 if (ToGrid) { file://Transfer value to Grid from Edit Box
  SetText(m_GridData  );
 } else {
  m_GridData  = GetText ();
  SetBackColorSel  (RGB(0,255,255));
 }
 return TRUE;
}

 

BOOL CGrid ::OnKeyPressGrid(short FAR* KeyAscii)
{

    switch ( (int) * KeyAscii) {

   case  VK_BACK : file://Backspace - delete the last character
      m_GridData.Delete  (m_GridData.GetLength ()-1,1);
            break;
   default :
      m_GridData = m_GridData + (TCHAR *) KeyAscii;
         break;
    }
    TransferValue (TRUE);
}


CGrid ::OnClickGrid()
{

 TransferValue (FALSE);
 
}


void CGrid::OnEnterCell()
{
 TransferValue (FALSE);
}

/*********************对CGrid的使用*********************/
/****************CDlgUserManager.cpp********************/
// DlgUserManager.cpp : implementation file


#include "stdafx.h"
#include "DlgUserManager.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgUserManager dialog


CDlgUserManager::CDlgUserManager(CWnd* pParent /*=NULL*/)
 : CDialog(CDlgUserManager::IDD, pParent)
{
 file://{{AFX_DATA_INIT(CDlgUserManager)
 file://}}AFX_DATA_INIT

}


void CDlgUserManager::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 file://{{AFX_DATA_MAP(CDlgUserManager)
 DDX_Control(pDX, IDC_USER_MANAGER_GRID, m_grid);
 file://}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgUserManager, CDialog)
 file://{{AFX_MSG_MAP(CDlgUserManager)
 ON_WM_DESTROY()
 ON_WM_CREATE()
 file://}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgUserManager message handlers

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

 CWnd * hwnd = GetDlgItem (IDC_USER_MANAGER_GRID);
 hGrid = hwnd->GetSafeHwnd();


 return TRUE;  // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}


BEGIN_EVENTSINK_MAP(CDlgUserManager, CDialog)
    file://{{AFX_EVENTSINK_MAP(CDlgUserManager)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, -600 /* Click */, OnClickUserManagerGrid, VTS_NONE)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, -601 /* DblClick */, OnDblClickUserManagerGrid, VTS_NONE)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, -602 /* KeyDown */, OnKeyDownUserManagerGrid, VTS_PI2 VTS_I2)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, 74 /* Compare */, OnCompareUserManagerGrid, VTS_I4 VTS_I4 VTS_PI2)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, -603 /* KeyPress */, OnKeyPressUserManagerGrid, VTS_PI2)
 ON_EVENT(CDlgUserManager, IDC_USER_MANAGER_GRID, -605 /* MouseDown */, OnMouseDownUserManagerGrid, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
 file://}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDlgUserManager::OnClickUserManagerGrid()
{
 // TODO: Add your control notification handler code here
  m_grid.OnClickGrid ();
}

void CDlgUserManager::OnDblClickUserManagerGrid()
{
 // TODO: Add your control notification handler code here
 m_grid.OnClickGrid ();
 
}

void CDlgUserManager::OnKeyPressUserManagerGrid(short FAR* KeyAscii)
{
 // TODO: Add your control notification handler code here
 m_grid.OnKeyPressGrid ( KeyAscii);
 
}


BOOL CDlgUserManager::PreTranslateMessage(MSG* pMsg)
{
 // TODO: Add your specialized code here and/or call the base class
 if (pMsg->message == WM_KEYDOWN  && (pMsg->hwnd == hGrid  ) ) {
 
  switch (pMsg->wParam ) {

        case VK_UP :
   m_grid.GoUp ();
   return TRUE;
   
  case VK_DOWN:
   m_grid.GoDown();
   return TRUE;
   
  case VK_LEFT :
   m_grid.GoLeft();
   return TRUE;
   
  case VK_RIGHT:
   m_grid.GoRight();
   return TRUE;
   
  }
  
   
 }  

 return CDialog::PreTranslateMessage(pMsg);
}


void CDlgUserManager::OnOK()
{
  
 try{
  file://delete all records of users
  _variant_t vRecsAffected(0L);


  _bstr_t bstrDelete("Delete from 3pw");
  (((CSltg2001App *)AfxGetApp())->m_SltgDbConn)->Execute(bstrDelete,&vRecsAffected,adOptionUnspecified);

  _bstr_t bstrInsert;
  CString strUser,strPasswd;
  for(int i=1;i<m_grid.GetRows();i++){
   strUser=m_grid.GetTextMatrix(i,1);
   if(strUser=="") continue;
   strPasswd=m_grid.GetTextMatrix(i,2);
   if(strPasswd.IsEmpty())
    strPasswd="";
   bstrInsert= ("insert into 3PW (用户名,密码) values ('" + _bstr_t((LPCTSTR)(strUser)) + _bstr_t("'")+_bstr_t(_T(","))+_bstr_t("'") + _bstr_t((LPCTSTR)(strPasswd)) + _bstr_t("'")+_bstr_t(")") );
   (((CSltg2001App *)AfxGetApp())->m_SltgDbConn)->Execute(bstrInsert,&vRecsAffected,adOptionUnspecified);
   
  }
  
  
 }
 
 catch(_com_error e){
  CString sError=e.ErrorMessage();
  AfxMessageBox(sError);
 }
 catch(...){
  AfxMessageBox("不能处理的错误");
 }
 
 
 CDialog::OnOK();
}
主要是增加了几个消息处理函数。
OnClickUserManagerGrid
OnDblClickUserManagerGrid
OnKeyDownUserManagerGrid
OnKeyPressUserManagerGrid
PreTranslateMessage
其中主要是PreTranslateMessage
最后的OnOK()函数是具体连接数据库,并把网格中的数据存到数据库中。
希望上面的内容能对大家有益。

。。。。。。。待续。。。。。。。。。。。。。。


 

本文地址:http://com.8s8s.com/it/it3074.htm