因為我有設置字體大小啊
正確用法為:你在目前的模式下,就應該要調整好文字、控件大小了
編譯以後,會隨著縮放~
如一開始不正確,縮放後還是不會正常的...
#Region 編譯資訊
;;使用管理員權限
#RequireAdmin
;;無系統匣圖示
;;#NoTrayIcon
;;https://www.autoitscript.com/wiki/AutoIt3Wrapper_Directives
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Icon=l2.ico
#AutoIt3Wrapper_UseX64=n
;;評論欄
#AutoIt3Wrapper_Res_Comment=
;;描述字段
#AutoIt3Wrapper_Res_Description=
;;文件版本
#AutoIt3Wrapper_Res_Fileversion=3.0.0.0
;;產品版本,默認是使用的 AutoIt3 版本。
;;#AutoIt3Wrapper_Res_ProductVersion=3.0.0.0
;;版權領域
#AutoIt3Wrapper_Res_LegalCopyright=Copyright © 2022
;;資源語言代碼
#AutoIt3Wrapper_Res_Language=1028
;;請求可用的最高權限
#AutoIt3Wrapper_Res_requestedExecutionLevel=highestAvailable
;;Dpi感知
#AutoIt3Wrapper_Res_HiDpi=y
#cs
#AutoIt3Wrapper_res_Compatibility=Windows7
#AutoIt3Wrapper_Res_Field=AutoIt 版本|%AutoItVer%
#AutoIt3Wrapper_Res_Field=短日期格式的 PC 日期|%date%
#AutoIt3Wrapper_Res_Field=長日期格式的 PC 日期|%longdate%
#AutoIt3Wrapper_Res_Field=PC 時間格式|%time%
#ce
#EndRegion 編譯資訊
;;原始碼檢錯
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 6 -w 7
;;#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
#cs
Au3Check [-q] [-d] [-w[-] n]... [-v[-] n]... [-I dir]... file.au3
-q : 靜態 (僅輸出錯誤/警告)
-d : 等價於 Opt("MustDeclareVars", 1)
-w 1: 已包含檔案 (打開)
-w 2: 缺少 # comments-end (打開)
-w 3: 變數已聲明 (關閉)
-w 4: 局部變數在全域範圍內使用 (關閉)
-w 5: 局部變數已聲明, 但未使用 (關閉)
-w 6: 使用 Dim 時警告 (關閉)
-w 7: 傳遞常數或表示式到 ByRef 時警告 (打開)
-I dir: 搜尋其他目錄的包含檔案
-v 1: 顯示包含路徑/檔案 (關閉)
-v 2: 顯示詞法標記 (關閉)
-v 3: 顯示未引用 UDF 和全域變數 (關閉)
退出程式碼:
0: 成功: 沒有錯誤或警告
1: 警告
2: 語法錯誤
3: 使用或輸入錯誤
#ce
#cs ____________________________________
Au3版本:3.3.14.5
SciTE版本:3.6.6
腳本作者:
腳本功能:
參考代碼:
#ce _______________腳本開始_________________
#Region 載入Fuction
#include <WinAPI.au3>
#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <File.au3>
#EndRegion 載入Fuction
#Region 前置工作
;;判斷是否以管理員身份執行
Local $Admin
If IsAdmin() Then
$Admin = "目前以管理員權限執行"
Else
$Admin = "目前非管理員權限執行,可能造成部份功能無法使用。"
MsgBox(262144 + 48, '權限提示', $Admin)
EndIf
;;切換起始目錄工作目錄到腳本所在路徑
FileChangeDir(@ScriptDir)
;;防止重複執行
_Singleton(@ScriptName)
#EndRegion 前置工作
#Region 宣告變數
Global $HiDpi
Global $MainGuiWidth = 700 ;;介面寬
Global $MainGuiLength = 500 ;;介面高
;;全局字體及大小(備用字體:'Comic Sans Ms'、'YaHei Consolas Hybrid')
Global $FontSize = 12, $Font_Xing = 400, $Font_Var = Default, $Font_Quality = 5
Global $Font_Name = 'Microsoft JhengHei'
Global $Title = '最佳程式碼實現 DPI-自適應縮放'
Global $Version = 'v1.2.0.0 2022.11.04'
Global $Author = ''
Global $Copyright = $Author & ' 2022 Unlicense.' ;;版權宣告
Global Const $FileLogPath = @ScriptDir & '\_Log\' & @YEAR & "_" & @MON & "_" & @MDAY & ".log"
Global $AutoIt_Error = ObjEvent("AutoIt.Error", "_AutoIt_Error") ;;攔截Com錯誤
Global $LogFlag = 'N' ;;啟用Log模式
#EndRegion 宣告變數
#Region Opt模式
Opt("GUIResizeMode", 1) ;;設定GUI默認縮放模式,afan版主提供
Opt("GUIOnEventMode", 1) ;;使用事件模式
#EndRegion Opt模式
;;判斷是否編譯,使用不同策略設定和取得DPI
If @Compiled Then
If _IsProcessDPIAware() Then
$HiDpi = _GetDpi()
Else
$HiDpi = 1
EndIf
Else
_SetProcessDPIAware()
$HiDpi = _GetDpi()
EndIf
Global $MainGUI = GUICreate($Title & ' ' & $Version, $MainGuiWidth, $MainGuiLength, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetFont($FontSize, $Font_Xing, $Font_Var, $Font_Name, $MainGUI, $Font_Quality) ;;全局字型
GUISetBkColor(0xE6E6E6) ;;將變更背景色彩
GUICtrlCreateLabel('最佳程式碼實現 DPI-自適應縮放', 10, 10)
GUICtrlCreateLabel('', 75, 30, 1, 300)
GUICtrlSetBkColor(-1, 0x003366)
GUICtrlCreateButton('按钮1', 10, 30)
GUICtrlCreateButton('按钮2', 100, 30, 100, 30)
GUICtrlCreateCheckbox('选择1', 10, 100)
GUICtrlCreateCheckbox('选择2', 100, 100, 100, 30)
DPI_Zoom($MainGUI, $HiDpi) ;;以Dpi進行視窗調整
GUISetState(@SW_SHOW, $MainGUI)
;;GUISetState(@SW_LOCK, $MainGUI) ;;鎖定
;;GUISetState(@SW_UNLOCK, $MainGUI) ;;解除鎖定
While 1
Sleep(500)
WEnd
#Region 基本函式
Func _Exit() ;;離開軟體
If MsgBox(262144+36, '', '確定要退出嗎?', 0, $MainGUI) = 6 Then
Exit
EndIf
EndFunc
Func _AutoIt_Error($oError)
If $LogFlag <> 'Y' Then Return
Local $errinfo = ""
;;Do anything here.
$errinfo = @ScriptName & " (" & $oError.scriptline & ") AutoIt.Error : ==> COM Error intercepted !" & @CRLF & '---------------------' & @CRLF & _
@TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
@TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
@TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
@TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
@TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
@TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
@TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
@TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
@TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF
_log($errinfo)
EndFunc ;==>_AutoIt_Error
Func _log($text)
_FileWriteLog($FileLogPath, $text)
EndFunc ;==>_log
#EndRegion 基本函式
#Region DPI函式
Func DPI_Zoom($hGUI, $hDpi) ;;縮放GUI,bfgxp提供
GUISetState(@SW_HIDE, $hGUI)
Local $aPos = WinGetPos($hGUI)
WinMove($hGUI, "", $aPos[0] - ($aPos[2] * $hDpi-$aPos[2])/2, $aPos[1] - ( $aPos[3] * $hDpi- $aPos[3])/2, $aPos[2] * $hDpi, $aPos[3] * $hDpi)
#cs
1.GUICreate建立的窗口大小是不包含邊框的,而WinMove改變窗口大小卻是包含邊框的。所以在WinMove之前,最好用WinGetPos獲取包含邊框的真實窗口大小,以便WinMove按比例放大
2.字型放大,經過我在不同DPI中的測試,最佳為 原字號 * (($HiDpi - 1) / 64 + 1)
另外提示愛玩的朋友:
WinMove之前的GUISetState(@SW_HIDE)很重要,沒有這個窗口中的控制項不會自動調整大小與位置
#ce
EndFunc
Func _SetProcessDPIAware()
Local $aRet
If @OSBuild < 9600 Then ;;判斷系統版本 win8.1(9600)
$aRet = DllCall("User32.dll", "BOOL", "IsProcessDPIAware") ;檢查處理程序是否支援DPI感知
Return $aRet[0]
If $aRet[0] = False Then
$aRet = DllCall('User32.dll', 'BOOL', 'SetProcessDPIAware') ;設定:系統DPI感知
If @error Then Return SetError(@error, 0, False)
Return
EndIf
Else
$aRet = DllCall("Shcore.dll", "BOOL", "GetProcessDpiAwareness", "HWND", 0, "int*", 0) ;檢查處理程序是否支援DPI感知
If @error Then Return SetError(@error, 0, False)
If $aRet[2] <> 2 Then ;;0:不支援DPI感知,1:系統DPI感知,2:監視器DPI感知
$aRet = DllCall("Shcore.dll", "BOOL", "SetProcessDpiAwareness", "int", 2) ;設定:監視器DPI感知
If @error Then Return SetError(@error, 0, False)
Return
EndIf
EndIf
EndFunc ;==>_SetProcessDPIAware
Func _GetDpi()
Local $iDPI
If @OSBuild < 9600 Then ;;判斷系統版本 win8.1(9600)
$iDPI = RegRead("HKCU\Control Panel\Desktop\WindowMetrics", "AppliedDPI")
Else
Local $hWnd = _WinAPI_MonitorFromWindow(_WinAPI_GetDesktopWindow())
Local $aRet = DllCall("Shcore.dll", "BOOL", "GetDpiForMonitor", "HANDLE", $hWnd, "int", 0, "int*", 0, "int*", 0)
If @error Then Return SetError(@error, 0, False)
$iDPI = $aRet[3]
EndIf
If $iDPI < 96 Or $iDPI > 480 Then $iDPI = 96
Return $iDPI / 96
EndFunc ;==>_GetDpi
Func _IsProcessDPIAware()
Local $aRet, $aDpi = 0
If @OSBuild < 9600 Then ;;判斷系統版本 win8.1(9600)
$aRet = DllCall("User32.dll", "BOOL", "IsProcessDPIAware") ;檢查處理程序是否支援DPI感知
If @error Then Return SetError(@error, 0, False)
$aDpi = $aRet[0]
Else
$aRet = DllCall("Shcore.dll", "BOOL", "GetProcessDpiAwareness", "HWND", 0, "int*", 0) ;檢查處理程序是否支援DPI感知
If @error Then Return SetError(@error, 0, False)
$aDpi = $aRet[2]
EndIf
Return $aDpi
EndFunc ;==>_IsProcessDPIAware
#EndRegion DPI函式
|