2012-12-07 10 views
8

एक Winforms अनुप्रयोग में सी # और .Net 4.0 का उपयोग करना: क्या एक बटन पर यूएसी ढाल जोड़ना और बटन पृष्ठभूमि छवि को बनाए रखना संभव है? कैसे?एक बटन में एक यूएसी शील्ड जोड़ें और अपनी पृष्ठभूमि छवि को बनाए रखें?

यह है कि मैं क्या इस समय का उपयोग कर रहा है, लेकिन यह चित्र भी निकल जाता ...

[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);  
} 

धन्यवाद!

+0

क्या आप बटन पर टेक्स्ट के बिना केवल ढाल प्रदर्शित करने का प्रयास कर रहे हैं? –

+0

बटन में कोई टेक्स्ट नहीं है। इसमें केवल एक छवि है, जिसे विधि कहा जाता है जिसे हटा दिया जाता है। – Daro

+0

imageres.dll से आइकन लोड करें और बटन पर छवि खींचने से पहले इसे पृष्ठभूमि छवि पर स्वयं खींचें। – Elmue

उत्तर

2

शायद यह मदद मिलेगी: http://blog.csharphelper.com/2011/03/03/add-uac-shields-to-buttons-menu-items-and-picture-boxes-in-c.aspx

यह यूएसी ढाल लेता है और एक बिटमैप कि कुछ भी है कि बिटमैप्स का समर्थन करता है पर रखा जा सकता देता है।

संपादित करें:

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); 
     } 
    } 
} 

यह यूएसी पृष्ठभूमि वाले चित्र पर ढाल आ रहा है: यहाँ कुछ किसी न किसी नमूना कोड है कि काम कर सकता है। आप बटन के शीर्ष पर छवि स्थान समायोजित कर सकते हैं। यह सिस्टम यूएसी ढाल के रूप में सुंदर नहीं दिखता है, लेकिन यह आपकी पृष्ठभूमि छवि के शीर्ष पर यूएसी शील्ड आइकन दिखाता है।

+0

नहीं। यह यूएसी शील्ड को बिटमैप के रूप में प्राप्त करता है और बिटमैप्स का समर्थन करने वाले किसी भी चीज़ पर रखता है। उस कोड को लें और बटन पर पृष्ठभूमि छवि जोड़ें और आपको मेरी समस्या दिखाई देगी। लेकिन फिर भी धन्यवाद। – Daro

+0

मैंने कुछ नमूना कोड शामिल करने के लिए अपना उत्तर अपडेट कर दिया है। मैं शुरुआत में उल्लेख करना भूल गया था कि आपको अपनी पृष्ठभूमि छवि पर यूएसी शील्ड खींचने की आवश्यकता होगी। –

+1

यह बेहद जटिल है! SystemIcons.Shield का उपयोग करें यदि आपके ढांचे द्वारा समर्थित है या इसे imageres.dll से लोड करें या उस आइकन को कहीं और डाउनलोड करें और इसे स्वयं पर खींचें। – Elmue

3

जैसा कि एल्मु ने उल्लेख किया है, सिस्टम इंक। शील्ड यूएसी शील्ड आइकन तक पहुंचने का सबसे आसान तरीका है। यहां दी गई विधि है जिसे मैं अन्य छवियों के शीर्ष पर जोड़ने के लिए उपयोग करता हूं:

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; 
    } 
संबंधित मुद्दे