2013-09-16 44 views
0

我正在嘗試使用picturebox控件和trackbar進行圖像幻燈片放映。軌跡條的最小值和最大值對應於要顯示的圖像數量。我使用計時器來獲取幻燈片的間隔時間以及跟蹤欄值變化。Mousewheel在C#winform中以編程方式向下滾動事件

現在,這裏是在picturebox中的每個圖像的主要內容我正在圖像上繪製一個矩形框。

當表格在第一張圖片加載時,我無法繪製第一張圖片。但如果我滾動鼠標滾輪,我可以做。

我需要幫助來觸發鼠標滾輪事件後第一個圖像加載。

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 test 
{ 

    public partial class Form1 : Form 
    { 
    public event MouseEventHandler MouseWheel; 

    //MouseEventArgs k = new MouseEventArgs(MouseButtons.Middle,0,0,-1); 

    string[] pics = { "C:\\Downloads\\folder_picture_green.png", "C:\\Downloads\\Aetherpal.ico", "C:\\Downloads\\folder_picture_green.png" }; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.trackBar1.Minimum = 0; 
     this.trackBar1.Maximum = (pics.Count() - 1); 
     //this.trackBar1.Maximum = 0; 

     imageupdate(0); 

     timer1.Start(); 
     timer1.Interval = 3000; 
     this.MouseWheel += test; 
     this.MouseWheel(null, null); 
    } 

    private void test(object sender, System.Windows.Forms.MouseEventArgs e) 
    { 
     MessageBox.Show("Scrolled"); 
    } 

    private void check(object sender, System.EventArgs e) 
    { 
     //if (Initializing == false) { return; } 
     if(this.trackBar1.Value < this.trackBar1.Maximum) 
     this.trackBar1.Value += 1; 
     else 
      timer1.Stop(); 
    } 

    private void Valuechange(object sender, System.EventArgs e) 
    { 
     imageupdate(this.trackBar1.Value); 
     if(this.trackBar1.Value < this.trackBar1.Maximum) 
      timer1.Start(); 
    }  

    private void imageupdate(int k) 
    { 
     this.pictureBox1.Refresh(); 
     this.pictureBox1.Image = new Bitmap(pics[k]); 
     Pen blackPen = new Pen(Color.Blue, 5); 
     this.pictureBox1.Refresh(); 
     using (Graphics g = this.pictureBox1.CreateGraphics()) 
     { 
      g.DrawRectangle(blackPen, 10, 10, 100, 50); 
     } 
    } 
    } 
} 
+0

又一個CreateGraphics()問題。最小化和恢復應用程序的窗口,看看你爲什麼不應該使用它。改爲執行pictureBox1的Paint事件。 –

回答

0

您可以將此代碼添加到您的形式(與MouseWheel當然)滾動表單:

private void Wheel(int ticks, bool down){ 
    //WM_MOUSEWHEEL = 0x20a 
    Message msg = Message.Create(Handle, 0x20a, new IntPtr((down ? -1 : 1)<<16), new IntPtr(MousePosition.X + MousePosition.Y << 16)); 
    for(int i = 0; i < ticks; i++) 
     WndProc(ref msg); 
} 
//Use it 
Wheel(120,true);//Wheel down 
Wheel(120,false);//Wheel up 

注意:我可以看到你定義自己的形式MouseWheel事件。這會隱藏基地MouseWheel事件,我不認爲你有什麼理由要這樣做,你自己的MouseWheel不能以作爲基地工作,你必須趕上win32 message並自己提出,我們應該使用改爲基於MouseWheel事件。 (也許你認爲你的表單類中沒有任何MouseWheel事件?)