Francesco Geri

Il blog di Francesco Geri
posts - 94, comments - 165, trackbacks - 2

Rimuovere il bordo interno delle form MDI (sunken)

Una Form MDI di default ha un bordo interno (che credo sia di tipo sunken), ovvero del tipo:

image

Per rimuoverlo si può ricorrere alle API windows.

A tal proposito ho preparato un modulo MDIUtil che crea un Extension method per aggiungere alla form MDIParent il metodo SetBevel con cui si può richiedere di mostrare o non mostrare il bordo.

Il codice del modulo è il seguente:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
 
Public Module MDIUtil
 
#Region " Bordo interno (sunken) "
 
    <DllImport("user32.dll")> _
    Private Function GetWindowLong(hWnd As IntPtr, nIndex As Integer) As Integer
    End Function
 
    <DllImport("user32.dll")> _
    Private Function SetWindowLong(hWnd As IntPtr, nIndex As Integer, dwNewLong As Integer) As Integer
    End Function
 
    <DllImport("user32.dll", ExactSpelling:=True)> _
    Private Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, _
        uFlags As UInteger) As Integer
    End Function
 
    Private Const GWL_EXSTYLE As Integer = -20
    Private Const WS_EX_CLIENTEDGE As Integer = &H200
    Private Const SWP_NOSIZE As UInteger = &H1
    Private Const SWP_NOMOVE As UInteger = &H2
    Private Const SWP_NOZORDER As UInteger = &H4
    Private Const SWP_NOREDRAW As UInteger = &H8
    Private Const SWP_NOACTIVATE As UInteger = &H10
    Private Const SWP_FRAMECHANGED As UInteger = &H20
    Private Const SWP_SHOWWINDOW As UInteger = &H40
    Private Const SWP_HIDEWINDOW As UInteger = &H80
    Private Const SWP_NOCOPYBITS As UInteger = &H100
    Private Const SWP_NOOWNERZORDER As UInteger = &H200
    Private Const SWP_NOSENDCHANGING As UInteger = &H400
 
    ''' <summary>
    ''' Consente di rimuovere il bordo 3D interno della form MDI (sunken).
    ''' Vedi post: http://stackoverflow.com/questions/7752696/how-to-remove-3d-border-sunken-from-mdiclient-component-in-mdi-parent-form
    ''' Oppure articolo: http://www.codeproject.com/Articles/8489/Getting-a-quot-Handle-quot-on-the-MDI-Client#Changing%20the%20Border%20Styles:
    ''' </summary>
    ''' <param name="form"></param>
    ''' <param name="show">False per rimuovere il sunken (3D interno), True per mostrarlo</param>
    ''' <returns></returns>
    <System.Runtime.CompilerServices.Extension>
 Public Function SetBevel(form As Form, show As Boolean) As Boolean
        For Each c As Control In form.Controls
            Dim client As MdiClient = TryCast(c, MdiClient)
            If client IsNot Nothing Then
                Dim windowLong As Integer = GetWindowLong(c.Handle, GWL_EXSTYLE)
 
                If show Then
                    windowLong = windowLong Or WS_EX_CLIENTEDGE
                Else
                    windowLong = windowLong And Not WS_EX_CLIENTEDGE
                End If
 
                SetWindowLong(c.Handle, GWL_EXSTYLE, windowLong)
 
                ' Update the non-client area.
                SetWindowPos(client.Handle, IntPtr.Zero, 0, 0, 0, 0, _
                    SWP_NOACTIVATE Or SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOOWNERZORDER Or SWP_FRAMECHANGED)
 
                Return True
            End If
        Next
        Return False
    End Function
 
#End Region
 
End Module

 

A questo punto nella form parent MDI si deve importare il modulo e usare il metodo SetBevel nella load, ovvero:

Imports itConsult.josh.joshDesigner.MDIUtil
 
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.SetBevel(False)
End Sub

 

Nei commenti del codice del modulo sono indicate anche le fonti da cui ho liberamente preso quel codice, ovvero questo post e questo post.

Print | posted on venerdì 1 marzo 2013 12:05 | Filed Under [ Tips .Net MDI ]

Powered by:
Powered By Subtext Powered By ASP.NET