将表单边框样式更改为“无”会导致使用线性渐变笔刷时出错
本文关键字:线性 渐变 出错 样式 边框 表单 | 更新日期: 2023-09-27 18:20:31
我的Windows窗体应用程序出现问题。我不得不更改配置以处理访问违规异常。
无论如何,我本质上是在制作一个像通知一样弹出在右下角的程序。这不是整个程序,而是我将要整合的东西。我对LinearGradientBrush类有一些问题。当我的窗口试图通过AccessViolationExcepttion绘制背景时,我注意到它在移动,所以我在OnPaintBackgroundMethod中设置了一个布尔值,以记录绘制过程的进行时间,并确保在函数运行时窗口不会移动。这似乎解决了问题。但是,当我通过取消FormBorderStyle面板中的边框来进一步自定义表单设计时,问题又回来了。我不知道为什么这会引起任何麻烦,我到处寻找解决方案,但都无济于事。非常感谢您的帮助。谢谢
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true" />
</runtime>
</configuration>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NewToasterNotifier
{
public partial class Form1 : Form
{
private Timer timer;
private int startPosX, startPosY, beginning, middle, speed, paintCount;
private bool rectangleIsDrawing;
public Form1()
{
InitializeComponent();
TopMost = true;
ShowInTaskbar = false;
timer = new Timer();
timer.Interval = 20;
timer.Tick += timer_Tick;
//This divides the window into three sections
//The beginning is first used as the scalar amaount of each interval
beginning = Height / 3;
//middle is where the middle section ends
middle = Screen.PrimaryScreen.WorkingArea.Height - 2 * beginning;
//and now beginnning is where the beginning section ends
//the true begginnning section starts at the screen.height
beginning = Screen.PrimaryScreen.WorkingArea.Height - beginning;
speed = 0;
}
//On load of the form
protected override void OnLoad(EventArgs e)
{
startPosX = Screen.PrimaryScreen.WorkingArea.Width - Width;
startPosY = Screen.PrimaryScreen.WorkingArea.Height;
SetDesktopLocation(startPosX, startPosY);
base.OnLoad(e);
timer.Start();
paintCount = 0;
}
//Override of the onPaintbackground, which allows us to make a different background on windows forms
protected override void OnPaintBackground(PaintEventArgs e)
{
rectangleIsDrawing = true;
paintCount++;
Console.Write("'n Painting... " + paintCount.ToString());
//implements a linear gradient to the background
using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle,
Color.Gray,
Color.Black,
90F))
{
paintThatShiz(e, brush);
}
}
//linear gradient needs to change on resize
protected override void OnResize(EventArgs e)
{
this.Invalidate();
base.OnResize(e);
}
//debugging purposes
private void paintThatShiz(PaintEventArgs e, LinearGradientBrush brush)
{
try { e.Graphics.FillRectangle(brush, this.ClientRectangle); }
catch (AccessViolationException x)
{
Console.Write("No, no, no!!!!!");
paintThatShiz(e, brush);
}
catch (InvalidOperationException z)
{
Console.Write("This is ridiculous, 'n");
Console.Write(z.StackTrace);
paintThatShiz(e, brush);
}
rectangleIsDrawing = false;
}
//called every 10ms for window to animate and stops when done
void timer_Tick(object sender, EventArgs e)
{
if (rectangleIsDrawing) { return; }
Console.Write("'n Starting to move");
// this if block is for the swing animation
//in the beginning it speeds up
if (startPosY > beginning)
{
speed++;
}//in the end it slows down
else if (startPosY < middle)
{
if (speed > 1)
{
speed--;
}
}
//in the middle it stays constant
Console.Write(speed.ToString() + " ");
startPosY -= speed;
//Stops when done
if (startPosY < Screen.PrimaryScreen.WorkingArea.Height - Height)
{
timer.Stop();
} // checks again to see if rectangle is drawing before accessing window location data
else {
if (rectangleIsDrawing) {
startPosY += speed;
return;
}
SetDesktopLocation(startPosX, startPosY);
}
}
}
}
终于明白了,不要将Windows窗体用于自定义外观的窗口。特别是线性渐变工具,它很有缺陷,实际上只是在表单顶部绘制矩形的一个技巧。我开始使用WPF,它很神奇。我无意自吹自擂,但我希望这能引导人们避免出于这些原因使用Windows窗体。