前段時(shí)間做游戲pc端,要求exe運(yùn)行去窗口,還要固定分辨率
unity有設(shè)置分辨率的API:Screen.SetResolution
但是使用這個(gè)會(huì)跟win32 api沖突
為了去窗口只有全部使用win32 api
使用win32 api必須先定義,有點(diǎn)像使用c++ dll一樣
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
API很多,具體的可以到https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx查詢
去邊框
//SetWindowLong參數(shù)
public const int GWL_EXSTYLE= -20; //獲得擴(kuò)展窗口風(fēng)格。
public const int GWL_HINSTANCE = -6; //獲得應(yīng)用實(shí)例的句柄。。
public const int GWL_HWNDPARENT = -8; //如果父窗口存在,獲得父窗口句柄。
public const int GWL_ID = -12; //獲得窗口標(biāo)識(shí)。
public const int GWL_STYLE = -16; //獲得窗口風(fēng)格。
public const int GWL_USERDATA = -21; //獲得與窗口有關(guān)的32位值。每一個(gè)窗口均有一個(gè)由創(chuàng)建該窗口的應(yīng)用程序使用的32位值。。
public const int GWL_WNDPROC = -4; //獲得窗口過(guò)程的地址,或代表窗口過(guò)程的地址的句柄。必須使用CallWindowProc函數(shù)調(diào)用窗口過(guò)程。
//window style參數(shù)
public const int WS_BORDER = 1; //The window has a thin-line border.
public const int WS_POPUP = 0x800000; //The windows is a pop-up window. This style cannot be used with the WS_CHILD style.
public static IntPtr DisableWindowBorder(IntPtr hwnd)
{
if (!CheckPlatform())
return IntPtr.Zero;
return SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
}
設(shè)置分辨率和窗口位置
//ShowWindow參數(shù)
public const int SW_FORCEMINIMIZE = 11; //在WindowNT5.0中最小化窗口,即使擁有窗口的線程被掛起也會(huì)最小化。在從其他線程最小化窗口時(shí)才使用這個(gè)參數(shù)。
public const int SW_HIDE = 0; //隱藏窗口并激活其他窗口
public const int SW_MAXIMIZE = 3; //最大化指定的窗口
public const int SW_MINIMIZE = 6; //最小化指定的窗口并且激活在Z序中的下一個(gè)頂層窗口。
public const int SW_RESTORE = 9; //激活并顯示窗口。如果窗口最小化或最大化,則系統(tǒng)將窗口恢復(fù)到原來(lái)的尺寸和位置。在恢復(fù)最小化窗口時(shí),應(yīng)用程序應(yīng)該指定這個(gè)標(biāo)志。
public const int SW_SHOW = 5; //在窗口原來(lái)的位置以原來(lái)的尺寸激活和顯示窗口
public const int SW_SHOWDEFAULT = 10; //依據(jù)在STARTUPINFO結(jié)構(gòu)中指定的SW_FLAG標(biāo)志設(shè)定顯示狀態(tài),STARTUPINFO 結(jié)構(gòu)是由啟動(dòng)應(yīng)用程序的程序傳遞給CreateProcess函數(shù)的。nCmdShow=10。
public const int SW_SHOWMAXIMIZED = 3; //激活窗口并將其最大化。nCmdShow=3。
public const int SW_SHOWMINIMIZED = 2; //激活窗口并將其最小化。nCmdShow=2。
public const int SW_SHOWMINNOACTIVE = 7; //窗口最小化,激活窗口仍然維持激活狀態(tài)。nCmdShow=7。
public const int SW_SHOWNA = 8; //以窗口原來(lái)的狀態(tài)顯示窗口。激活窗口仍然維持激活狀態(tài)。nCmdShow=8。
public const int SW_SHOWNOACTIVATE = 4; //以窗口最近一次的大小和狀態(tài)顯示窗口。激活窗口仍然維持激活狀態(tài)。nCmdShow=4。
public const int SW_SHOWNORMAL = 1; //激活并顯示一個(gè)窗口。如果窗口被最小化或最大化,系統(tǒng)將其恢復(fù)到原來(lái)的尺寸和大小。應(yīng)用程序在第一次顯示窗口的時(shí)候應(yīng)該指定此標(biāo)志。nCmdShow=1。
const uint SWP_SHOWWINDOW = 0x0040;
public static bool SetWindowPosition(IntPtr hWnd, int winWidth, int winHeight)
{
if (!CheckPlatform())
return false;
//顯示器支持的所有分辨率
int i = Screen.resolutions.Length;
int resWidth = Screen.resolutions[i - 1].width;
int resHeight = Screen.resolutions[i - 1].height;
int winPosX = resWidth / 2 - winWidth / 2;
int winPosY = resHeight / 2 - winHeight / 2;
return SetWindowPos(hWnd, 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);
}
還可以做最大化最小化之類的
做成之后發(fā)現(xiàn)雖然這樣實(shí)現(xiàn)了去邊框,設(shè)置分辨率,但是是有缺陷的
一般的標(biāo)準(zhǔn)windows應(yīng)用程序可以在任務(wù)欄最小化,關(guān)閉,這樣設(shè)置了后這種操作就不行了
不知道什么原因
后來(lái)做exe安裝打包,有一個(gè)比較好的方法,一般exe需要制作安裝程序
程序不做這些設(shè)置,打包的時(shí)候給exe加上運(yùn)行參數(shù)xx.exe -popup
unity有個(gè)運(yùn)行無(wú)窗口的參數(shù),給exe加上就行了,完美解決