|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Locale/Regionalization
Routines GetKeyboardLayout: Obtain the Keyboard Locale Identifier |
||
Posted: | Sunday May 21, 2000 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows NT4 | |
OS restrictions: | None | |
Author: | VBnet - Randy Birch | |
Related: |
EnumSystemLocales: Enumerate Installed and Supported System Locales GetLocaleInfo: Regional Locale Country Settings |
|
Prerequisites |
None. |
|
The
LoWord contained in the return value from the GetKeyboardLayout API is the LCID (Locale Identifier) value representing the country or region
for the current keyboard layout. This is similar to the LCID returned from a call to GetSystemDefaultLCID, which retrieves the default LCID
for the current system configuration. By passing the LCID from GetKeyboardLayout to GetLocaleInfo, various specific information about the
keyboard layout can be retrieved. You can then make appropriate use of the information for multi-language aware apps.
If you have created an application using the constants declared in GetLocaleInfo: Regional Locale Country Settings, you have everything you need with the exception of the GetKeyboardLayout declare. To keep this page short, refer to the above page for full descriptions of the constants used in this demo. |
BAS Module Code |
None. |
|
Form Code |
Add seven text boxes (Text1 - Text7) to a form, along with a command button (Command1) and the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const LOCALE_ILANGUAGE As Long = &H1 'language id Private Const LOCALE_SLANGUAGE As Long = &H2 'localized name of language Private Const LOCALE_SENGLANGUAGE As Long = &H1001 'English name of language Private Const LOCALE_SABBREVLANGNAME As Long = &H3 'abbreviated language name Private Const LOCALE_SCOUNTRY As Long = &H6 'localized name of country Private Const LOCALE_SENGCOUNTRY As Long = &H1002 'English name of country Private Const LOCALE_SABBREVCTRYNAME As Long = &H7 'abbreviated country name '#if(WINVER >= &H0400) Private Const LOCALE_SISO639LANGNAME As Long = &H59 'ISO abbreviated language name Private Const LOCALE_SISO3166CTRYNAME As Long = &H5A 'ISO abbreviated country name Private Declare Function GetKeyboardLayout Lib "user32" _ (ByVal dwLayout As Long) As Long Private 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 Private Sub Command1_Click() Dim hKeyboardID As Long Dim LCID As Long 'Identifies the thread to query, or is 'zero for the current thread. hKeyboardID = GetKeyboardLayout(0&) If hKeyboardID > 0 Then LCID = LoWord(hKeyboardID) If LCID Then Text1.Text = GetUserLocaleInfo(LCID, LOCALE_ILANGUAGE) Text2.Text = GetUserLocaleInfo(LCID, LOCALE_SCOUNTRY) Text3.Text = GetUserLocaleInfo(LCID, LOCALE_SENGCOUNTRY) Text4.Text = GetUserLocaleInfo(LCID, LOCALE_SENGLANGUAGE) Text5.Text = GetUserLocaleInfo(LCID, LOCALE_SISO3166CTRYNAME) Text6.Text = GetUserLocaleInfo(LCID, LOCALE_SISO639LANGNAME) Text7.Text = GetUserLocaleInfo(LCID, LOCALE_SABBREVLANGNAME) End If End If End Sub Private Function LoWord(wParam As Long) As Integer If wParam And &H8000& Then LoWord = &H8000& Or (wParam And &H7FFF&) Else LoWord = wParam And &HFFFF& End If End Function Public Function GetUserLocaleInfo(ByVal dwLocaleID As Long, _ ByVal dwLCType As Long) As String Dim sReturn As String Dim nSize As Long 'call the function passing the Locale type 'variable to first retrieve the required 'size of the string buffer needed nSize = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn)) 'if successful (nSize > 0) If nSize > 0 Then 'pad a buffer with spaces sReturn = Space$(nSize) 'and call again passing the buffer nSize = GetLocaleInfo(dwLocaleID, dwLCType, sReturn, Len(sReturn)) 'if successful (nSize > 0) If nSize > 0 Then 'nSize holds the size of the string 'including the terminating null GetUserLocaleInfo = Left$(sReturn, nSize - 1) End If End If End Function |
Comments |
While the GetSystemDefaultLCID function retrieves the system default
locale identifier, this is often inappropriate or insufficient in a networked
environment or under an operating system where multiple locales have
been installed. For example, it is possible for a network admin rolling
out a standard image to have the user's default locale set to one
differing from the base OS installation, and thus the system default
locale. In this situation Windows' provides an alternate API you can use to obtain the LCID for the current user ... GetUserDefaultLCID. Defined identically to GetSystemDefaultLCID, GetUserDefaultLCID function retrieves the user default–locale identifier and is therefore the most appropriate API to use when it is the user's locale you are interested in, rather than that of the system. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |