The WPF Troubleshooter Mode window is hidden and then displayed again as a modeless window

Source: Internet
Author: User
Tags bool time interval visibility

Phenomenon:

You can try this interesting but defect phenomenon: when we display the child window in ShowDialog (), it is obvious that the window is modal (that is, the main window waits for the return of the window and the main window will not be entered by the user). However, if the visibility of the child window is set to Visibility.hidden to hide the window, and then the visibility is set to visibility.visible to display the child window again, then the child window is modeless (i.e. the main window and the child window as the corresponding user input)

Case:

When the user does not operate the software to reach a certain time interval, the software will hide the software currently displayed all the Windows, and pop-up "lock window", when the user manually unlocked, "Lock window" hidden, all other windows are displayed again.

For a window in the above case, if the window is displayed or hidden by using the set visibility, the problem occurs when the hidden window is a modal dialog box that is displayed again.

Solution:

You can solve the problem in a relatively simple way: "Hide" is "invisible", "throw the window outside the display screen" is "invisible". (This belongs to the "Wo,kao" category rather than "Wow,sai", but it works well. "Writing software is to cheat into the"----with high-tech deceptive eyes, ears, write the game will be more deceptive)

Code:

 public class Windowvisibilityhelper:dependencyobject 


{


public static bool Getisvisible (DependencyObject obj)


{


return (bool) obj. GetValue (Isvisibleproperty);


}


public static void Setisvisible (DependencyObject obj, bool value)


{


obj. SetValue (isvisibleproperty, value);


}


public static readonly DependencyProperty Isvisibleproperty =


dependencyproperty.registerattached ("IsVisible", typeof (BOOL),


typeof (Windowvisibilityhelper),


New PropertyMetadata (True, Isvisiblepropertychangedcallback));


private static void Isvisiblepropertychangedcallback (DependencyObject obj,


DependencyPropertyChangedEventArgs e)


{


var window = (window) obj;


var visible = (bool) E.newvalue;


if (!visible)


{


setwindowstate (window, window. WindowState);


if (window. WindowState = = windowstate.maximized)


{


Window. WindowState = Windowstate.normal;


}


setwindowlocation (window, new Point window. Left, window. top));


window. left =-100000;


window. top =-100000;


setwindowisshowintaskbar (window, window. ShowInTaskbar);


window. ShowInTaskbar = false;


}


Else


{


window. WindowState = getwindowstate (window);


Point loc = getwindowlocation (window);


window. left = loc. X


window. top = loc. Y


window. ShowInTaskbar = getwindowisshowintaskbar (window);


window. Activate ();


window. Bringintoview ();


}


}


private static point getwindowlocation (DependencyObject obj)


{


return (point) obj. GetValue (Windowlocationproperty);


}


private static void Setwindowlocation (DependencyObject obj, point value)


{


obj. SetValue (windowlocationproperty, value);


}


private static readonly DependencyProperty Windowlocationproperty =


dependencyproperty.registerattached ("Windowlocation", typeof (Point),


typeof (Windowvisibilityhelper), New UIPropertyMetadata (new Point));


private static WindowState getwindowstate (DependencyObject obj)


{


return (windowstate) obj. GetValue (Windowstateproperty);


}


private static void Setwindowstate (DependencyObject obj, WindowState value)


{


obj. SetValue (windowstateproperty, value);


}


private static readonly DependencyProperty Windowstateproperty =


dependencyproperty.registerattached ("WindowState", typeof (WindowState),


typeof (Windowvisibilityhelper), New UIPropertyMetadata (Windowstate.normal));


private static bool Getwindowisshowintaskbar (DependencyObject obj)


{


return (bool) obj. GetValue (Windowisshowintaskbarproperty);


}


private static void Setwindowisshowintaskbar (DependencyObject obj, bool value)


{


obj. SetValue (windowisshowintaskbarproperty, value);


}


private static readonly DependencyProperty Windowisshowintaskbarproperty =


dependencyproperty.registerattached ("Windowisshowintaskbar", typeof (BOOL),


typeof (Windowvisibilityhelper), new UIPropertyMetadata (true);


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.