using System;
using System.Drawing;
using System.Windows.Forms;
namespace _1
{
partial class Form1
{
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Blue, 5);
Graphics g = this.CreateGraphics();
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;//实线
g.DrawLine(p, 10, 40, 200, 40);
g.Dispose();
p.Dispose();
}
/// <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()
{
button1 = new Button();
button2 = new Button();
SuspendLayout();
//
// button1
//
button1.BackColor = Color.Lime;
button1.ForeColor = Color.Lime;
button1.Location = new Point(150, 169);
button1.Name = "button1";
button1.Size = new Size(25, 27);
button1.TabIndex = 0;
button1.Text = "button1";
button1.UseVisualStyleBackColor = false;
//
// button2
//
button2.BackColor = Color.Lime;
button2.ForeColor = Color.Lime;
button2.Location = new Point(267, 169);
button2.Name = "button2";
button2.Size = new Size(25, 27);
button2.TabIndex = 1;
button2.Text = "button2";
button2.UseVisualStyleBackColor = false;
//
// Form1
//
AutoScaleDimensions = new SizeF(9F, 20F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ControlLightLight;
ClientSize = new Size(800, 450);
Controls.Add(button2);
Controls.Add(button1);
ForeColor = SystemColors.ControlText;
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
}
#endregion
private Button button1;
private Button button2;
}
}
测试没问题,就贴出来的代码来看,Form的Paint事件没加上
private void InitializeComponent()
{
button1 = new Button();
button2 = new Button();
SuspendLayout();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);//////
把你的画图的代码贴一下,看看哪里没画出来,是用 g.DrawLine 画的么
以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:
是的,你可以用C#的WinForms来画这张图。在你的窗体上创建一个Paint事件处理程序,并在其中编写绘图逻辑。你可以使用Graphics对象来绘制直线、矩形和文本。
以下是一个简单的例子,它演示了如何使用Graphics对象在窗体上绘制一条直线:
private void Form1_Paint(object sender, PaintEventArgs e)
{
// 创建一个Pen对象,设置颜色和线宽
Pen pen = new Pen(Color.Black, 2);
// 使用Graphics对象绘制直线
e.Graphics.DrawLine(pen, 20, 20, 100, 100);
// 释放Pen对象
pen.Dispose();
}
在这个例子中,我们在Paint事件处理程序中创建了一个Pen对象,并使用Graphics对象的DrawLine方法绘制了一条直线。最后,我们释放了Pen对象以释放资源。
你可以使用类似的方法来绘制矩形和文本。要绘制矩形,你可以使用Graphics对象的DrawRectangle方法。要绘制文本,你可以使用Graphics对象的DrawString方法。
希望这能帮助你开始在C#中绘制图形。
点击按钮弹出窗体
private void toolStripLabel1_Click(object sender, EventArgs e)
{
AddStudent add=AddStudent.AddStudentCreateInstance();
add.MdiParent=this;
add.Show();
}
需要弹出的窗体实现
private AddStudent()
{
InitializeComponent();
}
private static AddStudent _addStudent=null;
/// <summary>
/// 判断 AddStudent是否被构造过,如果构造过,不构造,否则构造
/// </summary>
/// <returns></returns>
public static AddStudent AddStudentCreateInstance()
{
if(_addStudent==null || _addStudent.IsDisposed)
{
_addStudent=new AddStudent();
}
return _addStudent;
}
第一步:把需要弹出的窗体的构造方法设置成“私有”
第二步:声明一个以窗体命为 类型的 变量并且等于null
第三步:写一个公开的静态方法,返回类型是窗体类名:
3.1判断是否等于null,如果等于null就创建对象返回,否则直接返回