簡體   English   中英

如何在 C# 中制作 1 個以上的透明圖片框層?

[英]How to make more than 1 transparent picturebox layer in C#?

我正在使用 3 個圖片框。 1 作為背景 & 2 作為背景上的透明層。 所有尺寸相同。 第 1 層用於繪制線條,第 2 層用於繪制形狀。 我正在使用選項卡控件來控制哪個圖層可見,哪個圖層隱藏。 但不知何故,即使它們都是透明的,也不能同時使兩個圖層可見。

我正在使用的代碼

public Form1()
        {
            InitializeComponent();
            bgLayer.Image = bmp;
            bgLayer.Controls.Add(lineLayer);
            bgLayer.Controls.Add(squareLayer);
            lineLayer.Location = new Point(0, 0);
            squareLayer.Location = new Point(0, 0);
            lineLayer.BackColor = Color.Transparent;
            squareLayer.BackColor = Color.Transparent;
        }

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                lineLayer.Visible = true;
                squareLayer.Visible = false;
                lineLayer.Enabled = true;
                squareLayer.Enabled = false;
            }
            else if (tabControl1.SelectedIndex == 2)
            {
                lineLayer.Visible = false;
                squareLayer.Visible = true;
                lineLayer.Enabled = false;
                squareLayer.Enabled = true;
            }
        }

有誰知道如何使兩個透明層同時可見? 選項卡控件 0 都是可見的,1 僅是picturebox1,2 僅是picturebox3。 選項卡控件 1 和 2 工作正常,但 0 僅顯示圖層 picturebox1。

嘗試添加lineLayer.Controls.Add(squareLayer); 但它使程序緩沖區在執行時不會停止

使用 WinForm 的PictureBox是不可能的:WinForms 不支持 alpha 混合(甚至索引透明)控件的 z 排序,您可以使用 WPF 或 HTML+CSS 的方式。 它唯一允許控件在繪制自己之前重新呈現其父Control的背景(請注意,父控件也必須剪輯其子控件,因為 WinForms 中的所有Control子類都封裝了 User32 hWnd。唯一的解決方法是創建一個沒有非客戶區的新頂級窗口,這可能很痛苦)。

唯一的解決方法是使用一個自定義繪制的控件來重繪其覆蓋的OnPaint事件內的堆疊圖像,或者在每次希望外觀更改並使用單個PictureBox時重新生成內存中的Bitmap請參見此處: 使重疊PictureBox 透明在 C#.net 中

暫無
暫無

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

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