2012-12-07 25 views
8

Sử dụng C# và .Net 4.0 trong ứng dụng winforms: Có thể thêm lá chắn UAC vào một nút và giữ nguyên hình nền của nút không? Làm sao?Thêm một lá chắn UAC vào một nút và giữ lại hình nền của nó?

Đây là những gì tôi đang sử dụng vào lúc này, nhưng nó xóa ảnh ...

[DllImport("user32.dll")] 
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); 

public static void UACToButton(Button button, bool add) 
{ 
    const Int32 BCM_SETSHIELD = 0x160C; 
    if (add) 
     { 
     // The button must have the flat style 
     button.FlatStyle = FlatStyle.System; 
     if (button.Text == "") 
     // and it must have text to display the shield 
      button.Text = " "; 
      SendMessage(button.Handle, BCM_SETSHIELD, 0, 1);   
     } 
     else 
      SendMessage(button.Handle, BCM_SETSHIELD, 0, 0);  
} 

Cảm ơn!

+0

Bạn đang cố gắng chỉ hiển thị khiên mà không có văn bản trên nút? –

+0

Nút không có văn bản. Nó chỉ có một hình ảnh, được xóa khi phương thức được gọi. – Daro

+0

Tải biểu tượng từ imageres.dll và vẽ nó trên của riêng bạn trên hình nền trước khi vẽ hình ảnh trên nút. – Elmue

Trả lời

2

lẽ điều này sẽ giúp: http://blog.csharphelper.com/2011/03/03/add-uac-shields-to-buttons-menu-items-and-picture-boxes-in-c.aspx

Phải mất lá chắn UAC và trả về một bitmap có thể được đặt trên bất cứ điều gì có hỗ trợ bitmap.

EDIT: Dưới đây là một số mẫu mã thô mà có thể làm việc:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace UAC_Test 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     [DllImport("user32.dll")] 
     public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); 

     // Make the button display the UAC shield. 
     public static void AddShieldToButton(Button btn) 
     { 
      const Int32 BCM_SETSHIELD = 0x160C; 

      // Give the button the flat style and make it display the UAC shield. 
      btn.FlatStyle = System.Windows.Forms.FlatStyle.System; 
      SendMessage(btn.Handle, BCM_SETSHIELD, 0, 1); 
     } 

     // Return a bitmap containing the UAC shield. 
     private static Bitmap shield_bm = null; 
     public static Bitmap GetUacShieldImage() 
     { 
      if (shield_bm != null) return shield_bm; 

      const int WID = 50; 
      const int HGT = 50; 
      const int MARGIN = 4; 

      // Make the button. For some reason, it must 
      // have text or the UAC shield won't appear. 
      Button btn = new Button(); 
      btn.Text = " "; 
      btn.Size = new Size(WID, HGT); 
      AddShieldToButton(btn); 

      // Draw the button onto a bitmap. 
      Bitmap bm = new Bitmap(WID, HGT); 
      btn.Refresh(); 
      btn.DrawToBitmap(bm, new Rectangle(0, 0, WID, HGT)); 

      // Find the part containing the shield. 
      int min_x = WID, max_x = 0, min_y = HGT, max_y = 0; 

      // Fill on the left. 
      for (int y = MARGIN; y < HGT - MARGIN; y++) 
      { 
       // Get the leftmost pixel's color. 
       Color target_color = bm.GetPixel(MARGIN, y); 

       // Fill in with this color as long as we see the target. 
       for (int x = MARGIN; x < WID - MARGIN; x++) 
       { 
        // See if this pixel is part of the shield. 
        if (bm.GetPixel(x, y).Equals(target_color)) 
        { 
         // It's not part of the shield. 
         // Clear the pixel. 
         bm.SetPixel(x, y, Color.Transparent); 
        } 
        else 
        { 
         // It's part of the shield. 
         if (min_y > y) min_y = y; 
         if (min_x > x) min_x = x; 
         if (max_y < y) max_y = y; 
         if (max_x < x) max_x = x; 
        } 
       } 
      } 

      // Clip out the shield part. 
      int shield_wid = max_x - min_x + 1; 
      int shield_hgt = max_y - min_y + 1; 
      shield_bm = new Bitmap(shield_wid, shield_hgt); 
      Graphics shield_gr = Graphics.FromImage(shield_bm); 
      shield_gr.DrawImage(bm, 0, 0, 
       new Rectangle(min_x, min_y, shield_wid, shield_hgt), 
       GraphicsUnit.Pixel); 

      // Return the shield. 
      return shield_bm; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Graphics gfx = Graphics.FromImage(button1.BackgroundImage); 
      Bitmap shield = GetUacShieldImage(); 
      gfx.DrawImage(shield, button1.Width/2, button1.Height/2); 
     } 
    } 
} 

này được vẽ UAC khiên lên ảnh nền. Bạn có thể điều chỉnh vị trí hình ảnh ở đầu nút. Nó trông không đẹp bằng lá chắn UAC của hệ thống, nhưng nó hiển thị biểu tượng lá chắn UAC ở phía trên hình nền của bạn.

+0

No. Nó nhận được lá chắn UAC như là một bitmap và đặt nó trên bất cứ thứ gì hỗ trợ bitmap. Lấy mã đó và thêm hình nền vào nút và bạn sẽ thấy vấn đề của tôi. Nhưng dù gì cũng cảm ơn. – Daro

+0

Tôi đã cập nhật câu trả lời của mình để bao gồm một số mã mẫu. Tôi quên đề cập ban đầu rằng bạn sẽ cần phải vẽ lá chắn UAC trên hình nền của bạn. –

+1

Điều này thật phức tạp! Sử dụng SystemIcons.Shield nếu được hỗ trợ bởi khung công tác của bạn hoặc tải nó từ imageres.dll hoặc tải xuống biểu tượng đó từ nơi khác và tự vẽ nó. – Elmue

3

Như Elmue đã đề cập, SystemIcons.Shield là cách dễ nhất để truy cập biểu tượng UAC Shield. Đây là phương pháp tôi sử dụng để thêm hình ảnh lên trên các hình ảnh khác:

public static Image AddUACShieldToImage(Image image) 
    { 
     var shield = SystemIcons.Shield.ToBitmap(); 
     shield.MakeTransparent(); 

     var g = Graphics.FromImage(image); 
     g.CompositingMode = CompositingMode.SourceOver; 
     g.DrawImage(shield, new Rectangle(image.Width/2, image.Height/2, image.Width/2, image.Height/2)); 

     return image; 
    } 
Các vấn đề liên quan