左右点击的不同功能

时间:2014-06-03 05:17:26

标签: c#

如何为右键单击和左键单击保留两个不同的事件? 我想zoomIn左键单击和zoomOut右键单击 如果有任何错误或错误请帮助我,我已按以下方式编写代码

我的意思是每次右键单击以及左键单击有两个不同的功能或事件 这是我的计划

 private void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right )
        {
            double zoomLevel = 1.1;
            System.Drawing.Rectangle screenSize = new System.Drawing.Rectangle();

            screenSize.Width = SystemInformation.VirtualScreen.Width ;
            screenSize.Height = SystemInformation.VirtualScreen.Height;

            //int zoomFactor = 10;

            Image img = pictureBox1.Image;
            Bitmap bitMapImg = new Bitmap(img);


            if (bitMapImg.Width < screenSize.Width && bitMapImg.Height < screenSize.Height)
            {
                Size newSize = new Size((int)(bitMapImg.Width / zoomLevel), (int)(bitMapImg.Height / zoomLevel));
                Bitmap bmp = new Bitmap(bitMapImg, newSize);

                pictureBox1.Image = (Image)bmp;
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            }
        }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {

            double zoomLevel = 1.1;
            System.Drawing.Rectangle screenSize = new System.Drawing.Rectangle();

            screenSize.Width = SystemInformation.VirtualScreen.Width * 10;
            screenSize.Height = SystemInformation.VirtualScreen.Height * 10;

            //int zoomFactor = 10;

            Image img = pictureBox1.Image;
            Bitmap bitMapImg = new Bitmap(img);


            if (bitMapImg.Width < screenSize.Width && bitMapImg.Height < screenSize.Height)
            {
                Size newSize = new Size((int)(bitMapImg.Width * zoomLevel), (int)(bitMapImg.Height * zoomLevel));
                Bitmap bmp = new Bitmap(bitMapImg, newSize);

                pictureBox1.Image = (Image)bmp;
                pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
            }

    }

3 个答案:

答案 0 :(得分:3)

你最好的选择是一个事件处理程序和一个开关:

switch(e.Button) {
    case whatever.Left: LeftMouseClick(e); break;
    case whatever.Right: RightMouseClick(e); break;
}

答案 1 :(得分:1)

仅使用一个事件处理程序pictureBox1_Click并使用if语句来决定要执行的操作:

private void pictureBox1_MouseClick_1(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right )
    {
       DoRightClickStuff();
    }
    else if (e.Button == System.Windows.Forms.MouseButtons.Left )
    {
        DoLeftClickStuff();
    }
 }

答案 2 :(得分:0)

  

您必须使用MouseUpMouseDown事件代替。{   Click event抓取right click

试试这个

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MessageBox.Show("Left");
        }
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("Right");
        }    
    }