الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

رسم Line عى Form

بدأه mr.beshoy في 8 نوفمبر 2008 · 3 رد · 1,007 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

سلام

انا هنا فى الكود التالى بجاول ارسم خط على فورمة

و لكن هناك error لا استطيع التغلب عليه

انا مبتدئ c# على فكرة

و هاك الكود

using System;

using System.Collections.Generic;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace painter

{

public partial class Form_draw : Form

{

Point p_start = new Point();

Point p_end = new Point();

public Form_draw()

{

InitializeComponent();

this.Controls.AddRange(new Control[] { Line_button, oval_button, rect_button, roundrect_button, erase_button });

}

public void Line_button_MouseDown(object sender, MouseEventArgs e)

{

p_start.X = e.X;

p_start.Y = e.Y;

p_end = p_start;

}

public void Line_button_MouseMove(object sender, MouseEventArgs e)

{

Form thisform = new Form();

p_end.X = e.X;

p_end.Y = e.Y;

if (Line_button.Click+=new System.EventHandler(sender, e))

{

ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);

p_end = new Point(e.X, e.Y);

ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);

}

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void Line_button_MouseUp(object sender, MouseEventArgs e)

{

Form thisform = new Form();

Graphics g = thisform.CreateGraphics();

Pen p = new Pen(Color.Blue, 2);

if (Line_button.Click+=new System.EventHandler(sender, e))

{

ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);

g.DrawLine(p, p_start, p_end);

}

}

private void Line_button_Click(object sender, EventArgs e)

{

}

}

}

و ده بقى كود الأدوات الى انا عملتلها drag and drop

namespace painter

{

public partial class Form_draw

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.IContainer components = null;

/// <summary>

/// Clean up any resources being used.

/// </summary>

/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

protected override void Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();

this.button8 = new System.Windows.Forms.Button();

this.button7 = new System.Windows.Forms.Button();

this.button6 = new System.Windows.Forms.Button();

this.erase_button = new System.Windows.Forms.Button();

this.roundrect_button = new System.Windows.Forms.Button();

this.rect_button = new System.Windows.Forms.Button();

this.oval_button = new System.Windows.Forms.Button();

this.Line_button = new System.Windows.Forms.Button();

this.tableLayoutPanel1.SuspendLayout();

this.SuspendLayout();

//

// tableLayoutPanel1

//

this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ControlDark;

this.tableLayoutPanel1.ColumnCount = 2;

this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));

this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));

this.tableLayoutPanel1.Controls.Add(this.button8, 1, 3);

this.tableLayoutPanel1.Controls.Add(this.button7, 0, 3);

this.tableLayoutPanel1.Controls.Add(this.button6, 1, 2);

this.tableLayoutPanel1.Controls.Add(this.erase_button, 0, 2);

this.tableLayoutPanel1.Controls.Add(this.roundrect_button, 1, 1);

this.tableLayoutPanel1.Controls.Add(this.rect_button, 0, 1);

this.tableLayoutPanel1.Controls.Add(this.oval_button, 1, 0);

this.tableLayoutPanel1.Controls.Add(this.Line_button, 0, 0);

this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 35);

this.tableLayoutPanel1.Name = "tableLayoutPanel1";

this.tableLayoutPanel1.RowCount = 4;

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.42017F));

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 49.57983F));

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 69F));

this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 72F));

this.tableLayoutPanel1.Size = new System.Drawing.Size(147, 274);

this.tableLayoutPanel1.TabIndex = 0;

//

// button8

//

this.button8.Location = new System.Drawing.Point(76, 204);

this.button8.Name = "button8";

this.button8.Size = new System.Drawing.Size(67, 67);

this.button8.TabIndex = 7;

this.button8.Text = "button8";

this.button8.UseVisualStyleBackColor = true;

//

// button7

//

this.button7.Location = new System.Drawing.Point(3, 204);

this.button7.Name = "button7";

this.button7.Size = new System.Drawing.Size(67, 67);

this.button7.TabIndex = 6;

this.button7.Text = "button7";

this.button7.UseVisualStyleBackColor = true;

//

// button6

//

this.button6.Location = new System.Drawing.Point(76, 135);

this.button6.Name = "button6";

this.button6.Size = new System.Drawing.Size(67, 63);

this.button6.TabIndex = 5;

this.button6.Text = "button6";

this.button6.UseVisualStyleBackColor = true;

//

// erase_button

