winform GDI基础(三)实现画笔
在程序窗口上使用鼠标画图
private Point pStart, pEnd; private bool isAllowDraw = false; private bool isOpenPen = false; private void Form1_MouseDown(object sender, MouseEventArgs e) { if (isOpenPen) { isAllowDraw = true; pStart = pEnd = e.Location; } } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (isOpenPen) { isAllowDraw = false; } } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (isOpenPen && isAllowDraw) { pEnd = e.Location; Graphics g = this.CreateGraphics(); g.SmoothingMode = SmoothingMode.HighQuality;//去掉锯齿 System.Drawing.Pen pen = new System.Drawing.Pen(Color.Red, 4); g.DrawLine(pen, pStart, pEnd); pStart = pEnd; } }