Я принял ответ Руфуса Л. с несколькими модификациями. Соответствующий код следует
Код: Выделить всё
public partial class ClsOfficeRibbonFooTab
{
private void FooTab_Load(object sender, RibbonUIEventArgs e)
{
.
.
.
}
private void CheckResolution()
{
// set the left position so that the expanded version of the form fits on the screen
Screen screen = Screen.FromHandle(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd));
if (screen.Bounds.Width < 1360 || screen.Bounds.Height < 768)
{
throw new FormatException(String.Format("The {0} is supported on screens with a resolution of 1360 by 768 or greater. Your screen is {1} by {2}", "Some caption text", screen.Bounds.Width, screen.Bounds.Height));
}
}
private void ObjButtonFoo_Click(object sender, RibbonControlEventArgs e)
{
using (ClsFormFoo objFormFoo = new ClsFormFoo(parentWindow: Globals.ThisAddIn.Application.ActiveWindow))
{
CheckResolution();
objFormFoo.ShowDialog();
}
}
}
public partial class ClsFormFoo : Form
{
// This form is a fixed dialog with a flyout on the right side.
// expandedWidth is a const set to the max width of this fixed dialog (i.e., the dialog with the flyout extended)
const int expandedWidth = 1345;
public ClsFormFoo(Microsoft.Office.Interop.Word.Window parentWindow)
{
InitializeComponent();
Top = parentWindow.Top;
}
private void ClsFormFoo_Load(object sender, EventArgs e)
{
Screen screen = Screen.FromHandle(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd));
// set the left position so that the expanded version of the form fits on the screen for all legal resolutions
int halfScreenWidth = (int)(screen.WorkingArea.Width / 2);
// This form is a fixed dialog with a flyout on the right side.
// expandedWidth is a const set to the max width of this fixed dialog (i.e., the dialog with the flyout extended)
int halfFormWidth = (int)(expandedWidth / 2);
this.Left = screen.Bounds.Left + ((int)(halfScreenWidth - halfFormWidth));
}
}
Моя надстройка VSTO предоставляет кнопка ленты, при нажатии которой вызывается ObjButtonFoo_Click, который, в свою очередь, отображает форму ClsFormFoo (см. Код ниже). ObjButtonFoo_Click включает код для создания представителя значения владельца IWin32Window для Word для передачи в ShowDialog.
При настройке с несколькими мониторами я ожидаю, что objFormFoo появится на том же мониторе, на котором отображается сам Word. Однако когда я запускаю Word на дополнительном мониторе и запускаю ObjButtonFoo_Click, objFormFoo появляется на основном мониторе
Что мне делать сделать, чтобы objFormFoo отображался на том же мониторе, на котором отображается сам Word, независимо от того, является ли он основным монитором или нет?
Примечание: я проверил, что winWordMain заполнен , то есть оно не равно нулю. См. winWordMain ниже
Код
Код: Выделить всё
private void ObjButtonFoo_Click(object sender, RibbonControlEventArgs e)
{
NativeWindow winWordMain = new NativeWindow();
winWordMain.AssignHandle(new IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd));
IntPtr(Globals.ThisAddIn.Application.ActiveWindow.Hwnd);
using (ClsFormFoo objFormFoo = new ClsFormFoo()
{
objFormFoo.ShowDialog(winWordMain);
}
winWordMain.ReleaseHandle();
}
Подробнее здесь: https://stackoverflow.com/questions/571 ... arent-form