標籤:對比 val padding handler png 安裝 order 同步 滑鼠
找了些例子,要麼龐大、要麼搞個安裝組件什麼的,我要求能用就行了。實在找例子修改麻煩,就做了一個。其實實現挺簡單,就是panel或圖片什麼的跟著滑鼠走就行了。
這裡panel自己可以加背景圖或直接搞個圖就行了。為了示範清楚,有個捲軸控制項做對比,與自訂的同步。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace customscroll{ public partial class Form1 : Form { int limt, set_x; //滾動位置最大值和固定的左右的位置 bool mouse_Press = false; //滑鼠按下 bool mouse_Wheel = false; //滑輪是否滾動 Point mouseOff; //存放當前滑鼠位置 public Form1() { InitializeComponent(); //向panel填充一堆內容 for (int i = 0; i < 25; i++) { Panel num_panel = new Panel(); Label num = new Label(); num.Text = i + "."; num.ForeColor = Color.FromArgb(255, 255, 255); num.Width = 30; num.Dock = DockStyle.Left; //設定滑鼠滑輪事件,需將mouse_Wheel 值為true,在move裡置,下面有,這裡就不寫了 //num.MouseWheel += new MouseEventHandler(OnMouseWheel); num_panel.Name = "Panel_" + i; num_panel.Height = 35; num_panel.Margin = new Padding(0, 0, 0, 0); num_panel.BackColor = Color.SteelBlue; num_panel.BorderStyle = BorderStyle.Fixed3D; num_panel.Dock = DockStyle.Top; num_panel.BorderStyle = System.Windows.Forms.BorderStyle.None; num_panel.Controls.Add(num); //設定滑鼠滑輪事件,需將mouse_Wheel 值為true,在move裡置,下面有,這裡就不寫了 //num_panel.MouseWheel += new MouseEventHandler(OnMouseWheel); Content_panel.Controls.Add(num_panel); //將內容裝入 //設定滑鼠滑輪事件,需將mouse_Wheel 值為true,在move裡置,下面有,這裡就不寫了 //Content_panel.MouseWheel += new MouseEventHandler(OnMouseWheel); } //裝內容panel自動大小 Content_panel.AutoSize = true; set_x = ScrollHard_panel.Location.X; //固定左右位置為當前位置 limt = ScrollBar_panel.Height - ScrollHard_panel.Height; //滾動最大高度 ScrollHard_panel.Location = new Point(set_x,0) ; //先將位置設定到最頂 vScrollBar1.Maximum = limt; //放了個vScrollBar組件,示範用的,和自訂的同步 //滑鼠滑輪事件 ScrollBar_panel.MouseWheel += new MouseEventHandler(OnMouseWheel); ScrollHard_panel.MouseWheel += new MouseEventHandler(OnMouseWheel); vScrollBar1.MouseWheel += new MouseEventHandler(OnMouseWheel); } //滑鼠滑輪事件 private void OnMouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { int set_y = 0; if (mouse_Wheel) //是否判斷滑鼠滑輪 { if (e.Delta > 0) //滑輪向上 { set_y = ScrollHard_panel.Location.Y - 10; //每次移動10 if (set_y < 0) { set_y = 0; } //超範圍 } if (e.Delta < 0) //滑輪向下 { set_y = ScrollHard_panel.Location.Y + 10; //每次移動10 if (set_y > limt) { set_y = limt; } //超範圍 } ScrollHard_panel.Location = new Point(set_x, set_y); //滾動塊的定位 vScrollBar1.Value = set_y; //示範用的捲軸,和自訂的同步 Content_panel.Top = -set_y; //裝內容的panel滾動顯示 } } //自訂滾動“塊”框滑鼠按下 private void ScrollHard_panel_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) //滑鼠左鍵 { mouseOff.Y = e.Y; //取當前位置 mouse_Press = true; //滑鼠按下 } } //自訂滾動“塊”滑鼠放開 private void ScrollHard_panel_MouseUp(object sender, MouseEventArgs e) { mouse_Press = false; //滑鼠放開 } //自訂滾動“塊”滑鼠離開範圍 private void ScrollHard_panel_MouseLeave(object sender, EventArgs e) { mouse_Wheel = false; //滑輪不可用 } ////自訂滾動“塊”滑鼠在範圍 private void ScrollHard_panel_MouseMove(object sender, MouseEventArgs e) { mouse_Wheel = true; //可以用滑輪 if (mouse_Press) //滑鼠按下狀態 { int set_y = ScrollHard_panel.Top + e.Y - mouseOff.Y; //計算當前縱向座標 if (set_y < 0) { set_y = 0; } //超範圍 else if (set_y > limt) { set_y = limt; } //超範圍 else { ScrollHard_panel.Location = new Point(set_x, set_y); } //滾動塊的定位 vScrollBar1.Value = set_y; //示範的捲軸和自訂的同步 Content_panel.Top = -set_y; //裝內容的panel滾動顯示 } } //在滾動“框”範圍內 private void ScrollBar_panel_MouseMove(object sender, MouseEventArgs e) { mouse_Wheel = true; //可以使用滑輪 } //離開滾動“框” private void ScrollBar_panel_MouseLeave(object sender, EventArgs e) { mouse_Wheel = false; //不可使用滑輪 } //自訂滾動“框”滑鼠放開 private void ScrollBar_panel_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) //滑鼠左鍵 { int set_y = e.Y; //當前縱座標 if (set_y > limt) { set_y = limt; } //超範圍 ScrollHard_panel.Location = new Point(set_x, set_y); //滾動塊定位 vScrollBar1.Value = set_y; //示範的捲軸,和自訂的同步 Content_panel.Top = -set_y;//裝內容的panel滾動顯示 mouse_Press = false; //滑鼠為放開狀態 } } //示範用的vScrollBar1組件,也可以控制裝內容的panel滾動顯示 private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) { Content_panel.Top = -vScrollBar1.Value; } }}
初學c# -- 學習筆記(五) winfrom自訂捲軸