作者:yifei(尹加俊)
信箱:yifei1900@163.com
为DBGrid增加鼠标滚动功能的演示,核心代码如下:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "MainFormUnit.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TMainForm *MainForm; //--------------------------------------------------------------------------- FARPROC OldProc = NULL; //用于保存旧的DBGrid控件的WindowProc //--------------------------------------------------------------------------- /** * 声明新的DBGrid的WindowProc */ LRESULT CALLBACK DBGridProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { //处理感兴趣的Window消息 if(uMsg == WM_MOUSEWHEEL) { if((short)HIWORD(wParam) > 0) { ::PostMessage(hWnd,WM_VSCROLL,SB_LINEUP,0); } else { ::PostMessage(hWnd,WM_VSCROLL,SB_LINEDOWN,0); } } //调用原DBGrid的WindowProc return (::CallWindowProc(OldProc,hWnd,uMsg,wParam,lParam)); } //--------------------------------------------------------------------------- __fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormShow(TObject *Sender) { this->ADOCn->ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExtractFilePath(Application->ExeName) + "Data.mdb;Persist Security Info=True"; this->ADOCn->Connected = true; this->DataSet->Open(); //设置新的窗体函数 OldProc = (FARPROC)::SetWindowLong(this->DBGrid->Handle,GWL_WNDPROC,(long)DBGridProc); } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action) { //还原窗体函数 ::SetWindowLong(this->DBGrid->Handle,GWL_WNDPROC,(long)OldProc); this->ADOCn->Connected = false; } //---------------------------------------------------------------------------
今天偶然看到了2ccc站点上有给DBGrid增加滚动功能的代码,想想这个功能还真的很实用(但是他们的演示代码真的不怎么样,没有一点实用性),又搜索了一下网络发现只有Delphi和VB的例子,于是我就把写了一份BCB的代码,没有用网络上那些演示代码的方法,采用了一种比较实用的方法。 |