VSTO Outlook: Getting Screen.FromControl(this).Bounds makes message preview not be displayed in the explorer

huangapple go评论48阅读模式
英文:

VSTO Outlook: Getting Screen.FromControl(this).Bounds makes message preview not be displayed in the explorer

问题

我有一个自定义任务窗格(ctp)位于我的VSTO Outlook插件的顶部。
我创建它如下所示:

this.myHostControl = new myHostControl();
this.myCtp = Globals.ThisAddIn.CustomTaskPanes.Add(myHostControl, "My Toolbar");
this.myCtp.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop;
this.myCtp.DockPositionRestrict = Microsoft.Office.Core.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
this.myCtp.Height = DEFAULT_CTP_HEIGHT;
this.myCtp.Visible = true;

我在ctp中嵌入了一个WPF用户控件。

WinForms用户控件myHostControl如下所示(仅显示相关部分以理解我的问题):

public partial class myHostControl: System.Windows.Forms.UserControl
{
    private Rectangle? myScreenBounds = null;

    public myHostControl()
    {
        InitializeComponent();

        // 获取当前屏幕的边界
        MyScreenBounds = System.Windows.Forms.Screen.FromControl(this).Bounds;
    }

    public myHostControl(int param1, int param2):this()
    {
        this.ElementHostCtl.Parent = this;
   
        // 获取WPF视图
        this.WpfView = new WpfView();

        // 将WPF视图设置为ElementHost的子元素
        this.ElementHostCtl.Child = this.WpfView;

        // 设置数据上下文
        this.WpfView.DataContext = new WpfViewModel();
    }

    private Rectangle? MyScreenBounds 
    {
        get => myScreenBounds;

        set
        {
            if (myScreenBounds != value)
            {
                myScreenBounds = value;
            }
        }

       // 更多内容
    }
}

public partial class WpfView : System.Windows.Controls.UserControl
{
    public WpfView ()
    {
        InitializeComponent();
    }

    // 更多内容
}

我注意到构造函数中的以下行:

MyScreenBounds = System.Windows.Forms.Screen.FromControl(this).Bounds;

导致当我在资源管理器窗口的消息列表中选择消息时,它不会加载其内容(消息正文)到预览区域,而是显示为空白区域。

如果我从构造函数中删除该行,那么它可以正常工作,也就是说,当我在资源管理器中选择一条消息时,其内容将正确显示在预览区域。

为什么Screen.FromControl(this).Bounds会导致消息的内容不显示在预览区域?如果我双击消息,检查器窗口会打开,然后我可以看到消息正文。

英文:

I have a custom task pane (ctp) at the top of my VSTO Outlook Add-in.
I create it as below:

this.myHostControl = new myHostControl();
this.myCtp = Globals.ThisAddIn.CustomTaskPanes.Add(myHostControl, "My Toolbar");
this.myCtp .DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop;
this.myCtp .DockPositionRestrict = Microsoft.Office.Core.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
this.myCtp.Height = DEFAULT_CTP_HEIGHT;
this.myCtp.Visible = true;

I am embedding a WPF User Control in the ctp.

The winforms user control, myHostControl, is like below (only showing relevant parts to understand my issue):

public partial class myHostControl: System.Windows.Forms.UserControl
{
    private Rectangle? myScreenBounds = null;

    public myHostControl()
    {
        InitializeComponent();

        // Gets the current screen bounds for the current screen.
        MyScreenBounds = System.Windows.Froms.Screen.FromControl(this).Bounds;
    }

    public myHostControl(int param1, int param2):this()
    {
        this.ElementHostCtl.Parent = this;

        // Gets the WPF view
        this.WpfView = new WpfView();

        // Sets wpf view as child of elementhost
        this.ElementHostCtl.Child = this.WpfView;

        // Sets the datacontext
        this.WpfView.DataContext = new WpfViewModel();
    }

    private Rectangle? MyScreenBounds 
    {
        get => myScreenBounds;

        set
        {
            if (myScreenBounds!= value)
            {
                myScreenBounds= value;
            }
        }
    }

   // More stuff
}


public partial class WpfView : System.Windows.Controls.UserControl
{
    public WpfView ()
    {
        InitializeComponent();
    }

    // more stuff
}

I have noticed that line below in the constructor:

MyScreenBounds = System.Windows.Froms.Screen.FromControl(this).Bounds;

makes that when I select a message from the message list in the explorer window, it does not load its content (message body) in the preview area, instead it is shown as an empty white area.

If I remove that line from the constructor, then it works, i mean, when i select a message from the messages list in the explorer its content is correctly displayed in the preview area.

Why Screen.FromControl(this).Bounds is causing the content of the messages not being shown in the preview area? If I double click on a message, the inspector window opens and then i can see the message body.

答案1

得分: 1

确保在你的CTP已经可见并已正确设置父窗口之后再调用该代码,否则其窗口句柄可能会过早创建。

英文:

Make sure you call that code after your CTP is already visible and is correctly parented - otherwise its window handle can get created prematurely.

huangapple
  • 本文由 发表于 2023年2月23日 23:20:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定