It is accomplished by implementing the MouseMove event for each of the rectangles. These rectangles will serve as the window’s edges and we will show the resizing cursors when the mouse moves over these. We start by adding a bunch of invisible (Fill=”Transparent”) rectangles, one for each side of the window and another one for each corner of the window, to our XAML defined template. To make the window resizable requires a bit more effort. If (Mouse.LeftButton = MouseButtonState.Pressed) Private void moveRectangle_PreviewMouseDown(object sender, MouseButtonEventArgs e) We add a PreviewMouseDown event handler for the Rectangle in the OnApplyTemplate method and implement it as below. You may have noticed that the template above contains a rectangle named “moveRectangle” and this one will serve as a ‘handle’ to drag our window.
This is done by overriding the OnApplyTemplate in our CustomWindow class.īutton minimizeButton = GetTemplateChild("minimizeButton") as Button īutton restoreButton = GetTemplateChild("restoreButton") as Button īutton closeButton = GetTemplateChild("closeButton") as Button Īt this stage our custom window is almost (the ThemeAttribute is missing) ready to be used in a WPF application but it cannot yet be dragged around on the screen nor resized.
Tutorial c builder w7 code#
Also these buttons have been assigned a name by the x:Name attribute for us to be able to find them in code and hook up their click event handlers. Note that the buttons for minimizing, restoring and closing the window have their own template defined above the actual custom window template. Instead, choose to add a simple text file, name it “Generic.xaml” and give it the below content. Since the project template is a generic class library you probably won’t find the option to add a WPF Resource dictionary when right-clicking the newly created folder and choosing Add. This style must be located in (or defined in a file referenced from) a file called “Generic.xaml” in the “Themes” folder.
Tutorial c builder w7 windows#
Inside this folder you can add different templates for each Windows theme but since theming is out of the scope of this article we will only add a so called fallback style. To define the style for the window we then add a “Themes” folder to the project.
New FrameworkPropertyMetadata(typeof(CustomWindow))) The default style must be located in a folder called “Themes” at the root of the class library project and for our custom window control to be able to find its default style we add a static constructor where the default value of the DefaultStyleKey property is set to the type object of our class.ĭefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), When you create a custom control in WPF you define a template for it in XAML. Protected void CloseClick(object sender, RoutedEventArgs e) WindowState = (WindowState = WindowState.Normal) ? WindowState.Maximized : WindowState.Normal Protected void RestoreClick(object sender, RoutedEventArgs e) Protected void MinimizeClick(object sender, RoutedEventArgs e) We add the three click event handlers shown below to take care of this. By extending the default Window class we can use its built-in functionality for minimizing, restoring and closing the window. The class will be public and inherit from the default WPF window class (). The next step is to add a new class called CustomWindow.cs to the project by right-clicking the project name in the solution explorer and choose Add->Class. You add a reference in Visual Studio by choosing “Add reference” from the Project menu or by right-clicking the project in the solution explorer and the required DLLs should be found under the. We also need to add a reference to PresentationCore.dll where the RoutedEventArgs class lives, and WindowsBase.dll. We will need a reference to the PresentationFramework.dll where the out-of-the-box class, which our custom window class will inherit from, is implemented. Start by creating a new C# class library in Visual Studio (File->New Project) and give it a suitable name like Mm.Wpf.Controls.Īfter removing the automatically generated Class1.cs from the project the first thing to do is to add some references. The solution includes two Visual Studio projects, the first one a class library where the control itself is implemented and the second one a sample WPF application which will have a reference to the class library and use the custom window control as its main window.
Tutorial c builder w7 how to#
This introductory post will provide a walkthrough on how to create your own custom looking window control with resize, drag, minimize, restore and close functionality and how to use it in your WPF applications. NET, C#, Custom Controls, Visual Studio, WPF | 89 Comments How to create a custom window in WPF Posted: Ma| Author: Magnus Montin | Filed under: Custom Controls, WPF | Tags.