How to enable IME on a custom UserControl?

G

Guest

We have a custom word processing type editor built with C# and .NET 2.0 and
we need to support typing in languages other than English. I want to be
able to use the Windows IME to enter in text similar to how it works in a
text box.

For example, if I put focus on a TextBox and then switch my language on the
language bar to Japanese or Chinese, several buttons appear on the language
bar including Input Mode, Input Style, Conversion Mode, etc. I can then
switch my Input Mode to Hiragana, for example. Then when I type in “kaâ€
into the textbox, a Japanese letter appears.

However on the same form, if I click on a control derived from UserControl,
the buttons on the language bar become disabled so I can’t change the Input
Mode. If I type “kaâ€, I get “ka†instead of the Japanese character.

What do I need to do in order to enable the buttons on the language bar so I
can change the input mode? It’s like I need to tell the IME that my control
is IME aware, but I have no idea how to do that.
 
L

Linda Liu [MSFT]

Hi Blue,

I performed a test based on your description and saw the same thing as you
did.

I create a Windows application project and add a UserControl into the
project (I don't add any control on the custom UserControl). Build the
project and drag my custom UserControl from Toolbox on the form. I also
add a textbox on the form. Build the program again and run it.

When I put the focus in the textbox, several buttons for the specified
language appear on the language bar. When I click on the usercontrol, these
buttons for the specified language are disabled.

I think this makes sense. Since there isn't any child control on my
usercontrol, I couldn't input any text into my usercontrol. The behavior of
those specific buttons being disabled at this time is reasonable. If I add
a textbox on the UserControl, the textbox gets focused when I click on the
usercontrol. In this case, those specified buttons on the language bar are
available.

Could you please tell me what contols you placed on your usercontrol?

I look forward to your reply.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Linda,

We don’t put any child controls on the user control. This is a “word
processing†like control we built ourselves. We catch events like key press
and draw the text ourselves.

We want to do something similar with text entered via the IME. I think (at
least I hope) we should be able to catch the events from IME and then draw
them in our user control. However, before we can do that we first need to
get the IME turn on when my control has focus.
 
L

Linda Liu [MSFT]

Hi Blue,

Thank you for your reply.

I will spend more time doing researching. If I have any new findings, I
will get it back to you ASAP.

I appreciate your patience.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Blue,

Sorry for my delayed response. I have spent much time reasearching on this
issue and found an answer finally.

The status, composition, and candidates windows form the user interface for
the IME. By default, the operation system automatically creates and manages
status, composition and candidates windows for all window that require text
input, e.g. TextBox, ComboBox and so on.

By default, the system associates the input context maintained by the
default IME with each window, i.e. a control on a form, as it is created.

When a window is activated or gets focused, the system sends the
WM_IME_SETCONTENT message to the application. Then the application sends
this message to the default IME window.

Note that at this time, those controls that don't require text input, e.g.
Button, UserControl, Panel and so on, will remove the association to the
default input context from themselves. So, the the default IME is disabled.
In order to enable the IME on a custom UserControl, we could call the input
method manager (IMM) function 'ImmAssociateContext' to associate the
default input context to the custom UserControl at this time.

We could catch the WM_IME_SETCONTENT message in the override WndProc method
of the custom UserControl.

The following is a sample.

using System.Runtime.InteropServices;

public partial class UserControl1 : UserControl
{
IntPtr m_hImc;

public const int WM_IME_SETCONTEXT = 0x0281;

[DllImport("Imm32.dll")]
public static extern IntPtr ImmGetContext(IntPtr hWnd);

[DllImport("Imm32.dll")]
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr
hIMC);

public UserControl1()
{
InitializeComponent();
}

private void UserControl1_Load(object sender, EventArgs e)
{
m_hImc = ImmGetContext(this.Handle);
}

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
// the usercontrol will receive a WM_IME_SETCONTEXT message
when it gets focused and loses focus respectively
// when the usercontrol gets focused, the m.WParam is 1
// when the usercontrol loses focus, the m.WParam is 0
// only when the usercontrol gets focused, we need to call the
IMM function to associate itself to the default input context
if (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)
{
ImmAssociateContext(this.Handle, m_hImc);
}
}
}

Build the project and add the custom UserControl on your form. Run the
problem and select the usercontrol on the form. You should see the IME is
enabled.

Hope this helps.
Please try my suggestion and let me know the result.


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top