C#Outlook-如何获取光标在MailItem的主题字段中的位置? [英] C# Outlook - How can I get the cursor position in the subject field of a MailItem?

查看:135
本文介绍了C#Outlook-如何获取光标在MailItem的主题字段中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#AddIn中MailItem的主题字段的当前光标位置插入文本。

I want to insert a text at the current cursor position in the subject field of a MailItem in my C# AddIn.

我还必须知道光标是否在

I additionally must know if the cursor is in the subject field.

有人知道怎么解决吗?

要清除我要执行的操作:
随着消息,我的加载项将打开WPF窗口,用户可以在其中选择几个标签(例如 {stuff})之一。
如果用户在WPF窗口中单击插入按钮,则AddIn必须将标签插入当前光标位置。实际上,我可以将其插入邮件正文中。

To clearify what I want to do: With the Message my AddIn opens a WPF Window, where the user can select one of a couple of tags (e.g. "{stuff}"). If the user hits the insert button in the WPF window, the AddIn must insert the tag at the current cursor position. Actually I can just insert it in the mail body.

如果光标位于主题字段中,则必须在主题字段中的光标位置添加标签。 / p>

If the cursor is in the subject field, the tag must be added in the subject field at the cursor position.

推荐答案

这不是一件容易的事,但我认为可能。

This is not an easy task, but possible I think.

从理论上讲,应该执行以下步骤:

Theoretically it should be possible by following these steps:

您可以通过调用 GetGUIThreadInfo (请参阅: pInvoke:GetGuiThreadInfo

You can get the Carret-Position of an active UI thread by calling GetGUIThreadInfo (refer to: pInvoke: GetGuiThreadInfo

求值结构包含一个 public System.Drawing.Rectangle rcCaret
,它是插入符号的边界矩形,在客户端坐标系中是相对的到窗口。(请参阅​​: MSD GuiThreadInfoStructure

与后续步骤一样, MSDN-Article: GetGUIThreadInfo函数

Than followup with the steps described also in the MSDN-Article: GetGUIThreadInfo function

MSDN说:

要获取 rcCaret 矩形中的实际插入点,请执行以下步骤:

MSDN says:
To get the actual insertion point in the rcCaret rectangle, perform the following steps:


  1. 致电 GetKeyboardLayout 检索当前的输入语言。

  1. Call GetKeyboardLayout to retrieve the current input language.

确定字符

调用 CreateFont 使用 Sans Serif作为字体,高度由 rcCaret ,并且宽度为零。对于 fnWeight ,调用 SystemParametersInfo(SPI_GETCARETWIDTH,0,pvParam,0)。如果pvParam大于1,则将fnWeight设置为700,否则将fnWeight设置为400。

Call CreateFont using Sans Serif for the font, the height given by rcCaret, and a width of zero. For fnWeight, call SystemParametersInfo(SPI_GETCARETWIDTH, 0, pvParam, 0). If pvParam is greater than 1, set fnWeight to 700, otherwise set fnWeight to 400.

在设备上下文(DC)中选择字体并使用 GetCharABCWidths 以获取相应游标的B宽度

Select the font into a device context (DC) and use GetCharABCWidths to get the B width of the appropriate cursor character.

rcCaret.left 中添加B宽度以获得实际的插入点。

Add the B width to rcCaret.left to obtain the actual insertion point.

提示:该函数可能无法在 GUITHREADINFO 调用结构检索前景线程的信息时的结构,例如当窗口丢失激活时。

Hint: The function may not return valid window handles in the GUITHREADINFO structure when called to retrieve information for the foreground thread, such as when a window is losing activation.

还有一个 CodeProject-Article(在任何应用程序中获得插入符位置),其中有人在其中显示工具提示通过评估屏幕位置的第三方应用程序(坐标)。

There is also a CodeProject-Article (Getting Caret Position Inside Any Application) where someone is showing up tooltips in 3rd party applications by evaluating the screenlocation (coordinates) of the carret.

长话短说...我不知道您的明确任务,但这听起来像'Hack'-您不能以更好的'正式'方式解决问题吗?就像附加到事件( PropertyChnaged )或其他可以检测主题更改的东西一样?

Long story short... I don't know your explicit task, but this feels like a 'Hack' - can't you solve it in a better 'formally' way? Like attaching to an event (PropertyChnaged) or something else to dedect subject changes?

UPDATE

最后,我能够从另一个应用程序的Subject-Field中读取值。
Microsoft Outlook电子邮件窗口中的主题字段是 RichEdit-Control (类名: RichEdit20WPT
,ControlID为 4101。
我无法为该控件挂上消息(不可能从其他应用程序/线程来,并且我还没有构建一个AddIn-我认为如果为该任务构建一个插件,它可能会附加: (附加到 WM_NOTIFY 并过滤 EN_MSGFILTER 键盘事件 ENM_KEYEVENTS

UPDATE
Finally I was able to read the value from the Subject-Field from another application. The Subject-Field in a Microsoft Outlook E-Mail-Window is a RichEdit-Control (ClassName: RichEdit20WPT) with the ControlID "4101". I was not able to hook on messages for this control (it is not possible from a different application/thread and I have not built an AddIn - I think its possible to attach if you build an add in for this task: (attach to WM_NOTIFY and filter EN_MSGFILTER for keyboard events ENM_KEYEVENTS)

但是您可以通过以下方式读取当前值:

But you can read the current value by:

 [DllImport("user32.dll")]
 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

 const int WM_GETTEXT = 0x000D;
 const int WM_GETTEXTLENGTH = 0x000E;

 public static string GetControlText(IntPtr hWndOfControl)
  {    
    StringBuilder title = new StringBuilder();

    // Get the size of the string 
    Int32 size = SendMessage((int)hWndOfControl, WM_GETTEXTLENGTH, 0, 0).ToInt32();

     // If Size > 0 ? -> Text available...
     if (size > 0)
      {
       title = new StringBuilder(size + 1);    
       SendMessage(hWndOfControl, (int)WM_GETTEXT, title.Capacity, title);
      }

     return title.ToString();
   }

如果您无法获得小刀的位置,也许可以设置将焦点移回到控件上,并使用 SendKeys 在当前位置插入文本。
也可以使用SendMessage WM_SETTEXT 来设置文本。
也许这会帮助您...

If you'r not able to get the carret position, maybe you can set the Focus back to the control and use SendKeys to insert the text at the current position. Setting the text can also be done my using SendMessage WM_SETTEXT. Maybe this will help you...

这篇关于C#Outlook-如何获取光标在MailItem的主题字段中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