none
Duda sobre un código que encontré en Internet WPF C# RRS feed

  • Pregunta

  • Un gran saludo creen que me puedan ayudar con el código que saque de una pagina web lo que pasa es que seguí todos los pasos como me indica en la pagina, pero no funciona, no se que error estoy cometiendo, creen que me puedan ayudar a que funcione, saludos

    A continuación les dejo la pagina o link donde saque el código y el código como yo lo desarrolle.

    ESTA ES LA PAGINA DONDE OBTUVE EL CÓDIGO "CLICK AQUI"

    Código de la "Clase"

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Shapes;
    using System;
    using System.Windows.Interop;
    using System.Runtime.InteropServices;
    
    namespace Mm.Wpf.Controls
    {
        public class CustomWindow : Window
        {
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
    
            protected void ResizeRectangle_PreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                Rectangle rectangle = sender as Rectangle;
                switch (rectangle.Name)
                {
                    case "top":
                        Cursor = Cursors.SizeNS;
                        ResizeWindow(ResizeDirection.Top);
                        break;
                    case "bottom":
                        Cursor = Cursors.SizeNS;
                        ResizeWindow(ResizeDirection.Bottom);
                        break;
                    case "left":
                        Cursor = Cursors.SizeWE;
                        ResizeWindow(ResizeDirection.Left);
                        break;
                    case "right":
                        Cursor = Cursors.SizeWE;
                        ResizeWindow(ResizeDirection.Right);
                        break;
                    case "topLeft":
                        Cursor = Cursors.SizeNWSE;
                        ResizeWindow(ResizeDirection.TopLeft);
                        break;
                    case "topRight":
                        Cursor = Cursors.SizeNESW;
                        ResizeWindow(ResizeDirection.TopRight);
                        break;
                    case "bottomLeft":
                        Cursor = Cursors.SizeNESW;
                        ResizeWindow(ResizeDirection.BottomLeft);
                        break;
                    case "bottomRight":
                        Cursor = Cursors.SizeNWSE;
                        ResizeWindow(ResizeDirection.BottomRight);
                        break;
                    default:
                        break;
                }
            }
    
            private HwndSource _hwndSource;
    
            protected override void OnInitialized(EventArgs e)
            {
                SourceInitialized += OnSourceInitialized;
                base.OnInitialized(e);
            }
    
            private void OnSourceInitialized(object sender, EventArgs e)
            {
                _hwndSource = (HwndSource)PresentationSource.FromVisual(this);
            }
    
            private void ResizeWindow(ResizeDirection direction)
            {
                SendMessage(_hwndSource.Handle, 0x112, (IntPtr)(61440 + direction), IntPtr.Zero);
            }
    
            private enum ResizeDirection
            {
                Left = 1,
                Right = 2,
                Top = 3,
                TopLeft = 4,
                TopRight = 5,
                Bottom = 6,
                BottomLeft = 7,
                BottomRight = 8,
            }
    
            public CustomWindow()
                         : base()
            {
                PreviewMouseMove += OnPreviewMouseMove;
            }
    
            protected void OnPreviewMouseMove(object sender, MouseEventArgs e)
            {
                if (Mouse.LeftButton != MouseButtonState.Pressed)
                    Cursor = Cursors.Arrow;
            }
    
            #region Click events
            protected void MinimizeClick(object sender, RoutedEventArgs e)
            {
                WindowState = WindowState.Minimized;
            }
    
            protected void RestoreClick(object sender, RoutedEventArgs e)
            {
                WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
            }
    
            protected void CloseClick(object sender, RoutedEventArgs e)
            {
                Close();
            }
            #endregion
    
            static CustomWindow()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow),
                    new FrameworkPropertyMetadata(typeof(CustomWindow)));
            }
    
            public override void OnApplyTemplate()
            {
                Button minimizeButton = GetTemplateChild("minimizeButton") as Button;
                if (minimizeButton != null)
                    minimizeButton.Click += MinimizeClick;
    
                Button restoreButton = GetTemplateChild("restoreButton") as Button;
                if (restoreButton != null)
                    restoreButton.Click += RestoreClick;
    
                Button closeButton = GetTemplateChild("closeButton") as Button;
                if (closeButton != null)
                    closeButton.Click += CloseClick;
    
                base.OnApplyTemplate();
    
                //Nuevo
                Grid resizeGrid = GetTemplateChild("resizeGrid") as Grid;
                if (resizeGrid != null)
                {
                    foreach (UIElement element in resizeGrid.Children)
                    {
                        Rectangle resizeRectangle = element as Rectangle;
                        if (resizeRectangle != null)
                        {
                            resizeRectangle.PreviewMouseDown += ResizeRectangle_PreviewMouseDown;
                            resizeRectangle.MouseMove += ResizeRectangle_MouseMove;
                        }
                    }
                }
    
                base.OnApplyTemplate();
            }
    
            private void moveRectangle_PreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                if (Mouse.LeftButton == MouseButtonState.Pressed)
                    DragMove();
            }
           
            protected void ResizeRectangle_MouseMove(Object sender, MouseEventArgs e)
            {
                Rectangle rectangle = sender as Rectangle;
                switch (rectangle.Name)
                {
                    case "top":
                        Cursor = Cursors.SizeNS;
                        break;
                    case "bottom":
                        Cursor = Cursors.SizeNS;
                        break;
                    case "left":
                        Cursor = Cursors.SizeWE;
                        break;
                    case "right":
                        Cursor = Cursors.SizeWE;
                        break;
                    case "topLeft":
                        Cursor = Cursors.SizeNWSE;
                        break;
                    case "topRight":
                        Cursor = Cursors.SizeNESW;
                        break;
                    case "bottomLeft":
                        Cursor = Cursors.SizeNESW;
                        break;
                    case "bottomRight":
                        Cursor = Cursors.SizeNWSE;
                        break;
                    default:
                        break;
                }
            }
        }
    }
    

    Código de "Xaml"

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:Mm.Wpf.Controls">
        <!--  Button style -->
        <Style TargetType="{x:Type Button}" x:Key="WindowButtonStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ButtonBase}">
                        <Border
                                x:Name="Chrome"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Margin="0"
                                Background="{TemplateBinding Background}"
                                SnapsToDevicePixels="True">
                            <ContentPresenter
                                    ContentTemplate="{TemplateBinding ContentTemplate}"
                                    Content="{TemplateBinding Content}"
                                    ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Margin="{TemplateBinding Padding}"
                                    RecognizesAccessKey="True"
                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="FontFamily" Value="Webdings"/>
            <Setter Property="FontSize" Value="13.333" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="Margin" Value="0,2,3,0"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Gray" />
                </Trigger>
            </Style.Triggers>
        </Style>
    
        <!-- Window style -->
        <Style TargetType="{x:Type local:CustomWindow}">
            <Setter Property="WindowStyle" Value="None"/>
            <Setter Property="ResizeMode" Value="NoResize"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="Silver"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                        <Border BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}">
                            <Grid>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Rectangle x:Name="moveRectangle" Fill="Transparent"
                                               Grid.Row="0" Grid.Column="0"/>
                                    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
                                        <Button x:Name="minimizeButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="0" />
                                        <Button x:Name="restoreButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="1" />
                                        <Button x:Name="closeButton" Style="{StaticResource WindowButtonStyle}"
                                                Content="r" />
                                    </StackPanel>
                                    <Grid Background="{TemplateBinding Background}"
                                               Grid.Row="1" Grid.ColumnSpan="2" Margin="5,5,5,5">
                                        <AdornerDecorator>
                                            <ContentPresenter/>
                                        </AdornerDecorator>
                                    </Grid>
                                </Grid>
                                <Grid x:Name="resizeGrid">
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                VerticalAlignment="Top"
                Height="5"
                x:Name="top"
                Margin="5,0,5,0" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                x:Name="bottom"
                Height="5"
                VerticalAlignment="Bottom"
                Margin="5,0,5,0" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                HorizontalAlignment="Left"
                Margin="0,5,0,5"
                Width="5"
                x:Name="left"/>
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                Margin="0,5,0,5"
                Width="5"
                HorizontalAlignment="Right"
                x:Name="right" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                HorizontalAlignment="Left"
                VerticalAlignment="Bottom"
                Width="5"
                Height="5"
                x:Name="bottomLeft" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                VerticalAlignment="Bottom"
                Height="5"
                Width="5"
                HorizontalAlignment="Right"
                x:Name="bottomRight" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                HorizontalAlignment="Right"
                Width="5"
                Height="5"
                VerticalAlignment="Top"
                x:Name="topRight" />
                                    <Rectangle
                Stroke="Transparent"
                Fill="Transparent"
                HorizontalAlignment="Left"
                Width="6"
                VerticalAlignment="Top"
                Height="5"
                x:Name="topLeft" />
                                </Grid>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    El error que me marca es en "Xaml"

    Espero puedo ayudarme, se los agrades ere, profundamente. 



    sábado, 7 de abril de 2018 8:02

Respuestas

Todas las respuestas