限定窗口大小范围--windows message subclassing

发表于:2007-06-30来源:作者:点击数: 标签:
Imports System. Windows .Forms Imports System.ComponentModel Namespace vb City Namespace FormClasses Public Class frmRestricted Inherits Form @##Region Iridium form properties code Private mFormMinSize, mFormMaxSize As PointAPI Category(For
Imports System.Windows.Forms
Imports System.ComponentModel

Namespace vbCity
  Namespace FormClasses
    Public Class frmRestricted
      Inherits Form

@##Region " Iridium form properties code "

      Private mFormMinSize, mFormMaxSize As PointAPI

      <Category("FormSize"), _
          Description( _
          "The Minimum height that the form can be sized to.")> _
      Public Property FormMinHeight() As Int32
        Get
          @# SCS - A return of zero indicates that it has not been
          @# set.
           Return mFormMinSize.y
        End Get
        Set(ByVal Value As Int32)
          CheckBounds(Value, False)
          mFormMinSize.y = Value
        End Set
      End Property @# FormMinHeight
      
      <Category("FormSize"), _
          Description( _
          "The Maximum height that the form can be sized to.")> _
      Public Property FormMaxHeight() As Int32
        Get
          @# SCS - A return of zero indicates that it has not been
          @# set.
          Return mFormMaxSize.y
        End Get
        Set(ByVal Value As Int32)
          CheckBounds(Value, False)
          mFormMaxSize.y = Value
        End Set
      End Property @# FormMaxHeight

      <Category("FormSize"), _
          Description( _
          "The Minimum width that the form can be sized to.")> _
      Public Property FormMinWidth() As Int32
        Get
          @# SCS - A return of zero indicates that it has not been
          @# set.
          Return mFormMinSize.x
        End Get
        Set(ByVal Value As Int32)
          CheckBounds(Value, False)
          mFormMinSize.x = Value
        End Set
      End Property @# FormMinWidth
      
      <Category("FormSize"), _
          Description( _
          "The Maximum width that the form can be sized to.")> _
      Public Property FormMaxWidth() As Int32
        Get
          @# SCS - A return of zero indicates that it has not been
          @# set.
          Return mFormMaxSize.x
        End Get
        Set(ByVal Value As Int32)
          CheckBounds(Value, False)
          mFormMaxSize.x = Value
        End Set
      End Property @# FormMaxWidth
#End Region

      Private Sub CheckBounds(ByRef pintValue As Int32, _
          ByVal pblnCheckWidth As Boolean)
        If pintValue < 0 Then
          pintValue = 0
        Else
          @# SCS - Check that the Co-ords are not outside of the
          @# screen here?
          Dim ScreenPoint As System.Drawing.Rectangle
          ScreenPoint = Screen.PrimaryScreen.Bounds()

          If pblnCheckWidth Then
            If pintValue > ScreenPoint.Width Then pintValue = _
                ScreenPoint.Width
          Else
            If pintValue > ScreenPoint.Height Then pintValue = _
                ScreenPoint.Height
          End If

        End If
      End Sub

      Protected Overrides Sub WndProc( _
          ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_GETMINMAXINFO Then
          Dim mmiStruct As MinMaxInfo

          MyBase.WndProc(m)
          @# SCS - Get the information from lParam into our
          @# structure
          mmiStruct = CType(m.GetLParam(mmiStruct.GetType()), _
              MinMaxInfo)
          @# SCS - The following line does the same - I haven@#t yet
          @# tested for speed to
          @# see which is the quickest
          @#    mmiStruct = CType(Marshal.PtrToStructure(m.lParam,
          @# mmiStruct.GetType), MinMaxInfo)
          @# SCS - Set new structure values where applicable
          With mmiStruct

            If Not (mFormMaxSize.x.Equals(0)) Then
              .ptMaxTrackSize.x = mFormMaxSize.x
            End If
            
            If Not (mFormMaxSize.y.Equals(0)) Then
              .ptMaxTrackSize.y = mFormMaxSize.y
            End If
            
            If Not (mFormMinSize.x.Equals(0)) Then
              .ptMinTrackSize.x = mFormMinSize.x
            End If
            
            If Not (mFormMinSize.y.Equals(0)) Then
              .ptMinTrackSize.y = mFormMinSize.y
            End If
            @#.ptMaxPosition.x = 100
            @#.ptMaxSize.x = 200
          End With
          @# SCS - Copy the information back into lParam
          Marshal.StructureToPtr(mmiStruct, m.LParam, True)
          @# SCS - Return 0 because MSDN tells us to.
          m.Result() = New System.IntPtr()
        Else
          MyBase.WndProc(m)
        End If
      End Sub
    End Class
  End Namespace

  Namespace API
    Public Class clsGetMinMaxInfo
      
      @#Microsoft.Win32.Interop.Win.WM_GETMINMAXINFO
      Public Const WM_GETMINMAXINFO As Int32 = 100

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure PointAPI
        Public x As Int32
        Public y As Int32
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure MinMaxInfo
        Private ptReserved As PointAPI
        Private ptMaxSize As PointAPI
        Private ptMaxPosition As PointAPI
        Public ptMinTrackSize As PointAPI
        Public ptMaxTrackSize As PointAPI
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure WindowPos
        Public hwnd As Int32
        Public hWndInsertAfter As Int32
        Public x As Int32
        Public y As Int32
        Public cx As Int32
        Public cy As Int32
        Public flags As Int32
      End Structure
    End Class
  End Namespace

End Namespace

原文转自:http://www.ltesting.net