[C #] share a pop-up container layer, as you can click on the menu and then click it,

Source: Internet
Author: User

[C #] share a pop-up container layer, as you can click on the menu and then click it,

Applicable:. Net2.0 + Winform Project

------------------ 201508261813 Update (source code updated, Demo not updated )------------------

  • Re-draw and adjust the size of the handle (SizeGrip, the one in the lower right corner), because the system draws too much on the corner, it will cover that part of the border under XP, poor visual experience. Improvement

    AddedDrawSizeGripThe method is the drawing method, which isProtected virtualSo if you cannot see this, you can rewrite this method in the subclass to draw your own satisfaction (in addition to the question, I also referred to the VS2010 effect, but it is the opposite, VS is a convex effect, I am a collapsed style)
  • SupportedFour Sides + four cornersDrag to change the size of the floating layer to improve the experience.

Note: whether the floating layer can be adjusted depends on the SizeGripStyle attribute. There are three cases:

  • SizeGripStyle isShowAllows users to adjust the size, the handle will appear, move the mouse to the edge corner will produce visual changes and can be dragged
  • SizeGripStyle isHideThe user is not allowed to adjust the size, the handle does not appear, the mouse is moved to the edge corner does not produce visual changes, or you can drag to change the size
  • SizeGripStyle isAutoWhen the Modal is set to true (that is, opened through ShowDialog), it is consistent with Show. When the Modal is set to false (opened through Show), it is consistent with Hide, this is also the logic of the original Form, but the original Form will still follow the FormBorderStyle, but this class has fixed this attribute, so please pay attention to Auto, we recommend that you explicitly specify Show/Hide

------------------ 201508251458 update ------------------

  • The first control was previously activated in OnShown. After research, it was changed to TopMost = true to enable the floating layer to activate the first control in the same way as the normal form, at the same time, the OnShown rewriting is saved.
  • Solve the problem that the child control sometimes does not have a focus box (Focus dotted box.

Note: The last demo is not updated. please retrieve the FloatLayerBase. cs source code again.

------------------ 201508240846 original (updated )------------------

Background:

Sometimes we need to open a simple window to do something, such as entering something or clicking an item, which may be like this:

After returning the original form and getting the input, there is no problem, but a few days ago I suddenly came up with some ideas: Why do we have to pop up a form to the user with an eye-catching view, is it possible to quickly present a layer near the button to do this? Like the shortcut menu, the user is happy to do what to do in it, if you are not happy, just click it somewhere else and it will disappear. It would have been a lightweight and quick operation, DUANG ~ If a form is popped up, will it make the user feel awkward? the experience level is often very subtle. In any case, since I have started this idea, just try it.

First, I found a ready-made solution. Indeed, some cool people have done this in codecome codeproject.com:

Http://www.codeproject.com/Articles/17502/Simple-Popup-Control

A simple experience is indeed a great creation. The principle is that ToolStripControlHost can carry this capability of the custom control, so that the drop-down control ToolStripDropDown can pop up any custom control like a right-click menu (do not forget to right-click the menu ContextMenuStrip is inherited from ToolStripDropDown ), in this way, the menu is used as a container, and any simple or complex combination of controls can be displayed. At the same time, the menu is convenient and can be invoked as soon as possible. At that time, I was so happy to learn about this solution. It was just the effect I wanted. I felt like this was good, so I didn't have to make it myself.

However, I soon found out that ToolStripDropDown only has Show, and there is no ShowDialog, that is, it cannot be modeled (Modal, also called mode). In view of all MSDN models, I also use the stream mode). This is determined by the inherent capabilities of ToolStripDropDown. Since this solution is based on ToolStripDropDown, it is naturally limited here and cannot be modeled. The problem is that in some cases the call experience is poor (it is certainly not a patent for users to experience this kind of thing, and we also need to talk about experience ), for example, the pop-up control allows the user to input something. After the user clicks a button or something, return to the original form, get the user's input in the original form, and then do the following. Because non-Mode Show does not block the code, you cannot take the value and use value below Show for granted ~ This is obvious. To obtain the value, you may need to take some additional actions, such as responding to the close event of the pop-up control, or pass the original form into the pop-up control and do what should be done in the latter ~ And so on, there are certainly many ways to do this, but this is because only Show can bring about unnecessary things, what is better than popping up controls in a method,WaitIt is not natural to return and continue processing:

string s;using (Popup p = new Popup()){    if (p.ShowDialog() != DialogResult.OK) { return; }    s = p.InputText;}//go on...

So unfortunately, I have to say goodbye to this outstanding solution and build my own wheels. Inspired by this solution, I thought of using ContextMenu as a container (note that this menu class is very different from the ContextMenuStrip inherited from ToolStripDropDown mentioned above. The former is an OS-native menu, right-click the menu that appears on the desktop, icon, and text box ,. net operates such a menu by calling the API, while the latter is completely. net implementation. For more information, refer to MSDN, which is not expanded here), because the Show of ContextMenu is blocking, which is exactly what I mean. However, after some attempts, give up. Its menu item MenuItem does not support custom controls as ToolStripItem does. In the hope that I have limited capabilities, in short, I cannot get custom controls to ContextMenu, I have never seen text boxes, check boxes, and other strange things on the native menu,If you know how to extend the native menu, I hope you can give me some advice. Thank you!

I will try again. the idea of net is still a lot of different attempts. Form, Panel, UserControl, ContainerControl, Control, and so on seem suitable for the container layer, I even tried again on ToolStripDropDown, finally chose Form, and made some changes. I felt like I wanted something better: one is calledFloatLayerBaseIt inherits from System. windows. forms. form class, and applications that need to be displayed as floating layers inherit from FloatLayerBase for implementation. For example, the following NumInputDemo implementation that accepts user input values:

Appearance and features:
  • The parent window will not lose focus (the layer that won't grab focus is the good layer ):

    Of course, there are more men:

    There are other border styles, Which is available in demo.

  • YesHandle with size adjustment:

  • YesDrag the customer area:

Other applications:

These are just demos, not so nice and powerful. The point is that with this FloatLayerBase, You can implement your own floating application.

Instructions for use: Others:

During the compilation process, PopupFormBase was used as the class name, and the final release time was changed to the current FloatLayerBase. Therefore, the demo may still be based on the subclass and Method Name of the original name.

Download Demo:

Http://pan.baidu.com/s/1mgnGPGc

There is a Tester for you to experience.

 

-Wen Bi-

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.