【C#】マウス位置のDataGridViewをマウスホイールでスクロール
関連記事
- なんと!今週の土曜日も仕事してますた.来週の土曜日も仕事臭い.TCPソケット通信をゴリゴリ書いてる 【2025年08月30日(土)】
- 【ChatGPT o4】祝日なのに仕事ーバテすぎー今夜こそ絶命だーモルモルモル 【2025年03月20日(木)】
- 【#CSharp】制御系が動き出したべ 【2025年02月20日(木)】
- 【#CSharp】ひっさしぶりにガッツリなC#案件をやっているぞー 【2025年02月19日(水)】
- 今週もひたすらモノ書き #JAVA 祭りがまだまだ続く そろそろC#を書かせろー 【2024年12月04日(水)】
DataGridViewにフォーカスセットして,マウスホイールスクロールは
アチコチで事例があったけど
カーソルを置いただけで,マウスホイールが効くようにする事例,あまりなかったので
ちと難儀したので,書き留めておきまする
参考にしたページ
マウスのホイールをコロコロした時にマウスオーバー(エンター?)しているコントロールのスクロールバーを動かしたい@MSDNのページ
【C#.NET】TreeViewをスクロールバーを使用せずスクロールさせる方法@吟遊詩人の戯言
まずは,マウスカーソルインでコントロールを制御させるクラス.
NativeWindowを継承させて作成するとのこと
こちらのページをまんま使用しました
提供者さま,さんくすです
public class MessageForwarder : NativeWindow, IMessageFilter
{
private Control _Control;
private Control _PreviousParent;
private HashSet<int> _Messages;
private bool _IsMouseOverControl;
public MessageForwarder(Control control, int message)
: this(control, new int[] { message })
{
}
public MessageForwarder(Control control, IEnumerable<int> messages)
{
_Control = control;
AssignHandle(control.Handle);
_Messages = new HashSet<int>(messages);
_PreviousParent = control.Parent;
_IsMouseOverControl = false;
control.ParentChanged += new EventHandler(control_ParentChanged);
control.MouseEnter += new EventHandler(control_MouseEnter);
control.MouseLeave += new EventHandler(control_MouseLeave);
control.Leave += new EventHandler(control_Leave);
if (control.Parent != null)
{
Application.AddMessageFilter(this);
}
}
public bool PreFilterMessage(ref Message m)
{
if (_Messages.Contains(m.Msg) &&
_Control.CanFocus &&
!_Control.Focused &&
_IsMouseOverControl)
{
m.HWnd = _Control.Handle;
WndProc(ref m);
return true;
}
return false;
}
void control_ParentChanged(object sender, EventArgs e)
{
if (_Control.Parent == null)
{
Application.RemoveMessageFilter(this);
}
else
{
if (_PreviousParent == null)
{
Application.AddMessageFilter(this);
}
}
_PreviousParent = _Control.Parent;
}
void control_MouseEnter(object sender, EventArgs e)
{
_IsMouseOverControl = true;
}
void control_MouseLeave(object sender, EventArgs e)
{
_IsMouseOverControl = false;
}
void control_Leave(object sender, EventArgs e)
{
_IsMouseOverControl = false;
}
}
次,該当のDataGridViewが置いてあるフォームにコードを追加Win32APIを叩いて,スクロールを実現させまする
うちのブログに記載した,TreeViewをスクロールバーなしでスクロールさせるやり方をちょいと弄って?
using System.Runtime.InteropServices; //InteropServicesをusingしてくださいませ
#region API
#region Win32APIのSendMessage()を使って,コンポーネントに対しメッセージを直接投げつけます
[DllImport(“user32.dll”,CharSet=CharSet.Unicode)]
public static extern Int32 SendMessage(IntPtr hWnd, Int32 Msg, int wParam, int lParam);
//メッセージ関係の定数を定義
private const int WM_VSCROLL = 0x115; //WM_VSCROLLメッセージを使用します
private const int SB_LINEUP = 0; //上矢印を押した
private const int SB_LINEDOWN = 1; //下矢印を押した
private const int SB_PAGEUP = 2; //上ページアップを押した
private const int SB_PAGEDOWN = 3;
#endregion
private MessageForwarder m_WheelCtrl; //上記のクラスのインスタンスを格納しておきます
public Form1()
{
InitializeComponent();
m_WheelCtrl = new CMNCommon.Classes.MessageForwarder(dgvCtrl, 0x20A); //MessageForwarder のインスタンス生成.対象はDataGridView
dgvCtrl.MouseWheel += new MouseEventHandler(dgvCtrl_MouseWheel); //ホィールイベントを結びつける
}
private void dgvCtrl_MouseWheel(object sender, MouseEventArgs e)
{
//Win32APIを使って垂直スクロールのメッセージを飛ばす
if (e.Delta > 0) //アップ方向(下方向スクロール時)フラグが立っているとき
{
for(int i = 0; i < 10; i++)
{
SendMessage(dgvCtrl.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
}
}
else
{
for(int i = 0; i < 10; i++)
{
SendMessage(dgvCtrl.Handle, WM_VSCROLL, SB_LINEUP, 0);
}
}
}
[/csharp]
たぶん,このやり方
PictureBoxでのマウスホイールを使った,拡大縮小とかにも使えるはず?
参考にドゾー

コメント
「ニュウハクシミ」紙を食べる害虫 生息19都道府県に拡大
文化の日な祭日らしい.文化的に休むもうず?
【#HTVX】新型宇宙輸送船HTV-X 油井さんがロボットアームでキャッチ
【冬】来週11℃予想とか出てきてる.秋があったのか,今年(25/10/23)
【ツル】出水平野に冬の訪れを告げるツル第一陣・今季初飛来 鹿児島(25/10/21)
都城でプロ野球・ロッテ1軍が春季キャンプ実施へ
山形で開催の映画祭 監督など国内外の参加者が“芋煮”味わう
連休なんで生駒高原でコスモスを愛でてきた
連休の中日ですの.天気は良くなる方向じゃげ