概要
スタンアロンで実行時にWindowのタイトルバーを消す
サンプル
public class Sample : MonoBehaviour
{
// ウィンドウクラス名及びウィンドウ名
private const string CLASS_NAME = "UnityWndClass";
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindowByCaption(string className, string lpWindowName);
int GWL_STYLE = -16;
int SWP_NOSIZE = 1;
long WS_POPUP = 0x80000000;
long WS_VISIBLE = 0x10000000;
private IntPtr m_hWnd;
private void Awake(){
// 3秒後(最低1秒)に処理を実行する
// こうしないと、上手くいく場合といかない場合がある
// Unityのウィンドウ設定処理とかちあうから?
// ※UniRxを使わない場合はコルーチン等を利用して3秒(最低1秒)秒待ってから設定を行うと良い
Observable
.Timer(TimeSpan.FromMilliseconds(3000.0f))
.TakeUntilDestroy(this).Subscribe(_ => {
var handle = FindWindowByCaption(CLASS_NAME, Application.productName);
long windowStyle = WS_POPUP | WS_VISIBLE;
SetWindowLong(handle, GWL_STYLE, windowStyle)
// 必要ならウィンドウサイズの指定とかもここでやる
}
}
}
注意点
- タイトルバーを消した後に「Screen.SetResolution」を呼び出すとタイトルバーが復活する。
- 前回、フルスクリーンモードで実行していた場合は正しく動作しない”場合がある”
対処として、実行時のコマンドラインオプションでFullScreenModeをOFFにする
-screen-fullscreen 0
おまけ(Windowの最大化、最小化)
[DllImport("user32.dll", SetLastError = true)]
private static extern int PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsZoomed(IntPtr hwnd);
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_MAXIMIZE = 0xF030;
public const int SC_RESTORE = 0xF120;
void ToggleZoom()
{
if (IsZoomed(m_hWnd))
{
PostMessage(m_hWnd, WM_SYSCOMMAND, new IntPtr(SC_RESTORE), IntPtr.Zero);
}
else
{
PostMessage(m_hWnd, WM_SYSCOMMAND, new IntPtr(SC_MAXIMIZE), IntPtr.Zero);
}
}