API: Obtaining the Keyboard Locale Identifier
กระทู้เก่าบอร์ด อ.สุภาพ ไชยา

 324   1
URL.หัวข้อ / URL
API: Obtaining the Keyboard Locale Identifier

ถามไว้ที่ 
http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=33389&highlight=keyboard+language 
 
ดังนี้ 
Language on taskbar  
My computer installed two different languages, and my database was create to totally hide the taskbar during the forms is on screen. Can anyone please tell me how to show the language that keyboard selected on the form with code.  
 
Thanks in advance 
sjdth 
 
มีการติดตั้ง Windows 2 ภาษา แล้วได้ซ่อน Taskbar เวลาเข้าโปรแกรมฐานข้อมูลที่ได้สร้างขึ้น อยากรู้ว่าจะนำข้อมูลว่าขณะนี้เลือกภาษาอะไรอยู่บนฟอร์มได้อย่างไร 
 
ที่สะดุดตาผมคือคนที่ถามนี้เป็นคนไทยครับ ผมเลยสนใจที่จะค้นหาคำตอบให้ได้ เขาถามไว้เมื่อวันที่ 21 ผ่าน วันนี้ผมว่างจากงานก็เลยเข้าไปค้นใน Internet ผมมุ่งเป้าหมายไปที่ API ค้นหาหลายเว็บ แล้วมาเจอคำตอบอยู่ที่ http://www.mvps.org/vbnet/code/locale/getkeyboardlayout.htm 
 
ผมได้ปรับโค้ดของเขานิดหน่อย ดังนี้ครับ 
 
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 
 
         Text0 = GetUserLocaleInfo(LCID, LOCALE_ILANGUAGE) 
         Text2 = GetUserLocaleInfo(LCID, LOCALE_SCOUNTRY) 
         Text4 = GetUserLocaleInfo(LCID, LOCALE_SENGCOUNTRY) 
         Text6 = GetUserLocaleInfo(LCID, LOCALE_SENGLANGUAGE) 
         Text8 = GetUserLocaleInfo(LCID, LOCALE_SISO3166CTRYNAME) 
         Text10 = GetUserLocaleInfo(LCID, LOCALE_SISO639LANGNAME) 
         Text12 = 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 
 
โดยให้สร้าง Text Box ขึ้นมา 7 อัน (ให้สร้างเพียง 1 อัน แล้ว copy ไป paste ลงต่อกันไปเรื่อยๆ) จากนั้นก็เพิ่ม Command Button เข้าไปอีก 1 อัน ก็จะสามารถใช้โค้ดข้างบนได้แล้วครับ *** Edited by Supap Chaiya *** 12/7/2546 9:43:44

1 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R00731
ตัวอย่างโค้ดสำหรับเปลี่ยน Keyboard ให้เป็นภาษาที่เราต้องการเมื่อเคลื่อนเคอร์เซอร์ไปยัง Text Box ที่ต้องการ อยู่ที่ http://web.domaindlx.com/devforum/changekey.asp เป็นตัวอย่างของ VB แต่สามารถปรับมาใช้กับ Access ได้เลย โค้ดนี้จะมีประโยชน์สำหรับ Access 97 ครับ ส่วนเวอร์ชันที่สูงกว่ามันจะมี Property ชื่อ Keyboard Language ครับ แล้วจะทำให้โค้ดนี้เป็นแบบ Generic ได้อย่างไรดี ลองไปคิดต่อดูนะครับ เพราะถ้าหากมี Text Box ที่ต้องใส่ภาษาไทยอยู่ 10 อัน และภาษาอังกฤษอีก 10 กว่าอัน คงจะไม่สนุกแน่ที่ต้องเขียนโค้ดใส่ทุก Text Box ครับ
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.0881s