首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

图片拖动兑现不了拉

2011-12-11 
图片拖动实现不了拉嘿嘿今天弄了个图片框小程序,我想法很简单就是是一个比图片框大的图片通过鼠标拖动,可

图片拖动实现不了拉
嘿嘿   今天弄了个图片框小程序,我想法很简单就是是一个比图片框大的图片通过鼠标拖动,可以看到图片被遮住的部分。可是实现不了,也没有提示什么错误,希望高手能帮忙看看。
代码如下:
using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;

namespace   WindowsApplication8
{
        public   partial   class   Form1   :   Form
        {
                PictureBox   pb;
                Bitmap   bmp,lion;//lion是原图片,bmp是lion在图片框内的部分。
                int   x1,   y1,   x2,   y2,stpointx,stpointy,x,y;//stpointx,stpointy是图片框内的图片bmp左上角对应的在lion中的坐标,x1,x2,x3,x4分别对应鼠标的按下和弹起时的坐标。x,y为在X和Y方向拖动量。
                bool   dragstart   =   false;
                public   Form1()
                {
                        this.SuspendLayout();
                        this.Size   =   new   Size(400,400);
                        lion   =   (Bitmap)Bitmap.FromFile(@ "..\..\Resources\Water   lilies.jpg ");
                        pb   =   new   PictureBox();
                        pb.Size   =   new   Size(300,300);
                        bmp   =   new   Bitmap(pb.Width,pb.Height);
                        pb.BorderStyle   =   BorderStyle.Fixed3D;
                        pb.BackgroundImage   =   Bitmap.FromFile(@ "..\..\Resources\Water   lilies.jpg ");
                        pb.Location   =   new   Point((int)(this.ClientSize.Width   *   0.5   -   pb.Width   *   0.5),   (int)(this.ClientSize.Height   *   0.5   -   pb.Height   *   0.5));
                        this.Controls.AddRange(new   Control[]   {   pb});
                        this.ResumeLayout();
                        pb.MouseDown   +=   new   MouseEventHandler(pb_MouseDown);
                        pb.MouseUp   +=   new   MouseEventHandler(pb_MouseUp);
                        pb.MouseEnter   +=   new   EventHandler(pb_MouseEnter);
                        pb.MouseLeave   +=   new   EventHandler(pb_MouseLeave);
                }
                void   pb_MouseEnter(object   sender,   EventArgs   e)


                {
                        this.Cursor   =   Cursors.SizeAll;
                }
                void   pb_MouseLeave(object   sender,   EventArgs   e)
                {
                        this.Cursor   =   Cursors.Arrow;
                }
                void   pb_MouseDown(object   sender,MouseEventArgs   e)
                {
                        x1   =   e.X;
                        y1   =   e.Y;
                        dragstart   =   true;
                }
                void   pb_MouseUp(object   sender,MouseEventArgs   e)
                {
                        if(dragstart)
                        {
                                x2   =   e.X;
                                y2   =   e.Y;
                                x   =   x2   -   x1;
                                y   =   y2   -   y1;
                                if   (stpointx   -   x   <=   0)//移动后stpointx <=0,那么起始bmp起点在lion中的起始坐标就为0;
                                        stpointx   =   0;
                                else   if   (stpointx   -   x   +   pb.Width   > =   lion.Width)//如果bmp的最大横坐标在lion中超出了lion的Width,
                                        stpointx   =   lion.Width   -   pb.Width   -   1;
                                else   stpointx   =   stpointx   -   x;

                                if   (stpointy   -   y   <=   0)
                                        stpointy   =   0;
                                else   if   (stpointy   -   y   +   pb.Height   > =   lion.Width)


                                        stpointy   =   lion.Width   -   pb.Height   -   1;
                                else   stpointy   =   stpointy   -   y;

                                for   (int   i   =   0;   i   <   pb.Width;   i++)
                                        for   (int   j   =   0;   j   <   pb.Height;   j++)
                                        {
                                                bmp.SetPixel(i,j,   lion.GetPixel(stpointx+i,   stpointy+j));
                                        }
                                pb.BackgroundImage   =   bmp;
                               
                                                               
                        }
                }

             
        }
}

[解决办法]
http://blog.csdn.net/chengking/archive/2005/10/07/496739.aspx

热点排行