office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access用API调用Windows内置的颜色对话框

2020-10-17 08:00:00
tmtony8
原创
97

在应用程序中, 我们有时需要对图片或者文字颜色进行选择设置。

windows内置了颜色对话框,如果能直接调用就可方便使用。毕竟写个全颜色的对话框是非常困难的。



在模块了添加API代码


Public Type CHOOSECOLOR
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    rgbResult As Long
    lpCustColors As String
    flags As Long
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long




窗体中调用代码:

Private Sub Command3_Click()
    Dim NewColor As Long
    NewColor = ShowColor
    If NewColor = -1 Then Exit Sub
       Text1.ForeColor = NewColor
End Sub

Private Function ShowColor() As Long
    Dim cc As CHOOSECOLOR
    Dim Custcolor(16) As Long
    Dim lReturn As Long
    Dim CustomColors() As Byte

    '设置结构大小
    cc.lStructSize = Len(cc)
    '设置窗口句柄
    cc.hwndOwner = Me.Hwnd
    '设置应用程序例程
    cc.hInstance = CurrentProject.Application.hWndAccessApp
    '设置默认颜色
    cc.lpCustColors = StrConv(CustomColors, vbUnicode)
    '不设定标志
    cc.flags = 0

    '显示颜色对话框
    If CHOOSECOLOR(cc) <> 0 Then
        ShowColor = cc.rgbResult
        CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
    Else
        ShowColor = -1
    End If
End Function

    分享