栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C# GDI在控件上绘图的方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# GDI在控件上绘图的方法

本文以在chart控件上和窗体上画矩形为例子讲述了C# GDI在控件上绘图的方法。分享给大家供大家参考。具体方法如下:

具体的实现方法就不多解释了,备注很详细,代码也很简单。

主要功能代码如下:

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

namespace WFApp2
{
  public partial class data: Form
  {
    public data()
    {
      InitializeComponent();
      //窗体
      g = this.CreateGraphics();
      //chart控件
      g2 = this.chart1.CreateGraphics();
    }
   
    public Point firstPoint = new Point(0, 0); //鼠标第一点 
    public Point secondPoint = new Point(0, 0); //鼠标第二点 
    public bool begin = false;  //是否开始画矩形 
    /// 
    /// 在from上画矩形
    /// 
    Graphics g;

    /// 
    /// 在chart1控件上画矩形
    /// 
    Graphics g2;

    /// 
    /// 在窗体上按下鼠标事件
    /// 
    /// 
    /// 
    private void data_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }

    /// 
    /// 在窗体上鼠标移动开始绘图
    /// 
    /// 
    /// 
    private void data_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
 //清除窗体绘图面,相当于刷新了一次窗体界面然后重新绘制
 g.Clear(this.BackColor);
 //获取新的右下角坐标 
 secondPoint = new Point(e.X, e.Y);
 //获取两个数中的大者或小者
 int minX = Math.Min(firstPoint.X, secondPoint.X);
 int minY = Math.Min(firstPoint.Y, secondPoint.Y);
 int maxX = Math.Max(firstPoint.X, secondPoint.X);
 int maxY = Math.Max(firstPoint.Y, secondPoint.Y);

 //画框 
 g.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);
 //ControlPaint.DrawReversibleframe(new Rectangle(minX, minY, maxX - minX, maxY - minY),this.BackColor,frameStyle.Dashed);

      }
    }

    /// 
    /// 鼠标松开停止绘图
    /// 
    /// 
    /// 
    private void data_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// 
    /// 在chart控件上移动鼠标绘图
    /// 
    /// 
    /// 
    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {
      if (begin)
      {
 //重新在chart上面绘图,此处不能用clear方法,clear会清除整个绘图界面chart控件会被清除
 this.Refresh();
 //获取新的右下角坐标 
 secondPoint = new Point(e.X, e.Y);
 int minX = Math.Min(firstPoint.X, secondPoint.X);
 int minY = Math.Min(firstPoint.Y, secondPoint.Y);
 int maxX = Math.Max(firstPoint.X, secondPoint.X);
 int maxY = Math.Max(firstPoint.Y, secondPoint.Y);

 //画矩形
 g2.DrawRectangle(new Pen(Color.Red), minX, minY, maxX - minX, maxY - minY);

      }
    }

    /// 
    /// 鼠标松开停止绘图
    /// 
    /// 
    /// 
    private void chart1_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// 
    /// 在chart控件上按下鼠标
    /// 
    /// 
    /// 
    private void chart1_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }
  }
}

希望本文所述对大家的C#程序设计有所帮助

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/126114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号