SetLocaleInfo VB声明
Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long
说明
改变用户“地方”设置信息
返回值
Long,TRUE(非零)表示成功,否则返回零。会将GetLastError设置为下述值之一:ERROR_INVALID_ACCESS,ERROR_INVALID_FLAGS,ERROR_INVALID_PARAMETER
参数表
参数 类型及说明
Locale Long,要为其改变信息的地方ID
LCType Long,欲改变的信息类型。参考api32.txt,检视那些带 LOCALE_ 前缀的常数
lpLCData String,这个地方信息项目的新设置
注解
这个函数不会改变系统地方设置
Declare Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long
说明
改变用户“地方”设置信息
返回值
Long,TRUE(非零)表示成功,否则返回零。会将GetLastError设置为下述值之一:ERROR_INVALID_ACCESS,ERROR_INVALID_FLAGS,ERROR_INVALID_PARAMETER
参数表
参数 类型及说明
Locale Long,要为其改变信息的地方ID
LCType Long,欲改变的信息类型。参考api32.txt,检视那些带 LOCALE_ 前缀的常数
lpLCData String,这个地方信息项目的新设置
注解
这个函数不会改变系统地方设置
'
' BAS Moduel Code
'
'------------------------------------------------------------------------------Option ExplicitPublic thisCombo As ComboBoxPublic Const LOCALE_SLANGUAGE As Long = &H2 'localized name of language
Public Const LOCALE_SSHORTDATE As Long = &H1F 'short date format string
Public Const LOCALE_SLONGDATE As Long = &H20 'long date format string
Public Const DATE_LONGDATE As Long = &H2
Public Const DATE_SHORTDATE As Long = &H1
Public Const HWND_BROADCAST As Long = &HFFFF&
Public Const WM_SETTINGCHANGE As Long = &H1APublic Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As LongPublic Declare Function EnumDateFormats Lib "kernel32" _
Alias "EnumDateFormatsA" _
(ByVal lpDateFmtEnumProc As Long, _
ByVal Locale As Long, _
ByVal dwFlags As Long) As LongPublic Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(Destination As Any, _
Source As Any, _
ByVal Length As Long)Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As LongPublic Declare Function GetLocaleInfo Lib "kernel32" _
Alias "GetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String, _
ByVal cchData As Long) As LongPublic Declare Function SetLocaleInfo Lib "kernel32" _
Alias "SetLocaleInfoA" _
(ByVal Locale As Long, _
ByVal LCType As Long, _
ByVal lpLCData As String) As Long
Public Function GetUserLocaleInfo(ByVal dwLocaleID As Long, _
ByVal dwLCType As Long) As String Dim sReturn As String
Dim r As Long 'call the function passing the Locale type
'variable to retrieve the required size of
'the string buffer needed
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
'if successful..
If r Then
'pad the buffer with spaces
sReturn = Space$(r)
'and call again passing the buffer
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
'if successful (r > 0)
If r Then
'r holds the size of the string
'including the terminating null
GetUserLocaleInfo = Left$(sReturn, r - 1)
End If
End If
End Function
Public Function EnumCalendarDateProc(lpDateFormatString As Long) As Long 'application-defined callback function for EnumDateFormats
'populates combo assigned to global var thisCombo
thisCombo.AddItem StringFromPointer(lpDateFormatString)
'return 1 to continue enumeration
EnumCalendarDateProc = 1
End Function
Private Function StringFromPointer(lpString As Long) As String Dim pos As Long
Dim buffer As String
'pad a string to hold the data
buffer = Space$(128)
'copy the string pointed to by the return value
CopyMemory ByVal buffer, lpString, ByVal Len(buffer)
'remove the trailing null and trim
pos = InStr(buffer, Chr$(0))
If pos Then
StringFromPointer = Left$(buffer, pos - 1)
End IfEnd Function
'--end block--'
'------------------------------------------------------------------------------
'
' Form Code
'
'------------------------------------------------------------------------------
Option Explicit
Private Sub Form_Load() Command1.Enabled = Combo1.ListIndex > -1
Command2.Enabled = Combo2.ListIndex > -1
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
End Sub
Private Sub Command1_Click() Dim LCID As Long
Dim newFormat As String
LCID = GetSystemDefaultLCID()
newFormat = Combo1.Text
If newFormat <> "" Then
'set the new long date format
Call SetLocaleInfo(LCID, LOCALE_SLONGDATE, newFormat)
'send a system notification message that a change was made
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
'update the textbox
Text1.Text = GetUserLocaleInfo(LCID, LOCALE_SLONGDATE)
'assign the target combo box control and clear
Set thisCombo = Form1.Combo1
thisCombo.Clear
'enumerate new long date formats
Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_LONGDATE)
End If
End Sub
Private Sub Command2_Click() Dim LCID As Long
Dim newFormat As String
LCID = GetSystemDefaultLCID()
newFormat = Combo2.Text
If newFormat <> "" Then
'set the new long date format
Call SetLocaleInfo(LCID, LOCALE_SSHORTDATE, newFormat) 'send a system notification message that a change was made
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
'update the textbox and label
Text2.Text = GetUserLocaleInfo(LCID, LOCALE_SSHORTDATE)
'assign the target combo box control
Set thisCombo = Form1.Combo2
thisCombo.Clear
'enumerate new long date formats
Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_SHORTDATE)
End If
End Sub
Private Sub Command3_Click() 'open the control panel Regional Date Settings
Call Shell("rundll32.exe shell32.dll,Control_RunDLL intl.cpl,,4", vbNormalFocus)
End Sub
Private Sub Command4_Click() Dim LCID As Long
LCID = GetSystemDefaultLCID()
'show localized name of language
Text3.Text = GetUserLocaleInfo(LCID, LOCALE_SLANGUAGE)
'-------------------------
'assign the target combo box control
Set thisCombo = Form1.Combo1
'enumerate available long date formats
Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_LONGDATE)
'Show the user's Long date format string
Text1.Text = GetUserLocaleInfo(LCID, LOCALE_SLONGDATE)
'-------------------------
'assign the target combo box control
Set thisCombo = Form1.Combo2
'enumerate available short date formats
Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_SHORTDATE)
'Show the user's Short date format string
Text2.Text = GetUserLocaleInfo(LCID, LOCALE_SSHORTDATE)End Sub
Private Sub Text1_Change() Label1.Caption = Format$(Date, Text1.Text)End Sub
Private Sub Text2_Change() Label2.Caption = Format$(Date, Text2.Text)
End SubPrivate Sub Combo1_Click() Command1.Enabled = Combo1.ListIndex > -1
End Sub
Private Sub Combo2_Click() Command2.Enabled = Combo2.ListIndex > -1
End Sub