רציתי מכמה טעמים לקרוא לcolorpicker של חלונות מתוך C#
מצו"ב הקוד שגיבבתי מפה ומשם:
עריכה: מסתבר שסתם בזבזתי זמן ומתוך רשלנות שחכתי לבדוק אם דבר כזה כבר קיים ראה להלן בהמשך השרשור
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SystemColorPicker
{
public class ColorPicker
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct CHOOSECOLOR
{
public int lStructSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public uint rgbResult;
public IntPtr lpCustColors;
public uint flags;
public IntPtr lCustData;
public IntPtr lpfnHook;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpTemplateName;
}
[DllImport("comdlg32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ChooseColor(ref CHOOSECOLOR lpcc);
public static bool GetColor(ref uint col)
{
CHOOSECOLOR CS = new CHOOSECOLOR();
int structSize = Marshal.SizeOf(CS);
CS.lStructSize = structSize;
CS.hwndOwner = IntPtr.Zero;
CS.flags = 0x1 | 0x2;
CS.lpCustColors = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(uint)) * 16);
CS.rgbResult = col;
CS.hInstance = IntPtr.Zero;
bool result = ChooseColor(ref CS);
if (!result)
return false;
col = CS.rgbResult;
Marshal.FreeCoTaskMem(CS.lpCustColors);
return true;
}
}
}
וצורת השימוש בו היא
uint color = 0;
ColorPicker.GetColor(ref color);
האמת היא שאני מבין רק חלק מהקוד אשמח לקבל הצעות לשיפור ממי שבאמת מבין מה הולך פה
