public bool ShowDialog() { var cc = new CHOOSECOLOR(); cc.lStructSize = (int)Marshal.SizeOf <CHOOSECOLOR>(); cc.hwndOwner = GetActiveWindow(); cc.rgbResult = ColorPicker.ColorToUInt(Color); cc.Flags = CC_ANYCOLOR | CC_RGBINIT; cc.lpCustColors = Marshal.AllocHGlobal(64); for (int i = 0; i < 16; ++i) { Marshal.WriteInt32(cc.lpCustColors + i * 4, 0); } var ret = ChooseColorW(out cc); if (ret) { Color = ColorPicker.ColorFromUInt(cc.rgbResult); } Marshal.FreeHGlobal(cc.lpCustColors); return(ret); }
/// <summary> /// Initializes a new instance of the <b>ColorDialog</b> class. /// </summary> /// <remarks>When you create an instance of ColorDialog, the following read/write properties are set to initial values. /// <list type="table"><listheader><term>Property</term><term>Initial Value</term></listheader> /// <item><term>AllowFullOpen</term><term>true</term></item> /// <item><term>Color</term><term>Color.Black</term></item> /// <item><term>CustomColors</term><term>A null reference (Nothing in Visual Basic)</term></item> /// <item><term>FullOpen</term><term>false</term></item></list> /// /// <para>You can change the value for any of these properties through a separate call to the property.</para></remarks> public ColorDialog() { m_color = new CHOOSECOLOR(); //array for custom colors m_rawcustom = new int[16]; m_custhandle = GCHandle.Alloc(m_rawcustom, GCHandleType.Pinned); m_color.lpCustColors = m_custhandle.AddrOfPinnedObject(); }
private void OnMoreColorsClick(object sender, RoutedEventArgs e) { if (this.MoreColorsExecuting != null) { var args = new MoreColorsExecutingEventArgs(); this.MoreColorsExecuting(this, args); if (!args.Canceled) { var color = args.Color; if (RecentColors.Contains(color)) { RecentColors.Remove(color); } RecentColors.Insert(0, color); this.recentColorsListBox.SelectedIndex = 0; } } else { #pragma warning disable 618 var chooseColor = new CHOOSECOLOR(); var wnd = Window.GetWindow(this); if (wnd != null) { chooseColor.hwndOwner = new WindowInteropHelper(wnd).Handle; } chooseColor.Flags = CC_ANYCOLOR; if (customColors == IntPtr.Zero) { // Set custom colors) for (var i = 0; i < this.colorsArray.Length; i++) { this.colorsArray[i] = 0x00FFFFFF; } customColors = GCHandle.Alloc(this.colorsArray, GCHandleType.Pinned).AddrOfPinnedObject(); } chooseColor.lpCustColors = customColors; if (ChooseColor(chooseColor)) { var color = ConvertFromWin32Color(chooseColor.rgbResult); if (RecentColors.Contains(color)) { RecentColors.Remove(color); } RecentColors.Insert(0, color); this.recentColorsListBox.SelectedIndex = 0; } #pragma warning restore 618 } }
public void ChooseColorTest() { var cc = new CHOOSECOLOR { lStructSize = (uint)Marshal.SizeOf(typeof(CHOOSECOLOR)), rgbResult = System.Drawing.Color.Red, Flags = CC.CC_RGBINIT, hwndOwner = GetDesktopWindow() }; Assert.That(ChooseColor(ref cc), Is.True); }
/// <summary> /// Shows the dialog for the view model. /// </summary> /// <returns>How the dialog was closed.</returns> public new DialogResult ShowDialog() { if (_dialogService.HasDialogHandler(typeof(ColorPickerDialogViewModel))) { return(_dialogService.ShowDialog(this)); } var topMostWindow = _dialogService.GetTopMostDialog() ?? _dialogService.MainWindow; var windowInteropHelper = new WindowInteropHelper(topMostWindow); var customColors = Marshal.AllocCoTaskMem(_customColors.Length * Marshal.SizeOf(_customColors[0])); Marshal.Copy(_customColors, 0, customColors, _customColors.Length); var chooseColor = new CHOOSECOLOR(); chooseColor.hwndOwner = windowInteropHelper.Handle; chooseColor.rgbResult.R = SelectedColor.R; chooseColor.rgbResult.G = SelectedColor.G; chooseColor.rgbResult.B = SelectedColor.B; chooseColor.lpCustColors = customColors; chooseColor.Flags = CC_FULLOPEN | CC_RGBINIT; var result = DialogResult.Cancel; if (ChooseColor(chooseColor)) { SelectedColor = System.Windows.Media.Color.FromRgb(chooseColor.rgbResult.R, chooseColor.rgbResult.G, chooseColor.rgbResult.B); result = DialogResult.Ok; } Marshal.Copy(customColors, _customColors, 0, _customColors.Length); Marshal.FreeCoTaskMem(customColors); return(result); }
// Class initialization function public bool Init(Control ctrlParent) { // Allocate the array for initial colors. int cbColorData = 16 * 4; IntPtr ipColors = LocalAlloc(LMEM_FIXED, cbColorData); if (ipColors == IntPtr.Zero) { return(false); } m_cc = new CHOOSECOLOR(); m_cc.lStructSize = Marshal.SizeOf(m_cc); m_cc.hwndOwner = GetHwndFromControl(ctrlParent); m_cc.hInstance = IntPtr.Zero; m_cc.rgbResult = 0; m_cc.lpCustColors = ipColors; m_cc.Flags = CC_RGBINIT; m_cc.lCustData = 0; m_cc.lpfnHook = IntPtr.Zero; m_cc.lpTemplateName = IntPtr.Zero; return(true); }
public static extern bool ChooseColorA([In, Out] CHOOSECOLOR pChoosecolor);//对应的win32API
public static extern bool ChooseColor(CHOOSECOLOR lpcc);
private static extern bool ChooseColorW(out CHOOSECOLOR lpcc);
public static extern int ChooseColor(ref CHOOSECOLOR lpcc);
private static extern bool ChooseColor(CHOOSECOLOR lpcc);
private static extern bool ChooseColor([In, Out] CHOOSECOLOR lpchoosecolor);