//

this.erase_button.Image = global::painter.Properties.Resources.erase;

this.erase_button.Location = new System.Drawing.Point(3, 135);

this.erase_button.Name = "erase_button";

this.erase_button.Size = new System.Drawing.Size(67, 63);

this.erase_button.TabIndex = 4;

this.erase_button.UseVisualStyleBackColor = true;

//

// roundrect_button

//

this.roundrect_button.Image = global::painter.Properties.Resources.roundrect;

this.roundrect_button.Location = new System.Drawing.Point(76, 70);

this.roundrect_button.Name = "roundrect_button";

this.roundrect_button.Size = new System.Drawing.Size(67, 59);

this.roundrect_button.TabIndex = 3;

this.roundrect_button.UseVisualStyleBackColor = true;

//

// rect_button

//

this.rect_button.Image = global::painter.Properties.Resources.rect;

this.rect_button.Location = new System.Drawing.Point(3, 70);

this.rect_button.Name = "rect_button";

this.rect_button.Size = new System.Drawing.Size(67, 59);

this.rect_button.TabIndex = 2;

this.rect_button.UseVisualStyleBackColor = true;

//

// oval_button

//

this.oval_button.Image = global::painter.Properties.Resources.circle;

this.oval_button.Location = new System.Drawing.Point(76, 3);

this.oval_button.Name = "oval_button";

this.oval_button.Size = new System.Drawing.Size(67, 61);

this.oval_button.TabIndex = 1;

this.oval_button.UseVisualStyleBackColor = true;

//

// Line_button

//

this.Line_button.Image = global::painter.Properties.Resources.line;

this.Line_button.Location = new System.Drawing.Point(3, 3);

this.Line_button.Name = "Line_button";

this.Line_button.Size = new System.Drawing.Size(67, 61);

this.Line_button.TabIndex = 8;

this.Line_button.UseVisualStyleBackColor = true;

this.Line_button.Click += new System.EventHandler(this.Line_button_Click);

this.Line_button.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseDown);

this.Line_button.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseMove);

this.Line_button.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseUp);

//

// Form_draw

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.BackColor = System.Drawing.SystemColors.ButtonShadow;

this.ClientSize = new System.Drawing.Size(783, 482);

this.Controls.Add(this.tableLayoutPanel1);

this.Name = "Form_draw";

this.Text = "painter";

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

this.tableLayoutPanel1.ResumeLayout(false);

this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;

private System.Windows.Forms.Button button8;

private System.Windows.Forms.Button button7;

private System.Windows.Forms.Button button6;

private System.Windows.Forms.Button erase_button;

private System.Windows.Forms.Button roundrect_button;

private System.Windows.Forms.Button rect_button;

private System.Windows.Forms.Button oval_button;

private System.Windows.Forms.Button Line_button;

}

}

المشكلة فى الكود الاول كانت فى السطرين دول

if (Line_button.Click+=new System.EventHandler(sender, e))

ونفس السطر فى الدالة بتاعة الmouseup()

و ياريت لو حد يقولى ازاى اقدر ارسم على panel

بدل الform

شكرا

معلش انا اعدت طبع الكود تانى لانه متنسقش المرة الاولى

