📄 extendedit.cpp
字号:
// ExtendEdit.cpp : implementation file
//
#include "stdafx.h"
#include "商品库存管理系统.h"
#include "ExtendEdit.h"
#include "RxGrid.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern _ConnectionPtr cnn;
extern CMyApp theApp;
#define VER_WIN2000 3435973836
/////////////////////////////////////////////////////////////////////////////
// CExtendEdit
#define TitBarWidth 22; //标题栏及边线的宽度
#define LineWidth 2;
CExtendEdit::CExtendEdit()
{
m_bInputValid=true;
}
CExtendEdit::~CExtendEdit()
{
}
BEGIN_MESSAGE_MAP(CExtendEdit, CEdit)
//{{AFX_MSG_MAP(CExtendEdit)
ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
ON_WM_KEYDOWN()
ON_WM_KILLFOCUS()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CExtendEdit::ShowGrid()
{
//如果记录集中没有记录
if(theApp.GetRecordCount(rst)<0)
{
IsShow=false;
return;
}
//在编辑框下方画Grid
CRect thisRect,ParentRect,GridRect;
this->GetWindowRect(&thisRect);
GridParent->GetWindowRect(&ParentRect);
GridRect.top=thisRect.top-ParentRect.top-TitBarWidth;
GridRect.left=thisRect.left-ParentRect.left-LineWidth;
GridRect.bottom=GridRect.top+(thisRect.bottom-thisRect.top);
GridRect.right=GridRect.left+(thisRect.right-thisRect.left);
GridRect.top=GridRect.bottom;
GridRect.bottom=(ParentRect.bottom-ParentRect.top)-20;
GridRect.right=(ParentRect.right-ParentRect.left)-20;
int nStyle=WS_TABSTOP|WS_THICKFRAME|WS_BORDER|LVS_EDITLABELS|LVS_REPORT|LVS_SHOWSELALWAYS;
if(Grid.Create(nStyle,GridRect,GridParent,ID_GRID)==false)
{
IsShow=false;
return ;
}
//设置Grid的扩展风格
Grid.SetExtendedStyle(LVS_EX_FULLROWSELECT|
LVS_EX_HEADERDRAGDROP|
LVS_EX_GRIDLINES|
LVS_EX_ONECLICKACTIVATE|
LVS_EX_FLATSB);
Grid.SetBkColor(RGB(255,255,223));
Grid.SetTextBkColor(RGB(255,255,223));
Grid.SetTextColor(RGB(0,0,0));
Grid.EnableAutomation();
IsShow=true;
Grid.SetRecordset(rst);
//判断自动调整后列表中所有列的宽度
int ColWidth=0,RowHeight=0;
for(int i=0;i<rst->GetFields()->GetCount();i++)
{
ColWidth=ColWidth+Grid.GetColumnWidth(i);
}
if(ColWidth<GridRect.right-GridRect.left)
GridRect.right=GridRect.left+ColWidth+10;
for(i=0;i<theApp.GetRecordCount(rst)+1;i++)
{
RowHeight=RowHeight+30;
}
if(RowHeight<GridRect.bottom-GridRect.top)
GridRect.bottom=GridRect.top+RowHeight+10;
Grid.MoveWindow(GridRect);
Grid.ShowWindow(SW_SHOW);
Grid.SetHotItem(0);
return;
}
void CExtendEdit::HideGrid()
{
Grid.DestroyWindow();
IsShow=false;
}
void CExtendEdit::Refresh()
{
Grid.rst=rst;
Grid.Refresh();
}
void CExtendEdit::OnChange()
{
CString sText,sSQL;
if(IsPopShow==false)
return;
GetWindowText(sText);
if(IsOnChange==false)
return;
if (Changed)
{ //判断是否为空
if(sText.IsEmpty()==false)
{
if(sText.GetAt(0)>='A' && sText.GetAt(0)<='z') //判断是不输入的是字母
{
//判断是否启动联想输入
if(this->assn==true)
sSQL.Format("SELECT * FROM %s WHERE 简称 LIKE '%s%%'",DataBase,sText);
else
goto sign;
}
else
sign: sSQL.Format("SELECT * FROM %s WHERE %s LIKE '%s%%'",DataBase,Field,sText);
}
else
sSQL.Format("SELECT * FROM %s WHERE %s LIKE '%s%%'",DataBase,Field,sText);
rst=cnn->Execute((_bstr_t)sSQL,NULL,adCmdText);
}
if(theApp.GetRecordCount(rst)>0)
{
//判断是否已经显示表格
if(IsShow==true)
Refresh();
else
ShowGrid();
}
}
BOOL CExtendEdit::PreTranslateMessage(MSG* pMsg)
{
CString RunTimeClass;
CSize size;
CString CellText;
CRxGrid * Parent =(CRxGrid *)GetParent();
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13)
{
if(IsShow==true)
{
CString CellText;
try{
Grid.rst->MoveFirst();
Grid.rst->Move(Grid.GetHotItem());
CellText=(char*)(_bstr_t)Grid.rst->GetCollect((_bstr_t)Field);
}
catch(...)
{
CellText="";
}
IsOnChange=false;
SetWindowText(CellText);
HideGrid();
IsOnChange=true;
}
else
{
CString RunTimeClass;
RunTimeClass.Format("CListCtrl");
if(this->GetParent()->GetRuntimeClass()->m_lpszClassName==RunTimeClass)
{
CRect rect;
CString CellText;
this->GetWindowText(CellText);
Parent->SetItemText(Parent->GetRow(),Parent->GetCol(),CellText);
this->IsOnChange=false;
int i=Parent->GetColumnCount()-1;
if(Parent->GetCol()<i)
{
CellText=Parent->GetItemText(Parent->GetRow(),Parent->GetCol()+1);
Parent->m_Col=Parent->m_Col+1;
size.cx=45*Parent->m_Col;
}
else
{
size.cx=-45*Parent->m_Col;
CellText=Parent->GetItemText(Parent->GetRow()+1,0);
Parent->m_Col=0;
if(Parent->GetRow()>=Parent->GetItemCount()-1)
{
if(Parent->AutoInsert==true)
{
Parent->InsertItem(Parent->m_Row+1,"");
Parent->m_Row=Parent->m_Row+1;
}
else
{
Parent->m_Row=Parent->m_Row+1;
pMsg->wParam=9;
}
}
else
Parent->m_Row=Parent->m_Row+1;
}
this->SetWindowText(CellText);
Parent->Scroll(size);
Parent->GetSubItemRect(Parent->m_Row,Parent->m_Col,LVIR_LABEL,rect);
this->MoveWindow(rect);
this->IsOnChange=true;
}
}
}
return CEdit::PreTranslateMessage(pMsg);
}
void CExtendEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(IsShow==true) //如果提示表格已显示
{
if(nChar==40)
{
Grid.SetHotItem(Grid.GetHotItem()+1);
LVITEM item;
item.mask=LVIF_DI_SETITEM ;
item.iItem=Grid.GetHotItem();
item.iSubItem=0;
Grid.SetItem(&item);
}
if(nChar==38)
{ if(Grid.GetHotItem()>0)
Grid.SetHotItem(Grid.GetHotItem()-1);
}
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CExtendEdit::OnKillFocus(CWnd* pNewWnd)
{
if(IsShow==true)
this->HideGrid();
CString RunTimeClass;
RunTimeClass.Format("CListCtrl");
if(this->GetParent()->GetRuntimeClass()->m_lpszClassName==RunTimeClass)
{
CRxGrid * Parent =(CRxGrid *)GetParent();
if(Parent)
Parent->EndEdit(m_bInputValid);
m_bInputValid=true;
}
CEdit::OnKillFocus(pNewWnd);
}
int CExtendEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
//设置字体、字号
LOGFONT logfont;
logfont.lfCharSet=134;
logfont.lfClipPrecision=0;
logfont.lfEscapement=0;
logfont.lfHeight=-12;
logfont.lfItalic=0;
logfont.lfOrientation=0;
logfont.lfOutPrecision=0;
logfont.lfPitchAndFamily=2;
logfont.lfQuality=0,
logfont.lfStrikeOut=0;
logfont.lfUnderline=0;
logfont.lfWeight=400;
logfont.lfWidth=0;
strcpy(logfont.lfFaceName,"宋体");
m_cFont.DeleteObject();
m_cFont.CreateFontIndirect(&logfont);
this->SetFont(&m_cFont);
return 0;
}
bool CExtendEdit::SetPopRecordset(_RecordsetPtr prst)
{
if(theApp.GetRecordCount(prst)<=0)
return false;
this->rst=prst;
Changed=false;
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -