国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C# GDI在控件上繪圖

2019-11-17 02:54:19
字體:
來源:轉載
供稿:網友
C# GDI在控件上繪圖

本文以以在chart控件上和窗體上畫矩形為例子

不多解釋了,代碼很簡單。

還有一些童鞋要別的源碼,給我發郵箱吧

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);        }            }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 淮阳县| 察隅县| 稻城县| 庆城县| 望城县| 华容县| 新宾| 增城市| 聂荣县| 乌什县| 龙口市| 济阳县| 凤阳县| 阿尔山市| 海城市| 库尔勒市| 广平县| 包头市| 沙河市| 贵港市| 襄樊市| 济宁市| 安宁市| 珠海市| 娱乐| 理塘县| 共和县| 新乡市| 巴青县| 灵台县| 德惠市| 瓦房店市| 梅河口市| 自治县| 保山市| 疏勒县| 五常市| 娄底市| 肃南| 益阳市| 宜州市|