程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# GDI在控件上畫圖的辦法

C# GDI在控件上畫圖的辦法

編輯:C#入門知識

C# GDI在控件上畫圖的辦法。本站提示廣大學習愛好者:(C# GDI在控件上畫圖的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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;  //能否開端畫矩形 
    /// <summary>
    /// 在from上畫矩形
    /// </summary>
    Graphics g;

    /// <summary>
    /// 在chart1控件上畫矩形
    /// </summary>
    Graphics g2;

    /// <summary>
    /// 在窗體上按下鼠標事宜
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }

    /// <summary>
    /// 在窗體上鼠標挪動開端畫圖
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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);

      }
    }

    /// <summary>
    /// 鼠標松開停滯畫圖
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void data_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// <summary>
    /// 在chart控件上挪動鼠標畫圖
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    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);

      }
    }

    /// <summary>
    /// 鼠標松開停滯畫圖
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseUp(object sender, MouseEventArgs e)
    {
      begin = false;
    }

    /// <summary>
    /// 在chart控件上按下鼠標
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void chart1_MouseDown(object sender, MouseEventArgs e)
    {
      begin = true;
      firstPoint = new Point(e.X, e.Y);
    }
  }
}

願望本文所述對年夜家的C#法式設計有所贊助

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved