下面的代码示例演示如何创建以 75% 的不透明度显示的窗体。该代码示例创建一个新窗体,该窗体位于屏幕的中央,其 Opacity 属性被设置为更改窗体的不透明度。该代码示例还设置 Size 属性来提供大于默认窗体大小的窗体。该代码示例假定该示例中定义的方法是从事件处理程序的另一个窗体或其他方法中调用。private void CreateMyOpaqueForm() { // Create a new form. Form form2 = new Form(); // Set the text displayed in the caption. form2.Text = "My Form"; // Set the opacity to 75%. form2.Opacity = .75; // Size the form to be 300 pixels in height and width. form2.Size = new Size(300,300); // Display the form in the center of the screen. form2.StartPosition = FormStartPosition.CenterScreen; // Display the form as a modal dialog box. form2.ShowDialog(); }
这里有个变暗的例子.你参考一下么 // Form1.cs // Compile with csc /t:winexe form1.csusing System; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Forms;public partial class Form1 : Form { Bitmap bg; Timer timer = new Timer(); Form form; public Form1() { Rectangle workArea = Screen.PrimaryScreen.WorkingArea; using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) { // Get HBITMAP of desktop window IntPtr desktopDC = g.GetHdc(); const int OBJ_BITMAP = 7; IntPtr desktopBitmap = GetCurrentObject(desktopDC, OBJ_BITMAP); Bitmap bm = Bitmap.FromHbitmap(desktopBitmap); // change to 256 indexed color bg = bm.Clone(workArea, System.Drawing.Imaging.PixelFormat.Format24bppRgb); MemoryStream bitStream = new MemoryStream(); bg.Save(bitStream, ImageFormat.Gif); bg = new Bitmap(bitStream); } this.SuspendLayout(); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.TopMost = true; this.WindowState = FormWindowState.Maximized; this.ResumeLayout(false); timer.Interval = 200; timer.Enabled = true; timer.Tick += delegate { ColorPalette palette = bg.Palette; for (int i = 0; i < palette.Entries.Length; i++) { Color c = palette.Entries[i]; int gray = (c.R + c.G + c.B) / 3; gray = gray - gray / 10; int r = Math.Max(0, c.R - (c.R - gray) / 3); int g = Math.Max(0, c.G - (c.G - gray) / 3); int b = Math.Max(0, c.B - (c.B - gray) / 3); palette.Entries[i] = Color.FromArgb(r, g, b); } bg.Palette = palette; Invalidate(); }; // show a form form = new Form(); form.Text = "Ctrl+F4 to close me"; form.Show(this); form.Disposed += delegate { Close(); }; this.Activated += delegate { form.Activate(); }; } protected override void OnPaintBackground(PaintEventArgs e) { } protected override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImageUnscaled(bg, 0, 0); } static void Main() { Application.Run(new Form1()); } [System.Runtime.InteropServices.DllImport("gdi32.dll")] internal extern static IntPtr GetCurrentObject(IntPtr hdc, ushort objectType); }
{
// Create a new form.
Form form2 = new Form();
// Set the text displayed in the caption.
form2.Text = "My Form";
// Set the opacity to 75%.
form2.Opacity = .75;
// Size the form to be 300 pixels in height and width.
form2.Size = new Size(300,300);
// Display the form in the center of the screen.
form2.StartPosition = FormStartPosition.CenterScreen; // Display the form as a modal dialog box.
form2.ShowDialog();
}
// Form1.cs
// Compile with csc /t:winexe form1.csusing System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;public partial class Form1 : Form
{
Bitmap bg;
Timer timer = new Timer();
Form form; public Form1()
{
Rectangle workArea = Screen.PrimaryScreen.WorkingArea;
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{
// Get HBITMAP of desktop window
IntPtr desktopDC = g.GetHdc();
const int OBJ_BITMAP = 7;
IntPtr desktopBitmap = GetCurrentObject(desktopDC, OBJ_BITMAP);
Bitmap bm = Bitmap.FromHbitmap(desktopBitmap); // change to 256 indexed color
bg = bm.Clone(workArea, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
MemoryStream bitStream = new MemoryStream();
bg.Save(bitStream, ImageFormat.Gif);
bg = new Bitmap(bitStream);
} this.SuspendLayout();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
this.ResumeLayout(false); timer.Interval = 200;
timer.Enabled = true;
timer.Tick += delegate
{
ColorPalette palette = bg.Palette;
for (int i = 0; i < palette.Entries.Length; i++)
{
Color c = palette.Entries[i];
int gray = (c.R + c.G + c.B) / 3;
gray = gray - gray / 10; int r = Math.Max(0, c.R - (c.R - gray) / 3);
int g = Math.Max(0, c.G - (c.G - gray) / 3);
int b = Math.Max(0, c.B - (c.B - gray) / 3);
palette.Entries[i] = Color.FromArgb(r, g, b);
}
bg.Palette = palette;
Invalidate();
}; // show a form
form = new Form();
form.Text = "Ctrl+F4 to close me";
form.Show(this); form.Disposed += delegate { Close(); };
this.Activated += delegate { form.Activate(); };
}
protected override void OnPaintBackground(PaintEventArgs e)
{
} protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(bg, 0, 0);
} static void Main()
{
Application.Run(new Form1());
} [System.Runtime.InteropServices.DllImport("gdi32.dll")]
internal extern static IntPtr GetCurrentObject(IntPtr hdc, ushort objectType);
}
没办法的话..下午就改用渐变Opacity 值了..