第26周-window程序设计(基础篇)-第5章(图形基础)-DEVCAPS1.C

1、DEVCAPS1.C

/*DEVCAPS1.C*/
#include<windows.h>
#define NUMLINE(int) (sizeof devcaps/sizeof devcaps[0]) //定义数组元素个数
struct
 {
   int iIndex;
   TCHAR*szLabel;
   TCHAR*szDesc; //这个有什么用?
  }
devcaps[] = 
 {
   HORZSIZE,TEXT("HORZSIZE"),TEXT("Width in milimeters:"), //毫米宽
   VERTSIZE, TEXT ("VERTSIZE"),TEXT ("Height in millimeters:"),
   HORZRES, TEXT ("HORZRES"), TEXT ("Width in pixels:"), //像素宽
   VERTRES, TEXT ("VERTRES"), TEXT ("Height in raster lines:"),//光栅线高度
   BITSPIXEL, TEXT ("BITSPIXEL"),TEXT ("Color bits per pixel:"),//<span style="font-family: Arial, Helvetica, sans-serif;">//每像素颜色位数</span>
   PLANES, TEXT ("PLANES"), TEXT ("Number of color planes:"), //彩色平面数目
   NUMBRUSHES, TEXT ("NUMBRUSHES"), TEXT ("Number of device brushes:"), //设备刷数量
   NUMPENS, TEXT ("NUMPENS"), TEXT ("Number of device pens:"), 
   NUMMARKERS, TEXT ("NUMMARKERS"), TEXT ("Number of device markers:"),//设备标记数量
   NUMFONTS, TEXT ("NUMFONTS"), TEXT ("Number of device fonts:"),
   NUMCOLORS, TEXT ("NUMCOLORS"), TEXT ("Number of device colors:"),
   PDEVICESIZE, TEXT ("PDEVICESIZE"),TEXT ("Size of device structure:"),//设备结构的尺寸
   ASPECTX, TEXT ("ASPECTX"), TEXT ("Relative width of pixel:"),//相对宽度的像素
   ASPECTY, TEXT ("ASPECTY"), TEXT ("Relative height of pixel:"),
   ASPECTXY, TEXT ("ASPECTXY"), TEXT ("Relative diagonal of pixel:"),//相对对角线像素
   LOGPIXELSX, TEXT ("LOGPIXELSX"), TEXT ("Horizontal dots per inch:"),//每英寸水平点(典型值96)
   LOGPIXELSY, TEXT ("LOGPIXELSY"), TEXT ("Vertical dots per inch:"), //每英寸高度点<span style="font-family: Arial, Helvetica, sans-serif;">(典型值120)</span>
   SIZEPALETTE, TEXT ("SIZEPALETTE"),TEXT ("Number of palette entries:"),//调色盘条目
   NUMRESERVED, TEXT ("NUMRESERVED"),TEXT ("Reserved palette entries:"), //保留调色盘条目
   COLORRES, TEXT ("COLORRES"), TEXT ("Actual color resolution:")//当前颜色分辨率(该值表示一种色彩,指定了红绿蓝,即RGB色彩)
}

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
 {
   static TCHAR szAppName[] = TEXT ("DevCaps1") ;
   HWND hwnd ;
   MSG msg ;
   WNDCLASS wndclass ;

   wndclass.style = CS_HREDRAW | CS_VREDRAW ;
   wndclass.lpfnWndProc= WndProc ;
   wndclass.cbClsExtra = 0 ;
   wndclass.cbWndExtra = 0 ;
   wndclass.hInstance = hInstance ;
   wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
   wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
   wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
   wndclass.lpszMenuName= NULL ;
   wndclass.lpszClassName= szAppName ;

   if (!RegisterClass (&wndclass))
   {
     MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
          szAppName, MB_ICONERROR) ;
     return 0 ;
    }

   hwnd = CreateWindow (szAppName, TEXT ("Device Capabilities"),
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT,
          CW_USEDEFAULT, CW_USEDEFAULT,
          NULL, NULL, hInstance, NULL) ;

   ShowWindow (hwnd, iCmdShow) ;
   UpdateWindow (hwnd) ;

   while (GetMessage (&msg, NULL, 0, 0))
    {
       TranslateMessage (&msg) ;
       DispatchMessage (&msg) ;
     }
   return msg.wParam ;
  }

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
   static int cxChar, cxCaps, cyChar ;
   TCHAR szBuffer[10] ;
   HDC hdc ;
   int i ;
   PAINTSTRUCT ps ;
   TEXTMETRIC tm ;

   switch (message)
   {
    case WM_CREATE:
      hdc = GetDC (hwnd) ;
      GetTextMetrics (hdc, &tm) ;
      cxChar= tm.tmAveCharWidth ;
      cxCaps= (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; //width of capital letters 大写字母的宽度,先判断字母是否为变宽字体。是为低位1,否为0。
      cyChar= tm.tmHeight + tm.tmExternalLeading ;
      ReleaseDC (hwnd, hdc) ;
      return 0 ;

    case WM_PAINT:
      hdc = BeginPaint (hwnd, &ps) ;
      for (i = 0 ; i < NUMLINES ; i++)
       {
         TextOut ( hdc, 0, cyChar * i,
           devcaps[i].szLabel,
         lstrlen (devcaps[i].szLabel)) ;

         TextOut ( hdc, 14 * cxCaps, cyChar * i,
           devcaps[i].szDesc,
           lstrlen (devcaps[i].szDesc)) ;
           SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

         TextOut (hdc, 14*cxCaps+35*cxChar, cyChar*i, szBuffer,
           wsprintf (szBuffer, TEXT ("%5d"),
           GetDeviceCaps(hdc, devcaps[i].iIndex))) ;
           SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
       }
     EndPaint (hwnd, &ps) ;
     return 0 ;

   case WM_DESTROY:
     PostQuitMessage (0) ;
     return 0 ;
   }
  return DefWindowProc (hwnd, message, wParam, lParam) ;
 }

