您的位置:首页 > 其它

winForm pictureBox拖动图片and动态new 窗体&移动Panel窗体头

2013-09-25 12:30 399 查看
 

public void getScreen()

        {

            this.saveFileDialog2.Filter = "Image files (*.JPG)|*.JPG";

            this.saveFileDialog2.FilterIndex = 0;

            this.saveFileDialog2.RestoreDirectory = true;

            this.saveFileDialog2.Title = "导出文件保存路径";

            this.saveFileDialog2.FileName = "屏幕截图";

            if (this.saveFileDialog2.ShowDialog() == DialogResult.Cancel)

            {

                this.Cursor = Cursors.Default;

            }

            else

            {

                this.Refresh();

                Image image = new Bitmap(0x800, 0x600);

                Graphics graphics = Graphics.FromImage(image);

                graphics.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(800, 600));

                IntPtr hdc = graphics.GetHdc();

                graphics.ReleaseHdc(hdc);

                image.Save(this.saveFileDialog2.FileName);

                MessageBox.Show("OK", "提示!");

            }

        }

==================pictureBox拖动图片====================================

   private void pictureBox1_MouseHover(object sender, EventArgs e)

        {

            if (this.pictureBox1.Image != null)

            {

                this.pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;

                Cursor = Cursors.Hand;

            }

        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)

        {

            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

            Cursor = Cursors.Default;

            p.X = 0;

            p.Y = 0;

            driftX = 0;

            driftY = 0;

            mx = 0;

            my = 0;        

        }

        bool wselected = false;

        Point p = new Point();

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            wselected = true;

            p.X = e.X;

            p.Y = e.Y;   

        }

        int driftX = 0, driftY = 0;

        int mx = 0, my = 0;

        Graphics g;

        Bitmap bm;

  

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

            try

            {

                if (wselected && this.pictureBox1.Image != null&&Cursors.Hand==Cursor)

                {

                    this.Invalidate();

                    driftX = p.X - e.X;

                    driftY = p.Y - e.Y;                

                    mx = mx - driftX;

                    my = my - driftY;

                    bm = new Bitmap(this.pictureBox1.Image);                   

                    g = pictureBox1.CreateGraphics();

                    g.Clear(pictureBox1.BackColor);                   

                    g.DrawImage(bm, mx, my);          

                 bm.Dispose();

                g.Dispose();        

                    p.X = e.X;

                    p.Y = e.Y;

                }

            }

            finally

            { }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

        {

            wselected = false;

        }

 =============图片另存为================

 SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif|Png 图片|*.png|Wmf  图片|*.wmf";

            saveFileDialog.FilterIndex = 0;

            if (this.pictureBox1.Image == null)

            {

                MessageBox.Show("没有预览图片!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            else if (saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                if (pictureBox1.Image != null)

                {

                     Bitmap bit = new Bitmap(pictureBox1.Image);

                    bit.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

                    bit.Dispose();

                }

            }

 //////////////////////////动态new 窗体

 Type t = Type.GetType("MyTest.Form6");//窗体名要加上程序集名称 

            Form f = (Form)System.Activator.CreateInstance(t, new object[] { new string[] { "测试1", "测试2" } });

            f.Show();

//winform Panel 作标题栏(移动Panel)

private bool IsMouseDown = false;

        private Point mouseOffset;

        private void panelTop_MouseDown(object sender, MouseEventArgs e)

        {

            if (e.Button == MouseButtons.Left)

            {

                IsMouseDown = true;

            }

            mouseOffset = new Point(-e.X, -e.Y);

        }

        private void panelTop_MouseMove(object sender, MouseEventArgs e)

        {

            if (IsMouseDown == true)

            {

                Point mousePos = Control.MousePosition;

                mousePos.Offset(mouseOffset.X, mouseOffset.Y);

                this.Location = mousePos;

            }            

        }

        private void panelTop_MouseUp(object sender, MouseEventArgs e)

        {

            IsMouseDown = false;

        }

//winform Panel 作标题栏(移动Panel)/////结束
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: