none
Creen que me puedan ayudar a convertir un código de VB.Net a C# WPF RRS feed

  • Pregunta

  • Buenas tardes y un cordial saludo, tengo un código en VB.Net que permite estirar o ajustar la ventana sin necesidad de colocar borde al formulario algo como esta imagen.

    Mi pregunta es la siguiente, el código que les mostrare a continuación me gustaría pasarlo o convertirlo a un proyecto que tengo en WPF de C#, pero la verdad no tengo la menor idea de como convertirlo, el código lo necesito en C#, he probado paginas que te hacen la convención pero la vedad no me convierten al 100% el código de VB.NET a C#, espero me aya explicado, les dejo a continuación mi código, espero me puedan ayudar, se los agradezco mucho, saludos.   

    Imports System.Runtime.InteropServices
    
    Private _resizeDir As ResizeDirection = ResizeDirection.None
    
       Public Enum ResizeDirection
            None = 0
            Left = 1
            TopLeft = 2
            Top = 3
            TopRight = 4
            Right = 5
            BottomRight = 6
            Bottom = 7
            BottomLeft = 8
        End Enum
    
        Public Property resizeDir() As ResizeDirection
            Get
                Return _resizeDir
            End Get
            Set(ByVal value As ResizeDirection)
                _resizeDir = value
    
                'Change cursor
                Select Case value
                    Case ResizeDirection.Left
                        Me.Cursor = Cursors.SizeWE
    
                    Case ResizeDirection.Right
                        Me.Cursor = Cursors.SizeWE
    
                    Case ResizeDirection.Top
                        Me.Cursor = Cursors.SizeNS
    
                    Case ResizeDirection.Bottom
                        Me.Cursor = Cursors.SizeNS
    
                    Case ResizeDirection.BottomLeft
                        Me.Cursor = Cursors.SizeNESW
    
                    Case ResizeDirection.TopRight
                        Me.Cursor = Cursors.SizeNESW
    
                    Case ResizeDirection.BottomRight
                        Me.Cursor = Cursors.SizeNWSE
    
                    Case ResizeDirection.TopLeft
                        Me.Cursor = Cursors.SizeNWSE
    
                    Case Else
                        Me.Cursor = Cursors.Default
                End Select
            End Set
         End Property
    
         #Region " Functions and Constants "
    
           <DllImport("user32.dll")>
           Public Shared Function ReleaseCapture() As Boolean
           End Function
    
           <DllImport("user32.dll")>
           Public Shared Function SendMessage(ByVal hWnd As 
           IntPtr, ByVal Msg As Integer, ByVal wParam As 
           Integer, ByVal lParam As Integer) As Integer
           End Function
    
           Private Const WM_NCLBUTTONDOWN As Integer = &HA1
           Private Const HTBORDER As Integer = 18
           Private Const HTBOTTOM As Integer = 15
           Private Const HTBOTTOMLEFT As Integer = 16
           Private Const HTBOTTOMRIGHT As Integer = 17
           Private Const HTCAPTION As Integer = 2
           Private Const HTLEFT As Integer = 10
           Private Const HTRIGHT As Integer = 11
           Private Const HTTOP As Integer = 12
           Private Const HTTOPLEFT As Integer = 13
           Private Const HTTOPRIGHT As Integer = 14
    
         #End Region
    
         #Region " Moving & Resizing methods "
    
         Private Sub MoveForm()
         ReleaseCapture()
         SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
           End Sub
    
           Private Sub ResizeForm(ByVal direction As 
           ResizeDirection)
    
            Dim dir As Integer = -1
            Select Case direction
                Case ResizeDirection.Left
                    dir = HTLEFT
                Case ResizeDirection.TopLeft
                    dir = HTTOPLEFT
                Case ResizeDirection.Top
                    dir = HTTOP
                Case ResizeDirection.TopRight
                    dir = HTTOPRIGHT
                Case ResizeDirection.Right
                    dir = HTRIGHT
                Case ResizeDirection.BottomRight
                    dir = HTBOTTOMRIGHT
                Case ResizeDirection.Bottom
                    dir = HTBOTTOM
                Case ResizeDirection.BottomLeft
                    dir = HTBOTTOMLEFT
            End Select
    
            If dir <> -1 Then
             ReleaseCapture()
             SendMessage(Me.Handle, WM_NCLBUTTONDOWN, dir, 0)
            End If
    
          End Sub
        #End Region
    
    

       


    jueves, 1 de marzo de 2018 1:35

