Results 1 to 4 of 4

Thread: WPF Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    3

    WPF Code

    Hi all,

    I'm a newbie in WPF and C#. I'm working on a small project and I want to make a custom window with borderless feature.
    I did some googling on the internet and found a sample for that.
    The problem is I didn't understand the code of the file.cs, and here is the first part of the code:

    PHP Code:
    namespace Custom_Window_Chrome_Demo
    {
        public class 
    ThemedWindow System.Windows.Window
        
    {
            private 
    Point cursorOffset;
            private 
    double restoreTop;
            private 
    FrameworkElement borderLeft;
            private 
    FrameworkElement borderTopLeft;
            private 
    FrameworkElement borderTop;
            private 
    FrameworkElement borderTopRight;
            private 
    FrameworkElement borderRight;
            private 
    FrameworkElement borderBottomRight;
            private 
    FrameworkElement borderBottom;
            private 
    FrameworkElement borderBottomLeft;
            private 
    FrameworkElement caption;
            private 
    FrameworkElement frame;
            private 
    Button minimizeButton;
            private 
    Button maximizeButton;
            private 
    Button closeButton;
            private 
    IntPtr handle;
            public 
    ThemedWindow()
            {
                
    SourceInitialized += (sendere) =>
                {
                    
    handle = new WindowInteropHelper(this).Handle;
                    
    HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WndProc));
                };
                
    Style = (Style)TryFindResource("ThemedWindowStyle");
            }
            public 
    override void OnApplyTemplate()
            {
                
    base.OnApplyTemplate();
                
    RegisterFrame();
                
    RegisterBorders();
                
    RegisterCaption();
                
    RegisterMinimizeButton();
                
    RegisterMaximizeButton();
                
    RegisterCloseButton();
            }
            private 
    void RegisterCloseButton()
            {
                
    closeButton = (Button)GetTemplateChild("PART_WindowCaptionCloseButton");
                if (
    closeButton != null)
                {
                    
    closeButton.Click += (sendere) => Close();
                }
            }
            private 
    void RegisterMaximizeButton()
            {
                
    maximizeButton = (Button)GetTemplateChild("PART_WindowCaptionMaximizeButton");
                if (
    maximizeButton != null)
                {
                    
    maximizeButton.Click += (sendere) =>
                    {
                        if (
    WindowState == System.Windows.WindowState.Normal)
                        {
                            
    WindowState System.Windows.WindowState.Maximized;
                        }
                        else
                        {
                            
    WindowState System.Windows.WindowState.Normal;
                        }
                    };
                }
            }
            private 
    void RegisterMinimizeButton()
            {
                
    minimizeButton = (Button)GetTemplateChild("PART_WindowCaptionMinimizeButton");
                if (
    minimizeButton != null)
                {
                    
    minimizeButton.Click += (sendere) => WindowState System.Windows.WindowState.Minimized;
                }
            } 
    I'll post the second part of the code in the next post.


    Please, can someone explain the for me?
    I'll really appreciate the help.


    Thanks!

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    3

    Re: WPF Code

    Hi all,

    This is the second part of the code:

    PHP Code:
            private void RegisterBorderEvents(WindowBorderEdge borderEdgeFrameworkElement border)
            {
                
    border.MouseEnter += (sendere) =>
                {
                    if (
    WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    {
                        switch (
    borderEdge)
                        {
                            case 
    WindowBorderEdge.Left:
                            case 
    WindowBorderEdge.Right:
                                
    border.Cursor Cursors.SizeWE;
                                break;
                            case 
    WindowBorderEdge.Top:
                            case 
    WindowBorderEdge.Bottom:
                                
    border.Cursor Cursors.SizeNS;
                                break;
                            case 
    WindowBorderEdge.TopLeft:
                            case 
    WindowBorderEdge.BottomRight:
                                
    border.Cursor Cursors.SizeNWSE;
                                break;
                            case 
    WindowBorderEdge.TopRight:
                            case 
    WindowBorderEdge.BottomLeft:
                                
    border.Cursor Cursors.SizeNESW;
                                break;
                        }
                    }
                    else
                    {
                        
    border.Cursor Cursors.Arrow;
                    }
                };
                
    border.MouseLeftButtonDown += (sendere) =>
                {
                    if (
    WindowState != WindowState.Maximized && ResizeMode == ResizeMode.CanResize)
                    {
                        
    Point cursorLocation e.GetPosition(this);
                        
    Point cursorOffset = new Point();
                        switch (
    borderEdge)
                        {
                            case 
    WindowBorderEdge.Left:
                                
    cursorOffset.cursorLocation.X;
                                break;
                            case 
    WindowBorderEdge.TopLeft:
                                
    cursorOffset.cursorLocation.X;
                                
    cursorOffset.cursorLocation.Y;
                                break;
                            case 
    WindowBorderEdge.Top:
                                
    cursorOffset.cursorLocation.Y;
                                break;
                            case 
    WindowBorderEdge.TopRight:
                                
    cursorOffset.= (Width cursorLocation.X);
                                
    cursorOffset.cursorLocation.Y;
                                break;
                            case 
    WindowBorderEdge.Right:
                                
    cursorOffset.= (Width cursorLocation.X);
                                break;
                            case 
    WindowBorderEdge.BottomRight:
                                
    cursorOffset.= (Width cursorLocation.X);
                                
    cursorOffset.= (Height cursorLocation.Y);
                                break;
                            case 
    WindowBorderEdge.Bottom:
                                
    cursorOffset.= (Height cursorLocation.Y);
                                break;
                            case 
    WindowBorderEdge.BottomLeft:
                                
    cursorOffset.cursorLocation.X;
                                
    cursorOffset.= (Height cursorLocation.Y);
                                break;
                        }
                        
    this.cursorOffset cursorOffset;
                        
    border.CaptureMouse();
                    }
                };
                
    border.MouseMove += (sendere) =>
                {
                    if (
    WindowState != WindowState.Maximized && border.IsMouseCaptured && ResizeMode == ResizeMode.CanResize)
                    {
                        
    Point cursorLocation e.GetPosition(this);
                        
    double nHorizontalChange = (cursorLocation.cursorOffset.X);
                        
    double pHorizontalChange = (cursorLocation.cursorOffset.X);
                        
    double nVerticalChange   = (cursorLocation.cursorOffset.Y);
                        
    double pVerticalChange   = (cursorLocation.cursorOffset.Y);
                        switch (
    borderEdge)
                        {
                            case 
    WindowBorderEdge.Left:
                                if (
    Width nHorizontalChange <= MinWidth
                                    break;
                                
    Left   += nHorizontalChange;
                                
    Width  -= nHorizontalChange;
                                break;
                            case 
    WindowBorderEdge.TopLeft:
                                if (
    Width nHorizontalChange <= MinWidth)
                                    break;
                                
    Left   += nHorizontalChange;
                                
    Width  -= nHorizontalChange;
                                if (
    Height nVerticalChange <= MinHeight)
                                    break;
                                
    Top    += nVerticalChange;
                                
    Height -= nVerticalChange;
                                break;
                            case 
    WindowBorderEdge.Top:
                                if (
    Height nVerticalChange <= MinHeight
                                    break;
                                
    Top    += nVerticalChange;
                                
    Height -= nVerticalChange;
                                break;
                            case 
    WindowBorderEdge.TopRight:
                                if (
    pHorizontalChange <= MinWidth)
                                    break;
                                
    Width  pHorizontalChange;
                                if (
    Height nVerticalChange <= MinHeight)
                                    break;
                                
    Top    += nVerticalChange;
                                
    Height -= nVerticalChange;
                                break;
                            case 
    WindowBorderEdge.Right:
                                if (
    pHorizontalChange <= MinWidth)
                                    break;
                                
    Width pHorizontalChange;
                                break;
                            case 
    WindowBorderEdge.BottomRight:
                                if (
    pHorizontalChange <= MinWidth)
                                    break;
                                
    Width  pHorizontalChange;
                                if (
    pVerticalChange <= MinHeight)
                                    break;
                                
    Height pVerticalChange;
                                break;
                            case 
    WindowBorderEdge.Bottom:
                                if (
    pVerticalChange <= MinHeight)
                                    break;
                                
    Height pVerticalChange;
                                break;
                            case 
    WindowBorderEdge.BottomLeft:
                                if (
    Width nHorizontalChange <= MinWidth)
                                    break;
                                
    Left   += nHorizontalChange;
                                
    Width  -= nHorizontalChange;
                                if (
    pVerticalChange <= MinHeight)
                                    break;
                                
    Height pVerticalChange;
                                break;
                        }
                    }
                };
                
    border.MouseLeftButtonUp += (sendere) =>
                {
                    
    border.ReleaseMouseCapture();
                };
            }
            private 
    void RegisterBorders()
            {
                
    borderLeft          = (FrameworkElement)GetTemplateChild("PART_WindowBorderLeft");
                
    borderTopLeft       = (FrameworkElement)GetTemplateChild("PART_WindowBorderTopLeft");
                
    borderTop           = (FrameworkElement)GetTemplateChild("PART_WindowBorderTop");
                
    borderTopRight      = (FrameworkElement)GetTemplateChild("PART_WindowBorderTopRight");
                
    borderRight         = (FrameworkElement)GetTemplateChild("PART_WindowBorderRight");
                
    borderBottomRight   = (FrameworkElement)GetTemplateChild("PART_WindowBorderBottomRight");
                
    borderBottom        = (FrameworkElement)GetTemplateChild("PART_WindowBorderBottom");
                
    borderBottomLeft    = (FrameworkElement)GetTemplateChild("PART_WindowBorderBottomLeft");
                
    RegisterBorderEvents(WindowBorderEdge.LeftborderLeft);
                
    RegisterBorderEvents(WindowBorderEdge.TopLeftborderTopLeft);
                
    RegisterBorderEvents(WindowBorderEdge.TopborderTop);
                
    RegisterBorderEvents(WindowBorderEdge.TopRightborderTopRight);
                
    RegisterBorderEvents(WindowBorderEdge.RightborderRight);
                
    RegisterBorderEvents(WindowBorderEdge.BottomRightborderBottomRight);
                
    RegisterBorderEvents(WindowBorderEdge.BottomborderBottom);
                
    RegisterBorderEvents(WindowBorderEdge.BottomLeftborderBottomLeft);
            }
            private 
    void RegisterCaption()
            {
                
    caption = (FrameworkElement)GetTemplateChild("PART_WindowCaption");
                if (
    caption != null)
                {
                    
    caption.MouseLeftButtonDown += (sendere) =>
                    {
                        
    restoreTop e.GetPosition(this).Y;
                        if (
    e.ClickCount == && e.ChangedButton == System.Windows.Input.MouseButton.Left && (ResizeMode != ResizeMode.CanMinimize && ResizeMode != ResizeMode.NoResize))
                        {
                            if (
    WindowState != System.Windows.WindowState.Maximized)
                            {
                                
    WindowState System.Windows.WindowState.Maximized;
                            }
                            else
                            {
                                
    WindowState System.Windows.WindowState.Normal;
                            }
                            return;
                        }
                        
    DragMove();
                    };
                    
    caption.MouseMove += (sendere) =>
                    {
                        if (
    e.LeftButton == MouseButtonState.Pressed && caption.IsMouseOver)
                        {
                            if (
    WindowState == WindowState.Maximized)
                            {
                                
    WindowState WindowState.Normal;
                                
    Top restoreTop 10;
                                
    DragMove();
                            }
                        }
                    };
                }
            }
            private 
    void RegisterFrame()
            {
                
    frame = (FrameworkElement)GetTemplateChild("PART_WindowFrame");
            }
            private 
    void WmGetMinMaxInfo(IntPtr hwndIntPtr lParam)
            {
                
    MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParamtypeof(MINMAXINFO));
                
    int MONITOR_DEFAULTTONEAREST 0x00000002;
                
    IntPtr monitor NativeMethods.MonitorFromWindow(hwndMONITOR_DEFAULTTONEAREST);
                if (
    monitor != System.IntPtr.Zero)
                {
                    
    MONITORINFO monitorInfo = new MONITORINFO();
                    
    NativeMethods.GetMonitorInfo(monitormonitorInfo);
                    
    RECT rcWorkArea monitorInfo.rcWork;
                    
    RECT rcMonitorArea monitorInfo.rcMonitor;
                    
    mmi.ptMaxPosition.Math.Abs(rcWorkArea.left rcMonitorArea.left);
                    
    mmi.ptMaxPosition.Math.Abs(rcWorkArea.top rcMonitorArea.top);
                    
    mmi.ptMaxSize.Math.Abs(rcWorkArea.right rcWorkArea.left);
                    
    mmi.ptMaxSize.Math.Abs(rcWorkArea.bottom rcWorkArea.top);
                }
                
    Marshal.StructureToPtr(mmilParamtrue);
            }
            private 
    IntPtr WndProc(IntPtr hwndint msgIntPtr wParamIntPtr lParamref bool handled)
            {
                switch (
    msg)
                {
                    case 
    0x0024:
                        
    WmGetMinMaxInfo(hwndlParam);
                        
    handled true;
                        break;
                }
                return 
    IntPtr.Zero;
            }
            private 
    enum WindowBorderEdge
            
    {
                
    Left,
                
    TopLeft,
                
    Top,
                
    TopRight,
                
    Right,
                
    BottomRight,
                
    Bottom,
                
    BottomLeft
            
    }
        }

    I'll appreciate your help.


    Thanks!

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: WPF Code

    That's odd, I'd expect some XAML when we are talking about WPF. Where did you get that? Do you have an image of the Window you want?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    3

    Re: WPF Code

    It's a class file (file.cs) that responsible for the window1.xaml size in min-max features.
    I thought that someone will explain the code for me


    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width