C#監視窗口的鼠標滾輪事件和鍵盤事件

this.KeyPreview = true;
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);

this.KeyPreview = true是要讓窗體優先響應按鍵事件。ide

void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                ShowPrevPicture();
            }
            else
            {
                ShowNextPicture();
            }
        }

我這裏是讓鼠標滾輪往下滾時顯示下一張圖,往上滾時顯示上一張。函數

按鍵事件的話須要重載ProcessDialogKey函數this

protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                case Keys.Up:
                    ShowPrevPicture();
                    return true;
                case Keys.Right:
                case Keys.Down:
                case Keys.Enter:
                    ShowNextPicture();
                    return true;
                case Keys.Delete:
                    DeleteCurrentPicutreFromHardDisk();
                    return true;
                case Keys.Subtract:
                    DownImageLevel(PictureList.ElementAt(curIndex));
                    DeleteCurrentPicutureFromShowList();
                    return true;
                case Keys.Add:
                case Keys.Insert:
                    UpImageLevel(PictureList.ElementAt(curIndex));
                    DeleteCurrentPicutureFromShowList();
                    return true;
            }
            return false;
        }
相關文章
相關標籤/搜索