對FlowLayoutPanel添加鼠標滾輪事件函數
在mainform中添加事件this
his.flowLayoutPanel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.flowLayoutPanel1_MouseWheel);
添加滾輪事件函數:spa
private void flowLayoutPanel1_MouseWheel(object sender, MouseEventArgs e) { if (!(flowLayoutPanel1.VerticalScroll.Visible == false || (flowLayoutPanel1.VerticalScroll.Value == 0 && e.Delta > 0) || (flowLayoutPanel1.VerticalScroll.Value == lastRightPanelVerticalScrollValue && e.Delta < 0))) { flowLayoutPanel1.VerticalScroll.Value += 10; lastRightPanelVerticalScrollValue = flowLayoutPanel1.VerticalScroll.Value; flowLayoutPanel1.Refresh(); flowLayoutPanel1.Invalidate(); flowLayoutPanel1.Update(); } }
除此以外還要說的一點是,觸發鼠標的滾動事件後,處理事件的函數參數 MouseEventArgs e 中有個Delta屬性,默認狀況下向上滾動e.Delta=120,向下滾動e.Delta=-120。以上的程序還不是特別完美,由於當Panel控件較大而沒有顯示滾動條時,或滾動條已在最上方而滾輪又是向上滾動,或滾動條已在最下方而滾輪又是向下滾動時,一樣會執行Panel.Refresh();Panel.Invalidate();Panel.Update();等窗體重繪代碼,佔用較多資源。所以能夠在執行這些代碼前先對Panel的當前情況作判斷。code
int lastRightPanelVerticalScrollValue = -1;//爲鼠標滾動事件提供一個靜態變量,用來存儲上次滾動後的VerticalScroll.Value
添加FlowLayoutPanel的鼠標進入事件和鼠標點擊事件orm
private void flowLayoutPanel1_MouseEnter(object sender, EventArgs e) { flowLayoutPanel1.Focus(); } private void flowLayoutPanel1_MouseClick(object sender, MouseEventArgs e) { flowLayoutPanel1.Focus(); }