簡體   English   中英

如何減少對True Transparency WinForm更新的閃爍?或者有更好的方法嗎?

[英]How can i reduce flickering on updates to my True Transparency WinForm? or is there a better way to do this?

這對我來說一直是個問題,我一直試圖制作一個透明度好的自定義繪制表單。

這是我現在可以得到的盡可能接近,我在半小時前就開始了..

編輯:我從頭開始制作自定義表單設計和控件,就像我最新的Sunilla http://i.stack.imgur.com/rqvDe.png 我想要在它上面留下一個好的陰影,或者制作另一種看起來有點像窗戶的設計。

它將form.opacity設置為0%然后它每隔500毫秒直接抓取一個屏幕圖像(屏幕上的任何內容,當前正在運行的程序,桌面等),並將其設置為背景並帶來透明度回到100%。 所以我可以在上面畫任何東西,它看起來很完美! 但我得到的唯一問題是當它完成工作時,它會閃爍。 是的,我嘗試將DoubleBuffering設置為true,沒有區別。

聽到主要代碼:

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 TTTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Opacity = 100;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            Opacity = 0;
            Bitmap img = new Bitmap(this.Width, this.Height);
            Graphics gr = Graphics.FromImage(img);
            gr.CopyFromScreen(Location, Point.Empty, Size);
            this.BackgroundImage = img;
            Opacity = 100;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            ExtDrawing2D.FillRoundRect(g, new SolidBrush(Color.FromArgb(100, 255, 255, 255)), new RectangleF(1, 1, Width - 3, Height - 3), 4f);
            g.DrawPath(new Pen(Color.FromArgb(100, 0, 0, 0)), ExtDrawing2D.GetRoundedRect(new RectangleF(0, 0, Width - 1, Height - 1), 5f));
            g.DrawPath(new Pen(Color.FromArgb(100, 255,255,255)), ExtDrawing2D.GetRoundedRect(new RectangleF(1,1, Width - 3, Height - 3), 4f));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1_Tick(sender, e);
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            ExtDrawing2D.FillRoundRect(g, new SolidBrush(Color.FromArgb(150, 255,255,255)), new RectangleF(1, 1, panel1.Width - 3, panel1.Height - 3), 2f);
            g.DrawPath(new Pen(Color.FromArgb(100, 0, 0, 0)), ExtDrawing2D.GetRoundedRect(new RectangleF(0, 0, panel1.Width - 1, panel1.Height - 1), 3f));
            g.DrawPath(new Pen(Color.FromArgb(100, 255, 255, 255)), ExtDrawing2D.GetRoundedRect(new RectangleF(1, 1, panel1.Width - 3, panel1.Height - 3), 2f));
        }
    }
}

注意: ExtDrawing2D是一個單獨的類,通過繪制幾乎完美的圓角來完成大量繁重的工作。 並不是問題,我半年前開發了它,並且在我的所有項目中從未遇到過問題。

結果:

如果有更好的方法來刺傷這個整體問題,我很高興喜歡聽到它,盡管我已經在網上瀏覽了很長時間。

我嘗試了你的代碼,因為我無法弄清楚你想要實現的目標,是的,它閃爍了很多。 但這是因為您將不透明度設置為0並且每半秒將其設置為1。 如果看起來你正在嘗試模擬透明窗口。 為什么不使用透明窗口? 即。 FormBorderStyle = None並將BackColor和TransparencyKey設置為相同的顏色(例如,紅色)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM