6

这一直是我的一个问题,我一直在尝试制作具有良好透明度的自定义绘制表格。

这是我现在可以得到的最接近的地方,我半小时前才开始..

编辑: 我从头开始制作自定义表单设计和控件,例如我最新的 Sunilla http://i.stack.imgur.com/rqvDe.png。我想在上面有一个很好的阴影,或者做另一个看起来有点像 windows areo 的设计。

它将 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 是一个单独的类,它在绘制几乎完美的圆角方面做了很多繁重的工作。AND 不是问题,我是半年前开发的,在我所有的项目中都没有遇到过问题。

结果:

如果有更好的方法来解决这个整体问题,我很高兴听到它,尽管我已经在网上寻找了很长时间。

4

1 回答 1

1

我尝试了您的代码,因为我无法弄清楚您要实现的目标,是的,它闪烁很多。但这是因为您将不透明度设置为 0 并每半秒返回 1。如果看起来您正在尝试模拟透明窗口。为什么不直接使用透明窗口?IE。FormBorderStyle=None 并将 BackColor 和 TransparencyKey 设置为相同的颜色(例如红色)。

于 2012-06-26T01:20:47.690 回答