首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

有什么办法可以在界面上画斜线,该怎么解决

2011-12-31 
有什么办法可以在界面上画斜线下面这个类是一个用户控件,在C#里面画直线的,有什么办法可以画斜线呢?******

有什么办法可以在界面上画斜线

下面这个类是一个用户控件,在C#里面画直线的,有什么办法可以画斜线呢?
**********************************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace Line
{
   
  public class LineControl : Control
  {
  private LineType m_lineType = LineType.Horizontal;
  private Color m_lineColor = Color.Blue;
  private Color m_shadowColor = Color.Gray;
  private int m_lineWidth = 2;
  private Size m_shadowOffset = new Size(1, 1);
  public LineControl()
  {
  this.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
  this.SetStyle(ControlStyles.UserMouse |
  ControlStyles.Selectable |
  ControlStyles.StandardClick |
  ControlStyles.StandardDoubleClick, false);
  }
  [Category("线")]
  [Description("阴影的偏移量")]
  [DefaultValue(typeof(Size), "1,1")]
  public Size ShadowOffset
  {
  get { return m_shadowOffset; }
  set
  {

  m_shadowOffset = value;
  this.SetSize();
  this.Invalidate();
  }
  }
  [Category("线")]
  [Description("宽度")]
  [DefaultValue(2)]
  public int LineWidth
  {
  get { return m_lineWidth; }
  set
  {
  if (value < 1) value = 1;
  m_lineWidth = value;
  this.SetSize();
  this.Invalidate();
  }
  }
  [Category("线")]
  [Description("类型")]
  [DefaultValue(typeof(LineType), "Horizontal")]
  public LineType LineType
  {
  get { return m_lineType; }
  set
  {
  if (m_lineType != value)
  {
  int linelen = 0;
  if (m_lineType == LineType.Horizontal)
  {
  linelen = this.Width;
  }
  else
  {
  linelen = this.Height;
  }
  m_lineType = value;
  if (value == LineType.Horizontal)
  {
  this.Width = linelen;
  }
  else
  {
  this.Height = linelen;
  }
  this.SetSize();
  this.Invalidate();
  }
  }
  }
  [Category("线")]
  [Description("颜色")]
  [DefaultValue(typeof(Color), "Blue")]
  public Color LineColor
  {
  get { return m_lineColor; }
  set
  {
  m_lineColor = value;
  this.Invalidate();


  }
  }
  [Category("线")]
  [Description("阴影的颜色")]
  [DefaultValue(typeof(Color), "Gray")]
  public Color ShadowColor
  {
  get { return m_shadowColor; }
  set
  {
  m_shadowColor = value;
  this.Invalidate();
  }
  }
  protected override void OnPaint(PaintEventArgs e)
  {
  Pen p1 = new Pen(this.LineColor, this.LineWidth);
  Pen p2 = new Pen(this.ShadowColor, this.LineWidth);
  if (this.LineType == LineType.Horizontal)
  {
  e.Graphics.DrawLine(p2, new Point(ShadowOffset.Width, ShadowOffset.Height), new Point(this.Right, ShadowOffset.Height));
  e.Graphics.DrawLine(p1, Point.Empty, new Point(this.Right - ShadowOffset.Width, 0));
  }
  else
  {
  e.Graphics.DrawLine(p2, new Point(ShadowOffset.Width, ShadowOffset.Height), new Point(ShadowOffset.Width, this.Bottom));
  e.Graphics.DrawLine(p1, Point.Empty, new Point(0, this.Bottom - ShadowOffset.Height));
  }
  p1.Dispose();
  p2.Dispose();
  }
  protected override void OnSizeChanged(EventArgs e)
  {
  base.OnSizeChanged(e);
  SetSize();
  }
  private void SetSize()
  {
  if (this.LineType == LineType.Horizontal)
  {
  this.Height = LineWidth + ShadowOffset.Height;
  }
  else
  {
  this.Width = LineWidth + ShadowOffset.Width;
  }
  }
  }
  /// <summary>
  /// 线的类型
  /// </summary>
  public enum LineType
  {
  /// <summary>
  /// 横线
  /// </summary>
  Horizontal,
  /// <summary>
  /// 竖线
  /// </summary>
  Vertical

  }
}
**********************************************************************************************************

[解决办法]
两个点不就确定一条线吗?
两个点的横纵坐标都不一样,那不就是斜线吗.?

[解决办法]

探讨

两个点不就确定一条线吗?
两个点的横纵坐标都不一样,那不就是斜线吗.?

[解决办法]
画条线那么复杂 0.0
[解决办法]
用Rectangle或者两个Point...

热点排行