システムの短い日付形式を変更 <TOP>
システムの地域設定の短い日付形式をプログラムによって変更します。
GetDateFormat
日付表示の書式を取得
GetTimeFormat
時刻をフォーマットし、指定された地域に対応する時刻文字列を作成
SetLocaleInfo
地域情報を設定
PostMessage メッセージをポスト
GetSystemDefaultLCID システムの既定ロケール識別子を取得
「日付・時刻の書式を取得」と組み合わせています。
'================================================================ '= システムの短い日付形式を変更 '= (SetLocaleInfo.bas) '================================================================ #include "Windows.bi" #define LOCALE_SSHORTDATE &H1F '短い形式 #define WM_SETTINGCHANGE &H1A '変更を通知 #define HWND_BROADCAST &HFFFF 'トップレベルウィンドウ に対してメッセージを送る Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End Type ' 日付表示の書式を取得 Declare Function Api_GetDateFormat& Lib "Kernel32" Alias "GetDateFormatA" (ByVal Locale&, ByVal dwFlags&, lpDate As SYSTEMTIME, ByVal lpFormat$, ByVal lpDateStr$, ByVal cchDate&) ' 時刻をフォーマットし、指定された地域に対応する時刻文字列を作成 Declare Function Api_GetTimeFormat& Lib "kernel32" Alias "GetTimeFormatA" (ByVal Locale&, ByVal dwFlags&, lpTime As SYSTEMTIME, ByVal lpFormat As Any, ByVal lpTimeStr$, ByVal cchTime&) ' 地域情報を設定 Declare Function Api_SetLocaleInfo& Lib "kernel32" Alias "SetLocaleInfoA" (ByVal Locale&, ByVal LCType&, ByVal lpLCData$) ' スレッドに関連付けられているメッセージキューにメッセージをポストする Declare Function Api_PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hWnd&, ByVal wMsg&, ByVal wParam&, ByVal lParam&) ' システムの既定ロケール識別子を取得 Declare Function Api_GetSystemDefaultLCID& Lib "kernel32" Alias "GetSystemDefaultLCID" () Var Shared Text(1) As Object Var Shared Button(1) As Object Var Shared Timer1 As Object For i = 0 To 1 Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14 Button(i).Attach GetDlgItem("Button" & Trim$(Str$(i + 1))) : Button(i).SetFontSize 14 Next Timer1.Attach GetDlgItem("Timer1") '================================================================ '= '================================================================ Declare Sub MainForm_Start edecl () Sub MainForm_Start() Timer1.SetInterval 100 Timer1.Enable -1 End Sub '================================================================ '= '================================================================ Declare Sub Timer1_Timer edecl () Sub Timer1_Timer() Var Buffer As String Var st As SYSTEMTIME Var Ret As Long st.wDay = Val(Right$(Today$, 2)) st.wMonth = Val(Mid$(Today$, 6, 2)) st.wYear = Val(Left$(Today$, 4)) st.wHour = Val(Left$(Time$, 2)) st.wMinute = Val(Mid$(Time$, 4, 2)) st.wSecond = Val(Right$(Time$, 2)) Buffer = String$(255, 0) Ret = Api_GetDateFormat(ByVal 0, 0, st, ByVal 0, Buffer, Len(Buffer)) Buffer = Left$(Buffer, InStr(1, Buffer, Chr$(0)) - 1) Text(0).SetWindowText Buffer Buffer = String$(255, 0) Ret = Api_GetTimeFormat(ByVal 0, 0, st, ByVal 0, Buffer, Len(Buffer)) Buffer = Left$(Buffer, InStr(1, Buffer, Chr$(0)) - 1) Text(1).SetWindowText Buffer End Sub '================================================================ '= dd-MMM-yy '================================================================ Declare Sub Button1_on edecl () Sub Button1_on() Var dwLCID As Long Var Ret As Long dwLCID = Api_GetSystemDefaultLCID() If Api_SetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, "dd-MMM-yy") = False Then A% = MessageBox("", "Failed", 0, 2) Exit Sub End If Ret = Api_PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0) End Sub '================================================================ '= yyyy/MMM/dd '================================================================ Declare Sub Button2_on edecl () Sub Button2_on() Var dwLCID As Long Var Ret As Long dwLCID = Api_GetSystemDefaultLCID() If Api_SetLocaleInfo(dwLCID, LOCALE_SSHORTDATE, "yyyy/MMM/dd") = False Then A% = MessageBox("", "Failed", 0, 2) Exit Sub End If Ret = Api_PostMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0) End Sub '================================================================ '= '================================================================ While 1 WaitEvent Wend Stop End