This article will demonstrate how to retrive system's local name and short/long date format. It will also show you how to change current short/long date format of the system.
To implement Copy/Paste Demo
- Create a standard exe project - Add a module - Place 3 labels on the form1 (to diaplay local name, short date format and long date format) - Place 2 command buttons on the form1 (to change short/long date format) - Place 2 list box on the form1 (to display all short/long date formats available)
Place the following code in the form1 declaration
Form1.frm |
Click here to copy the following block | Private Sub Command1_Click() Call ChangeSystemShortDateFormat End Sub
Private Sub Command2_Click() ChangeSystemLongDateFormat End Sub
Private Sub Form_Load() Call ShowLocal Call ShowCurrentDateFormats Call FillShortAndLongDateFormats End Sub
Sub ShowLocal() Dim LCID As Long LCID = GetSystemDefaultLCID()
Label1 = GetUserLocaleInfo(LCID, LOCALE_SLANGUAGE) End Sub
Sub ShowCurrentDateFormats() Dim LCID As Long LCID = GetSystemDefaultLCID()
Label2 = GetUserLocaleInfo(LCID, LOCALE_SSHORTDATE)
Label3 = GetUserLocaleInfo(LCID, LOCALE_SLONGDATE) End Sub
Sub FillShortAndLongDateFormats() Dim LCID As Long LCID = GetSystemDefaultLCID() List1.Clear List2.Clear Set targetCtl = Me.List1 Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_SHORTDATE)
Set targetCtl = Me.List2 Call EnumDateFormats(AddressOf EnumCalendarDateProc, LCID, DATE_LONGDATE) End Sub
Sub ChangeSystemLongDateFormat() Dim LCID As Long Dim sNewFormat As String
LCID = GetSystemDefaultLCID()
If List2.ListIndex < 0 Then Exit Sub sNewFormat = List2.List(List2.ListIndex)
If Len(sNewFormat) > 0 Then Call SetLocaleInfo(LCID, LOCALE_SLONGDATE, sNewFormat)
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
Call ShowLocal Call ShowCurrentDateFormats Call FillShortAndLongDateFormats End If End Sub
Sub ChangeSystemShortDateFormat() Dim LCID As Long Dim sNewFormat As String
LCID = GetSystemDefaultLCID()
If List1.ListIndex < 0 Then Exit Sub sNewFormat = List1.List(List1.ListIndex)
If Len(sNewFormat) > 0 Then Call SetLocaleInfo(LCID, LOCALE_SSHORTDATE, sNewFormat)
Call PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0&, ByVal 0&)
Call ShowLocal Call ShowCurrentDateFormats Call FillShortAndLongDateFormats End If End Sub |
Place the following code in the module1
Module1.bas |
Click here to copy the following block | Option Explicit
Public targetCtl
Public Const LOCALE_SLANGUAGE As Long = &H2 Public Const LOCALE_SSHORTDATE As Long = &H1F Public Const LOCALE_SLONGDATE As Long = &H20 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 = &H1A
Public Declare Function PostMessage Lib "user32" _ Alias "PostMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long
Public Declare Function EnumDateFormats Lib "kernel32" _ Alias "EnumDateFormatsA" _ (ByVal lpDateFmtEnumProc As Long, _ ByVal Locale As Long, _ ByVal dwFlags As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (Destination As Any, _ Source As Any, _ ByVal Length As Long)
Public Declare Function GetSystemDefaultLCID Lib "kernel32" () As Long
Public Declare Function GetLocaleInfo Lib "kernel32" _ Alias "GetLocaleInfoA" _ (ByVal Locale As Long, _ ByVal LCType As Long, _ ByVal lpLCData As String, _ ByVal cchData As Long) As Long
Public 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
r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn))
If r Then sReturn = Space$(r) r = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn)) If r Then GetUserLocaleInfo = Left$(sReturn, r - 1) End If End If End Function
Public Function EnumCalendarDateProc(DateFormatString As Long) As Long
targetCtl.AddItem StringFromPointer(DateFormatString)
EnumCalendarDateProc = 1 End Function
Private Function StringFromPointer(lpString As Long) As String Dim pos As Long Dim buffer As String
buffer = Space$(128)
CopyMemory ByVal buffer, lpString, ByVal Len(buffer)
pos = InStr(buffer, Chr$(0))
If pos Then StringFromPointer = Left$(buffer, pos - 1) End If End Function |
|