2015-11-04 17 views
0

我遇到了Windows窗體應用程序的問題。我不得不改變配置來處理訪問衝突異常。使用線性漸變畫筆將窗體邊框樣式更改爲無,導致錯誤

無論如何,我基本上做了一個程序,彈出在右下角像通知。這不是整個計劃,但我會整合。我在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); 
     } 



    } 


} 

}

回答

0

終於想通了,dont't使用Windows窗體定製尋找窗口。特別是線性漸變工具,它非常有問題,而且真的只是在表單頂部繪製一個矩形的黑客工具。我開始使用WPF,它很棒。不要吝嗇自己的號角,但我希望這樣可以讓人們清楚使用Windows Forms的原因。