office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

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

Access用API調用Windows內置的顔色對話框

2020-10-17 08:00:00
tmtony8
原創
217

在應用程序中, 我們有時需要對圖片或者文字顔色進行選擇設置。

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

    分享