How do you tell if a Windows Form is open but behind another window?

℡╲_俬逩灬. 提交于 2020-01-02 04:20:26

问题


Calling Form.Visible will return true regardless of whether the form is maximized, minimized, or has a FormWindowState of Normal.

What I want to know is how to tell if the form is open but "hidden" behind another application's window.

If that's the case, I want to bring it to the front and actually make it visible to the user.

I tried the BringToFront() method but it didn't work. I also tried calling the Show() method but if the form is behind another application's window, it remains that way.

The only workaround I found to the problem is setting the Form's FormWindowState to Minimized/Maximized and then normal, but that's a bit of a hack and doesn't look nice.

Can someone tell me how to tell if the form is behind another window and how to bring it to the front?


回答1:


Strange.

this.Activate() should do the trick.

You can always try a 'horrible hack method', which I feel guilty for spreading. But if this.Activate() doesn't work, for the sake of testing you might try:

this.TopMost = true;
this.Focus();
this.BringToFront();
this.TopMost = false;

I've never seen this recommended as a solution, but it might work to show you the functionality. I'd be more concerned about why this.Activate() isn't working if the above-mentioned code does.

As for detecting the window, you cannot use a command to detect it via C# like that. Check the following questions answers for more info: How to check if window is really visible in Windows Forms?




回答2:


Try to wire below,

private void frmMyForm_Deactivate(object sender, EventArgs e)
    {
        // Raise your flag here.
    }

By wiring above event, it will tell you whenever the form is minimized, partially/totally hided by another form.




回答3:


Well, as you know, windows might or might not have Focus. If a windows is focused, it is therefore that the user have clicked it. You can try the Focused property. Otherwise, I don't think there is a property which tells you if another window is above yours.
You can "give" focus, therefore "popping out" the window with the Focus() method.

Note: A window can be both focused and be under another window.
To determine if your window isn't under another window, I'm afraid you must go deeper.



来源:https://stackoverflow.com/questions/11366600/how-do-you-tell-if-a-windows-form-is-open-but-behind-another-window

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