سؤال

هل من الممكن إنشاء مؤشر من صورة ويكون شبه شفاف؟

أقوم حاليًا بالتقاط صورة مخصصة وأقوم بتركيب صورة مؤشر الماوس.سيكون أمرا رائعا لو تمكنت من جعل هذا شبه شفاف، ولكن ليس ضروريا.رجال المبيعات يحبون لامعة.

أفعل حاليًا شيئًا مثل هذا:

Image cursorImage = customImage.GetThumbnailImage(300, 100, null, IntPtr.Zero);
cursorImage.SetResolution(96.0F, 96.0F);
int midPointX = cursorImage.Width / 2;
int midPointY = cursorImage.Height / 2;
Bitmap cursorMouse = GetCursorImage(cursorOverlay);
Graphics cursorGfx = Graphics.FromImage(cursorImageCopy);
cursorGfx.DrawImageUnscaled(cursorMouse, midPointX, midPointY);

Cursor tmp = new Cursor(cursorImage.GetHicon());

نص بديل http://members.cox.net/dustinbrooks/drag.jpg

هل كانت مفيدة؟

المحلول

لقد حاولت اتباع المثال، وكان يعمل بشكل جيد ...

    public struct IconInfo
    {
        public bool fIcon;
        public int xHotspot;
        public int yHotspot;
        public IntPtr hbmMask;
        public IntPtr hbmColor;
    }


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);

    [DllImport("user32.dll")]
    public static extern IntPtr CreateIconIndirect(ref IconInfo icon);

    public static Cursor CreateCursor(Bitmap bmp, int xHotSpot, int yHotSpot)
    {
        IntPtr ptr = bmp.GetHicon();
        IconInfo tmp = new IconInfo();
        GetIconInfo(ptr, ref tmp);
        tmp.xHotspot = xHotSpot;
        tmp.yHotspot = yHotSpot;
        tmp.fIcon = false;
        ptr = CreateIconIndirect(ref tmp);
        return new Cursor(ptr);
    }

ولقد قمت بوضع هذا على حدث النقر على الزر (يمكنك الاتصال من المكان الذي تريده):

Bitmap b = new Bitmap("D:/Up.png");
this.Cursor = CreateCursor(b, 5, 5);

ويتم حفظ صورة Up.png بنسبة عتامة تصل إلى 75% في AdobePhotoshop.

نصائح أخرى

في أعلى رأسي (سأحاول ذلك أولاً):

  1. إنشاء صورة نقطية جديدة بنفس حجم الصورة الأصلية، ولكن ببنية ARGB
  2. رسم الصورة:الصورة النقطية الموجودة إلى الصورة النقطية الجديدة
  3. الوصول إلى بيانات الصورة النقطية الأولية، واستبدال البايت بـ 128

يجب أن يكون لديك صورة نقطية شبه شفافة لطيفة هناك.

إذا سمح الأداء، يمكنك البحث عن وحدات بكسل شفافة بالكامل وتعيين A إلى صفر لها!

إذا كنت تريد تعيين شفافية الصورة النقطية المخصصة لمؤشر الماوس "أثناء التنقل"، فقد تجد هذه الوظيفة مفيدة.يستخدم مصفوفة ألوان لتعيين مقدار الشفافية لأي صورة نقطية معينة وسيعيد الصورة المعدلة.للحصول على لمسة من الشفافية فقط TranspFactor يجب أن يكون بين 225 و 245، فقط جربه.(تحتاج إلى استيراد System.Drawing وSystem.Drawing.Imaging)

public static Bitmap GetBMPTransparent(Bitmap bmp, int TranspFactor)

{

Bitmap transpBmp = new Bitmap(bmp.Width, bmp.Height);
using (ImageAttributes attr = new ImageAttributes()) {
    ColorMatrix matrix = new ColorMatrix { Matrix33 = Convert.ToSingle(TranspFactor / 255) };
    attr.SetColorMatrix(matrix);
    using (Graphics g = Graphics.FromImage(transpBmp)) {
        g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr);
    }
}
return transpBmp;

}

هذا سهل جدًا، فأنا لا أستخدم API.

الرمز هو

  Bitmap img = new Bitmap(new Bitmap(@"image.png"), 30, 30); //this is the size of cursor

  Icon icono = Icon.FromHandle(img.GetHicon()); //create the Icon object 

  Cursor = new Cursor(icono.Handle); //the icon Object has the stream to create a Cursor.

آمل أن يكون هذا هو الحل الخاص بك

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top