2012-03-31 81 views
2

我一直在試圖模仿Windows 7的截屏工具,它是如何將半透明灰色圖層疊加在屏幕上的,該圖層在選擇區域內變得完全透明。我走得很近。我正在顯示覆蓋整個屏幕的無邊界50%不透明灰色窗體,並且具有紫紅色的TransparencyKey。然後在那個表格的頂部畫兩個矩形。用於透明的固體紫紅色矩形和用於紅色邊框的另一個矩形。它有效,但前提是我做了三件事中的一件,其中沒有一件是選擇。C#透明實心矩形以50%不透明的形式繪製

  1. 禁止雙緩衝,這使得形式閃爍同時提請
  2. 更改桌面顏色模式從32位
  3. 到16bit的製作形式100%不透明

這裏是我的代碼。任何有關如何使這項工作的建議?

public partial class frmBackground : Form 
{ 
    Rectangle rect; 

    public frmBackground() 
    { 
     InitializeComponent(); 

     this.MouseDown += new MouseEventHandler(frmBackground_MouseDown); 
     this.MouseMove += new MouseEventHandler(frmBackground_MouseMove); 
     this.Paint += new PaintEventHandler(frmBackground_Paint); 
     this.DoubleBuffered = true; 
     this.Cursor = Cursors.Cross; 
    } 

    private void frmBackground_MouseDown(object sender, MouseEventArgs e) 
    { 
     Bitmap backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height); 
     rect = new Rectangle(e.X, e.Y, 0, 0); 
     this.Invalidate(); 
    } 

    private void frmBackground_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
      rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top); 

     this.Invalidate(); 
    } 

    private void frmBackground_Paint(object sender, PaintEventArgs e) 
    { 
     Pen pen = new Pen(Color.Red, 3); 
     e.Graphics.DrawRectangle(pen, rect); 

     SolidBrush brush = new SolidBrush(Color.Fuchsia); 
     e.Graphics.FillRectangle(brush, rect); 
    } 
} 

回答

0

可以使用

Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue)) 

如果α-從0到255,所以128的阿爾法值會給你50%opactity。

此解決方案發現在this question

+0

不,它不會。如果他的表格有0.2不透明度並且刷子有128個alpha,他將在他的矩形上得到0.2 * 128阿爾法 – dimitris93 2015-04-24 09:35:31