作者:yifei(尹加俊)
為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的代碼,沒有用網絡上那些演示代碼的方法,采用了一種比較實用的方法。