显示颜色选择器供用户在VBA Powerpoint中选择 [英] Show color picker for user to choose from in VBA powerpoint
本文介绍了显示颜色选择器供用户在VBA Powerpoint中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为powerpoint外接程序,我需要使用它来提供给定的颜色-最好是RGB形式。在VBA中是否可以显示颜色选择器?
I am working on an add-in for powerpoint where I need the use to supply a given color - preferably in RGB terms. Is there a way in VBA to display the colorpicker?
推荐答案
在 Excel 中,获得调色板颜色很容易。这将根据工作表中选定的颜色更改工作表1中单元格的背景:
In Excel getting the palette colors is easy. This changes the background of the cells in worksheet 1, as per the selected color from the palette:
Sub TestMe()
Dim rgbSet As Variant: rgbSet = Application.Dialogs(xlDialogEditColor).Show(1)
If rgbSet Then Worksheets(1).Cells.Interior.Color = ThisWorkbook.Colors(1)
End Sub
在 PowerPoint 中(以及其他VBA托管应用程序),该任务需要一个外部dll:
In PowerPoint (and other VBA hosting applications), the task requires an external dll:
Option Explicit
Private Declare Function ChooseColor_Dlg Lib "comdlg32.dll" _
Alias "ChooseColorA" (pcc As CHOOSECOLOR_TYPE) As Long
Private Type CHOOSECOLOR_TYPE
lStructSize As Long
hwndOwner As Long
hInstance As Long
rgbResult As Long
lpCustColors As Long
flags As Long
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Const CC_ANYCOLOR = &H100
Private Const CC_ENABLEHOOK = &H10
Private Const CC_ENABLETEMPLATE = &H20
Private Const CC_ENABLETEMPLATEHANDLE = &H40
Private Const CC_FULLOPEN = &H2
Private Const CC_PREVENTFULLOPEN = &H4
Private Const CC_RGBINIT = &H1
Private Const CC_SHOWHELP = &H8
Private Const CC_SOLIDCOLOR = &H80
在同一模块中,编写代码:
In the same module, write the code:
Private Sub TestMe()
Dim CC_T As CHOOSECOLOR_TYPE, Retval As Variant
Static BDF(16) As Long
BDF(0) = RGB(0, 255, 0) 'first defined color
BDF(1) = RGB(255, 0, 0) 'second defined color
BDF(2) = RGB(0, 0, 255) 'third defined color
With CC_T
.lStructSize = Len(CC_T)
.flags = CC_RGBINIT Or CC_ANYCOLOR Or CC_FULLOPEN Or _
CC_PREVENTFULLOPEN
.rgbResult = RGB(0, 255, 0)
.lpCustColors = VarPtr(BDF(0))
End With
Retval = ChooseColor_Dlg(CC_T)
If Retval <> 0 Then
Dim labelObj As Object
Set labelObj = ActivePresentation.Slides(1).Shapes.AddLine(100, 100, 200, 200).Line
With labelObj
.Weight = 25
.ForeColor.RGB = CC_T.rgbResult
End With
End If
End Sub
这是最终结果:
这篇关于显示颜色选择器供用户在VBA Powerpoint中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文