fc2ブログ
 

Technology へようこそ
ここは技術者の「経験」と「ノウハウ」のブログです


2010年12月08日

枠なしウィンドウの作り方

某台湾製萌えキャラ騒ぎをはじめ、次期バージョンの概要も発表され、微妙に盛り上がりつつある Silverlight ですが、実際に利用されているのを見かけるのは相変わらずメディアプレイヤーくらいな気もする今日この頃。

と、それはさておき、今回は最新の「Another TL」で採用した『枠なしウィンドウ(OOB限定)』の実装コードをご紹介。

まぁ、実際にはたいしたことはしておらず、プロジェクトプロパティでOOB設定のウィンドウスタイルを変更したうえで、ウィンドウサイズの変更/移動、マウスカーソルの形状変更くらいをコーディングしてあげれば出来上がりです。
マウスドラッグによるウィンドウ移動やリサイズの実装が1メソッドで実現できるのでえらく簡単です。

・プロジェクトプロパティ - 「ブラウザ外実行の設定」


// ウィンドウサイズ変更/移動
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!Application.Current.IsRunningOutOfBrowser) return;

Point pos = e.GetPosition(this);
double borderSize = 8;
Window win = Application.Current.MainWindow;

if (pos.X <= borderSize)
if (pos.Y <= borderSize)
win.DragResize(WindowResizeEdge.TopLeft);
else if (pos.Y >= this.ActualHeight - borderSize)
win.DragResize(WindowResizeEdge.BottomLeft);
else
win.DragResize(WindowResizeEdge.Left);
else if (pos.X >= this.ActualWidth - borderSize)
if (pos.Y <= borderSize)
win.DragResize(WindowResizeEdge.TopRight);
else if (pos.Y >= this.ActualHeight - borderSize)
win.DragResize(WindowResizeEdge.BottomRight);
else
win.DragResize(WindowResizeEdge.Right);
else
if (pos.Y <= borderSize)
win.DragResize(WindowResizeEdge.Top);
else if (pos.Y >= this.ActualHeight - borderSize)
win.DragResize(WindowResizeEdge.Bottom);
else
win.DragMove();
}

// マウスカーソル形状設定
private void UserControl_MouseMove(object sender, MouseEventArgs e)
{
if (!Application.Current.IsRunningOutOfBrowser) return;

Point pos = e.GetPosition(this);
double borderSize = 8;

if (pos.X <= borderSize)
if (pos.Y <= borderSize)
this.Cursor = Cursors.SizeNWSE;
else if (pos.Y >= this.ActualHeight - borderSize)
this.Cursor = Cursors.SizeNESW;
else
this.Cursor = Cursors.SizeWE;
else if (pos.X >= this.ActualWidth - borderSize)
if (pos.Y <= borderSize)
this.Cursor = Cursors.SizeNESW;
else if (pos.Y >= this.ActualHeight - borderSize)
this.Cursor = Cursors.SizeNWSE;
else
this.Cursor = Cursors.SizeWE;
else
if (pos.Y <= borderSize)
this.Cursor = Cursors.SizeNS;
else if (pos.Y >= this.ActualHeight - borderSize)
this.Cursor = Cursors.SizeNS;
else
this.Cursor = Cursors.Arrow;
}

巷では Silverlight/WPF に関して MVVM による実装が注目されているようなので、こうしてイベントをチマチマと書くやり方はいずれ時代遅れになってしまいそうな気配ですが、その辺りは今後の状況を伺いつつ臨機応変に対応していけばよろしいかと。

[ posted by ken ]

この記事に対するコメント


この記事に対するコメントの投稿














管理者にだけ表示を許可する



この記事に対するトラックバック
トラックバックURL
http://comfair2.blog24.fc2.com/tb.php/485-e937d379
この記事にトラックバックする(FC2ブログユーザー)