单词:Capabilities能力、dots点、resolution分辨率、actual当前、planes平面、marker、pixel像素、entry数目、palette调色板

编译代码:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>

#define NUMLINES (int)(sizeof devcaps/sizeof devcaps[0])

struct
 {
     int iIndex;
     TCHAR *szLable;
     TCHAR *szDesc;
 }
 devcaps[] =
 {
   HORZSIZE,TEXT("HORZSIZE"),TEXT("Width in milimeters:"), //毫米宽
   VERTSIZE, TEXT ("VERTSIZE"),TEXT ("Height in millimeters:"),
   HORZRES, TEXT ("HORZRES"), TEXT ("Width in pixels:"), //像素宽
   VERTRES, TEXT ("VERTRES"), TEXT ("Height in raster lines:"),//光栅线高度
   BITSPIXEL, TEXT ("BITSPIXEL"),TEXT ("Color bits per pixel:"),//<span style="font-family: Arial, Helvetica, sans-serif;">//每像素颜色位数</span>
   PLANES, TEXT ("PLANES"), TEXT ("Number of color planes:"), //彩色平面数目
   NUMBRUSHES, TEXT ("NUMBRUSHES"), TEXT ("Number of device brushes:"), //设备刷数量
   NUMPENS, TEXT ("NUMPENS"), TEXT ("Number of device pens:"),
   NUMMARKERS, TEXT ("NUMMARKERS"), TEXT ("Number of device markers:"),//设备标记数量
   NUMFONTS, TEXT ("NUMFONTS"), TEXT ("Number of device fonts:"),
   NUMCOLORS, TEXT ("NUMCOLORS"), TEXT ("Number of device colors:"),
   PDEVICESIZE, TEXT ("PDEVICESIZE"),TEXT ("Size of device structure:"),//设备结构的尺寸
   ASPECTX, TEXT ("ASPECTX"), TEXT ("Relative width of pixel:"),//相对宽度的像素
   ASPECTY, TEXT ("ASPECTY"), TEXT ("Relative height of pixel:"),
   ASPECTXY, TEXT ("ASPECTXY"), TEXT ("Relative diagonal of pixel:"),//相对对角线像素
   LOGPIXELSX, TEXT ("LOGPIXELSX"), TEXT ("Horizontal dots per inch:"),//每英寸水平点(典型值96)
   LOGPIXELSY, TEXT ("LOGPIXELSY"), TEXT ("Vertical dots per inch:"), //每英寸高度点<span style="font-family: Arial, Helvetica, sans-serif;">(典型值120)</span>
   SIZEPALETTE, TEXT ("SIZEPALETTE"),TEXT ("Number of palette entries:"),//调色盘条目
   NUMRESERVED, TEXT ("NUMRESERVED"),TEXT ("Reserved palette entries:"), //保留调色盘条目
   COLORRES, TEXT ("COLORRES"), TEXT ("Actual color resolution:")//当前颜色分辨率(该值表示一种色彩,指定了红绿蓝,即RGB色彩)
 };
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("Devcaps");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_HREDRAW|CS_VREDRAW;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Devcaps"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static int cxChar,cxCaps,cyChar;
    TCHAR szBuffer[10];
    HDC hdc;
    int i;
    PAINTSTRUCT ps;
    TEXTMETRIC tm;
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            hdc = GetDC(hwnd);
            GetTextMetrics(hdc,&tm);
            cxChar = tm.tmAveCharWidth;
            cxCaps = (tm.tmPitchAndFamily &1?3:2)*cxChar/2;
            cyChar = tm.tmHeight +tm.tmExternalLeading;
            ReleaseDC(hwnd,hdc);
            return 0;

        case WM_PAINT:
            hdc = BeginPaint(hwnd,&ps);
            for(i = 0;i< NUMLINES;i++)
            {
                TextOut(hdc,0,cyChar*i,devcaps[i].szLable,
                        lstrlen(devcaps[i].szLable));
                TextOut(hdc,14*cxCaps,cyChar*i,devcaps[i].szDesc,
                        lstrlen(devcaps[i].szDesc));
                        SetTextAlign(hdc,TA_RIGHT|TA_TOP);
                TextOut(hdc,14*cxCaps+35*cxChar,cyChar*i,szBuffer,
                        wsprintf(szBuffer,TEXT("%5d"), //注意这里与上面的第4个参数不同,它用wspringtf()函数,同时与缓冲区搭配
                        GetDeviceCaps(hdc,devcaps[i].iIndex)));
                        SetTextAlign(hdc,TA_LEFT|TA_TOP);
            }
        EndPaint(hwnd,&ps);
        return 0;

        case WM_DESTROY:
            PostQuitMessage(0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
想要获取电脑精确的硬件信息,可以通过调用操作系统提供的API来获取。下面以Windows操作系统为例,介绍如何获取一些常见的硬件信息: 1. 获取CPU信息 可以使用WMI (Windows Management Instrumentation)来获取CPU信息。以下是C#代码示例: ```csharp using System.Management; public static string GetCpuInfo() { string cpuInfo = ""; ManagementClass mc = new ManagementClass("win32_processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["Name"].Value.ToString(); break; } return cpuInfo; } ``` 2. 获取内存信息 可以使用GlobalMemoryStatusEx函数来获取内存信息。以下是C#代码示例: ```csharp using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] public struct MEMORYSTATUSEX { public uint dwLength; public uint dwMemoryLoad; public ulong ullTotalPhys; public ulong ullAvailPhys; public ulong ullTotalPageFile; public ulong ullAvailPageFile; public ulong ullTotalVirtual; public ulong ullAvailVirtual; public ulong ullAvailExtendedVirtual; } [DllImport("kernel32.dll")] public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); public static ulong GetTotalMemory() { MEMORYSTATUSEX memStatusEx = new MEMORYSTATUSEX(); memStatusEx.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); GlobalMemoryStatusEx(ref memStatusEx); return memStatusEx.ullTotalPhys; } ``` 3. 获取显卡信息 可以使用EnumDisplayDevices和GetDeviceCaps函数来获取显卡信息。以下是C#代码示例: ```csharp using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] public struct DISPLAY_DEVICE { public uint cb; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] public string DeviceName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceString; public uint StateFlags; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceID; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string DeviceKey; } [DllImport("user32.dll")] public static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags); [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex); public static string GetGraphicsCardInfo() { DISPLAY_DEVICE displayDevice = new DISPLAY_DEVICE(); displayDevice.cb = (uint)Marshal.SizeOf(typeof(DISPLAY_DEVICE)); EnumDisplayDevices(null, 0, ref displayDevice, 0); IntPtr hdc = CreateDC(displayDevice.DeviceName, null, null, IntPtr.Zero); int devCaps = GetDeviceCaps(hdc, 6); string graphicsCardInfo = string.Format("{0} (Memory: {1} MB)", displayDevice.DeviceString.Trim(), devCaps / 1024); return graphicsCardInfo; } ``` 需要注意的是,以上代码只是获取硬件信息的一种方式,不同的操作系统和硬件配置可能需要不同的API和方法来获取精确的硬件信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值