Example #1
0
 private void OnMoreColorsClick(object sender, RoutedEventArgs e)
 {
     if (MoreColorsExecuting != null)
     {
         MoreColorsExecutingEventArgs args = new MoreColorsExecutingEventArgs();
         MoreColorsExecuting(this, args);
         if (!args.Canceled)
         {
             Color color = args.Color;
             if (RecentColors.Contains(color))
             {
                 RecentColors.Remove(color);
             }
             RecentColors.Insert(0, color);
             recentColorsListBox.SelectedIndex = 0;
         }
     }
     else
     {
         NativeMethods.CHOOSECOLOR chooseColor = new NativeMethods.CHOOSECOLOR();
         Window wnd = Window.GetWindow(this);
         if (wnd != null)
         {
             chooseColor.hwndOwner = new WindowInteropHelper(wnd).Handle;
         }
         chooseColor.Flags = NativeMethods.CC_ANYCOLOR;
         if (customColors == IntPtr.Zero)
         {
             // Set custom colors)
             for (int i = 0; i < colorsArray.Length; i++)
             {
                 colorsArray[i] = 0x00FFFFFF;
             }
             customColors = GCHandle.Alloc(colorsArray, GCHandleType.Pinned).AddrOfPinnedObject();
         }
         chooseColor.lpCustColors = customColors;
         if (NativeMethods.ChooseColor(chooseColor))
         {
             Color color = ConvertFromWin32Color(chooseColor.rgbResult);
             if (RecentColors.Contains(color))
             {
                 RecentColors.Remove(color);
             }
             RecentColors.Insert(0, color);
             recentColorsListBox.SelectedIndex = 0;
         }
     }
 }
Example #2
0
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            NativeMethods.WndProc     hookProcPtr = new NativeMethods.WndProc(HookProc);
            NativeMethods.CHOOSECOLOR cc          = new NativeMethods.CHOOSECOLOR();
            IntPtr custColorPtr = Marshal.AllocCoTaskMem(64);

            try
            {
                Marshal.Copy(customColors, 0, custColorPtr, 16);
                cc.hwndOwner    = hwndOwner;
                cc.hInstance    = Instance;
                cc.rgbResult    = ColorTranslator.ToWin32(color);
                cc.lpCustColors = custColorPtr;

                int flags = Options | (NativeMethods.CC_RGBINIT | NativeMethods.CC_ENABLEHOOK);
                // Our docs say AllowFullOpen takes precedence over FullOpen; ChooseColor implements the opposite
                if (!AllowFullOpen)
                {
                    flags &= ~NativeMethods.CC_FULLOPEN;
                }

                cc.Flags = flags;

                cc.lpfnHook = hookProcPtr;
                if (!SafeNativeMethods.ChooseColor(cc))
                {
                    return(false);
                }

                if (cc.rgbResult != ColorTranslator.ToWin32(color))
                {
                    color = ColorTranslator.FromOle(cc.rgbResult);
                }

                Marshal.Copy(custColorPtr, customColors, 0, 16);
                return(true);
            }
            finally
            {
                Marshal.FreeCoTaskMem(custColorPtr);
            }
        }
 public static extern bool ChooseColor([In, Out] NativeMethods.CHOOSECOLOR cc);
Example #4
0
 private void OnMoreColorsClick(object sender, RoutedEventArgs e)
 {
     if (MoreColorsExecuting != null)
     {
         MoreColorsExecutingEventArgs args = new MoreColorsExecutingEventArgs();
         MoreColorsExecuting(this, args);
         if (!args.Canceled)
         {
             Color color = args.Color;
             if (RecentColors.Contains(color)) RecentColors.Remove(color);
             RecentColors.Insert(0, color);
             recentColorsListBox.SelectedIndex = 0;
         }
     }
     else
     {
         NativeMethods.CHOOSECOLOR chooseColor = new NativeMethods.CHOOSECOLOR();
         Window wnd = Window.GetWindow(this);
         if (wnd != null) chooseColor.hwndOwner = new WindowInteropHelper(wnd).Handle;
         chooseColor.Flags = NativeMethods.CC_ANYCOLOR;
         if (customColors == IntPtr.Zero)
         {
             // Set custom colors)
             for (int i = 0; i < colorsArray.Length; i++) colorsArray[i] = 0x00FFFFFF;
             customColors = GCHandle.Alloc(colorsArray, GCHandleType.Pinned).AddrOfPinnedObject();
         }
         chooseColor.lpCustColors = customColors;
         if (NativeMethods.ChooseColor(chooseColor))
         {
             Color color = ConvertFromWin32Color(chooseColor.rgbResult);
             if (RecentColors.Contains(color)) RecentColors.Remove(color);
             RecentColors.Insert(0, color);
             recentColorsListBox.SelectedIndex = 0;
         }
     }
 }