![](/img/trans.png)
[英]Scrollbar doesn't appear in parent control if the child is docked to the edge
[英]Why doesn't the child form appear on the same screen as the parent form?
更新資料
我接受了Rufus L的幾個mod的答案,相關代碼如下
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 Add-In
提供一個功能區按鈕,當單擊該功能區按鈕時,將調用ObjButtonFoo_Click
,該按鈕又顯示一個ClsFormFoo
表單(請參見下面的代碼 )。 ObjButtonFoo_Click
包含用於創建代表Word的IWin32Window所有者值的代碼,以傳遞給ShowDialog。
在多監視器設置中,我希望objFormFoo
會出現在顯示Word本身的同一監視器上。 然而,當我打開Word中的輔助監視器上,並導致ObjButtonFoo_Click
執行, objFormFoo
出現在主顯示器上
如何使objFormFoo
出現在與Word本身顯示在同一監視器上的監視器上, objFormFoo
是否是主監視器?
注意:我驗證了winWordMain是否已填充,即它不是null。 請參閱下面的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();
}
winWordMain
我沒有VSTO對此進行測試,但是在我看來,您可以直接獲取要用作父級的ActiveWindow
的位置,然后將其用作放置子窗體的參考:
private void allRootsWithChilds_CheckedChanged(object sender, EventArgs e)
{
var winWordMain = new NativeWindow();
var parent = Globals.ThisAddIn.Application.ActiveWindow;
winWordMain.AssignHandle(new IntPtr(parent.Hwnd));
using (var objFormFoo = new ClsFormFoo())
{
// Set the Left and Top properties so this form is centered over the parent
objFormFoo.Left = parent.Left + (parent.Width - objFormFoo.Width) / 2;
objFormFoo.Top = parent.Top + (parent.Height - objFormFoo.Height) / 2;
objFormFoo.ShowDialog(winWordMain);
}
winWordMain.ReleaseHandle();
}
我曾經在C#上制作了一個簡單的屏幕保護程序。 我使用以下代碼在所需的屏幕或所有屏幕上打開了屏幕保護程序。
您必須從所需的屏幕取bounds
,並將其傳遞給表單。
// Constructor
public NameForm(Rectangle bounds)
{
InitializeComponent();
this.Bounds = bounds;
}
// In my case, this is opening the screensaver on all screens
foreach (Screen screen in Screen.AllScreens)
{
NameForm form = new NameForm (screen.Bounds);
form.Show();
}
您只需要將表單的StartPosition
屬性設置為FormStartPosition.CenterParent
值:
loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(parentWindowdle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.