How to make form topmost to the application only?

牧云@^-^@ 提交于 2019-12-09 11:30:48

问题


I am making excel add-in in which clicking on menu item or toolbar button, Form opened. I have set form's topmost to true but it remain topmost to all applications of windows xp. I just need to remain topmost to Microsoft Excel only.

I have choosed project in Visual Studio 2008, in Excel ->2003.

Please tell me how to do that with any way ........


回答1:


You can set your Form's owner to be the Microsoft Excel window. In Windows owned windows are always displayed above their owner. Dialogs and things like the search box in Excel are owned windows, which is what keeps them displayed above their owner.

There are a couple of ways you can set a Form's parent:

  1. The Form.Owner property (although the owner has to be another form)
  2. Use the Form.Show(IWin32Window owner) overload. (See this blog post for how translate an window handle to an IWin32Window).
  3. Use SetWindowLong() with the GWLP_HWNDPARENT parameter.
  4. Use ShowDialog() as Mikael Svenson suggested.

This does require you to know the Excel applications window handle.




回答2:


[Edit - changed code]

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

void func()
{
   Form1 f = new Form1();
   SetParent(f.Handle, (IntPtr)ThisAddIn.ExcelApplication.Hwnd);
   f.Show();
}


来源:https://stackoverflow.com/questions/2613494/how-to-make-form-topmost-to-the-application-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!