Aracılığıyla paylaş


Try it: Make a nonrectangular window

Bu sayfa yalnızca WPF projeleri için geçerlidir.

In Visual Studio 2012 için Blend applications, you may want to create a window that has the visual appearance of being a nonrectangular shape at run time. Common examples of this include desktop applets, widgets, and media players. You can create a nonrectangular window by changing several properties of the Window object in your application, and then creating an event handler method that lets you move the window without the need of a title bar.

To make a nonrectangular window transparent

  1. In the Objects and Timeline panel, select the Window object, and then, under Appearance in the Properties panel, change the WindowStyle property to None to remove the window shell (the title bar). Press F5 to see how the window appears without the default shell. Notice that the standard Minimize, Maximize, Restore, and Close buttons are no longer visible. Also, notice how you can no longer drag the window. Press Alt+F4 to close the window.

    Not

    For information about the other WindowStyle options, see "WindowStyle" in the Windows Presentation Foundation Windows Overview topic on MSDN.

  2. Under Appearance in the Properties panel, select the AllowsTransparency check box. Notice that the window border is now no longer visible.

  3. To add the transparency to the window, you can set the Background property of the Window object to No brush JJ170173.706bbd5c-c0e0-43a1-9604-297f019d0275(tr-tr,VS.110).png under Brushes in the Properties panel. Alternatively, if you want the user to be able to click the invisible area of the window, you can set the Background property to Solid color brush JJ170173.3a66ec96-47bb-47fc-8876-6b9456feec3a(tr-tr,VS.110).png, and then set the Alpha property for the background brush to 1. This maintains the clickable area of the window while still making that area invisible.

  4. Finally, in the Objects and Timeline panel, click LayoutRoot to enable the object, and then add objects from the Tools panel to the artboard. You can create various effects by setting an OpacityMask brush on an object.

    For information about how to do this, see Create an opacity mask.

    You can also add shapes and drawn paths by using drawing tools such as Ellipse JJ170173.8938cfdf-9b75-4a33-bc88-b0636e114a0d(tr-tr,VS.110).png and Pen JJ170173.894f8612-e0ed-4e00-84cf-a9bc8f38fc54(tr-tr,VS.110).png, and then move the objects behind other objects (right-click an object and then click Order). The contents of the LayoutRoot will effectively define your application's outline.

  5. Press F5 again to see how the window now appears. Notice that you still cannot drag the window. Press Alt+F4 to close the window.

Add code to enable dragging of the window at run time

After you make the window transparent, you will effectively lose the ability to reposition the window without a title bar. To make the window movable again, you will have to add an event handler to the window and then add a small amount of code to the related code-behind file.

To add code to enable dragging of the window at run time

  1. Save your project to the hard disk by clicking Save All on the File menu. (You cannot add event handler methods to a project that has never been saved.)

  2. With the Window object selected in the Objects and Timeline panel, click Events JJ170173.6c67bb3b-e8a2-4a63-bad5-54d5c15b04dd(tr-tr,VS.110).png in the Properties panel.

  3. Next to MouseLeftButtonDown, type OnMouseLeftButtonDown and then press the ENTER key.

  4. Modify the generated event handler method in your code-behind file so that your event handler resembles the following:

    private void OnMouseLeftButtonDown(object sender,
             System.Windows.Input.MouseButtonEventArgs e)
    {
      this.DragMove();
    }
    
    Private Sub OnMouseLeftButtonDown(ByVal sender As System.Object,
             ByVal e As System.Windows.Input.MouseButtonEventArgs)
      Me.DragMove()
    End Sub
    
  5. Press F5 to run the application.

  6. You can add more event handler methods, such as a method for the Click event of a button that will call the Close() method in your code-behind file.

    For more information about how to create event handler methods, see Create a new event handler method.