WPF instance show -- how to obtain the image of the UI element

Source: Internet
Author: User

WPF instance show -- how to obtain the image of the UI element

 

The title is hard to explain. Let me explain it again.

 

For example, I want to drag an element on the UI. During the drag process, I need to move the shadow of the UI element with the mouse (but the UI remains in the original position). When I open the mouse, the UI element is moved to a new location.

 

This is a common requirement. The first step to achieve this requirement is to obtain the picture of this UI element. This step is actually very simple. The core is to use the image brush subclass visualbrush.

 

The following is a simple example.

 

This is the UI description,

  1. <Window X: class = "wpfapplicationimage. window1"
  2. Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title = "window1" Height = "300" width = "400" background = "steelblue">
  5. <Grid margin = "10">
  6. <Grid. columndefinitions>
  7. <Columndefinition width = "160" type = "codeph" text = "/codeph"/>
  8. <Columndefinition width = "*"/>
  9. <Columndefinition width = "160" type = "codeph" text = "/codeph"/>
  10. </Grid. columndefinitions>
  11. <Stackpanel X: Name = "stackpanelleft" background = "white">
  12. <Button X: Name = "BTN" content = "OK" Height = "40"/>
  13. </Stackpanel>
  14. <Button content = ">" grid. Column = "1" margin = "5" Click = "button_click"/>
  15. <Stackpanel X: Name = "stackpanelright" background = "white" grid. Column = "2"/>
  16. </GRID>
  17. </WINDOW>

The UI effect is as follows:

 

 

Click the button in the middle to add the image of the button in the left stackpanel to the stackpanel on the right. The Code is as follows (the core is to use visualbrush ):

 

  1. Private void button_click (Object sender, routedeventargs E)
  2. {
  3. Visualbrush vbrush = new visualbrush (BTN );
  4. Rectangle rect = new rectangle ();
  5. Rect. width = BTN. width;
  6. Rect. Height = BTN. height;
  7. Rect. Fill = vbrush;
  8. Rect. Opacity = 0.5; // to indicate the image, I set the opacity to 50%
  9. This. stackpanelright. Children. Add (rect );
  10. }

Click a few and the result is as follows:

 

 

The core problem is solved, and the rest is easy. If you want to perform drag-and-drop operations, you can only use the mouse event and offset calculation tips: P

==========================================

I wrote a complete example and uploaded it to the resource. Unfortunately, I don't know why it cannot be displayed. The name is "WPF drag effect source code ". When I can see it myself, I will post the link ~~~

 

Place a prefix ~~~~

 

 

 

 

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.