INDEX
Microsoft Access のフォームでマウスホイールを回した時のイベントを拾う方法。Access 2000 の時の内容、以降のバージョンで修正されてるかは不明。
概要
Microsoft Office の Microsoft Access にある標準のフォームでは、マウスのホイールを回したときにそのイベントを拾うことが出来ない。なので、Win32API を使って、フォームをサブクラス化。フォームで発生したマウスのホイールのメッセージを取得できるようにする。
方法としては2種類。VBかVCを使って、サブクラス化するActiveX DLLを作成し、Access からDLLを参照する。もう一つは、DLLを使用せず、Access 内にすべてのコードを記載する。
ただし、Access のみで実現する方法は、Microsoft Office Visual Basic Editor 読み込み後の、ウィンドウのサブクラス化には問題があるため、ActiveX DLL を作成し、アプリケーションから参照する方法を強く推奨する。
Microsoft のサポートで公開されている内容をまとめたものです。詳しくは、http://support.microsoft.com/kb/278379/ja をご覧ください。
MouseWheel ActiveX DLL を利用してイベントを拾う
下記のDLL作成手順で作成したDLL (もしくは、一番下のファイルの中身のDLL) を利用してホイールのイベントを拾うフォームを作成します。
参照設定の追加
Microsoft Access から Visual Basic Editor を起動し、[ツール] メニューの [参照設定] を選択して参照設定画面を開きます。「参照可能なライブラリ ファイル」の一覧から「MouseWheel」にチェックをします。もし、一覧に表示されない場合は、[参照] ボタンを押下して、MouseWheel.DLL を選択し開きます。
フォームのクラスモジュールへの追加
対象のフォームに以下のコードを追加します。既に Load, Close イベントがある場合は、既存とあわせて追加します。
1 |
Option Compare Database
Option Explicit
Private WithEvents clsMouseWheel As MouseWheel.CMouseWheel
Private Sub Form_Load()
Set clsMouseWheel = New MouseWheel.CMouseWheel
Set clsMouseWheel.Form = Me
clsMouseWheel.SubClassHookForm
End Sub
Private Sub Form_Close()
clsMouseWheel.SubClassUnHookForm
Set clsMouseWheel.Form = Nothing
Set clsMouseWheel = Nothing
End Sub
Private Sub clsMouseWheel_MouseWheel(Cancel As Integer)
MsgBox "You cannot use the mouse wheel to scroll records."
Cancel = True
End Sub
|
ホイールイベントの確認
フォームビューでイベントを追加したフォームを開き、マウスのホイールを回します。
ホイールを回すと次のメッセージが表示されます。You cannot use the mouse wheel to scroll through records.また、テーブルのレコードと連携している場合でも、現在のレコードが変わりません。
Visual Basic 6.0 で ActiveX DLL を作成する
Microsoft Visual Basic 6.0 を起動し、新規作成で「ActiveX DLL」プロジェクトを作成し、開きます。以下のコードを追加して、[ファイル] メニューの [MouseWheel.dll の作成] で DLL を作成します。
クラスモジュール(CMouseWheel)の追加
新しいクラスモジュールを作成して、以下のコードを入力します。オブジェクト名は、CMouseWheel として保存します。また、プロパティで「Instancing」を「5 - MultiUse」に設定します。
1 |
Option Compare Text
Option Explicit
Private frm As Object
Private intCancel As Integer
Public Event MouseWheel(Cancel As Integer)
Public Property Set Form(frmIn As Object)
Set frm = frmIn
End Property
Public Property Get MouseWheelCancel() As Integer
MouseWheelCancel = intCancel
End Property
Public Sub SubClassHookForm()
lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf WindowProc)
Set CMouse = Me
End Sub
Public Sub SubClassUnHookForm()
Call SetWindowLong(frm.hwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub
Public Sub FireMouseWheel()
RaiseEvent MouseWheel(intCancel)
End Sub
|
標準モジュール(basSubClassWindow)の追加
新しい標準モジュールを作成して、以下のコードを入力します。オブジェクト名は、basSubClassWindow として保存します。
1 |
Option Compare Text
Option Explicit
Public CMouse As CMouseWheel
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = -4
Public Const WM_MouseWheel = &H20A
Public lpPrevWndProc As Long
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case uMsg
Case WM_MouseWheel
CMouse.FireMouseWheel
If CMouse.MouseWheelCancel = False Then
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End If
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End Select
End Function
|
Microsoft Access のみを使用してイベントを拾う
警告 : 可能な限り上記の ActiveX DLL を使用する方法を使用するようにしてください。下記の方法を使用できるのは、アプリケーションのユーザーが Microsoft Access 内で Visual Basic Editor を読み込まない場合に限られます。
Microsoft Office Visual Basic Editor 読み込み後のウィンドウのサブクラス化には問題があるため、Visual Basic Editor を開いた後に Microsoft Access のフォームを開いたり閉じたりすると以下のコードによって Microsoft Access が応答を停止します。また、一度でも Visual Basic Editor を読み込んだ場合は、フォームを表示したり、コードのテストを行う前に、Microsoft Access を再起動する必要があります。
標準モジュール(basSubClassWindow)の追加
VBEを起動し、新しい標準モジュールを作成して、以下のコードを入力します。オブジェクト名は、basSubClassWindow として保存します。
1 |
Option Compare Database
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = -4
Public Const WM_MouseWheel = &H20A
Public lpPrevWndProc As Long
Public CMouse As CMouseWheel
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Look at the message passed to the window. If it is
'a mouse wheel message, call the FireMouseWheel procedure
'in the CMouseWheel class, which in turn raises the MouseWheel
'event. If the Cancel argument in the form event procedure is
'set to False, then we process the message normally, otherwise
'we ignore it. If the message is something other than the mouse
'wheel, then process it normally
Select Case uMsg
Case WM_MouseWheel
CMouse.FireMouseWheel
If CMouse.MouseWheelCancel = False Then
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End If
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End Select
End Function
|
クラスモジュール(CMouseWheel)の追加
VBEを起動し、新しいクラスモジュールを作成して、以下のコードを入力します。オブジェクト名は、CMouseWheel として保存します。
1 |
Option Compare Database
Option Explicit
Private frm As Access.Form
Private intCancel As Integer
Public Event MouseWheel(Cancel As Integer)
Public Property Set Form(frmIn As Access.Form)
'Define Property procedure for the class which
'allows us to set the Form object we are
'using with it. This property is set from the
'form class module.
Set frm = frmIn
End Property
Public Property Get MouseWheelCancel() As Integer
'Define Property procedure for the class which
'allows us to retrieve whether or not the Form
'event procedure canceled the MouseWheel event.
'This property is retrieved by the WindowProc
'function in the standard basSubClassWindow
'module.
MouseWheelCancel = intCancel
End Property
Public Sub SubClassHookForm()
'Called from the form's OnOpen or OnLoad
'event. This procedure is what "hooks" or
'subclasses the form window. If you hook the
'the form window, you must unhook it when completed
'or Access will crash.
lpPrevWndProc = SetWindowLong(frm.hwnd, GWL_WNDPROC, AddressOf WindowProc)
Set CMouse = Me
End Sub
Public Sub SubClassUnHookForm()
'Called from the form's OnClose event.
'This procedure must be called to unhook the
'form window if the SubClassHookForm procedure
'has previously been called. Otherwise, Access will
'crash.
Call SetWindowLong(frm.hwnd, GWL_WNDPROC, lpPrevWndProc)
End Sub
Public Sub FireMouseWheel()
'Called from the WindowProc function in the
'basSubClassWindow module. Used to raise the
'MouseWheel event when the WindowProc function
'intercepts a mouse wheel message.
RaiseEvent MouseWheel(intCancel)
End Sub
|
フォームのクラスモジュールへの追加
対象のフォームに以下のコードを追加します。既に Load, Close イベントがある場合は、既存とあわせて追加します。
1 |
Option Compare Database
Option Explicit
'Declare a module level variable as the custom class
'and give us access to the class's events
Private WithEvents clsMouseWheel As CMouseWheel
Private Sub Form_Load()
'Create a new instance of the class,
'and set the class's Form property to
'the current form
Set clsMouseWheel = New CMouseWheel
Set clsMouseWheel.Form = Me
'Subclass the current form by calling
'the SubClassHookForm method in the class
clsMouseWheel.SubClassHookForm
End Sub
Private Sub Form_Close()
'Unhook the form by calling the
'SubClassUnhook form method in the
'class, and then destroy the object
'variable
clsMouseWheel.SubClassUnHookForm
Set clsMouseWheel.Form = Nothing
Set clsMouseWheel = Nothing
End Sub
Private Sub clsMouseWheel_MouseWheel(Cancel As Integer)
'This is the event procedure where you can
'decide what to do when the user rolls the mouse.
'If setting Cancel = True, we disable the mouse wheel
'in this form.
MsgBox "You cannot use the mouse wheel to scroll through records."
Cancel = True
End Sub
|
ホイールイベントの確認
一度、Microsoft Access を終了し再度開きます。フォームビューでイベントを追加したフォームを開き、マウスのホイールを回します。
ホイールを回すと次のメッセージが表示されます。You cannot use the mouse wheel to scroll through records.また、テーブルのレコードと連携している場合でも、現在のレコードが変わりません。
MouseWheel - MS-Access extension ActiveX DLL
本ソフトウェアは、Microsoft Access のフォームでマウスホイールを回した時のイベントを拾うための機能拡張用DLLです。本ページで記述した内容、Microsoft のサポートで公開されている内容で、ActiveX DLL を作成したものです。
- ダウンロード: MouseWheel.cab(586)
- MD5:C795F6E236ECCEA3AC5202D9F80C0871
- SHA1:A37FFB64EC57EC1B5D9F6CFDF2F0CD915ACAED82
使用条件・配布条件
- 営利・非営利問わず自由に利用してください。
- アーカイブの内容を変更しなければ自由に配布を行ってかまいません。
- 営利目的での配布、雑誌・書籍への収録や添付はお断り致します。
- ただし、自身で開発されたAccessアプリケーションのファイル(*.MDB, *.ADP)と同梱して配布される場合は、レジストリ登録などを始めとするすべてのサポートを開発者か配布者が行うことを前提で、営利・非営利問わず配布してかまいません。
免責事項
本ソフトウェアを使用した結果生じたいあらゆる事象(損害,損失など)において作者は一切責任を負わないものとします。なお本ソフトウェアにバグが発見された場合、作者は修正及びバージョンアップの義務を負わないものとします。
最終更新時間:2011年04月15日 12時59分50秒 指摘や意見などあればSandBoxのBBSへ。
MouseWheel.cab