質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2861閲覧

【Unity】タイトルバーを非表示にする

aaachi

総合スコア69

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2019/05/21 14:15

いつもお世話になっております。

Unity で タイトルバーを非表示にする スクリプトを実装したところ、
タイトルバーの高さが短くなったものの、完全に消えきることが出来ません。

詳しい方いられましたら、ご教示頂けますと幸いです。

###実装したいこと
exeで
・タイトルバーの非表示
・位置をディスプレイの左上端にする

###実装スクリプト
こちらのスクリプトを引っ張ってきました。
https://qiita.com/gatosyocora/items/b9bc12b4d56ac763e8c0
http://tvmllab.com/unity%E3%81%A7dll%E3%82%92%E4%BD%BF%E3%81%86/

①WindowsControl.cs

C#

1using UnityEngine; 2using System.Collections; 3 4using System; 5using System.Runtime.InteropServices; 6 7namespace Utils { 8 9 // ここからコードを拝借させてもらいました! 10 // http://answers.unity3d.com/questions/13523/is-there-a-way-to-set-the-position-of-a-standalone.html 11 // http://stackoverflow.com/questions/2825528/removing-the-title-bar-of-external-application-using-c-sharp 12 13 /* 14 * 注意点! 15 * 実行するアプリケーションはfullscreenではなく、windowedの状態にしておく必要がある。 16 */ 17 public class WindowControl : MonoBehaviour { 18 19 [SerializeField] string windowName = "demo"; 20 [SerializeField] int x = 0; 21 [SerializeField] int y = 0; 22 [SerializeField] int width = 1920; 23 [SerializeField] int height = 1080; 24 [SerializeField] bool hideTitleBar = true; 25 26 [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 27 private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); 28 29 [DllImport("user32.dll", EntryPoint = "FindWindow")] 30 public static extern IntPtr FindWindow(System.String className, System.String windowName); 31 32 // Sets window attributes 33 [DllImport("user32.dll")] 34 public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 35 36 // Gets window attributes 37 [DllImport("user32.dll")] 38 public static extern int GetWindowLong(IntPtr hWnd, int nIndex); 39 40 // assorted constants needed 41 public static int GWL_STYLE = -16; 42 public static int WS_CHILD = 0x40000000; //child window 43 public static int WS_BORDER = 0x00800000; //window with border 44 public static int WS_DLGFRAME = 0x00400000; //window with double border but no title 45 public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 46 47 void Awake () { 48 var window = FindWindow(null, windowName); 49 if(hideTitleBar) { 50 int style = GetWindowLong(window, GWL_STYLE); 51 SetWindowLong(window, GWL_STYLE, (style & ~WS_CAPTION)); 52 } 53 SetWindowPos(window, 0, x, y, width, height, width * height == 0 ? 1 : 0); 54 } 55 56 } 57 58}

②user32.dll  を Pluginsフォルダにおく

C#

1extern "C" __declspec(dllexport) int Add(int a, int b) 2{ 3 return(a + b); 4}

###現在起きている状況
イメージ説明
上図のように、タイトルバーが少し残ってしまいます。

 

何卒宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

「Project Settings...」の「Player」→「Resolution and Presentation」→「Standalone Player Options」の「Resizable Window」のチェックボックスがオンの場合、オフにしてみるのはどうでしょうか。

ただし、私の試したところですと上部のみならず外周全部が消え、枠なしウィンドウになってしまいました(そしてウィンドウサイズも変えられません...)。

もしリサイズできるようにしたい場合、Resizable Windowはオンのままで、スクリプトを下記のようにしてみるとどうでしょうか?
すみませんが、コードを自動整形した結果ご質問者さんのコードと若干レイアウトが変わってしまいました。
やっていることとしては単にウィンドウ上端をちょっとずらしているだけです。ずらし量は多分6ピクセルでちょうどいいと思うのですが、適宜調整してみてください。
また、私はWindows APIを直にいじることには慣れておらず、はたしてこれで妥当なのかちょっと自信がありませんので、異常がないか詳しく検証していただくことをおすすめします...

C#

1using System; 2using System.Runtime.InteropServices; 3using UnityEngine; 4 5namespace Utils 6{ 7 public class WindowControl : MonoBehaviour 8 { 9 // assorted constants needed 10 public static int GWL_WNDPROC = -4; 11 public static int GWL_STYLE = -16; 12 public static int WS_CHILD = 0x40000000; //child window 13 public static int WS_BORDER = 0x00800000; //window with border 14 public static int WS_DLGFRAME = 0x00400000; //window with double border but no title 15 public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar 16 public static uint WM_NCCALCSIZE = 0x0083; 17 18 [SerializeField] private string windowName = "demo"; 19 [SerializeField] private int x; 20 [SerializeField] private int y; 21 [SerializeField] private int width = 1920; 22 [SerializeField] private int height = 1080; 23 [SerializeField] private bool hideTitleBar = true; 24 25 private Func<IntPtr, uint, IntPtr, IntPtr, IntPtr> customWndProcDelegate; 26 private IntPtr defaultWndProcPtr; 27 28 private void Awake() 29 { 30 #if UNITY_STANDALONE_WIN 31 var window = FindWindow(null, this.windowName); 32 if (this.hideTitleBar) 33 { 34 var style = GetWindowLong(window, GWL_STYLE); 35 SetWindowLong(window, GWL_STYLE, style & ~WS_CAPTION); 36 37 // ウィンドウプロシージャを独自のメソッドに差し替えて、返ってきた本来のプロシージャを覚えておく 38 this.customWndProcDelegate = this.CustomWndProc; 39 this.defaultWndProcPtr = SetWindowLongPtr( 40 window, 41 GWL_WNDPROC, 42 Marshal.GetFunctionPointerForDelegate(this.customWndProcDelegate)); 43 } 44 45 SetWindowPos( 46 window, 47 0, 48 this.x, 49 this.y, 50 this.width, 51 this.height, 52 (this.width * this.height) == 0 ? 1 : 0); 53 #endif 54 } 55 56 #if UNITY_STANDALONE_WIN 57 // 独自のウィンドウプロシージャ 58 private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) 59 { 60 // まずデフォルトのプロシージャを実行し、結果を受け取る 61 var result = CallWindowProc(this.defaultWndProcPtr, hWnd, msg, wParam, lParam); 62 63 // 非クライアント領域サイズ計算メッセージが飛んできたら... 64 if ((msg == WM_NCCALCSIZE) && ((int)wParam == 1)) 65 { 66 // 上端を6ピクセルずらす 67 var ncCalcSizeParams = Marshal.PtrToStructure<NCCALCSIZE_PARAMS>(lParam); 68 ncCalcSizeParams.rgrc[0].Top -= 6; 69 Marshal.StructureToPtr(ncCalcSizeParams, lParam, false); 70 } 71 72 return result; 73 } 74 #endif 75 76 [StructLayout(LayoutKind.Sequential)] 77 public struct RECT 78 { 79 public int Left, Top, Right, Bottom; 80 } 81 82 [StructLayout(LayoutKind.Sequential)] 83 public struct WINDOWPOS 84 { 85 public IntPtr hwnd; 86 public IntPtr hwndInsertAfter; 87 public int x; 88 public int y; 89 public int cx; 90 public int cy; 91 public uint flags; 92 } 93 94 [StructLayout(LayoutKind.Sequential)] 95 private struct NCCALCSIZE_PARAMS 96 { 97 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] 98 public readonly RECT[] rgrc; 99 public WINDOWPOS lppos; 100 } 101 102 #if UNITY_STANDALONE_WIN 103 [DllImport("user32.dll", EntryPoint = "SetWindowPos")] 104 private static extern bool SetWindowPos( 105 IntPtr hWnd, 106 int hWndInsertAfter, 107 int x, 108 int Y, 109 int cx, 110 int cy, 111 int wFlags); 112 113 [DllImport("user32.dll", EntryPoint = "FindWindow")] 114 public static extern IntPtr FindWindow(string className, string windowName); 115 116 // Sets window attributes 117 [DllImport("user32.dll")] 118 public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 119 120 // Gets window attributes 121 [DllImport("user32.dll")] 122 public static extern int GetWindowLong(IntPtr hWnd, int nIndex); 123 124 [DllImport("user32.dll")] 125 public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong); 126 127 // 使っていないが、一応Setと対になるGetも宣言 128 [DllImport("user32.dll")] 129 private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); 130 131 [DllImport("user32.dll")] 132 private static extern IntPtr CallWindowProc( 133 IntPtr lpPrevWndFunc, 134 IntPtr hWnd, 135 uint Msg, 136 IntPtr wParam, 137 IntPtr lParam); 138 #endif 139 } 140}

投稿2019/05/21 22:42

編集2019/05/21 22:55
Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

aaachi

2019/05/22 02:38

Bongo様 いつもお世話になっております。 ありがとうございます! 今回、枠も消し、リサイズを受け付けないようにしたかったので、 「Resizable Window」のチェックボックス オフにすることで 無事解決致しました! リサイズしたくなった場合にも スクリプト参考にさせて頂きます! 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問