熟悉C#画图的高手请进!(100分)

  • 主题发起人 主题发起人 草在墙头
  • 开始时间 开始时间

草在墙头

Unregistered / Unconfirmed
GUEST, unregistred user!
现在一为图片的背景上画上一个(50,50,100,100)的Rect,当鼠标选中这个矩形并进行拖动时,
这个矩形始终跟随着鼠标。
如何实现?
急!在线等待!问题解决后另有100分相送!
 
你看我理解的对吧,就是你要写一个画图程序,可以画线,矩形,圆,多边形等。
然后对画好的对象你可以进行拖动,编辑大小等。
(有个大虾,已经用DELPHI很好的实现了这种系统,并且在DFW上有留言,你查查吧)
要完成这个问题,你需要好好设计你的系统。
1。你的那个矩形,如何存储,它已什么形式存在?
2。你如何选中它?
有了上面的保证,其它的都是体力活了,就在鼠标的移动或什么操作中,改变它的位置,大小等就是了。
当然期间你也需要把已画图好的擦了去(setrop2)。
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication10
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private bool bMove=false;
private System.Windows.Forms.Button button1;
//private const int R2_NOTXORPEN = 10;
private const int PS_DASHDOT = 10;
private IntPtr m_hDC;
//private Graphics g;
private long InvertPen;//这个变量是设置橡皮圈效果所创建的画笔
private long OldPen;//这个变量表示画出最终形状所使用的画笔
private int ordx=0;
private int ordy=0;//原始位置
[DllImport("GDI32.DLL")]
public static extern int SetROP2(IntPtr hdc,int nDrawMode);
[DllImport("GDI32.DLL")]
public static extern long SelectObject(IntPtr hdc,long hgdiobj );
[DllImport("GDI32.DLL")]
public static extern long CreatePen(int fnPenStyle,int nWidth,Color crColor);
[DllImport("GDI32.DLL")]
public static extern bool Ellipse(IntPtr hdc,int nLeftRect,int nTopRect,int nRightRect,int nBottomRect);
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
m_g=this.CreateGraphics();
m_hDC=m_g.GetHdc();
bMove=true;
ordx=e.X;
ordy=e.Y;
DrawSelectRect(ordx,ordy,e.X,e.Y);
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=false;
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (bMove)
{
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx=e.X;
ordy=e.Y;
}
}
private void DrawSelectRect(int x,int y,int x1,int y1)
{
InvertPen=CreatePen(PS_DASHDOT,1,Color.Red);
OldPen=SelectObject(m_hDC,InvertPen);
int nOldDrawMode=SetROP2(m_hDC,R2_NOTXORPEN);
Ellipse(m_hDC,x,y,x1,y1);
SetROP2(m_hDC,nOldDrawMode);
}
}
}
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication10
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private bool bMove=false;
private System.Windows.Forms.Button button1;
private const int R2_NOTXORPEN = 10;
private const int PS_DASHDOT = 10;
private Graphics m_g;
private IntPtr m_hDC;

private long InvertPen;
private long OldPen;
private int ordx=0;
private int ordy=0;
private int ordx1=0;
private int ordy1=0;
[DllImport("GDI32.DLL")]
public static extern int SetROP2(IntPtr hdc,int nDrawMode);
[DllImport("GDI32.DLL")]
public static extern long SelectObject(IntPtr hdc,long hgdiobj );
[DllImport("GDI32.DLL")]
public static extern long CreatePen(int fnPenStyle,int nWidth,int crColor);
[DllImport("GDI32.DLL")]
public static extern bool Ellipse(IntPtr hdc,int nLeftRect,int nTopRect,
int nRightRect,int nBottomRect);

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=true;
m_g=this.CreateGraphics();
m_hDC=m_g.GetHdc();
ordx=e.X;
ordy=e.Y;
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
pt1.X=e.X;
pt1.Y=e.Y;
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=false;
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (bMove)
{
DrawSelectRect(ordx,ordy,ordx1,ordy1);
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
}
}
private void DrawSelectRect(int x,int y,int x1,int y1)
{
InvertPen=CreatePen(PS_DASHDOT,1,Color.Red.ToArgb());
OldPen=SelectObject(m_hDC,InvertPen);
int nOldDrawMode=SetROP2(m_hDC,R2_NOTXORPEN);
Ellipse(m_hDC,x,y,x1,y1);
SetROP2(m_hDC,nOldDrawMode);
}
}
}
 
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication10
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private bool bMove=false;
private System.Windows.Forms.Button button1;
private const int R2_NOTXORPEN = 10;
private const int PS_DASHDOT = 10;
private Graphics m_g;
private IntPtr m_hDC;

private long InvertPen;
private long OldPen;
private int ordx=0;
private int ordy=0;
private int ordx1=0;
private int ordy1=0;
[DllImport("GDI32.DLL")]
public static extern int SetROP2(IntPtr hdc,int nDrawMode);
[DllImport("GDI32.DLL")]
public static extern long SelectObject(IntPtr hdc,long hgdiobj );
[DllImport("GDI32.DLL")]
public static extern long CreatePen(int fnPenStyle,int nWidth,int crColor);
[DllImport("GDI32.DLL")]
public static extern bool Ellipse(IntPtr hdc,int nLeftRect,int nTopRect,
int nRightRect,int nBottomRect);

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=true;
m_g=this.CreateGraphics();
m_hDC=m_g.GetHdc();
ordx=e.X;
ordy=e.Y;
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
bMove=false;
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (bMove)
{
DrawSelectRect(ordx,ordy,ordx1,ordy1);
DrawSelectRect(ordx,ordy,e.X,e.Y);
ordx1=e.X;
ordy1=e.Y;
}
}
private void DrawSelectRect(int x,int y,int x1,int y1)
{
InvertPen=CreatePen(PS_DASHDOT,1,Color.Red.ToArgb());
OldPen=SelectObject(m_hDC,InvertPen);
int nOldDrawMode=SetROP2(m_hDC,R2_NOTXORPEN);
Ellipse(m_hDC,x,y,x1,y1);
SetROP2(m_hDC,nOldDrawMode);
}
}
}
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
602
import
I
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部