Making Stuff Faster
Design, Code, Database Performance

Color Picker Visual Studio Macro

Monday, March 13, 2006 11:36 AM

Every now and then I need to create a color object in my code, but don’t know exactly what color I want.  So I created this little macro to popup the ColorDialog, then insert a little line of code for the color you picked.

Nothing magic or special here, just another useful macro.  The only problem with it is the color dialog comes up behind Visual Studio so you have to Alt-Tab to see it.  A bit annoying, I know.  If anyone figures that one out I'll post the fix.

Public Sub ColorPicker()
Dim colorDlg As New ColorDialog
colorDlg.AllowFullOpen = True
colorDlg.AnyColor = True
colorDlg.FullOpen = True
colorDlg.SolidColorOnly = False
Dim ret As DialogResult = colorDlg.ShowDialog()
If ret = DialogResult. Cancel Then
Return
End If
Dim color As System.Drawing.Color = colorDlg.Color
Dim code As String
If color.IsNamedColor Then
code = "Color color = Color." + color.Name + ";"
Else
code = "Color color = Color.FromArgb(" + color.ToArgb().ToString() + ");"
End If
Dim textSelection As TextSelection = DTE.ActiveDocument.Selection()
Dim edit As EditPoint = textSelection.TopPoint.CreateEditPoint()
edit.Insert(code)
End Sub


Feedback

# re: Color Picker Visual Studio Macro

Hi there,

If you add the following class to your code:

'This class is used to set the proper parent to any UI that you may display from within a macro.
Public Class WinWrapper
Implements System.Windows.Forms.IWin32Window

Overridable ReadOnly Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
Get
Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
Return iptr
End Get
End Property
End Class

and then change your line

Dim ret As DialogResult = colorDlg.ShowDialog()

to:

Dim winptr As WinWrapper = New WinWrapper
Dim ret As DialogResult = colorDlg.ShowDialog(winptr)

You should be cooking with gas!

(Info from the following article http://www.codeproject.com/macro/VSReadFileOutWin.asp)

Carl 3/13/2006 11:02 PM | Carl Wright

# re: Color Picker Visual Studio Macro

hii..i dont find color dialog control in Ajax extension in Toolbox...plz can u help me out...actually i want to pop up a color dialog box whenever a specific image is clicked and when they choose the color from the color dialog box..then that color should get into a texbox.. 7/23/2009 11:46 AM | Pavan

# re: Color Picker Visual Studio Macro

hii..i dont find Color Dialog Control in Ajax extension in Toolbox...plz can u help me out...actually i want to pop up a color dialog box whenever a specific image is clicked and when they choose the color from the color dialog box, that color(not color code) should come into a text box.. 7/23/2009 11:49 AM | pavan

# re: Color Picker Visual Studio Macro

Class WinWrapper work perfectly with SaveFileDialog too. Thank you! 9/10/2009 12:38 AM | Dest

Post a comment