elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [SOURCE] Añadir magnetismo a los bordes de una ventana/Form
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [SOURCE] Añadir magnetismo a los bordes de una ventana/Form  (Leído 2,961 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.810



Ver Perfil
[SOURCE] Añadir magnetismo a los bordes de una ventana/Form
« en: 1 Diciembre 2015, 22:14 pm »

He estado refactorizando un viejo snippet, el cual es de mis favoritos, por ese motivo lo posteo aquí para hacer una mención especial y no en el apartado de snippets.

Lo que voy a mostrar es la forma más sencilla (copy&paste) para añadir magnetismo a una ventana.

Personalmente considero que todos deberiamos implementar esta funcionalidad en nuestras aplicaciones, ya que es una funcionalidad muy útil para mantener la organizción de las ventanas en la pantalla, cosa que cualquier usuario-final de su aplicación lo sabrá agradecer.

El magnetismo de ventanas consiste en que, al mover la ventana/Form cerca de un borde de la pantalla, la ventana se adhiera a dicho borde.



Nota: Esta funcionalidad estará incluida en la próxima versión de mi API ElektroKit: http://foro.elhacker.net/net/elektrokit_v10_api_de_proposito_general_para_desarrolladores_de_net-t444997.0.html



El siguiente código está escrito en Vb.Net (es suficiente con copiar, pegar y usar) pero se puede compilar en una dll para desarrolladores de código-C#.

La Class tiene dos propiedades importantes de personalización, la primera propiedad es WindowMagnetizer.Threshold, que indica el margen, en píxeles, en el que se debe producir el magnetismo. Yo suelo utilizar un valor de 35 píxeles ya que soy muy basto moviendo el ratón, pero creo que un valor de 20 seria lo apropiado de forma generalizada.

La otra propiedad se llama WindowMagnetizer.AllowOffscreen, que como su propio nombre indica por si mismo, sirve para habilitar o deshabilitar el poder mover la ventana fuera de los límites de la pantalla activa. (he tenido en cuenta la existencia de una pantalla dual).

El uso de esta class es muy, muy sencillo, tanto como esto:

Código
  1. Private magnetizer As New WindowMagnetizer(Me) With
  2.    {
  3.        .Enabled = True,
  4.        .AllowOffscreen = True,
  5.        .Threshold = 30
  6.    }

Código
  1. private WindowMagnetizer magnetizer;
  2.  
  3. private void Form1_Load(object sender, EventArgs e) {
  4.  
  5.   magnetizer = new WindowMagnetizer(this)
  6.                    {
  7.                        Enabled = true,
  8.                        AllowOffscreen = true,
  9.                        Threshold = 30
  10.                    };  
  11. }

Sin más, el código fuente:

Código
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 01-December-2015
  4. ' ***********************************************************************
  5.  
  6. #Region " Public Members Summary "
  7.  
  8. #Region " Constructors "
  9.  
  10. ' WindowMagnetizer.New(IWin32Window)
  11.  
  12. #End Region
  13.  
  14. #Region " Properties "
  15.  
  16. ' WindowMagnetizer.Handle As IntPtr
  17. ' WindowMagnetizer.OwnerWindow As IWin32Window
  18. ' WindowMagnetizer.Threshold As Integer
  19. ' WindowMagnetizer.Enabled As Boolean
  20. ' WindowMagnetizer.AllowOffscreen As Boolean
  21.  
  22. #End Region
  23.  
  24. #Region " Methods "
  25.  
  26. ' WindowMagnetizer.Dispose()
  27.  
  28. #End Region
  29.  
  30. #End Region
  31.  
  32. #Region " Usage Examples "
  33.  
  34. 'Private magnetizer As New WindowMagnetizer(Me) With
  35. '    {
  36. '        .Enabled = True,
  37. '        .AllowOffscreen = True,
  38. '        .Threshold = 30
  39. '    }
  40.  
  41. #End Region
  42.  
  43. #Region " Option Statements "
  44.  
  45. Option Explicit On
  46. Option Strict On
  47. Option Infer Off
  48.  
  49. #End Region
  50.  
  51. #Region " Imports "
  52.  
  53. Imports System
  54. Imports System.ComponentModel
  55. Imports System.Drawing
  56. Imports System.Linq
  57. Imports System.Runtime.InteropServices
  58. Imports System.Windows.Forms
  59.  
  60. ' Imports Elektro.Interop.Win32
  61. ' Imports Elektro.Interop.Win32.Enums
  62. ' Imports Elektro.Interop.Win32.Types
  63.  
  64. #End Region
  65.  
  66. #Region " Window Magnetizer "
  67.  
  68.    ''' ----------------------------------------------------------------------------------------------------
  69.    ''' <summary>
  70.    ''' Add magnetism to the edges of a window,
  71.    ''' in this way, by bringing the window to a screen edge, the edge of the window adheres it to the edge of the screen.
  72.    ''' </summary>
  73.    ''' ----------------------------------------------------------------------------------------------------
  74.    ''' <example> This is a code example.
  75.    ''' <code>
  76.    ''' Private magnetizer As New WindowMagnetizer(Me) With
  77.    '''     {
  78.    '''         .Enabled = True,
  79.    '''         .AllowOffscreen = True,
  80.    '''         .Threshold = 30
  81.    '''     }
  82.    ''' </code>
  83.    ''' </example>
  84.    ''' ----------------------------------------------------------------------------------------------------
  85.    Public Class WindowMagnetizer : Inherits NativeWindow : Implements IDisposable
  86.  
  87. #Region " Private Fields "
  88.  
  89.        ''' ----------------------------------------------------------------------------------------------------
  90.        ''' <summary>
  91.        ''' Determines whether the owner window is being resized by one of its edges.
  92.        ''' </summary>
  93.        ''' ----------------------------------------------------------------------------------------------------
  94.        Protected isResizing As Boolean
  95.  
  96. #End Region
  97.  
  98. #Region " Properties "
  99.  
  100.        ''' ----------------------------------------------------------------------------------------------------
  101.        ''' <summary>
  102.        ''' Gets the window that owns this <see cref="WindowMagnetizer"/> instance.
  103.        ''' </summary>
  104.        ''' ----------------------------------------------------------------------------------------------------
  105.        ''' <value>
  106.        ''' The window.
  107.        ''' </value>
  108.        ''' ----------------------------------------------------------------------------------------------------
  109.        Public Overridable ReadOnly Property OwnerWindow As IWin32Window
  110.            <DebuggerStepThrough>
  111.            Get
  112.                Return Me.ownerWindowB
  113.            End Get
  114.        End Property
  115.        ''' ----------------------------------------------------------------------------------------------------
  116.        ''' <summary>
  117.        ''' ( Backing field )
  118.        ''' The window that owns this <see cref="WindowMagnetizer"/> instance.
  119.        ''' </summary>
  120.        ''' ----------------------------------------------------------------------------------------------------
  121.        Protected ownerWindowB As IWin32Window
  122.  
  123.        ''' ----------------------------------------------------------------------------------------------------
  124.        ''' <summary>
  125.        ''' Gets the handle for the window that owns this <see cref="WindowMagnetizer"/> instance.
  126.        ''' </summary>
  127.        ''' ----------------------------------------------------------------------------------------------------
  128.        ''' <value>
  129.        ''' The handle.
  130.        ''' </value>
  131.        ''' ----------------------------------------------------------------------------------------------------
  132.        Public Overridable Shadows ReadOnly Property Handle As IntPtr
  133.            <DebuggerStepThrough>
  134.            Get
  135.                Return MyBase.Handle
  136.            End Get
  137.        End Property
  138.  
  139.        ''' ----------------------------------------------------------------------------------------------------
  140.        ''' <summary>
  141.        ''' Gets or sets, in pixels, the minimum threshold that the magnetic window needs to dock it on the nearest window border.
  142.        ''' <para></para>
  143.        ''' (Default value is <c>20</c>))
  144.        ''' </summary>
  145.        ''' ----------------------------------------------------------------------------------------------------
  146.        ''' <value>
  147.        ''' The minimum threshold that the magnetic window needs to dock it on the nearest window border.
  148.        ''' </value>
  149.        ''' ----------------------------------------------------------------------------------------------------
  150.        Public Overridable Property Threshold As Integer
  151.            <DebuggerStepThrough>
  152.            Get
  153.                Return Me.thresholdB
  154.            End Get
  155.            <DebuggerStepThrough>
  156.            Set(ByVal value As Integer)
  157.                Me.thresholdB = value
  158.            End Set
  159.        End Property
  160.        ''' ----------------------------------------------------------------------------------------------------
  161.        ''' <summary>
  162.        ''' ( Backing field )
  163.        ''' The minimum threshold that the magnetic window needs to dock it on the nearest window border.
  164.        ''' </summary>
  165.        ''' ----------------------------------------------------------------------------------------------------
  166.        Protected thresholdB As Integer
  167.  
  168.        ''' ----------------------------------------------------------------------------------------------------
  169.        ''' <summary>
  170.        ''' Gets or sets a value indicating whether the magnetizer is enabled.
  171.        ''' </summary>
  172.        ''' ----------------------------------------------------------------------------------------------------
  173.        ''' <value>
  174.        ''' <see langword="True"/> if the magnetizer is enabled, otherwise, <see langword="False"/>.
  175.        ''' </value>
  176.        ''' ----------------------------------------------------------------------------------------------------
  177.        Public Overridable Property Enabled As Boolean
  178.            <DebuggerStepThrough>
  179.            Get
  180.                Return Me.enabledB
  181.            End Get
  182.            <DebuggerStepThrough>
  183.            Set(ByVal value As Boolean)
  184.                Me.enabledB = value
  185.            End Set
  186.        End Property
  187.        ''' ----------------------------------------------------------------------------------------------------
  188.        ''' <summary>
  189.        ''' ( Backing field )
  190.        ''' A value indicating whether the magnetizer is enabled.
  191.        ''' </summary>
  192.        ''' ----------------------------------------------------------------------------------------------------
  193.        Protected enabledB As Boolean
  194.  
  195.        ''' ----------------------------------------------------------------------------------------------------
  196.        ''' <summary>
  197.        ''' Gets or sets a value indicating whether the window can be moved off-screen.
  198.        ''' <para></para>
  199.        ''' Default value is <see langword="True"/>.
  200.        ''' </summary>
  201.        ''' ----------------------------------------------------------------------------------------------------
  202.        ''' <value>
  203.        ''' <see langword="True"/> if the window can be moved off-screen, otherwise, <see langword="False"/>.
  204.        ''' </value>
  205.        ''' ----------------------------------------------------------------------------------------------------
  206.        Public Overridable Property AllowOffscreen As Boolean
  207.            <DebuggerStepThrough>
  208.            Get
  209.                Return Me.allowOffscreenB
  210.            End Get
  211.            <DebuggerStepThrough>
  212.            Set(ByVal value As Boolean)
  213.                Me.allowOffscreenB = value
  214.            End Set
  215.        End Property
  216.        ''' ----------------------------------------------------------------------------------------------------
  217.        ''' <summary>
  218.        ''' ( Backing field )
  219.        ''' A value indicating whether the window can be moved off-screen.
  220.        ''' </summary>
  221.        ''' ----------------------------------------------------------------------------------------------------
  222.        Protected allowOffscreenB As Boolean
  223.  
  224. #End Region
  225.  
  226. #Region " Constructors "
  227.  
  228.        ''' ----------------------------------------------------------------------------------------------------
  229.        ''' <summary>
  230.        ''' Prevents a default instance of the <see cref="WindowMagnetizer"/> class from being created.
  231.        ''' </summary>
  232.        ''' ----------------------------------------------------------------------------------------------------
  233.        <DebuggerNonUserCode>
  234.        Private Sub New()
  235.        End Sub
  236.  
  237.        ''' ----------------------------------------------------------------------------------------------------
  238.        ''' <summary>
  239.        ''' Initializes a new instance of the <see cref="WindowMagnetizer"/> class.
  240.        ''' </summary>
  241.        ''' ----------------------------------------------------------------------------------------------------
  242.        ''' <param name="window">
  243.        ''' The <see cref="IWin32Window"/> window that owns this instance (eg. a <see cref="Form"/> window).
  244.        ''' </param>
  245.        ''' ----------------------------------------------------------------------------------------------------
  246.        <DebuggerStepThrough>
  247.        Public Sub New(ByVal window As IWin32Window)
  248.  
  249.            Me.allowOffscreenB = True
  250.            Me.thresholdB = 20
  251.            Me.ownerWindowB = window
  252.  
  253.            MyBase.AssignHandle(window.Handle)
  254.  
  255.        End Sub
  256.  
  257. #End Region
  258.  
  259. #Region " Private Methods "
  260.  
  261.        ''' ----------------------------------------------------------------------------------------------------
  262.        ''' <summary>
  263.        ''' If the margin between the specified <paramref name="window"/>
  264.        ''' and the nearest border of the active screeen is lower than the value specified in <paramref name="threshold"/>,
  265.        ''' then it docks the window to the border.
  266.        ''' </summary>
  267.        ''' ----------------------------------------------------------------------------------------------------
  268.        ''' <param name="window">
  269.        ''' The magnetic window.
  270.        ''' </param>
  271.        '''
  272.        ''' <param name="windowPosHandle">
  273.        ''' A pointer to a <see cref="Interop.Win32.Types.WindowPos"/> structure that contains the
  274.        ''' new size and position of the <paramref name="window"/>.
  275.        ''' </param>
  276.        '''
  277.        ''' <param name="threshold">
  278.        ''' The minimum threshold that the window needs to dock it on the nearest desktop border.
  279.        ''' </param>
  280.        ''' ----------------------------------------------------------------------------------------------------
  281.        Protected Overridable Sub DockToNearestScreenBorder(ByVal window As IWin32Window,
  282.                                                            ByVal windowPosHandle As IntPtr,
  283.                                                            Optional ByVal threshold As Integer = 0I)
  284.  
  285.            Dim workingArea As Rectangle =
  286.                Screen.FromControl(DirectCast(window, Control)).WorkingArea ' Active screen.
  287.  
  288.            workingArea.Width = 0
  289.            workingArea.Height = 0
  290.  
  291.            Screen.AllScreens.ToList.ForEach(
  292.                Sub(scr As Screen)
  293.                    workingArea.Width += scr.WorkingArea.Width
  294.                    workingArea.Height += scr.WorkingArea.Height
  295.                End Sub)
  296.  
  297.            Dim windowPos As WindowPos =
  298.                CType(Marshal.PtrToStructure(windowPosHandle, GetType(WindowPos)), WindowPos)
  299.  
  300.            If (windowPos.Y = 0) OrElse (windowPos.X = 0) Then
  301.                ' Nothing to do.
  302.                Exit Sub
  303.            End If
  304.  
  305.            Dim win32Rect As Rect
  306.            Dim rect As Rectangle
  307.            NativeMethods.GetWindowRect(window.Handle, win32Rect)
  308.            rect = win32Rect
  309.  
  310.            ' Top border
  311.            If ((windowPos.Y >= -threshold) AndAlso
  312.               ((workingArea.Y > 0) AndAlso (windowPos.Y <= (threshold + workingArea.Y)))) _
  313.            OrElse ((workingArea.Y <= 0) AndAlso (windowPos.Y <= threshold)) Then
  314.  
  315.                windowPos.Y = workingArea.Y
  316.  
  317.            End If
  318.  
  319.            ' Left border
  320.            If (windowPos.X >= (workingArea.X - threshold)) AndAlso
  321.               (windowPos.X <= (workingArea.X + threshold)) Then
  322.  
  323.                windowPos.X = workingArea.X
  324.  
  325.            ElseIf (windowPos.X <= (workingArea.X - threshold)) AndAlso
  326.                   Not (Me.allowOffscreenB) Then
  327.  
  328.                windowPos.X = workingArea.X
  329.  
  330.            End If
  331.  
  332.            ' Right border.
  333.            If ((windowPos.X + rect.Width) <= (workingArea.Right + threshold)) AndAlso
  334.               ((windowPos.X + rect.Width) >= (workingArea.Right - threshold)) Then
  335.  
  336.                windowPos.X = (workingArea.Right - rect.Width)
  337.  
  338.            ElseIf ((windowPos.X + rect.Width) >= (workingArea.Right + threshold)) AndAlso
  339.                   Not (Me.allowOffscreenB) Then
  340.  
  341.                windowPos.X = (workingArea.Right - rect.Width)
  342.  
  343.            End If
  344.  
  345.            ' Bottom border.
  346.            If ((windowPos.Y + rect.Height) <= (workingArea.Bottom + threshold)) AndAlso
  347.               ((windowPos.Y + rect.Height) >= (workingArea.Bottom - threshold)) Then
  348.  
  349.                windowPos.Y = (workingArea.Bottom - rect.Height)
  350.  
  351.            ElseIf ((windowPos.Y + rect.Height) >= (workingArea.Bottom + threshold)) AndAlso
  352.                   Not (Me.allowOffscreenB) Then
  353.  
  354.                windowPos.Y = (workingArea.Bottom - rect.Height)
  355.  
  356.            End If
  357.  
  358.            ' Marshal it back.
  359.            Marshal.StructureToPtr(structure:=windowPos, ptr:=windowPosHandle, fDeleteOld:=True)
  360.  
  361.        End Sub
  362.  
  363. #End Region
  364.  
  365. #Region " Window Procedure (WndProc) "
  366.  
  367.        ''' ----------------------------------------------------------------------------------------------------
  368.        ''' <summary>
  369.        ''' Invokes the default window procedure associated with this window to process windows messages.
  370.        ''' </summary>
  371.        ''' ----------------------------------------------------------------------------------------------------
  372.        ''' <param name="m">
  373.        ''' A <see cref="T:Message"/> that is associated with the current Windows message.
  374.        ''' </param>
  375.        ''' ----------------------------------------------------------------------------------------------------
  376.        <DebuggerStepThrough>
  377.        Protected Overrides Sub WndProc(ByRef m As Message)
  378.  
  379.            Select Case m.Msg
  380.  
  381.                Case WindowsMessages.WmSizing
  382.                    Me.isResizing = True
  383.  
  384.                Case WindowsMessages.WmExitSizeMove
  385.                    Me.isResizing = False
  386.  
  387.                Case WindowsMessages.WmWindowPosChanging
  388.  
  389.                    If Not (Me.isResizing) AndAlso (Me.enabledB) Then
  390.                        Me.DockToNearestScreenBorder(window:=Me.ownerWindowB,
  391.                                                     windowPosHandle:=m.LParam,
  392.                                                     threshold:=Me.thresholdB)
  393.                    End If
  394.  
  395.            End Select
  396.  
  397.            MyBase.WndProc(m)
  398.  
  399.        End Sub
  400.  
  401. #End Region
  402.  
  403. #Region " Hidden Base Members "
  404.  
  405.        <EditorBrowsable(EditorBrowsableState.Never)>
  406.        <DebuggerNonUserCode>
  407.        Public Shadows Function ReferenceEquals(ByVal objA As Object, ByVal objB As Object) As Boolean
  408.            Return Object.ReferenceEquals(objA, objB)
  409.        End Function
  410.  
  411.        <EditorBrowsable(EditorBrowsableState.Never)>
  412.        <DebuggerNonUserCode>
  413.        Public Shadows Function GetHashCode() As Integer
  414.            Return MyBase.GetHashCode
  415.        End Function
  416.  
  417.        <EditorBrowsable(EditorBrowsableState.Never)>
  418.        <DebuggerNonUserCode>
  419.        Public Shadows Function [GetType]() As Type
  420.            Return MyBase.GetType
  421.        End Function
  422.  
  423.        <EditorBrowsable(EditorBrowsableState.Never)>
  424.        <DebuggerNonUserCode>
  425.        Public Shadows Function Equals(ByVal obj As Object) As Boolean
  426.            Return MyBase.Equals(obj)
  427.        End Function
  428.  
  429.        <EditorBrowsable(EditorBrowsableState.Never)>
  430.        <DebuggerNonUserCode>
  431.        Public Shadows Function ToString() As String
  432.            Return MyBase.ToString
  433.        End Function
  434.  
  435.        <EditorBrowsable(EditorBrowsableState.Never)>
  436.        <DebuggerNonUserCode>
  437.        Public Shadows Sub AssignHandle(ByVal handle As IntPtr)
  438.            MyBase.AssignHandle(handle)
  439.        End Sub
  440.  
  441.        <EditorBrowsable(EditorBrowsableState.Never)>
  442.        <DebuggerNonUserCode>
  443.        Public Shadows Sub CreateHandle(ByVal cp As CreateParams)
  444.            MyBase.CreateHandle(cp)
  445.        End Sub
  446.  
  447.        <EditorBrowsable(EditorBrowsableState.Never)>
  448.        <DebuggerNonUserCode>
  449.        Public Shadows Sub DestroyHandle()
  450.            MyBase.DestroyHandle()
  451.        End Sub
  452.  
  453.        <EditorBrowsable(EditorBrowsableState.Never)>
  454.        <DebuggerNonUserCode>
  455.        Public Shadows Sub ReleaseHandle()
  456.            MyBase.ReleaseHandle()
  457.        End Sub
  458.  
  459.        <EditorBrowsable(EditorBrowsableState.Never)>
  460.        <DebuggerNonUserCode>
  461.        Public Shadows Function FromHandle(ByVal handle As IntPtr) As NativeWindow
  462.            Return NativeWindow.FromHandle(handle)
  463.        End Function
  464.  
  465.        <EditorBrowsable(EditorBrowsableState.Never)>
  466.        <DebuggerNonUserCode>
  467.        Public Shadows Function GetLifeTimeService() As Object
  468.            Return MyBase.GetLifetimeService
  469.        End Function
  470.  
  471.        <EditorBrowsable(EditorBrowsableState.Never)>
  472.        <DebuggerNonUserCode>
  473.        Public Shadows Function InitializeLifeTimeService() As Object
  474.            Return MyBase.InitializeLifetimeService
  475.        End Function
  476.  
  477.        <EditorBrowsable(EditorBrowsableState.Never)>
  478.        <DebuggerNonUserCode>
  479.        Public Shadows Function CreateObjRef(ByVal requestedType As Type) As System.Runtime.Remoting.ObjRef
  480.            Return MyBase.CreateObjRef(requestedType)
  481.        End Function
  482.  
  483.        <EditorBrowsable(EditorBrowsableState.Never)>
  484.        <DebuggerNonUserCode>
  485.        Public Shadows Sub DefWndProc(ByRef m As Message)
  486.            MyBase.DefWndProc(m)
  487.        End Sub
  488.  
  489. #End Region
  490.  
  491. #Region " IDisposable Implementation "
  492.  
  493.        ''' ----------------------------------------------------------------------------------------------------
  494.        ''' <summary>
  495.        ''' To detect redundant calls when disposing.
  496.        ''' </summary>
  497.        ''' ----------------------------------------------------------------------------------------------------
  498.        Protected isDisposed As Boolean
  499.  
  500.        ''' ----------------------------------------------------------------------------------------------------
  501.        ''' <summary>
  502.        ''' Releases all the resources used by this instance.
  503.        ''' </summary>
  504.        ''' ----------------------------------------------------------------------------------------------------
  505.        <DebuggerStepThrough>
  506.        Public Sub Dispose() Implements IDisposable.Dispose
  507.  
  508.            Me.Dispose(isDisposing:=True)
  509.            GC.SuppressFinalize(obj:=Me)
  510.  
  511.        End Sub
  512.  
  513.        ''' ----------------------------------------------------------------------------------------------------
  514.        ''' <summary>
  515.        ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  516.        ''' Releases unmanaged and - optionally - managed resources.
  517.        ''' </summary>
  518.        ''' ----------------------------------------------------------------------------------------------------
  519.        ''' <param name="isDisposing">
  520.        ''' <see langword="True"/>  to release both managed and unmanaged resources;
  521.        ''' <see langword="False"/> to release only unmanaged resources.
  522.        ''' </param>
  523.        ''' ----------------------------------------------------------------------------------------------------
  524.        <DebuggerStepThrough>
  525.        Protected Overridable Sub Dispose(ByVal isDisposing As Boolean)
  526.  
  527.            If (Not Me.isDisposed) AndAlso (isDisposing) Then
  528.  
  529.                With Me
  530.                    .enabledB = False
  531.                    .AllowOffscreen = True
  532.                    .thresholdB = 0
  533.                End With
  534.  
  535.                MyBase.ReleaseHandle()
  536.                MyBase.DestroyHandle()
  537.  
  538.            End If
  539.  
  540.            Me.isDisposed = True
  541.  
  542.        End Sub
  543.  
  544. #End Region
  545.  
  546.    End Class
  547.  
  548. #End Region

P/Invokes:

Código
  1.        <SuppressUnmanagedCodeSecurity>
  2.        <DllImport("user32.dll", SetLastError:=True)>
  3.        Public Shared Function GetWindowRect(ByVal hwnd As IntPtr,
  4.                                             ByRef rect As Rect
  5.        ) As <MarshalAs(UnmanagedType.Bool)> Boolean
  6.        End Function



Código
  1. Public Enum WindowsMessages As Integer
  2.  
  3.        WmSizing = &H214
  4.        WmExitSizeMove = &H232
  5.        WmWindowPosChanging = &H46
  6.  
  7. End Enum


Código
  1. Imports System
  2. Imports System.Diagnostics
  3. Imports System.Linq
  4. Imports System.Runtime.InteropServices
  5.  
  6. #Region " Window Pos "
  7.  
  8.    ''' ----------------------------------------------------------------------------------------------------
  9.    ''' <summary>
  10.    ''' Contains information about the size and position of a window.
  11.    ''' </summary>
  12.    ''' ----------------------------------------------------------------------------------------------------
  13.    ''' <remarks>
  14.    ''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms632612%28v=vs.85%29.aspx"/>
  15.    ''' </remarks>
  16.    ''' ----------------------------------------------------------------------------------------------------
  17.    <DebuggerStepThrough>
  18.    <StructLayout(LayoutKind.Sequential)>
  19.    Public Structure WindowPos
  20.  
  21. #Region " Fields "
  22.  
  23.        ''' <summary>
  24.        ''' A handle to the window.
  25.        ''' </summary>
  26.        Public Hwnd As IntPtr
  27.  
  28.        ''' <summary>
  29.        ''' The position of the window in Z order (front-to-back position).
  30.        ''' This member can be a handle to the window behind which this window is placed,
  31.        ''' or can be one of the special values listed with the 'SetWindowPos' function.
  32.        ''' </summary>
  33.        Public HwndInsertAfter As IntPtr
  34.  
  35.        ''' <summary>
  36.        ''' The position of the left edge of the window.
  37.        ''' </summary>
  38.        Public X As Integer
  39.  
  40.        ''' <summary>
  41.        ''' The position of the top edge of the window.
  42.        ''' </summary>
  43.        Public Y As Integer
  44.  
  45.        ''' <summary>
  46.        ''' The window width, in pixels.
  47.        ''' </summary>
  48.        Public Width As Integer
  49.  
  50.        ''' <summary>
  51.        ''' The window height, in pixels.
  52.        ''' </summary>
  53.        Public Height As Integer
  54.  
  55.        ''' <summary>
  56.        ''' Flag containing the window position.
  57.        ''' </summary>
  58.        Public Flags As Integer
  59.  
  60. #End Region
  61.  
  62.    End Structure
  63.  
  64. #End Region

Código
  1. Imports System
  2. Imports System.Diagnostics
  3. Imports System.Drawing
  4. Imports System.Linq
  5. Imports System.Runtime.InteropServices
  6.  
  7. #Region " Rect "
  8.  
  9.    ''' ----------------------------------------------------------------------------------------------------
  10.    ''' <summary>
  11.    ''' Defines the coordinates of the upper-left and lower-right corners of a rectangle.
  12.    ''' </summary>
  13.    ''' ----------------------------------------------------------------------------------------------------
  14.    ''' <remarks>
  15.    ''' <see href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd162897%28v=vs.85%29.aspx"/>
  16.    ''' <para></para>
  17.    ''' <see href="http://www.pinvoke.net/default.aspx/Structures/rect.html"/>
  18.    ''' </remarks>
  19.    ''' ----------------------------------------------------------------------------------------------------
  20.    <DebuggerStepThrough>
  21.    <StructLayout(LayoutKind.Sequential)>
  22.    Public Structure Rect
  23.  
  24. #Region " Properties "
  25.  
  26.        ''' ----------------------------------------------------------------------------------------------------
  27.        ''' <summary>
  28.        ''' Gets or sets the x-coordinate of the upper-left corner of the rectangle.
  29.        ''' </summary>
  30.        ''' ----------------------------------------------------------------------------------------------------
  31.        ''' <value>
  32.        ''' The x-coordinate of the upper-left corner of the rectangle.
  33.        ''' </value>
  34.        ''' ----------------------------------------------------------------------------------------------------
  35.        Public Property Left As Integer
  36.  
  37.        ''' ----------------------------------------------------------------------------------------------------
  38.        ''' <summary>
  39.        ''' Gets or sets the y-coordinate of the upper-left corner of the rectangle.
  40.        ''' </summary>
  41.        ''' ----------------------------------------------------------------------------------------------------
  42.        ''' <value>
  43.        ''' The y-coordinate of the upper-left corner of the rectangle.
  44.        ''' </value>
  45.        ''' ----------------------------------------------------------------------------------------------------
  46.        Public Property Top As Integer
  47.  
  48.        ''' ----------------------------------------------------------------------------------------------------
  49.        ''' <summary>
  50.        ''' Gets or sets the x-coordinate of the lower-right corner of the rectangle.
  51.        ''' </summary>
  52.        ''' ----------------------------------------------------------------------------------------------------
  53.        ''' <value>
  54.        ''' The x-coordinate of the lower-right corner of the rectangle.
  55.        ''' </value>
  56.        ''' ----------------------------------------------------------------------------------------------------
  57.        Public Property Right As Integer
  58.  
  59.        ''' ----------------------------------------------------------------------------------------------------
  60.        ''' <summary>
  61.        ''' Gets or sets the y-coordinate of the lower-right corner of the rectangle.
  62.        ''' </summary>
  63.        ''' ----------------------------------------------------------------------------------------------------
  64.        ''' <value>
  65.        ''' The y-coordinate of the lower-right corner of the rectangle.
  66.        ''' </value>
  67.        ''' ----------------------------------------------------------------------------------------------------
  68.        Public Property Bottom As Integer
  69.  
  70. #End Region
  71.  
  72. #Region " Constructors "
  73.  
  74.        ''' ----------------------------------------------------------------------------------------------------
  75.        ''' <summary>
  76.        ''' Initializes a new instance of the <see cref="Rect"/> struct.
  77.        ''' </summary>
  78.        ''' ----------------------------------------------------------------------------------------------------
  79.        ''' <param name="left">
  80.        ''' The x-coordinate of the upper-left corner of the rectangle.
  81.        ''' </param>
  82.        '''
  83.        ''' <param name="top">
  84.        ''' The y-coordinate of the upper-left corner of the rectangle.
  85.        ''' </param>
  86.        '''
  87.        ''' <param name="right">
  88.        ''' The x-coordinate of the lower-right corner of the rectangle.
  89.        ''' </param>
  90.        '''
  91.        ''' <param name="bottom">
  92.        ''' The y-coordinate of the lower-right corner of the rectangle.
  93.        ''' </param>
  94.        ''' ----------------------------------------------------------------------------------------------------
  95.        Public Sub New(ByVal left As Integer,
  96.                       ByVal top As Integer,
  97.                       ByVal right As Integer,
  98.                       ByVal bottom As Integer)
  99.  
  100.            Me.Left = left
  101.            Me.Top = top
  102.            Me.Right = right
  103.            Me.Bottom = bottom
  104.  
  105.        End Sub
  106.  
  107.        ''' ----------------------------------------------------------------------------------------------------
  108.        ''' <summary>
  109.        ''' Initializes a new instance of the <see cref="Rect"/> struct.
  110.        ''' </summary>
  111.        ''' ----------------------------------------------------------------------------------------------------
  112.        ''' <param name="rect">
  113.        ''' The <see cref="Rectangle"/>.
  114.        ''' </param>
  115.        ''' ----------------------------------------------------------------------------------------------------
  116.        Public Sub New(ByVal rect As Rectangle)
  117.  
  118.            Me.New(rect.Left, rect.Top, rect.Right, rect.Bottom)
  119.  
  120.        End Sub
  121.  
  122. #End Region
  123.  
  124. #Region " Operator Conversions "
  125.  
  126.        ''' ----------------------------------------------------------------------------------------------------
  127.        ''' <summary>
  128.        ''' Performs an implicit conversion from <see cref="Rect"/> to <see cref="Rectangle"/>.
  129.        ''' </summary>
  130.        ''' ----------------------------------------------------------------------------------------------------
  131.        ''' <param name="rect">The <see cref="Rect"/>.
  132.        ''' </param>
  133.        ''' ----------------------------------------------------------------------------------------------------
  134.        ''' <returns>
  135.        ''' The resulting <see cref="Rectangle"/>.
  136.        ''' </returns>
  137.        ''' ----------------------------------------------------------------------------------------------------
  138.        Public Shared Widening Operator CType(rect As Rect) As Rectangle
  139.  
  140.            Return New Rectangle(rect.Left, rect.Top, (rect.Right - rect.Left), (rect.Bottom - rect.Top))
  141.  
  142.        End Operator
  143.  
  144.        ''' ----------------------------------------------------------------------------------------------------
  145.        ''' <summary>
  146.        ''' Performs an implicit conversion from <see cref="Rectangle"/> to <see cref="Rect"/>.
  147.        ''' </summary>
  148.        ''' ----------------------------------------------------------------------------------------------------
  149.        ''' <param name="rect">The <see cref="Rectangle"/>.
  150.        ''' </param>
  151.        ''' ----------------------------------------------------------------------------------------------------
  152.        ''' <returns>
  153.        ''' The resulting <see cref="Rect"/>.
  154.        ''' </returns>
  155.        ''' ----------------------------------------------------------------------------------------------------
  156.        Public Shared Widening Operator CType(rect As Rectangle) As Rect
  157.  
  158.            Return New Rect(rect)
  159.  
  160.        End Operator
  161.  
  162. #End Region
  163.  
  164.    End Structure
  165.  
  166. #End Region

Espero que esto les pueda servir de ayuda.

Saludos!


« Última modificación: 1 Diciembre 2015, 22:39 pm por Eleкtro » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Canviar form (ventana)
Programación Visual Basic
Dayak 3 1,203 Último mensaje 13 Septiembre 2006, 03:41 am
por JRIOS
Redimencionar un form sin bordes vb.net
.NET (C#, VB.NET, ASP)
Zeroql 2 8,268 Último mensaje 8 Febrero 2010, 18:18 pm
por Zeroql
¿Como añadir un scrollbar a una ventana muy grande?(Python)
Scripting
XD YO 8 12,553 Último mensaje 9 Marzo 2010, 15:05 pm
por XD YO
Mover Form sin ventana en c++ Builder
Programación C/C++
saawyeer 0 1,721 Último mensaje 3 Abril 2013, 05:11 am
por saawyeer
Dimensionar form sin bordes
Programación Visual Basic
rapbyone 1 3,485 Último mensaje 26 Octubre 2014, 04:11 am
por daniel.r.23
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines