如何使用WPF开发具有通用窗口所有属性的自定义窗口
(最小化、最大化、恢复和关闭)以及与这些相关的所有属性 按钮,可以在窗口的标题位置添加任何控件 请告诉我该怎么做如何使用WPF开发具有通用窗口所有属性的自定义窗口,wpf,custom-controls,Wpf,Custom Controls,(最小化、最大化、恢复和关闭)以及与这些相关的所有属性 按钮,可以在窗口的标题位置添加任何控件 请告诉我该怎么做 提前谢谢我的建议是不要这样做。这在WPF中是不需要的 只需更改窗口模板,您就会得到所需的内容 编辑: 这里有一个例子 I am going to develop my own window where I'll have my own Caption Buttons 该资源应在全球范围内可用。您可以在XAML中引用它,如下所示: ... 在我的项目中,设计师往往会被这种引用
提前谢谢我的建议是不要这样做。这在WPF中是不需要的 只需更改窗口模板,您就会得到所需的内容 编辑: 这里有一个例子
I am going to develop my own window where I'll have my own Caption Buttons
该资源应在全球范围内可用。您可以在XAML中引用它,如下所示:
...
在我的项目中,设计师往往会被这种引用扼杀(可能是因为它是一个WinForms项目,主体由WPF元素组成)。因此,我们必须在“代码隐藏”窗口构造函数中以编程方式设置样式:
Style=(Style)FindResource(Constants.DEFAULT\u WINDOW\u Style)
使用值为“WindowsStyle”的常量声明
以下是结果(我为这些空内容道歉,但这是保密的):
我没有说它很漂亮…我的建议是不要这样做。这在WPF中是不需要的 只需更改窗口模板,您就会得到所需的内容 编辑: 这里有一个例子
I am going to develop my own window where I'll have my own Caption Buttons
该资源应在全球范围内可用。您可以在XAML中引用它,如下所示:
...
在我的项目中,设计师往往会被这种引用扼杀(可能是因为它是一个WinForms项目,主体由WPF元素组成)。因此,我们必须在“代码隐藏”窗口构造函数中以编程方式设置样式:
Style=(Style)FindResource(Constants.DEFAULT\u WINDOW\u Style)
使用值为“WindowsStyle”的常量声明
以下是结果(我为这些空内容道歉,但这是保密的):
我没说它很漂亮…我需要它,请给我发过去apurva3633@gmail.com我会感谢你的帮助…@SharpUrBrain:他应该把它贴在这里让大家都能享受:)非常感谢Timors我真的在寻找这样的例子我需要它,请给我发过去apurva3633@gmail.com我会感谢你的帮助帮助…@SharpUrBrain:他应该把它贴在这里让大家都能欣赏:)非常感谢Timors我真的在寻找这样的例子
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
namespace MyNamespace {
public partial class StyleWindow {
private void m_btnClose_Click(object sender, RoutedEventArgs e) {
Window window = ((FrameworkElement)sender).TemplatedParent as Window;
if (window != null)
{
window.Close();
}
}
private void m_btnMaximine_Click(object sender, RoutedEventArgs e) {
Window window = ((FrameworkElement)sender).TemplatedParent as Window;
if (window != null)
{
BitmapImage bitmap = new BitmapImage();
if (window.WindowState == WindowState.Maximized) {
window.WindowState = WindowState.Normal;
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"Images/max.PNG", UriKind.Relative);
bitmap.EndInit();
((sender as Button).Content as Image).Source = bitmap;
} else {
window.WindowState = WindowState.Maximized;
bitmap.BeginInit();
bitmap.UriSource = new Uri(@"Images/normal.PNG", UriKind.Relative);
bitmap.EndInit();
((sender as Button).Content as Image).Source = bitmap;
}
}
}
private void m_btnMinimine_Click(object sender, RoutedEventArgs e) {
Window window = ((FrameworkElement)sender).TemplatedParent as Window;
if (window != null)
{
window.WindowState = WindowState.Minimized;
}
}
private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Window window = ((FrameworkElement)sender).TemplatedParent as Window;
if (window != null)
{
window.DragMove();
}
}
#region sizing event handlers
void OnSizeSouth(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.South);
}
}
void OnSizeNorth(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.North);
}
}
void OnSizeEast(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.East);
}
}
void OnSizeWest(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.West);
}
}
void OnSizeNorthWest(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.NorthWest);
}
}
void OnSizeNorthEast(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.NorthEast);
}
}
void OnSizeSouthEast(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.SouthEast);
}
}
void OnSizeSouthWest(object sender, System.Windows.Input.MouseButtonEventArgs e) {
Window wnd = ((FrameworkElement) sender).TemplatedParent as Window;
if (wnd != null) {
WindowInteropHelper helper = new WindowInteropHelper(wnd);
DragSize(helper.Handle, SizingAction.SouthWest);
}
}
#endregion
#region P/Invoke and helper method
const int WM_SYSCOMMAND = 0x112;
const int SC_SIZE = 0xF000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
void DragSize(IntPtr handle, SizingAction sizingAction) {
if (System.Windows.Input.Mouse.LeftButton == System.Windows.Input.MouseButtonState.Pressed) {
SendMessage(handle, WM_SYSCOMMAND, (IntPtr) (SC_SIZE + sizingAction), IntPtr.Zero);
SendMessage(handle, 514, IntPtr.Zero, IntPtr.Zero);
}
}
#endregion
#region helper enum
public enum SizingAction {
North = 3,
South = 6,
East = 2,
West = 1,
NorthEast = 5,
NorthWest = 4,
SouthEast = 8,
SouthWest = 7
}
}
#endregion
}