Skip to content

Instantly share code, notes, and snippets.

@hexagit
Last active December 17, 2018 08:27
Show Gist options
  • Select an option

  • Save hexagit/6f97973c75d3c845c76d9921705bdf32 to your computer and use it in GitHub Desktop.

Select an option

Save hexagit/6f97973c75d3c845c76d9921705bdf32 to your computer and use it in GitHub Desktop.
Windows7を便利にしたい1
using System;
using System.Runtime.InteropServices;
namespace MouseUnderScroll
{
// Win32API
public class WinAPI
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public struct MouseLowLevelHookStruct
{
public POINT pt;
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(POINT point);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnhookWindowsHookEx(IntPtr hHook);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern IntPtr GetModuleHandle(string lpModuleName);
// フックコードに関する定数
public const int HC_ACTION = 0;
// ウィンドウズフックの定数
public const int WH_MOUSE_LL = 14;
// ウィンドウメッセージの定数
public const int WM_MOUSEWHEEL = 0x020A;
}
public class MouseHook
{
private IntPtr _hHook = IntPtr.Zero;
private WinAPI.HookProc _mouseHookProcedure = null;
// コンストラクタ
public MouseHook()
{
_mouseHookProcedure = new WinAPI.HookProc(LowLevelMouseHookProc);
_hHook = WinAPI.SetWindowsHookEx(WinAPI.WH_MOUSE_LL, _mouseHookProcedure, WinAPI.GetModuleHandle(null), 0);
}
// デストラクタ
~MouseHook()
{
WinAPI.UnhookWindowsHookEx(_hHook);
}
// マウス入力のフックプロシージャ
private int LowLevelMouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if( nCode == WinAPI.HC_ACTION ) {
if( (int)wParam == WinAPI.WM_MOUSEWHEEL ) {
WinAPI.MouseLowLevelHookStruct MyMouseHookStruct = (WinAPI.MouseLowLevelHookStruct)Marshal.PtrToStructure(lParam, typeof(WinAPI.MouseLowLevelHookStruct));
SendMouseWheelMessage(WinAPI.WindowFromPoint(MyMouseHookStruct.pt), MyMouseHookStruct.pt, MyMouseHookStruct.mouseData);
}
}
return WinAPI.CallNextHookEx(_hHook, nCode, wParam, lParam);
}
// マウスホイール操作のメッセージをウィンドウに送信
private void SendMouseWheelMessage(IntPtr hWnd, WinAPI.POINT point, int mouseData)
{
uint mousePos = U16to32((uint)point.x, (uint)point.y);
WinAPI.SendMessage(hWnd, WinAPI.WM_MOUSEWHEEL, (IntPtr)mouseData, (IntPtr)mousePos);
}
// 2つの16bitを1つの32bitにする
private uint U16to32(uint lo, uint hi)
{
uint result = 0;
result += (lo & 0xffff);
result += (hi & 0xffff) << 16;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment