【C#】マウス位置のDataGridViewをマウスホイールでスクロール
関連記事
【ChatGPT o4】祝日なのに仕事ーバテすぎー今夜こそ絶命だーモルモルモル 【2025年03月20日(木)】
- 【#CSharp】制御系が動き出したべ 【2025年02月20日(木)】
- 【#CSharp】ひっさしぶりにガッツリなC#案件をやっているぞー 【2025年02月19日(水)】
- 今週もひたすらモノ書き #JAVA 祭りがまだまだ続く そろそろC#を書かせろー 【2024年12月04日(水)】
- 【#CSHARP】ソリューション内でサービス,Winformアプリを混在で作成させ,バイナリ出力先を同じにするとサービスが起動しない 【2024年02月08日(木)】
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でのマウスホイールを使った,拡大縮小とかにも使えるはず?
参考にドゾー
コメント
【#桜】国富:大坪の一本桜を見物に行った!咲いてなかったドロドロドロ(25/3/22)
3月下旬なのに鰐塚山が雪を被ってる.これはヤバス(25/3/19)
JR日豊線・仙巌園駅3月15日開業 周辺含む総整備費は12億円、開業合わせ磯ビーチハウスにカフェオープン
【#確定申告】税務がオワタ!所得税&消費税申告書を提出した!財務省の役人どもはこの恨み,祝ってやるドロドロドロ
【#火事】都農町のキャンプ場近くで山林火災 現在も消火活動中(27日午後6時15分現在)
宮崎ー台湾 国際定期便が週2往復に増便へ 来月30日から(25/2/15)
『にっぽん縦断 こころ旅』4月から田中美佐子が新たな旅人に 火野正平さんが14年間担当
オリックス・バファローズのキャンプ見学に行ってきた,昨日(爆(25/2/9)