0
1

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 5 years have passed since last update.

【VB.NET】WPFタイトルバーのないフォームをドラッグで移動する

Last updated at Posted at 2019-06-11

C#でのやり方しか書いていなくて辛い

基本的にC#でできることはVB.NETでできることは分かったのですが、あまりにも情報が少なすぎて辛い。
タイトル通り本来のタイトルバーのあるフォームを消しつつもドラッグをしてフォームを動かせるようにします

xamlでフォームを消し、動かしたい図形を設定する

Window.xaml

<Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        WindowStyle="None"
        AllowsTransparency="True"
        Background="Transparent"
        Title="MainWindow" Height="308.033" Width="190.203">

    <Canvas>
        <Thumb DragDelta="MoveWindow" Width="186" Height="308" >
            <Thumb.Template>
                <ControlTemplate>
                    <Rectangle Width="180" Height="301" RadiusX="5" RadiusY="5" Fill="White" Margin="0,0,0,0" Stroke="Blue" />
                </ControlTemplate>
            </Thumb.Template>
        </Thumb>
    </Canvas>
</Window>

c5afc3bcd0f113472939ff488d00414d.png

本来のフォームを削除

WindowStyle="None"→枠を削除
AllowsTransparency="True"→ウィンドウを透明にできるようする
Background="Transparent"→本来のウィンドウを透明に

少し小さめのフォームをRectangleで作成

< Thumb >:ユーザーがドラッグできるコントロールを作成
< Rectangle >:ドラッグする図形としてRectangleを指定

< Thumb > に設定してあるDragDeltaイベントを記述

drag&drop.vb
Public Class Window1
    Public Sub MoveWindow(sender As Object, e As Primitives.DragDeltaEventArgs)
        Left += e.HorizontalChange
        Top += e.VerticalChange
    End Sub
End Class

oklri-ix83s.gif

これで動くようになりました。

参考

C#版

0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?