Respuestas

  • Aquí le dejo mi versión de ese código.  Es más corta creo yo.

        public enum ResizeDirection
        {
            None = 0,
            Left,
            TopLeft,
            Top,
            TopRight,
            Right,
            BottomRight,
            Bottom,
            BottomLeft
        }
        public partial class Form1 : Form
        {
            private static Cursor[] _allCursors = new Cursor[]
            {
                Cursors.SizeWE,
                Cursors.SizeNWSE,
                Cursors.SizeNS,
                Cursors.SizeNESW
            };
    
            [DllImport("user32")]
            private static extern int SendMessage(IntPtr hWnd, uint msg, uint wParam, IntPtr lParam);
    
            [DllImport("user32")]
            private static extern bool ReleaseCapture();
    
            private const uint WM_NCLBUTTONDOWN = 0xA1;
            private const uint HTCAPTION = 2;
            private const uint HTLEFT = 10;
            private const uint HTRIGHT = 11;
            private const uint HTTOP = 12;
            private const uint HTTOPLEFT = 13;
            private const uint HTTOPRIGHT = 14;
            private const uint HTBOTTOM = 15;
            private const uint HTBOTTOMLEFT = 16;
            private const uint HTBOTTOMRIGHT = 17;
            private const uint HTBORDER = 18;
    
            private uint[] _allDirections = new uint[]
            {
                HTLEFT, HTTOPLEFT, HTTOP, HTTOPRIGHT, HTRIGHT, HTBOTTOMRIGHT, HTBOTTOM, HTBOTTOMLEFT
            };
    
            private ResizeDirection m_resizeDir;
            public ResizeDirection ResizeDir
            {
                get { return m_resizeDir; }
                set
                {
                    m_resizeDir = value;
                    if (m_resizeDir == ResizeDirection.None)
                    {
                        Cursor = Cursors.Default;
                        return;
                    }
                    int curIndex = (int)value % _allCursors.Length - 1;
                    Cursor = _allCursors[curIndex];
                }
            }
    
            private void MoveForm()
            {
                SendMoveMessage(HTCAPTION);
            }
    
            private void SendMoveMessage(uint param)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, param, IntPtr.Zero);
            }
    
            private void ResizeForm(ResizeDirection direction)
            {
                if (direction == ResizeDirection.None)
                {
                    return;
                }
                int dirIndex = (int)direction - 1;
                uint dir = _allDirections[dirIndex];
                SendMoveMessage(dir);
            }
            ... //ETC.  Aquí va el resto del código del formulario.
        }
    

    Si necesita esto en más de un formulario, entonces le recomiendo poner este código en una clase base y luego hacer que los demás formularios hereden de esta clase base.


    Jose R. MCP
    Code Samples

    jueves, 1 de marzo de 2018 3:33
    Moderador

Todas las respuestas

  • Aquí le dejo mi versión de ese código.  Es más corta creo yo.

        public enum ResizeDirection
        {
            None = 0,
            Left,
            TopLeft,
            Top,
            TopRight,
            Right,
            BottomRight,
            Bottom,
            BottomLeft
        }
        public partial class Form1 : Form
        {
            private static Cursor[] _allCursors = new Cursor[]
            {
                Cursors.SizeWE,
                Cursors.SizeNWSE,
                Cursors.SizeNS,
                Cursors.SizeNESW
            };
    
            [DllImport("user32")]
            private static extern int SendMessage(IntPtr hWnd, uint msg, uint wParam, IntPtr lParam);
    
            [DllImport("user32")]
            private static extern bool ReleaseCapture();
    
            private const uint WM_NCLBUTTONDOWN = 0xA1;
            private const uint HTCAPTION = 2;
            private const uint HTLEFT = 10;
            private const uint HTRIGHT = 11;
            private const uint HTTOP = 12;
            private const uint HTTOPLEFT = 13;
            private const uint HTTOPRIGHT = 14;
            private const uint HTBOTTOM = 15;
            private const uint HTBOTTOMLEFT = 16;
            private const uint HTBOTTOMRIGHT = 17;
            private const uint HTBORDER = 18;
    
            private uint[] _allDirections = new uint[]
            {
                HTLEFT, HTTOPLEFT, HTTOP, HTTOPRIGHT, HTRIGHT, HTBOTTOMRIGHT, HTBOTTOM, HTBOTTOMLEFT
            };
    
            private ResizeDirection m_resizeDir;
            public ResizeDirection ResizeDir
            {
                get { return m_resizeDir; }
                set
                {
                    m_resizeDir = value;
                    if (m_resizeDir == ResizeDirection.None)
                    {
                        Cursor = Cursors.Default;
                        return;
                    }
                    int curIndex = (int)value % _allCursors.Length - 1;
                    Cursor = _allCursors[curIndex];
                }
            }
    
            private void MoveForm()
            {
                SendMoveMessage(HTCAPTION);
            }
    
            private void SendMoveMessage(uint param)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, param, IntPtr.Zero);
            }
    
            private void ResizeForm(ResizeDirection direction)
            {
                if (direction == ResizeDirection.None)
                {
                    return;
                }
                int dirIndex = (int)direction - 1;
                uint dir = _allDirections[dirIndex];
                SendMoveMessage(dir);
            }
            ... //ETC.  Aquí va el resto del código del formulario.
        }
    

    Si necesita esto en más de un formulario, entonces le recomiendo poner este código en una clase base y luego hacer que los demás formularios hereden de esta clase base.


    Jose R. MCP
    Code Samples

    jueves, 1 de marzo de 2018 3:33
    Moderador
  • Gracias, se te agradese mucho
    jueves, 1 de marzo de 2018 7:10