📄 mydialog.cpp
字号:
// MyDialog.cpp : implementation file
//
#include "stdafx.h"
#include "DBgridTest.h"
#include "MyDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "dbgrid32.h"
/////////////////////////////////////////////////////////////////////////////
// CMyDialog dialog
CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
: CDialog(CMyDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyDialog)
m_iMaxRow=0;
//}}AFX_DATA_INIT
}
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDialog)
DDX_Control(pDX, IDC_LIST1, m_ListBox);
DDX_Control(pDX, IDC_DBGRID1, m_DBGrid);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_LBN_KILLFOCUS(IDC_LIST1, OnKillfocusList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDialog message handlers
void CMyDialog::OnOK()
{
// TODO: Add extra validation here
if(m_DBGrid.GetDataChanged())
{
if(m_DBGrid.GetAddNewMode() == 0) // 0 - No AddNew
//触发UnboundWriteData事件记录之
m_DBGrid.SetBookmark(m_DBGrid.GetBookmark());
else // 1 - AddNew Current 2 - AddNew Pending
//触发UnboundAddData事件记录之
m_DBGrid.SetRow(m_DBGrid.GetRow());
}
//CDialog::OnOK();
}
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
Column Col; //列对象
//设置第1列的标题
COleVariant varIndex1((short)1, VT_I2);
LPDISPATCH pCol1 = m_DBGrid.GetColumns(varIndex1);
Col.AttachDispatch(pCol1); //与IDispatch指针建立关联
Col.SetCaption(_T("学历"));
Col.SetButton(TRUE); //设置按钮属性
//得到Columns对象
Columns Cols;
LPDISPATCH pCols = m_DBGrid.GetColumns();
Cols.AttachDispatch(pCols);
//添加第2列并设置其标题
LPDISPATCH pCol2 = Cols.GetAdd(2);
Col.AttachDispatch(pCol2);
Col.SetCaption(_T("职务"));
Col.SetVisible(TRUE); //动态添加的列缺省不可见
Cols.DetachDispatch(); //与IDispatch指针断开关联
Col.DetachDispatch();
m_DBGrid.Refresh(); //刷新显示
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BEGIN_EVENTSINK_MAP(CMyDialog, CDialog)
//{{AFX_EVENTSINK_MAP(CMyDialog)
ON_EVENT(CMyDialog, IDC_DBGRID1, 18 /* UnboundReadData */, OnUnboundReadDataDbgrid1, VTS_DISPATCH VTS_PVARIANT VTS_BOOL)
ON_EVENT(CMyDialog, IDC_DBGRID1, 19 /* UnboundWriteData */, OnUnboundWriteDataDbgrid1, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(CMyDialog, IDC_DBGRID1, 20 /* UnboundAddData */, OnUnboundAddDataDbgrid1, VTS_DISPATCH VTS_PVARIANT)
ON_EVENT(CMyDialog, IDC_DBGRID1, 21 /* UnboundDeleteRow */, OnUnboundDeleteRowDbgrid1, VTS_PVARIANT)
ON_EVENT(CMyDialog, IDC_DBGRID1, 33 /* ButtonClick */, OnButtonClickDbgrid1, VTS_I2)
ON_EVENT(CMyDialog, IDC_DBGRID1, 14 /* Scroll */, OnScrollDbgrid1, VTS_PI2)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMyDialog::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
CString sText;
int iOption = m_ListBox.GetCurSel();
m_ListBox.GetText(iOption, sText);//得到选中项的文本
m_ListBox.ResetContent();
m_ListBox.ShowWindow(SW_HIDE); //隐藏列表框
m_DBGrid.SetText(sText);//显示在当前单元格中
}
void CMyDialog::OnKillfocusList1()
{
// TODO: Add your control notification handler code here
m_ListBox.ResetContent();
m_ListBox.ShowWindow(SW_HIDE);
}
void CMyDialog::OnUnboundReadDataDbgrid1(LPDISPATCH RowBuf, VARIANT FAR* StartLocation, BOOL ReadPriorRows)
{
// TODO: Add your control notification handler code here
long lRowsFetched = 0L, lCurRow = 0L, i = 0L;
short incr = 0;
if(ReadPriorRows)
incr = -1; //从后往前读
else
incr = 1; //从前往后读
if(StartLocation->vt == VT_NULL)
{
if(ReadPriorRows)
lCurRow = m_iMaxRow - 1L; //从最后一行开始读
else
lCurRow = 0L; //从第一行开始读
}
else //从StartLocation的后一行或前一行开始读
lCurRow = StartLocation->lVal + incr;
RowBuffer Rowbuf;
Rowbuf.AttachDispatch(RowBuf);
long lRowCount = Rowbuf.GetRowCount(); //总行数
short nColCount = Rowbuf.GetColumnCount();//总列数
CString sCellText; //单元格中的内容
while(i < lRowCount)
{
if(lCurRow < 0L || lCurRow >= m_iMaxRow)
{ //数据读完了,返回读取的行数
Rowbuf.SetRowCount(lRowsFetched);
Rowbuf.DetachDispatch();
return;
}
for(short j = 0; j < nColCount; j ++)
{ //将数据读入表格单元格
sCellText = m_aData[lCurRow]->GetAt(j);
COleVariant newVariant(sCellText);
Rowbuf.SetValue(i, j, newVariant);
}
//将行索引作为该行的Bookmark
COleVariant varValue(lCurRow);
Rowbuf.SetBookmark(i, varValue);
lCurRow += incr;
lRowsFetched ++; i ++;
}
Rowbuf.SetRowCount(lRowsFetched);
Rowbuf.DetachDispatch();
}
void CMyDialog::OnUnboundWriteDataDbgrid1(LPDISPATCH RowBuf, VARIANT FAR* WriteLocation)
{
// TODO: Add your control notification handler code here
VARIANT varResult; //接收返回的字符串
CString str;
RowBuffer Rowbuf;
Rowbuf.AttachDispatch(RowBuf);
short nColCount = Rowbuf.GetColumnCount();//总列数
long lCurRow = WriteLocation->lVal;//得到被编辑的行的索引
for(short i = 0; i < nColCount; i ++)
{
varResult = Rowbuf.GetValue(0L, i);
if(varResult.vt != VT_NULL)//表明此单元格内容被修改
{
str = varResult.bstrVal;
m_aData[lCurRow]->SetAt(i, str);//记录结果
}
}
Rowbuf.DetachDispatch();
}
void CMyDialog::OnUnboundAddDataDbgrid1(LPDISPATCH RowBuf, VARIANT FAR* NewRowBookmark)
{
// TODO: Add your control notification handler code here
VARIANT varResult; CString str;
RowBuffer Rowbuf;
Rowbuf.AttachDispatch(RowBuf);
short nColCount = Rowbuf.GetColumnCount(); //总列数
CStringArray * pRowData = new CStringArray; //为新数据分配空间
pRowData->SetSize(nColCount);
m_aData.SetAtGrow(m_iMaxRow, pRowData);
for(short i = 0; i < nColCount; i ++)
{ //记录新输入的数据
varResult = Rowbuf.GetValue(0L, i);
if(varResult.vt != VT_NULL)//当前单元格输入了数据
{
str = varResult.bstrVal;
m_aData[m_iMaxRow]->SetAt(i, str);
}
}
Rowbuf.DetachDispatch();
//为新增的行设置Bookmark
NewRowBookmark->vt = VT_I4;
NewRowBookmark->lVal = m_iMaxRow;
m_iMaxRow ++;
}
void CMyDialog::OnUnboundDeleteRowDbgrid1(VARIANT FAR* Bookmark)
{
// TODO: Add your control notification handler code here
long lCurRow = Bookmark->lVal;//获得用户删除的行的索引
CStringArray * pDelRow = m_aData[lCurRow]; // 获得指向该行的指针
m_aData.RemoveAt(lCurRow);//删除数据
delete pDelRow;
m_iMaxRow --; // 行数减一
}
void CMyDialog::OnButtonClickDbgrid1(short ColIndex)
{
// TODO: Add your control notification handler code here
RECT rect; Column Col;
COleVariant CurCol(ColIndex);
LPDISPATCH pCol = m_DBGrid.GetColumns(CurCol);
Col.AttachDispatch(pCol);
//得到当前单元格的位置和宽度、高度
float LeftEdge = Col.GetLeft();
float Width = Col.GetWidth();
float Height = m_DBGrid.GetRowHeight();
short nCurRow = m_DBGrid.GetRow();
float Top = m_DBGrid.RowTop(nCurRow);
Col.DetachDispatch();
//将列表框移到合适的位置并设置其大小
m_DBGrid.GetWindowRect(&rect);
ScreenToClient(&rect);
m_ListBox.MoveWindow((short)(rect.left + LeftEdge + Width),
(short)(-Top+rect.top), (int)Width, (int)(5*Height));
m_ListBox.AddString(_T("高中"));
m_ListBox.AddString(_T("大专"));
m_ListBox.AddString(_T("本科"));
m_ListBox.AddString(_T("研究生"));
m_ListBox.BringWindowToTop();
m_ListBox.ShowWindow(SW_SHOW);//从隐藏变为显示
m_ListBox.SetFocus();
}
void CMyDialog::OnScrollDbgrid1(short FAR* Cancel)
{
// TODO: Add your control notification handler code here
if(m_ListBox.IsWindowVisible())
*Cancel = TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -