【问题标题】:Transparent child form in parent form with transparency and background具有透明度和背景的父窗体中的透明子窗体
【发布时间】:2016-03-23 17:22:48
【问题描述】:

我有一个带有背景图像和透明度键的表单。 在该表单中,我放置了必须完全透明的子表单以显示父表单的背景。如果我为子窗体设置另一个透明度键 - 它根本不会获得透明度,并且如果我设置父窗体的透明度键 - 子窗体会切穿父窗体的背景图像。

我需要使用表单而不是用户控件,所以这是一个问题。而且我不想将重复的背景图像设置为子窗体。

我在视觉上工作。这是设计师的代码:

  1. 那是我的父母

        // 
        // Main
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.DarkRed;
        this.BackgroundImage = global::NWN_Tsuki.Properties.Resources.Book;
        this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
        this.ClientSize = new System.Drawing.Size(800, 665);
        this.Controls.Add(this.tableLayoutPanel1);
        this.Controls.Add(this.CloseBtn);
        this.DoubleBuffered = true;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.KeyPreview = true;
        this.MaximumSize = new System.Drawing.Size(800, 665);
        this.MinimumSize = new System.Drawing.Size(800, 665);
        this.Name = "Main";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Header";
        this.TransparencyKey = System.Drawing.Color.DarkRed;
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Main_KeyDown);
        this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Main_MouseDown);
        this.tableLayoutPanel1.ResumeLayout(false);
        this.ResumeLayout(false);
    
  2. 这就是我将孩子插入父母面板的方式:

        private void LeftPanel_Paint(object sender, PaintEventArgs e) {
        ToC toc = new ToC();
        toc.TopLevel = false;
        toc.AutoScroll = true;
        this.LeftContent.Controls.Add(toc);
        toc.FormBorderStyle = FormBorderStyle.None;
        toc.Dock = DockStyle.Fill;
        toc.Show(); 
    }
    
  3. 这是我的孩子:

        // 
        // ToC
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Silver;
        this.ClientSize = new System.Drawing.Size(428, 396);
        this.Controls.Add(this.Ch7Btn);
        this.Controls.Add(this.Ch6Btn);
        this.Controls.Add(this.Ch5Btn);
        this.Controls.Add(this.Ch4Btn);
        this.Controls.Add(this.Ch3Btn);
        this.Controls.Add(this.Ch2Btn);
        this.Controls.Add(this.Ch1Btn);
        this.Controls.Add(this.label1);
        this.Name = "ToC";
        this.Text = "ToC";
        this.TransparencyKey = System.Drawing.Color.Silver;
        this.ResumeLayout(false);
    

这就是当孩子在父母中是 Silver 时的情况,现在重要的是它具有透明键 Silver。 如果我将this.BackColor = System.Drawing.Color.DarkRed; 设置为 Child - 它会穿透父母的背景。

这是我的意思的一些图像。

Child with other then parent transparency key

Child with same as parent transparency

【问题讨论】:

  • 向我们展示一些代码,以便我们了解您的尝试。

标签: c# winforms


【解决方案1】:

我不确定为什么您的子窗体的背景会穿透主窗体。 您可以尝试两件事。

  1. 尝试将两者都设置为您很可能不会使用的不同颜色。喜欢Color.Magenta。 (虽然我知道您可能已经尝试过)

  2. 您应该可以将背景颜色设置为透明here

    Button1.BackColor = 颜色.透明;

【讨论】:

  • 好吧,我在示例中尝试了 DarkRed。如果两者具有相同的颜色 - 孩子刺穿父母。如果孩子有其他颜色 - 它不会变得透明。问题是我需要将透明颜色设置为整个表单,而不是单个元素,因为我在面板中显示表单。如果我将控制颜色设置为透明-它将相对于它的父级透明-我的“子”表单。并且子窗体仍然根本不透明或会刺穿父窗体。
  • 你的 Main 需要那个透明键吗?也许这就是它刺穿它的原因
  • 是的,它需要透明度,主要原因是PNG-8格式的背景图片在屏幕上呈现简单打开的书本效果,没有任何边框。您可以在屏幕截图上看到它 - 推出了解决方案,您可以在其中看到无边框的书。
  • 好吧,我试图从主窗体中删除透明度。一样的效果。所以这不是问题。孩子或根本不透明,或者即使父母不透明也能感知父母。
  • 然后你将主背景色和父背景色的透明度键设置为深红色,然后将主背景色和子透明度键设置为另一种颜色,如蓝色。这有什么不同吗?
猜你喜欢
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 2011-05-06
  • 2021-05-20
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
相关资源
最近更新 更多