namespace painter
{
	public partial  class Form_draw
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}


		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
			this.button8 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.erase_button = new System.Windows.Forms.Button();
			this.roundrect_button = new System.Windows.Forms.Button();
			this.rect_button = new System.Windows.Forms.Button();
			this.oval_button = new System.Windows.Forms.Button();
			this.Line_button = new System.Windows.Forms.Button();
			this.tableLayoutPanel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// tableLayoutPanel1
			// 
			this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.ControlDark;
			this.tableLayoutPanel1.ColumnCount = 2;
			this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.tableLayoutPanel1.Controls.Add(this.button8, 1, 3);
			this.tableLayoutPanel1.Controls.Add(this.button7, 0, 3);
			this.tableLayoutPanel1.Controls.Add(this.button6, 1, 2);
			this.tableLayoutPanel1.Controls.Add(this.erase_button, 0, 2);
			this.tableLayoutPanel1.Controls.Add(this.roundrect_button, 1, 1);
			this.tableLayoutPanel1.Controls.Add(this.rect_button, 0, 1);
			this.tableLayoutPanel1.Controls.Add(this.oval_button, 1, 0);
			this.tableLayoutPanel1.Controls.Add(this.Line_button, 0, 0);
			this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 35);
			this.tableLayoutPanel1.Name = "tableLayoutPanel1";
			this.tableLayoutPanel1.RowCount = 4;
			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.42017F));
			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 49.57983F));
			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 69F));
			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 72F));
			this.tableLayoutPanel1.Size = new System.Drawing.Size(147, 274);
			this.tableLayoutPanel1.TabIndex = 0;
			// 
			// button8
			// 
			this.button8.Location = new System.Drawing.Point(76, 204);
			this.button8.Name = "button8";
			this.button8.Size = new System.Drawing.Size(67, 67);
			this.button8.TabIndex = 7;
			this.button8.Text = "button8";
			this.button8.UseVisualStyleBackColor = true;
			// 
			// button7
			// 
			this.button7.Location = new System.Drawing.Point(3, 204);
			this.button7.Name = "button7";
			this.button7.Size = new System.Drawing.Size(67, 67);
			this.button7.TabIndex = 6;
			this.button7.Text = "button7";
			this.button7.UseVisualStyleBackColor = true;
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(76, 135);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(67, 63);
			this.button6.TabIndex = 5;
			this.button6.Text = "button6";
			this.button6.UseVisualStyleBackColor = true;
			// 
			// erase_button
			// 
			this.erase_button.Image = global::painter.Properties.Resources.erase;
			this.erase_button.Location = new System.Drawing.Point(3, 135);
			this.erase_button.Name = "erase_button";
			this.erase_button.Size = new System.Drawing.Size(67, 63);
			this.erase_button.TabIndex = 4;
			this.erase_button.UseVisualStyleBackColor = true;
			// 
			// roundrect_button
			// 
			this.roundrect_button.Image = global::painter.Properties.Resources.roundrect;
			this.roundrect_button.Location = new System.Drawing.Point(76, 70);
			this.roundrect_button.Name = "roundrect_button";
			this.roundrect_button.Size = new System.Drawing.Size(67, 59);
			this.roundrect_button.TabIndex = 3;
			this.roundrect_button.UseVisualStyleBackColor = true;
			// 
			// rect_button
			// 
			this.rect_button.Image = global::painter.Properties.Resources.rect;
			this.rect_button.Location = new System.Drawing.Point(3, 70);
			this.rect_button.Name = "rect_button";
			this.rect_button.Size = new System.Drawing.Size(67, 59);
			this.rect_button.TabIndex = 2;
			this.rect_button.UseVisualStyleBackColor = true;
			// 
			// oval_button
			// 
			this.oval_button.Image = global::painter.Properties.Resources.circle;
			this.oval_button.Location = new System.Drawing.Point(76, 3);
			this.oval_button.Name = "oval_button";
			this.oval_button.Size = new System.Drawing.Size(67, 61);
			this.oval_button.TabIndex = 1;
			this.oval_button.UseVisualStyleBackColor = true;
			// 
			// Line_button
			// 
			this.Line_button.Image = global::painter.Properties.Resources.line;
			this.Line_button.Location = new System.Drawing.Point(3, 3);
			this.Line_button.Name = "Line_button";
			this.Line_button.Size = new System.Drawing.Size(67, 61);
			this.Line_button.TabIndex = 8;
			this.Line_button.UseVisualStyleBackColor = true;
			this.Line_button.Click += new System.EventHandler(this.Line_button_Click);
			this.Line_button.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseDown);
			this.Line_button.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseMove);
			this.Line_button.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Line_button_MouseUp);
			// 
			// Form_draw
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.BackColor = System.Drawing.SystemColors.ButtonShadow;
			this.ClientSize = new System.Drawing.Size(783, 482);
			this.Controls.Add(this.tableLayoutPanel1);
			this.Name = "Form_draw";
			this.Text = "painter";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.tableLayoutPanel1.ResumeLayout(false);
			this.ResumeLayout(false);

			   }


		#endregion

		private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
		private System.Windows.Forms.Button button8;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button erase_button;
		private System.Windows.Forms.Button roundrect_button;
		private System.Windows.Forms.Button rect_button;
		private System.Windows.Forms.Button oval_button;
		private System.Windows.Forms.Button Line_button;

	}
}

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace painter
{

	public partial class Form_draw : Form
	{
		Point p_start = new Point();
		Point p_end = new Point();

		public Form_draw()
		{
			InitializeComponent();
			this.Controls.AddRange(new Control[] { Line_button, oval_button, rect_button, roundrect_button, erase_button });
		}
		public void Line_button_MouseDown(object sender, MouseEventArgs e)
		{
			p_start.X = e.X;
			p_start.Y = e.Y;
			p_end = p_start;
		}
		public void Line_button_MouseMove(object sender, MouseEventArgs e)
		{
			Form thisform = new Form();
			p_end.X = e.X;
			p_end.Y = e.Y;
			if (Line_button.Click+=new System.EventHandler(sender, e))
			{
				ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);
				p_end = new Point(e.X, e.Y);
				ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);

			}
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}

		private void Line_button_MouseUp(object sender, MouseEventArgs e)
		{
			Form thisform = new Form();
			Graphics g = thisform.CreateGraphics();
			Pen p = new Pen(Color.Blue, 2);
			if (Line_button.Click+=new System.EventHandler(sender,  e))
			{
				ControlPaint.DrawReversibleLine(thisform.PointToScreen(p_start), thisform.PointToScreen(p_end), Color.Black);
				g.DrawLine(p, p_start, p_end);
			} 
		}

		private void Line_button_Click(object sender, EventArgs e)
		{

		}
	}
}
#2

