BCB的DBGrid不支持鼠标滑轮滚动,只能点击ScrollBar的上下键移动,很不方便,下面使用替换窗口过程模拟上下键点击来支持滑轮滚动
代码编写者:妖哥
FARPROC pOldProc = 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(pOldProc, hWnd, uMsg, wParam, lParam)); } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormShow(TObject *Sender) { // 设置新的窗体函数 pOldProc = (FARPROC)::SetWindowLong(DBGrid->Handle, GWL_WNDPROC, (long)DBGridProc); } //--------------------------------------------------------------------------- void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action) { // 还原窗体函数 ::SetWindowLong(this->DBGrid->Handle, GWL_WNDPROC, (long)pOldProc); }
本人封装的类:
#ifndef REPLACEPROC_H #define REPLACEPROC_H #include <windows.h> #include <map> using namespace std; class ReplaceDBGridWndProc { static map<HWND,WNDPROC> s_MapofOldProc; static WNDPROC s_OldWndProc; static LRESULT CALLBACK NewWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) { if (message == WM_MOUSEWHEEL) { if ((short)HIWORD(wParam) > 0) { ::PostMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0); } else { ::PostMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0); } } map<HWND,WNDPROC>::iterator it=s_MapofOldProc.find(hwnd); LRESULT result; if(it!=s_MapofOldProc.end()) result=CallWindowProc(it->second,hwnd,message,wParam,lParam); else result=DefWindowProc(hwnd,message,wParam,lParam); return result; } public: static void ReplaceWndProc(HWND hwnd) { s_MapofOldProc[hwnd]=(WNDPROC)SetWindowLong(hwnd,GWL_WNDPROC,(LONG)NewWndProc); } }; WNDPROC ReplaceDBGridWndProc::s_OldWndProc=NULL; map<HWND,WNDPROC> ReplaceDBGridWndProc::s_MapofOldProc; extern ReplaceDBGridWndProc g_ReplaceProc; #endif