Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi In the app in the What have you tried?

I expect that when I click another
application the language will charge
from Hebrew to English.

But it's not.

But when l come from the another app
to my app the language is changing.

can someone help in this.

Thank you.

What I have tried:

'                                                             120420_2050
' C:\WINDOWS\Microsoft.NET\Framework\v3.5\vbc /target:winexe my_form_120420_1840.vb >> out.txt
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Text
Imports System.Windows.Forms  
Imports System.Globalization
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports System.Diagnostics
Imports System.Drawing
Imports System.Threading
Imports System.Runtime.InteropServices
 
Module gimatria

 Sub Main()  
   Dim myForm1 As New my_Form1
   myForm1.ShowDialog()
   myForm1.Dispose()
 End Sub
  
End Module

'<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Class my_Form1

   Private nwl As MyNativeWindowListener
   Dim _englishInput As InputLanguage
   Dim _hebrewInput As InputLanguage

   Public Sub New()
     MyBase.New()

     'This call is required by the Windows Form Designer.
     InitializeComponent()

     'Add any initialization after the InitializeComponent() call

     _englishInput = GetInputLanguageByName("english")
     _hebrewInput = GetInputLanguageByName("hebrew")

     nwl = New MyNativeWindowListener(Me)

   End Sub

    Public Shared Function GetInputLanguageByName(ByVal inputName As String) As InputLanguage
        Dim lang As InputLanguage
        For Each lang In InputLanguage.InstalledInputLanguages
           If lang.Culture.EnglishName.ToLower().StartsWith(inputName) Then
              Return lang
           End If
        Next
        Return Nothing
    End Function

    Friend Sub ApplicationActivated(ByVal ApplicationActivated As Boolean)
        ' The application has been activated or deactivated
      If ApplicationActivated = False Then
         InputLanguage.CurrentInputLanguage = _englishInput
         Me.Text = "Application Active = " + ApplicationActivated.ToString()
      ElseIf ApplicationActivated = True Then
         InputLanguage.CurrentInputLanguage = _hebrewInput
         Me.Text = "Application Active = " + ApplicationActivated.ToString()
      End If
   End Sub

    Private Sub my_Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
       InputLanguage.CurrentInputLanguage = _englishInput
    End Sub

    Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles MyBase.DoubleClick
       InputLanguage.CurrentInputLanguage = _englishInput
    End Sub

End Class

'-----------------------------------------------------------------------------

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class my_Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(515, 253)
        Me.Name = "my_Form1"
        Me.Text = "my_Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.ResumeLayout(False)

    End Sub

End Class

'----------------------------------------------------------------------------------------------

' NativeWindow class to listen to operating system messages.
Friend Class MyNativeWindowListener
    Inherits NativeWindow

    ' Constant value was found in the "windows.h" header file.
    Private Const WM_ACTIVATEAPP As Integer = &H1C

    Private parent As my_Form1

    Public Sub New(ByVal parent As my_Form1)

        AddHandler parent.HandleCreated, AddressOf Me.OnHandleCreated
        AddHandler parent.HandleDestroyed, AddressOf Me.OnHandleDestroyed
        Me.parent = parent
    End Sub

    ' Listen for the control's window creation and hook into it.    
    Private Sub OnHandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Window is now created, assign handle to NativeWindow.
        AssignHandle(CType(sender, Form).Handle)
    End Sub

    Private Sub OnHandleDestroyed(ByVal sender As Object, ByVal e As EventArgs)
        ' Window was destroyed, release hook.
        ReleaseHandle()
    End Sub

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
        Protected Overrides Sub WndProc(ByRef m As Message)
        ' Listen for operating system messages

        Select Case (m.Msg)
            Case WM_ACTIVATEAPP

                ' Notify the form that this message was received.
                ' Application is activated or deactivated, 
                ' based upon the WParam parameter.
                parent.ApplicationActivated(m.WParam.ToInt32() <> 0)

        End Select

        MyBase.WndProc(m)
    End Sub
End Class

'----------------------------------------------------------------------------------------------
Posted
Comments
Dave Kreskowiak 12-Apr-20 14:40pm    
You don't appear to have any code in there will do call a service to do the translation for you.

Just changing the "input language" does NOT translate text for you. It just tells the system how to handle the input text. There is no translation done.
Member 13569650 12-Apr-20 15:08pm    
Dave thank you.

I don't need any translation.

Only switch the language
from English to Hebrew and
from Hebrew to English.

When my app activated the switch is ok but when my app
deactivated the switch not work.

Thank you again.
Dave Kreskowiak 12-Apr-20 19:51pm    
That's because the changes you made only apply to your application, not the system. Changing it for the system requires you call the Win32 functions to do it. There is no .NET class for it.

See SystemParametersInfoA function (winuser.h) - Win32 apps | Microsoft Docs[^]. Search for "SPI_SETDEFAULTINPUTLANG" on that page.
Member 13569650 13-Apr-20 1:33am    
Hi Dave and thank you for the help.

I feel you right.

But I am still confused because at run time I got this:

line 1 not work and do not throw any error !!!
line 2 run and work ok although line 1 failed to work

and line 3 run ok and it change system parameter (against your note)
and also line 4 works ok.

To see this working. You can compile my app by using the compile command
line at the top of the app source.

I feel you can get me out of my confused.


app code:

Friend Sub ApplicationActivated(ByVal ApplicationActivated As Boolean)

' The application has been activated or deactivated

If ApplicationActivated = False Then

1. InputLanguage.CurrentInputLanguage = _englishInput
2. Me.Text = "Application Active = " + ApplicationActivated.ToString()

ElseIf ApplicationActivated = True Then

3. InputLanguage.CurrentInputLanguage = _hebrewInput
4. Me.Text = "Application Active = " + ApplicationActivated.ToString()

End If

End Sub

Thank you a lot.

Dave Kreskowiak 13-Apr-20 11:40am    
No, I can't see any of that working as I don't have anything Hebrew, or any other non-English language installed and I have no idea what you mean by "not working".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900