画面サイズとDPIを得る方法(WindowsかX環境下で)
世の中にはWindowsか、Xのある環境しか無いと思っている恐しいコード。切り分けするためのマクロがわからんかった。
#ifdef WIN32 #include <windows.h> #else #include <Xlib.h> #endif typedef struct ScreenInfo ScreenInfo; struct ScreenInfo{ int width_px, height_px; int width_mm, height_mm; int dpi_width, dpi_height; }; ScreenInfo *getScreenInfo(ScreenInfo *info) { #ifdef WIN32 HDC dc; dc = GetDC(GetDesktopWindow()); if(dc == NULL) return NULL; info->width_px = GetDeviceCaps(dc, HORZRES); info->height_px = GetDeviceCaps(dc, VERTRES); info->width_mm = GetDeviceCaps(dc, HORZSIZE); info->height_mm = GetDeviceCaps(dc, VERTSIZE); ReleaseDC(GetDesktopWindow(), dc); #else Display *display = XOpenDisplay(NULL); if(display == NULL) return NULL; info->width_px = DisplayWidth(DEFAULT_DISPLAY, DefaultScreen(display)); info->height_px = DisplayHeight(DEFAULT_DISPLAY, DefaultScreen(display)); info->width_mm = DisplayWidthMM(DEFAULT_DISPLAY, DefaultScreen(display)); info->height_mm = DisplayHeightMM(DEFAULT_DISPLAY, DefaultScreen(display)); XCloseDisplay(display); #endif info->dpi_width = (info->width_px*254 + info->width_mm*5) / info->width_mm*10; info->dpi_height = (info->height_px*254 + info->height_mm*5) / info->height_mm*10; return info; }