1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UserformでGridSplitterのようなもの

Posted at

WPFのGridSplitterいいよね
WPFは死んでるとか言う人がいるけど、僕の心の中では元気です。
ちなみにUWPは死んでると思います。

UserformにはGridとかの仕組みがない以上、
あんなに便利にはならないけれど、それっぽいのはVBAでも作れます。

UserForm1(Labelコントロールを1つだけ挿入してね)
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = 1 Then
        Label1.Left = Label1.Left + X - Label1.Width * 0.5
        '他コントロールの配置処理(UserForm_Resizeに追記するのが良いと思う)
    End If
End Sub

kiji5_1.PNG

Label上でマウスクリック中、Labelが左右に追従します(多分)。

あとはイベント発生時に他のコントロールの配置を変更して、GridSplitter風のなにかが完成です。
上のUserformに下記コードを上書きすれば動きます。

UserForm1(サンプル全文)
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long
Private Declare PtrSafe Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Button = 1 Then Label1.Left = Label1.Left + X - Label1.Width * 0.5: UserForm_Resize
End Sub
Private Sub UserForm_Activate()
    Dim r As Long, hWnd As Long: hWnd = GetActiveWindow()
    Dim wStyle As Long: wStyle = GetWindowLong(hWnd, -16&) Or &H40000 Or &H30000
    Call SetWindowLong(hWnd, -16&, wStyle)
    Call DrawMenuBar(hWnd)
End Sub
Private Sub UserForm_Resize()
On Error GoTo err
    With Me.Controls(1)
        .Width = Me.Width - .Left - 20
    End With
    With Me.Controls(2)
        .Width = Label1.Left - .Left
        .Height = Me.Height - .Top - 40
    End With
    With Me.Controls(3)
        .Left = Label1.Left + Label1.Width
        .Width = Me.Width - .Left - 20
        .Height = Me.Height - .Top - 40
    End With
    With Label1
        .Height = Me.Height - .Top
    End With
err:
End Sub
Private Sub UserForm_Initialize()
    Me.Width = 800
    Me.Height = 600
    Me.BackColor = &HFFFFFF
    '//Separator
    With Label1
        .Top = 10
        .Left = 195
        .Width = 10
        .BackColor = &HEEEEEE
    End With
    '//パネル1
    With Me.Controls.Add("Forms.Frame.1")
        .caption = "それっぽい上パネル"
        .Top = 10
        .Left = 10
        .Height = 80
        .BorderStyle = fmBorderStyleSingle
    End With
    '//パネル2
    With Me.Controls.Add("Forms.Frame.1")
        .caption = "それっぽい左パネル"
        .Top = Me.Controls(1).Height + 10
        .Left = 10
        .BorderStyle = fmBorderStyleSingle
        .BackColor = &HFFEEFF
    End With
    '//パネル3
    With Me.Controls.Add("Forms.Frame.1")
        .caption = "メイン画面"
        .Top = Me.Controls(1).Height + 10
        .BorderStyle = fmBorderStyleSingle
        .BackColor = &HFFFFEE
    End With
    With Me.Controls(1)
        With .Controls.Add("Forms.Label.1")
            .Top = 10
            .Left = 10
            .Width = 80
            .Height = 50
            .caption = "それっぽいボタン1"
            Set .Picture = CommandBars.GetImageMso("GroupPlay", 32, 32)
        End With
        With .Controls.Add("Forms.Label.1")
            .Top = 10
            .Left = 90
            .Width = 80
            .Height = 50
            .caption = "それっぽいボタン2"
            Set .Picture = CommandBars.GetImageMso("FileSave", 32, 32)
        End With
    End With
End Sub

(注意)全画面可能にする処理は32bit専用です。
64bitの方はlong=>longPtrにしたり、いろいろ書き直してください。
本コードでPCやデータに異常・損害が発生しても、作成者は一切責任を取りません。自己責任でお願いいたします。

UserForm_Initialize()で適当にそれっぽいコントロールを生成して、
UserForm_Resize()で再配置処理を書いてます。

ボタン風ラベルのアイコンは以前の記事を参照してください(今回はハリボテですが)。
https://qiita.com/torimaro/items/6265be0375a4a00bd36a

こんな感じで動きます(画像が汚くてすいません)。
うーんモダンなUIですばらしいなあ。

mv.gif

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?