WPF: better understanding of the dialog box and showdialog Method

Source: Internet
Author: User

 

Directory

  • 1. Standard dialog box
  • 2. About the showdialog Method
  • 3. Download the demo project

 

 

Returned directory
1. Standard dialog box

First, a standard dialog box should be strictly availableAt leastFeatures:

  1. As long as the parent form is displayed, it will be displayed and overwrite the parent form.
  2. The window title of the dialog box is not displayed in the taskbar, And the taskbar only displays the name of the main form.
  3. For the mode dialog box (Model DIALOG), only when the dialog box is closed, the parent form behind the dialog box gets the focus. The modeless dialog box does not have such restrictions.

 

For example, the font selection dialog box in notepad is a mode dialog box, and its behavior fully complies with the above features.

 

 

In addition to the above three points. The second is some details, such as whether the icon should be displayed in the dialog box?

Generally, most dialogs do not have icons, but you can add icons to the dialog box.

 

There is also the dialog box size problem. Should the dialog box allow users to adjust the size?

I personally think that if there are no special requirements, each dialog box should allow users to adjust the size. After all, a fixed-size dialog box is too small in a large resolution environment or large in a small resolution environment. Of course, some dialog boxes will automatically adjust the size according to the display environment parameters before display. However, if possible, it is easier for users to adjust the size.

 

 

Now, I understand the basic requirements and features of the dialog box. Next I will explain how to create a better dialog box for the WPF framework.

First, we needShowintaskbarSet property to false so that the dialog box is not displayed in the taskbar.

 

The second is the dialog box color. the background color of the standard dialog box should be the same as that of the Windows form control. In WPF, the default color of the form is white, in Windows 7, most aero themes with relatively large brightness may not be obvious. Adjust the theme to a classic windows topic, and then the background of the dialog box is obvious. For example, the dialog box in Notepad:

 

The systemcolors type in WPF provides system predefined color encapsulation, which can be displayed as a system control color by moving a simple dynamicresource tag extension. Controlcolor is the color we want. In WPF, because the background color type is both a brush and a resource key is bound with dynamicresource, The controlbrushkey attribute of systemcolors is used. (Dynamicresource can be used to change the color of the current program at any time based on system color changes ).

 

The Code is as follows:

Background = "{dynamicresource {X: static systemcolors. controlbrushkey }}"

 

 

Next, for the dialog box with a fixed size, set the resizemode attribute of the window type to noresize. the dialog box size is fixed as follows:

 

For a dialog box that can adjust the size, you can directly use the default style, but in this case, the maximize and minimize buttons are still present. This dialog box looks uncomfortable and rare, therefore, you can set windowstyle to toolwindow, which is the tool window, so that only the close button is displayed (however, the close button of the tool window is slightly smaller ):

 

For most Windows dialogs with a standard adjustable size, such as opening a file dialog box, only the standard close button is displayed in the upper right corner (larger than the close button in the tool window ), at the same time, there is a adjust size indicator area in the lower right corner to prompt the user that although I look like a fixed size dialog box, I can adjust the size:

 

For the zoom prompt area in the lower-right corner, you can set the resizemode of the window type to canresizewithgrip.

 

The following code provides functions that can be implemented in WPF:

Showintaskbar = "false"

Resizemode = "canresizewithgrip"

Background = "{dynamicresource {X: static systemcolors. controlbrushkey }}"

 

Unfortunately, the current WPF cannot directly hide the Minimize button and maximize button, and does not support hiding the form icon. For solutions, refer to: WPF: do not show the form or disable maximize, minimize, close button, icon, and dialog box display.

 

After all the above functions are implemented, the result of the most standard window dialog box that can be adjusted is as follows (note that the background is not white ):

 

The typical theme is as follows:

 

The above are the standard Windows dialog box. However, I personally think that since the dialog box should allow users to change the size, why should we remove the maximize button? With the maximize button, the dialog box can be immediately filled with the screen without the user's mouse to zoom in. Of course, the Minimize button should not exist because the dialog box is not displayed in the taskbar.

 

Therefore, the ideal dialog box should be like this (it may look a little less elegant but most practical ):

 

(The download section below will show an instance project that allows you to quickly create the above dialog box)

 

 

Returned directory
2. About the showdialog Method

OK. After the dialog box is created, the problem is how to display it. There are still some problems that need to be vigilant.

 

In the Windows Forms framework, the method for displaying the dialog box form is: form. showdialog has an overload that can specify the owner of the form:

Public dialogresult showdialog (iwin32window owner );

The Form Type also inherits from the iwin32window interface. So through this method, we can quickly display a dialog box and set its owner.

 

 

In WPF, the form: window type does not provide such overload. Its showdialog method only has the following prototype:

Public bool? Showdialog ();

 

When calling showdialog, developers often forget to set the owner attribute of the dialog box. What are the consequences? Let's take a look at it.

 

For example, we have already created a dialog box and displayed it in the main form according to the mode dialog box, but the owner is not set to directly call showdialog:

New mydialog (). showdialog ();

 

Result:

 

OK. It looks good. The dialog box is displayed on the main form, and the focus cannot be obtained without closing the main form.

 

However, when you click the main form icon in the taskbar, you will find that this dialog box is displayed after the main form:

In this case, the focus still cannot be obtained for the main form, and only the subsequent dialog box can be closed.

 

The problem is that when you maximize the main form, click the main form icon in the taskbar, And the dialog box is gone. The problem is that the main form is in the maximized state, and you cannot close the dialog box behind it! However, the main form cannot obtain the focus, so the program will not die, but it will be finished because it cannot be operated. (If windowstyle is not a toolwindow dialog box, you can switch to another window or click the task bar icon again to appear on the main form again. If not, the program can only be forcibly ended through the Task Manager .)

 

Therefore, no matter what you think of the problem above, I believe no one wants to annoy users because of this small problem. Set the owner as follows:

VaR DLG = new mydialog ();

DLG. Owner = this;

DLG. showdialog ();

 

In this way, not only will the above problem be solved. When the mode dialog box is not closed but the user tries to click the main form, the dialog box will not stop flashing to prompt the user to complete the dialog box first.

 

Of course, the showdialog of WPF mentioned above does not provide an overload to quickly set the owner attribute. We can write an extension method. One can set the owner, and the other will automatically use the main form of the current application (through application. Current. mainwindow) as the owner of the dialog box and display the dialog box.

Static class extends wextension

{

Public static void showdialog (this window win, window owner)

{

Win. Owner = owner;

Win. showdialog ();

}

 

Public static void showdialogex (this window win)

{

Win. showdialog (application. Current. mainwindow );

}

}

 

In this way, it is more convenient to use:

New mydialog (). showdialogex ();

// Or

New mydialog (). showdialog (this );

 

 

 

Returned directory
3. Download the demo project

I have introduced a lot of content above, but the content is scattered, which may be difficult for readers to implement.

This is because many of the operations that are not built into WPF (but Windows Forms have implementations...), such as disabling the maximize button and minimize button, and hiding the form icon. If you want to implement it yourself, you may need to deal with getwindowlongptr and setwindowlongptr of Win32 API. You may refer to the following article :. net (C #) platform call: does not rely on the platform's getwindowlongptr and setwindowlongptr APIs

 

I also wrote a simple Win32 form API encapsulation project to implement all the above functions. for demonstration of the project source code, refer:

WPF: Do not display or disable the form to maximize, minimize, close button, icon, and dialog box display

 

 

 

 

 

Related Article

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.