关闭

橡皮筋

2030人阅读 评论(2) 收藏 举报

橡皮筋

参见:GDI+编程10个基本技巧”

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;


using System.Runtime.InteropServices;


namespace GDIPlus

{

/// <summary>

/// Form5 µÄժҪ˵Ã÷¡£

/// </summary>

public class Form5 : System.Windows.Forms.Form

{

/// <summary>

/// ±ØÐèµÄÉè¼ÆÆ÷±äÁ¿¡£

/// </summary>

private System.ComponentModel.Container components = null;


public Form5()

{

//

// Windows ´°ÌåÉè¼ÆÆ÷Ö§³ÖËù±ØÐèµÄ

//

InitializeComponent();


//

// TODO: ÔÚ InitializeComponent µ÷ÓúóÌí¼ÓÈκι¹Ô캯Êý´úÂë

//

}


/// <summary>

/// ÇåÀíËùÓÐÕýÔÚʹÓõÄ×ÊÔ´¡£

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}


#region Windows ´°ÌåÉè¼ÆÆ÷Éú³ÉµÄ´úÂë

/// <summary>

/// Éè¼ÆÆ÷Ö§³ÖËùÐèµÄ·½·¨ - ²»ÒªÊ¹ÓôúÂë±à¼­Æ÷ÐÞ¸Ä

/// ´Ë·½·¨µÄÄÚÈÝ¡£

/// </summary>

private void InitializeComponent()

{

//

// Form5

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Name = "Form5";

this.Text = "Form5";

this.Load += new System.EventHandler(this.Form5_Load);


}

#endregion


private void Form5_Load(object sender, System.EventArgs e)

{

}


[DllImport("gdi32.dll")]

private static extern bool SetROP2(IntPtr hdc, int nDrawMode);


[DllImport("gdi32.dll")]

private static extern bool Rectangle(IntPtr hdc, int x1, int y1, int x2, int y2);


private int m_StartX, m_StartY;

private int m_EndX, m_EndY;

private int m_Step = 0;


protected override void OnMouseDown(MouseEventArgs e)

{

Graphics g = this.CreateGraphics();

IntPtr hdc = g.GetHdc();

m_Step += 1;

SetROP2(hdc, 10);


if(m_Step == 1)

{

this.m_StartX = e.X;

this.m_StartY = e.Y;

this.m_EndX = e.X;

this.m_EndY = e.Y;

}

else if(this.m_Step == 2)

{

Rectangle(hdc, this.m_StartX, this.m_StartY, e.X, e.Y);

Rectangle(hdc, this.m_StartX, this.m_StartY, e.X, e.Y);


this.m_Step = 0;

}


g.ReleaseHdc(hdc);


//base.OnMouseDown (e);

}


protected override void OnMouseMove(MouseEventArgs e)

{

Graphics g = this.CreateGraphics();

int prex, prey;


if(this.m_Step == 1)

{

IntPtr hdc = g.GetHdc();

prex = this.m_EndX;

prey = this.m_EndY;

SetROP2(hdc, 10);

Rectangle(hdc, this.m_StartX, this.m_StartY, prex, prey);

Rectangle(hdc, this.m_StartX, this.m_StartY, e.X, e.Y);

this.m_EndX = e.X;

this.m_EndY = e.Y;

g.ReleaseHdc(hdc);

}


//base.OnMouseMove (e);

}


protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

g.FillRectangle(Brushes.White, this.ClientRectangle);


//base.OnPaint (e);

}


}

}

参见:GDI+编程10个基本技巧”







0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:177212次
    • 积分:2823
    • 等级:
    • 排名:第13116名
    • 原创:100篇
    • 转载:3篇
    • 译文:0篇
    • 评论:23条
    最新评论