0

我需要显示一个停靠在表单左侧的 ColorDialog。

我知道如何将可本地化的对象定位到表单的左侧,但是如何更改 ColorDialog 的起始位置?

我想我需要像著名的“CenteredMessageBox”类那样子分类,但不知道该怎么做......

有什么帮助吗?

这是我糟糕的代码:

Private Sub GlobalHotkey_CTRL_X_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles GlobalHotkey_CTRL_X.Press
    ColorDialog1.Color = RGB
    ColorDialog1.ShowDialog()
End Sub

如果我可以将属性“位置”和“开始位置”添加到默认的 Colordialog 控件,可能会更好,但就像我说的那样,不知道怎么做,也许其他人以前做过这个类,可以在这里发布......

谢谢你。

更新:

这是我使用 Mzn 解决方案得到的:

在此处输入图像描述

4

1 回答 1

1

我在尝试实现您的目标时编写了此示例。

X 类是一个继承自 ColorDialog 的类,它的主窗口在创建后立即移动。

DllImport属性是 .NET P/Invoke 机制。它告诉编译器我们正在调用本机函数。它需要签名,正文必须保持空白。

RECT结构是来自 pinvoke.net 的样板代码

告诉我这对你是否有用,如果你有任何问题。:)

Imports System.Runtime.InteropServices

Public Class Form1
    Dim cd As New X

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Text = Handle.ToString()
    End Sub

    Private Sub Form1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDoubleClick
        cd.ShowDialog(Me)
    End Sub

    'import' a native function from user32.dll
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Public Shared Function MoveWindow(hwnd As IntPtr, x As Integer, y As Integer, cx As Integer, cy As Integer, _
                                                                  rpt As Boolean) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function GetClientRect(ByVal hWnd As System.IntPtr, ByRef lpRECT As RECT) As Integer
    End Function
End Class

Public Class X
    Inherits ColorDialog

    Protected Overrides Function HookProc(hWnd As IntPtr, msg As Integer, wparam As IntPtr, lparam As IntPtr) As IntPtr
        System.Diagnostics.Debug.Print(hWnd)

        'this call messes up things, like the border style
        'i don't know how to fix it for now, but there should
        'be a way to tell it to move without changing border style
        Form1.MoveWindow(hWnd, 50, 110, 333, 333, True)

        Return MyBase.HookProc(hWnd, msg, wparam, lparam)
    End Function

End Class


'I got this class from http://www.pinvoke.net/ very useful! use as is.
<StructLayout(LayoutKind.Sequential)> _
Public Structure RECT
    Private _Left As Integer, _Top As Integer, _Right As Integer, _Bottom As Integer

    Public Sub New(ByVal Rectangle As Rectangle)
        Me.New(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom)
    End Sub
    Public Sub New(ByVal Left As Integer, ByVal Top As Integer, ByVal Right As Integer, _
                   ByVal Bottom As Integer)
        _Left = Left
        _Top = Top
        _Right = Right
        _Bottom = Bottom
    End Sub

    Public Property X As Integer
        Get
            Return _Left
        End Get
        Set(ByVal value As Integer)
            _Right = _Right - _Left + value
            _Left = value
        End Set
    End Property
    Public Property Y As Integer
        Get
            Return _Top
        End Get
        Set(ByVal value As Integer)
            _Bottom = _Bottom - _Top + value
            _Top = value
        End Set
    End Property
    Public Property Left As Integer
        Get
            Return _Left
        End Get
        Set(ByVal value As Integer)
            _Left = value
        End Set
    End Property
    Public Property Top As Integer
        Get
            Return _Top
        End Get
        Set(ByVal value As Integer)
            _Top = value
        End Set
    End Property
    Public Property Right As Integer
        Get
            Return _Right
        End Get
        Set(ByVal value As Integer)
            _Right = value
        End Set
    End Property
    Public Property Bottom As Integer
        Get
            Return _Bottom
        End Get
        Set(ByVal value As Integer)
            _Bottom = value
        End Set
    End Property
    Public Property Height() As Integer
        Get
            Return _Bottom - _Top
        End Get
        Set(ByVal value As Integer)
            _Bottom = value + _Top
        End Set
    End Property
    Public Property Width() As Integer
        Get
            Return _Right - _Left
        End Get
        Set(ByVal value As Integer)
            _Right = value + _Left
        End Set
    End Property
    Public Property Location() As Point
        Get
            Return New Point(Left, Top)
        End Get
        Set(ByVal value As Point)
            _Right = _Right - _Left + value.X
            _Bottom = _Bottom - _Top + value.Y
            _Left = value.X
            _Top = value.Y
        End Set
    End Property
    Public Property Size() As Size
        Get
            Return New Size(Width, Height)
        End Get
        Set(ByVal value As Size)
            _Right = value.Width + _Left
            _Bottom = value.Height + _Top
        End Set
    End Property

    Public Shared Widening Operator CType(ByVal Rectangle As RECT) As Rectangle
        Return New Rectangle(Rectangle.Left, Rectangle.Top, Rectangle.Width, Rectangle.Height)
    End Operator
    Public Shared Widening Operator CType(ByVal Rectangle As Rectangle) As RECT
        Return New RECT(Rectangle.Left, Rectangle.Top, Rectangle.Right, Rectangle.Bottom)
    End Operator
    Public Shared Operator =(ByVal Rectangle1 As RECT, ByVal Rectangle2 As RECT) As Boolean
        Return Rectangle1.Equals(Rectangle2)
    End Operator
    Public Shared Operator <>(ByVal Rectangle1 As RECT, ByVal Rectangle2 As RECT) As Boolean
        Return Not Rectangle1.Equals(Rectangle2)
    End Operator

    Public Overrides Function ToString() As String
        Return "{Left: " & _Left & "; " & "Top: " & _Top & "; Right: " & _Right & "; Bottom: " & _Bottom & "}"
    End Function

    Public Overloads Function Equals(ByVal Rectangle As RECT) As Boolean
        Return Rectangle.Left = _Left AndAlso Rectangle.Top = _Top AndAlso Rectangle.Right = _Right AndAlso Rectangle.Bottom = _Bottom
    End Function
    Public Overloads Overrides Function Equals(ByVal [Object] As Object) As Boolean
        If TypeOf [Object] Is RECT Then
            Return Equals(DirectCast([Object], RECT))
        ElseIf TypeOf [Object] Is Rectangle Then
            Return Equals(New RECT(DirectCast([Object], Rectangle)))
        End If

        Return False
    End Function
End Structure
于 2013-05-06T09:16:21.967 回答