Hi
Sorry for the delay response.
According to my test, the Form in the MS Word Add-in does not respond to
any Shortcuts or Navigational keys such as Tab, arrow, escape. To fix the
problem, there are two ways to try:
1. Use Model Winform like below:
fm.ShowDialog()
NOTE: I did not recommend you use the Hook approach because that will hook
into the whole Office Application's message loop which may cause the whole
application to crash, if you did not have much concern about the modal
form, I prefer your choosing the first workaround.
Thanks!
2. Use use a WH_GETMESSAGE hook to capture the keystroke messages and call
the IsDialogMessage API. If IsDialogMessage returns TRUE, then do not pass
the message on to the message pump.
#region Windows Hook
// Win32: SetWindowsHookEx()
[DllImport("user32.dll")]
protected static extern IntPtr SetWindowsHookEx(HookType code, HOOKPROC
func,IntPtr hInstance,int threadID);
[DllImport("user32.dll")]
protected static extern int CallNextHookEx(IntPtr hhook, int code,
IntPtr wParam, IntPtr lParam);
[DllImport("user32")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll")]
static extern bool IsDialogMessage(IntPtr hDlg, [In] ref Message lpMsg);
protected IntPtr m_hhook = IntPtr.Zero;
public enum HookType: int
{
WH_GETMESSAGE = 3,
}
public delegate int HOOKPROC(int nCode, IntPtr wParam, IntPtr lParam);
public void SetHook()
{
// set the keyboard hook
m_hhook = SetWindowsHookEx(HookType.WH_GETMESSAGE, new
HOOKPROC(this.MyMessageProc), IntPtr.Zero, (int)
AppDomain.GetCurrentThreadId());
}
public int MyMessageProc(int nCode, IntPtr wParam, IntPtr lParam)
{
const int WM_KEYFIRST = 0x0100;
const int WM_KEYLAST = 0x0108;
const int PM_REMOVE = 1;
Message msg =
(Message)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam,
typeof(Message));
if (nCode >= 0 && PM_REMOVE == (int)wParam)
{
if ((msg.Msg >= WM_KEYFIRST) && (msg.Msg <= WM_KEYLAST))
{
bool i = IsDialogMessage(this.Handle, ref msg);
if (i)
{
msg.Msg = 0;
msg.LParam = IntPtr.Zero;
msg.WParam = IntPtr.Zero;
System.Runtime.InteropServices.Marshal.StructureToPtr(msg, lParam,
true);
}
else
{
}
}
}
return CallNextHookEx(m_hhook, nCode, wParam, lParam);
}
#endregion
Both work well. Let me know if you have any other question.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.