الكود غير مفهوم بالمرة

لو ممكن تكتب رسالة الخطأ يكون احسن

انا لي بعض الملاحظات ( انا بكلم على VS2005) :

- لا أعلم ما معنى الاتي

this.Controls.AddRange(new Control[] { Line_button, oval_button, rect_button, roundrect_button, erase_button });

if (Line_button.Click+=new System.EventHandler(sender, e))

- اين تعريف ذلك المتغير ControlPaint

- كما انك تأخذ احداثيات الماوس عند تحركه على زرار رسم الخط وليس عن تحركه على الفورم

بمنتهى البساطة يمكن ان يكون الكود بالشكل الاتي :

تعريف متغيرين عام كما فعلت من النوع Point

class Form_Draw: Form
{
Point startPoint;
Point endPoint;
bool flag=false;
.........
........
}

وفي Form_MouseUp تضع احداثيات نقطة البداية

if (flag==false)
{
startPoint= new Piont(e.X,e.Y);
flag=true;
}
else
{
endPoint=new Point(e.X,e.Y);
}

وفي حدث Form_Paint تكتب كود الرسم

graphics g=e.graphics;
g.DrawLine(startPoint,endPoint,pens.Red);
g.dispose();

للاسف ليس عندي حاليا vs حتى اجرب عليه ولكن جرب واخبرني ان كان هناك خطأ

تم تعديل هذه المشاركة بواسطة Prof.Mendl في 9 نوفمبر 2008 في 23:38

#3

اسف على التاخير بجد

عذرا على الكود الملغبط اول مرة

شوف حضرتك الكود ده كده انا بحاول ارسم على بانل اسمها drawpanel

و مع ذلك مفيش الخط

الكود اهو

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace painter
{
	public partial class Form1:Form
	{
		Point s_point=new Point();
		Point e_point = new Point();
		Pen myPen = new Pen(Color.Black);
		Graphics g;
		int[] shape ={ 0, 1, 2, 3, 4, 5 };	
		public Form1()
		{
			InitializeComponent();

		}


		private void Drawpanel_Paint(object sender, PaintEventArgs e)
		{
			g = e.Graphics;
				 for (int i = 0; i < shape.Length; ++i)
				 {
					 switch (shape)
					 {
						 case 0:
							 g.DrawLine(myPen, s_point, e_point);
							 break;
						 case 1:
							 break;
						 case 2:
							 break;
						 case 3:
							 break;
						 case 4:
							 break;
						 case 5:
							 break;
					 }
				 }
			 }

		private void Drawpanel_MouseDown(object sender, MouseEventArgs e)
		{
			s_point.X = e.X;
			s_point.Y = e.Y;
			//e_point = s_point;

		}

		private void Drawpanel_MouseUp(object sender, MouseEventArgs e)
		{
			e_point.X = e.X;
			e_point.Y = e.Y;

		}

		private void Line_button_Click(object sender, EventArgs e)
		{
			Drawpanel.Invalidate();
		}
	}
}
#4

الكود يعمل جيدا

كلاهما

فقط تأكد ان لون الخط مختلف تماما عن خلفية panel على سبيل المثال اجعل الخلفية بيضاء

مواضيع مشابهة