//本人用ip2000调试通过,请各位大虾执教!
//.cpp文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Wwdbgrid"
#pragma link "Wwdbigrd"
#pragma link "Wwdbcomb"
#pragma link "wwdbedit"
#pragma link "Wwdotdot"
#pragma link "wwdblook"
#pragma link "Wwdatsrc"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
MouseMoveFlag=0;
MovingFlag=0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::wwDBGrid1CalcCellColors(TObject *Sender,
TField *Field, TGridDrawState State, bool Highlight, TFont *AFont,
TBrush *ABrush)
{
if (((TwwDBGrid *)Sender)->IsSelected())
{
ABrush->Color = clLtGray; //记录选中后设置颜色
AFont->Color = clBlack;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::wwDBGrid1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if (MouseMoveFlag) //&& wwDBGrid1->MouseCoord(X,Y).X==0)
{
MovingFlag=1;
XXOld=XXTemp;//保存前一次mouseMove事件的坐标
distOld=XXOld.Y-BegRow;
XXMove=wwDBGrid1->MouseCoord(X,Y);
Edit2->Text=IntToStr(XXMove.Y);
temp=XXMove.Y;
if (temp==-1 || temp==0)//去掉mouse在wwDBgrid上下边界处产生错误行号
return;
MovRow=XXMove.Y;
distNew=MovRow-BegRow;//当前MouseMove事件的坐标
XXTemp=XXMove;
//动态选中拖动记录
if (abs(distNew)>abs(distOld))
{
if (distNew>0)
{
for (i=distOld+BegRow;i<distNew+BegRow;i++)
{
wwDBGrid1->SetActiveRow(i);
wwDBGrid1->SelectRecord();
}
}
else
{
for (i=distOld+BegRow;i>distNew+BegRow;i--)
{
wwDBGrid1->SetActiveRow(i-2);
wwDBGrid1->SelectRecord();
}
}
}
else if(abs(distNew)<abs(distOld))
{
if (distOld>=0)
{
for (i=BegRow+distOld;i>BegRow+distNew;i--)
{
wwDBGrid1->SetActiveRow(i-1);
wwDBGrid1->UnselectRecord();
wwDBGrid1->SetActiveRow(i-2);
}
}
else if (distOld<0)
{
for (i=BegRow+distOld-1;i<BegRow+distNew-1;i++)
{
wwDBGrid1->SetActiveRow(i);
wwDBGrid1->UnselectRecord();
wwDBGrid1->SetActiveRow(i+1);
}
}
}
}
Edit4->Text=wwDBGrid1->SelectedList->Count;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::wwDBGrid1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{ //mouse左键单选
if ((Button==mbLeft) && (!(Shift.Contains(ssCtrl))))// && wwDBGrid1->MouseCoord(X,Y).X==0)
{
wwDBGrid1->UnselectAll();
MouseMoveFlag=1;
XXBegin=wwDBGrid1->MouseCoord(X,Y);
BegRow=XXBegin.Y;
wwDBGrid1->SelectRecord();
XXTemp=XXBegin;
}
//按下Ctrl可以多选
if ((Button==mbLeft) && Shift.Contains(ssCtrl))// && wwDBGrid1->MouseCoord(X,Y).X==0)
{
MovingFlag=0;
XXCtrl=wwDBGrid1->MouseCoord(X,Y);
if (XXCtrl.Y!=-1 && XXCtrl.Y!=0)
CtrlRow=XXCtrl.Y;
wwDBGrid1->SetActiveRow(CtrlRow-1);
if (wwDBGrid1->IsSelected()) //如记录已经选中,则不选中该记录
wwDBGrid1->UnselectRecord();
else
wwDBGrid1->SelectRecord();
}
//Mouse在选中记录上按下右键,探出相应menu,否则清除选中记录
if (Button==mbRight)// && wwDBGrid1->MouseCoord(X,Y).X==0)
{
// if ()
MovingFlag=0;
XXRight=wwDBGrid1->MouseCoord(X,Y);
if (XXRight.Y!=-1 && XXRight.Y!=0)
wwDBGrid1->SetActiveRow(XXRight.Y-1);
if (wwDBGrid1->IsSelected())
PopupMenu1->Popup(ClientToScreen(Point(X,Y)).x,ClientToScreen(Point(X,Y)).y);
else
wwDBGrid1->UnselectAll();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::wwDBGrid1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{ //mouse处于拖动选中状态时,鼠标移动过快可能出现漏选记录
//在mouseUp事件中刷新选中记录
MouseMoveFlag=0;
XXTemp=wwDBGrid1->MouseCoord(X,Y);
if (XXBegin.X==0 & XXBegin.Y==0 && XXTemp.X==0 & XXTemp.Y==0)
return;//左上角按钮按下,程序返回
if ((Button==mbLeft) && (MovingFlag==1))// && wwDBGrid1->MouseCoord(X,Y).X==0)
{
temp=XXTemp.Y;
if ((temp!=-1) && (temp!=0))
{
MovRow=temp;
}
distNew=MovRow-BegRow;
wwDBGrid1->UnselectAll();
if (distNew>0)
{
if (BegRow==0)
BegRow=1; //如果在Title上按下Mouse,将鼠标位置设置为第一行
for (i=BegRow-1;i<MovRow;i++)
{
wwDBGrid1->SetActiveRow(i);
wwDBGrid1->SelectRecord();
}
}
else
{
for (i=BegRow+1;i>MovRow;i--)
{
wwDBGrid1->SetActiveRow(i-2);
wwDBGrid1->SelectRecord();
}
}
}
Edit4->Text=wwDBGrid1->SelectedList->Count;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::wwDBGrid1CellChanged(TObject *Sender)
{
wwDBGrid1->Repaint();//记录内容改变刷新
}
//---------------------------------------------------------------------------
.h file
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "Wwdbgrid.hpp"
#include "Wwdbigrd.hpp"
#include <ADODB.hpp>
#include <Db.hpp>
#include <Grids.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
#include <DBGrids.hpp>
#include "Wwdbcomb.hpp"
#include "wwdbedit.hpp"
#include "Wwdotdot.hpp"
#include <Mask.hpp>
#include "wwdblook.hpp"
#include "Wwdatsrc.hpp"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TADOTable *ADOTable1;
TwwDBGrid *wwDBGrid1;
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TEdit *Edit4;
TPopupMenu *PopupMenu1;
TMenuItem *N1;
TADOTable *ADOTable2;
TwwDataSource *wwDataSource1;
TwwDataSource *wwDataSource2;
TwwDBGrid *wwDBGrid2;
TwwDBLookupCombo *wwDBLookupCombo1;
TSplitter *Splitter1;
void __fastcall wwDBGrid1CalcCellColors(TObject *Sender, TField *Field,
TGridDrawState State, bool Highlight, TFont *AFont,
TBrush *ABrush);
void __fastcall wwDBGrid1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall wwDBGrid1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall wwDBGrid1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall wwDBGrid1CellChanged(TObject *Sender);
private: // User declarations
TGridCoord XXMove,XXBegin,XXTemp,XXOld,XXCtrl,XXRight;
short int MouseMoveFlag,MovingFlag;//MouseMoveFlag判断鼠标是否按下后移动,
//MovingFlag判断mouse是否处于拖动多选状态。
int BegRow,MovRow,temp,CtrlRow;//行号
short int distOld,distNew,i;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif