[英]How do I make my form transparent, but what I draw on it not?
我嘗試將表單的不透明度設置為50%,然后在其上繪制一個字符串。 似乎我在其上繪制的字符串也具有50%的不透明度。 我如何繪制非透明字符串,但讓表單背景顯示50%?
如果可能的話,也願意在WPF中這樣做,但我需要明確的指示或示例項目,因為我以前從未這樣做過。
為了澄清,我希望表單背景是一個黑色的80%不透明的底部,然后我想在它上面繪制文字等,讓它看起來100%不透明。
這在WPF中非常容易完成:
這是一個快速示例:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" AllowsTransparency="True" Background="#AAFFFFFF" WindowStyle="None">
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontWeight="Bold">Hello World!</TextBlock>
</Grid>
現在顯然,由於您已禁用標准Windows鑲邊,因此您需要提供自己的按鈕來關閉/最小化/最大化/拖動窗口。 你可以自己很容易地做到這一點,或者你可以考慮購買像Blendables這樣的東西,它帶有你可以使用的“Chromeless Window”控件。
您可以使用TransparencyKey。
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey.aspx
我想到的一個解決方法是使用兩個疊加在一起的窗口。 以50%不透明度渲染“底部”窗口,然后所有者將文本繪制到覆蓋在另一個上面的窗口。 如果您正在進行標簽式顯示,這可能很簡單但如果您需要大量用戶輸入可能會很快變得復雜,這可能需要過濾到“主”或“底部”窗口。
所以,我讓它工作,但它有點松鼠。 兩個窗口解決方案看起來很有希望,直到我發現.Net或Win32甚至隱含地引用它時會做一些奇怪的自動育兒事情。 可能與消息的抽取方式有關。 主要父母持有應用程序消息泵和int guess隱含地是父...
我嘗試了一堆變通方法,但下面的這個計時器工作效果最好。 在任何情況下,它可能是一個更好的做法的線索......
// the "main" or transparent window. Notice it just sets and runs the timer using System; using System.Windows.Forms; namespace TransparencyPlusNottransparentTest { public partial class FormMain : Form { public FormMain() { InitializeComponent(); }private void timer1_Tick(object sender, EventArgs e) { Program.ShowNontransparency(); } }
}
// the "top" or not transparent window. Notice it does owner draw on // transparent background. The design-time settings are also sans border etc. using System.Drawing; using System.Windows.Forms;
namespace TransparencyPlusNottransparentTest { public partial class FormTop : Form { public FormTop() { InitializeComponent(); BackColor = Color.Firebrick; TransparencyKey = Color.Firebrick; }
private void FormTop_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawString("Hello Whirrled!", new Font("Tahoma", 14f), Brushes.Black, 10f, 10f ); } }
}
// The control of this whole spiel. It instantiates both windows, // sets the main as the main app window and hosts the public // hacky method to force the non-transparent window to show up on top // and offset so it doesn't obscure the top of the main window. using System; using System.Drawing; using System.Windows.Forms;
namespace TransparencyPlusNottransparentTest { static class Program { private static FormMain _formMain; private static FormTop _formTop; private const int XY_OFFSET = 30;
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); _formTop = new FormTop(); _formTop.Show(null); _formMain = new FormMain(); Application.Run(_formMain); } public static void ShowNontransparency() { _formTop.Location = new Point( _formMain.Location.X + XY_OFFSET, _formMain.Location.Y + XY_OFFSET); _formTop.BringToFront(); } }
}
在ControlStyles枚舉MSDN中有一個名為SupportsTransparentBackColor的值。 在你的表單構造器中運行:
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
然后你可以將背景顏色設置為Color.Transparent。
希望這可以幫助。
使用TransparencyKey將窗口渲染為透明。 使用您的背景顏色創建一個空面板,並為其指定所需的不透明度。
然后在一個單獨的容器/面板(本身是上面創建的半透明面板的兄弟)中創建所有孩子,並讓它根本不渲染背景。
這應該給你半透明背景但可見控件的效果。
在winforms中,MSDN說:
使用“不透明度”屬性可以指定窗體及其控件的透明度級別。 當此屬性設置為小於100%(1.00)的值時,整個表單(包括邊框)將變得更加透明。
因此,您設置不透明度的窗體的任何子控件都將更改其不透明度。 如果要在單個控件上實現不同級別的不透明度,則必須切換到WPF。
我假設這是一個WinForms項目,雖然你說你願意嘗試WPF。
Lemme切換到我的橫向思維上限:你有什么特別的理由不能偽造它嗎? 如在,在PhotoShop Elements或Paint Shop Pro中制作一個位圖,或者你將設置為WinForm的整個背景,你只需繪制部分透明區域,控件將放在該位圖上?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.