将表单边框样式更改为无,在使用线性渐变画笔时会导致错误

时间:2015-11-04 20:07:06

标签: c# forms border linear-gradients

我遇到了Windows窗体应用程序的问题。我不得不更改配置以处理Access违例异常。

无论如何,我实际上正在制作一个像通知那样在右下角弹出的程序。它不是整个程序,而是我将要整合的东西。我在使用LinearGradientBrush类时遇到了一些问题。我注意到我的窗口在尝试通过AccessViolationExcetption绘制背景时正在移动,所以我在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);
        }



    }


}

}

1 个答案:

答案 0 :(得分:0)

最后弄清楚了,不要将Windows窗体用于自定义窗口。特别是线性渐变工具,这是非常错误的,实际上只是在窗体顶部绘制矩形的黑客。我开始使用WPF,这太神奇了。不要故意用自己的号角,但我希望这可以让人们清楚地使用Windows Forms,原因如此。