滚轮事件发生一定要激活当前的目标控件,比如目标控件要有焦点,事件本身简单,就是MouseWheel
下面举例panel:
PRivate void panelsplit_MouseEnter(object sender, EventArgs e) { this.panelsplit.MouseWheel += new MouseEventHandler(panelsplit_MouseWheel); this.panelsplit.Focus(); } private void panelsplit_MouseLeave(object sender, EventArgs e) { this.panelsplit.MouseWheel -= new MouseEventHandler(panelsplit_MouseWheel); } private int sumcount = 0; private int spacing = 60; //线条间距 private void panelsplit_MouseWheel(object sender, MouseEventArgs e) { sumcount += e.Delta; //负的放大,正的缩小 int newsp = sumcount / 120 + spacing; AddMsg(newsp); //... panelsplit.Refresh(); panelsplit.Invalidate(); panelsplit.Update(); }private void AddMsg(object msg) { bool scroll = false; if (this.listBox1.TopIndex == this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight)) scroll = true; this.listBox1.Items.Add(msg); if (scroll) this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight); this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1; }鼠标在控件上时响应滚动,移出的之后删除事件不响应,代码只是demo,具体看自己的设计了
新闻热点
疑难解答