我找到的一个解决方法只能基于整个form,不能针对指定的控件(如一个画图区域),那位高手能解决一下?
下面是我找到的在整个form上画十字线的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point currentPoint = new Point(0, 0);
private Pen pen = new Pen(Color.Black); private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.X != currentPoint.X || e.Y != currentPoint.Y)
{
currentPoint = new Point(e.X, e.Y);
this.Invalidate(true);
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawLine(pen, 0, currentPoint.Y, this.Width, currentPoint.Y); //绘制横线
e.Graphics.DrawLine(pen, currentPoint.X, 0, currentPoint.X, this.Height); //会制纵线
} private void Form1_Closed(object sender, System.EventArgs e)
{
pen.Dispose();
}
|
下面是我找到的在整个form上画十字线的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point currentPoint = new Point(0, 0);
private Pen pen = new Pen(Color.Black); private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.X != currentPoint.X || e.Y != currentPoint.Y)
{
currentPoint = new Point(e.X, e.Y);
this.Invalidate(true);
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawLine(pen, 0, currentPoint.Y, this.Width, currentPoint.Y); //绘制横线
e.Graphics.DrawLine(pen, currentPoint.X, 0, currentPoint.X, this.Height); //会制纵线
} private void Form1_Closed(object sender, System.EventArgs e)
{
pen.Dispose();
}
|
解决方案 »
- treeview控件的sorted属性在哪?
- 跪求ado.net技術內幕源碼
- C# Web系统布问题
- c#如何创建dll文件?
- ASPxGridView 嵌套级联菜单
- C# Timer控件问题 急啊!!!!
- datagrid中datagridboolcolumn问题(难)
- 求助:::ServerVersion = “sqlconn.ServerVersion”引发了“System.InvalidOperationException”类型的异常
- C#如何做一个类似openfiledialog中的选择目录的那个combobox一样的,可以显示树的comboBox
- 怎样将动态(通过项目)做好的报表 RPT文件绑定给ReportDocument 急在线等待/
- c#如何让另一台机子执行dos 命令 ??
- 看看你曾经回过的帖子:“分不多,c#程序员近来拿分了,只要分够,进者有份!”(就是现在过200楼的那个)欢迎回去再参与下!回复该帖的前10名各得1分!
{
base.OnPaint(e);
e.Graphics.DrawLine(pen, 0, currentPoint.Y, this.Width, currentPoint.Y); //绘制横线
e.Graphics.DrawLine(pen, currentPoint.X, 0, currentPoint.X, this.Height); //会制纵线
}
这个窗体重绘,当然只针对form,在this.clientRectange内有效,不针对某控件你可以写成单独事件public void MouseEnvents(MouseEventArgs e)
{
if (e.X != currentPoint.X ¦ ¦ e.Y != currentPoint.Y)
{
currentPoint = new Point(e.X, e.Y);
//this.Invalidate(true); ?????
}
}
public void Piants(Pen pen1,Pen pen2,PaintEventArgs e)
{
e.Graphics.DrawLine(pen1, 0, currentPoint.Y, this.Width, currentPoint.Y); //绘制横线
e.Graphics.DrawLine(pen2, currentPoint.X, 0, currentPoint.X, this.Height); //会制纵线
}////调用
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
MouseEnvents(e);
this.Invalidate(true);
}
protected override void OnPaint(PaintEventArgs e)
{
Piants(pen,pen,e);
}你只要在其他控件调用这两个自己写的事件就可以了!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point currentPoint = new Point(0, 0);
private Pen pen = new Pen(Color.Black); private void Form1_Closed(object sender, System.EventArgs e)
{
pen.Dispose();
}
public void MouseEnvents(MouseEventArgs e)
{
if (e.X != currentPoint.X || e.Y != currentPoint.Y)
{
currentPoint = new Point(e.X, e.Y);
//this.Invalidate(true); ?????
}
}
public void Piants(Pen pen1,Pen pen2,PaintEventArgs e)
{
e.Graphics.DrawLine(pen1, 0, currentPoint.Y, this.Width, currentPoint.Y); //绘制横线
e.Graphics.DrawLine(pen2, currentPoint.X, 0, currentPoint.X, this.Height); //会制纵线
}
protected override void OnPaint(PaintEventArgs e)
{
Piants(pen,pen,e);
} private void panel1_MouseMove(object sender, MouseEventArgs e)
{
MouseEnvents(e);
this.Invalidate(true);
}
}
}
{
MouseEnvents(e);
this.Invalidate(true);
}
这个不要(让子控件无效)
{
MouseEnvents(e);
} private void button1_Paint(object sender, PaintEventArgs e)
{
Piants(Pens.Red, Pens.Orange, e);
button1.Invalidate(true);//这个还是要,不然线不会动!
}