Skip to content

Instantly share code, notes, and snippets.

@yyamasak
Created November 28, 2019 08:07
Show Gist options
  • Save yyamasak/1b0aa9a0e2e54dca66c5bc31778fafd4 to your computer and use it in GitHub Desktop.
Save yyamasak/1b0aa9a0e2e54dca66c5bc31778fafd4 to your computer and use it in GitHub Desktop.
Get Windows DPI scaling factor
package require Ffidl
if {$::tcl_platform(osVersion) >= 6.3} {
namespace eval USER32 {
set MONITOR_DEFAULTTONULL 0x00
set MONITOR_DEFAULTTOPRIMARY 0x01
set MONITOR_DEFAULTTONEAREST 0x02
ffidl::callout MonitorFromWindow {int int} int [ffidl::symbol user32.dll MonitorFromWindow]
}
namespace eval SHCore {
set MDT_EFFECTIVE_DPI 0
set MDT_ANGULAR_DPI 1
set MDT_RAW_DPI 2
set MDT_DEFAULT $MDT_EFFECTIVE_DPI
ffidl::callout GetDpiForMonitor {pointer int pointer-var pointer-var} int [ffidl::symbol SHCore.dll GetDpiForMonitor]
}
proc get_dpi {MONITOR_DPI_TYPE} {
set hwnd [winfo id .]
set hmon [USER32::MonitorFromWindow $hwnd $USER32::MONITOR_DEFAULTTONEAREST]
set bxdpi [binary format i 0]
set bydpi [binary format i 0]
SHCore::GetDpiForMonitor $hmon $MONITOR_DPI_TYPE bxdpi bydpi
binary scan $bxdpi iu xdpi
binary scan $bydpi iu ydpi
list $xdpi $ydpi
}
proc get_effective_dpi_factor {} {
lassign [get_dpi $SHCore::MDT_EFFECTIVE_DPI] xdpi ydpi
expr {$xdpi / 96.0}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment