gpt4 book ai didi

c# - 如何停止闪烁 C# winforms

转载 作者:IT王子 更新时间:2023-10-29 03:43:58 25 4
gpt4 key购买 nike

我有一个本质上类似于绘画应用程序的程序。但是,我的程序有一些闪烁的问题。我的代码中有以下行(应该消除闪烁 - 但没有):

this.SetStyle(ControlStyles.AllPaintingInWmPaint 
| ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

我的代码(减去形状的父类(super class)和子类如下:

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;

namespace Paint
{
public partial class Paint : Form
{
private Point startPoint;
private Point endPoint;
private Rectangle rect = new Rectangle();
private Int32 brushThickness = 0;
private Boolean drawSPaint = false;
private List<Shapes> listOfShapes = new List<Shapes>();
private Color currentColor;
private Color currentBoarderColor;
private Boolean IsShapeRectangle = false;
private Boolean IsShapeCircle = false;
private Boolean IsShapeLine = false;

public SPaint()
{

InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);

currentColor = Color.Red;
currentBoarderColor = Color.DodgerBlue;
IsShapeRectangle = true;
}

private void panelArea_Paint(object sender, PaintEventArgs e)
{
Graphics g = panelArea.CreateGraphics();

if (drawSPaint == true)
{

Pen p = new Pen(Color.Blue);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

if (IsShapeRectangle == true)
{
g.DrawRectangle(p, rect);
}
else if (IsShapeCircle == true)
{
g.DrawEllipse(p, rect);
}
else if (IsShapeLine == true)
{
g.DrawLine(p, startPoint, endPoint);
}
}
foreach (Shapes shape in listOfShapes)
{

shape.Draw(g);

}
}

private void panelArea_MouseDown(object sender, MouseEventArgs e)
{

startPoint.X = e.X;
startPoint.Y = e.Y;

drawSPaint = true;
}

private void panelArea_MouseMove(object sender, MouseEventArgs e)
{


if (e.Button == System.Windows.Forms.MouseButtons.Left)
{

if (e.X > startPoint.X)
{
rect.X = startPoint.X;
rect.Width = e.X - startPoint.X;
}
else
{
rect.X = e.X;
rect.Width = startPoint.X - e.X;
}
if (e.Y > startPoint.Y)
{
rect.Y = startPoint.Y;
rect.Height = e.Y - startPoint.Y;
}
else
{
rect.Y = e.Y;
rect.Height = startPoint.Y - e.Y;
}


panelArea.Invalidate();

}

}

private void panelArea_MouseUp(object sender, MouseEventArgs e)
{

endPoint.X = e.X;
endPoint.Y = e.Y;

drawSPaint = false;

if (rect.Width > 0 && rect.Height > 0)
{
if (IsShapeRectangle == true)
{
listOfShapes.Add(new TheRectangles(rect, currentColor, currentBoarderColor, brushThickness));
}
else if (IsShapeCircle == true)
{
listOfShapes.Add(new TheCircles(rect, currentColor, currentBoarderColor, brushThickness));
}
else if (IsShapeLine == true)
{
listOfShapes.Add(new TheLines(startPoint, endPoint, currentColor, currentBoarderColor, brushThickness));
}

panelArea.Invalidate();
}
}


private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
{
IsShapeRectangle = true;
IsShapeCircle = false;
IsShapeLine = false;
}

private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
{
IsShapeRectangle = false;
IsShapeCircle = true;
IsShapeLine = false;
}

private void lineToolStripMenuItem_Click(object sender, EventArgs e)
{
IsShapeCircle = false;
IsShapeRectangle = false;
IsShapeLine = true;
}

private void ThicknessLevel0_Click(object sender, EventArgs e)
{
brushThickness = 0;
}

private void ThicknessLevel2_Click(object sender, EventArgs e)
{
brushThickness = 2;
}

private void ThicknessLevel4_Click(object sender, EventArgs e)
{
brushThickness = 4;
}

private void ThicknessLevel6_Click(object sender, EventArgs e)
{
brushThickness = 6;
}

private void ThicknessLevel8_Click(object sender, EventArgs e)
{
brushThickness = 8;
}

private void ThicknessLevel10_Click(object sender, EventArgs e)
{
brushThickness = 10;
}

private void ThicknessLevel12_Click(object sender, EventArgs e)
{
brushThickness = 12;
}

private void ThicknessLevel14_Click(object sender, EventArgs e)
{
brushThickness = 14;
}

private void FillColour_Click(object sender, EventArgs e)
{

ColorDialog fillColourDialog = new ColorDialog();
fillColourDialog.ShowDialog();
currentColor = fillColourDialog.Color;
panelArea.Invalidate();
}

private void button1_Click(object sender, EventArgs e)
{

ColorDialog fillColourDialog = new ColorDialog();
fillColourDialog.ShowDialog();
currentBoarderColor = fillColourDialog.Color;
panelArea.Invalidate();
}


}
}

如何停止闪烁?

*更新:*当我直接在表单上绘图时,这段代码实际上非常有效。但是,当我尝试在面板上绘图时,闪烁成为一个问题

最佳答案

对于“更干净的解决方案”,为了继续使用基本面板,您可以简单地使用反射来实现双缓冲,方法是将此代码添加到包含要在其中绘制的面板的表单中

    typeof(Panel).InvokeMember("DoubleBuffered", 
BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, DrawingPanel, new object[] { true });

其中“DrawingPanel”是您要进行双缓冲的面板的名称。

我知道自问这个问题以来已经过去了很长时间,但这可能对将来的某些人有所帮助。

关于c# - 如何停止闪烁 C# winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8046560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com