⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 devcaps2.c

📁 有关C语言的控制打印机的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------
   DEVCAPS2.C -- Displays Device Capability Information (Version 2)
                 (c) Charles Petzold, 1996
  ------------------------------------------------------------------*/

#include <windows.h>
#include <string.h>
#include <stdio.h>
#include "devcaps2.h"

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void DoBasicInfo    (HDC, HDC, int, int) ;
void DoOtherInfo    (HDC, HDC, int, int) ;
void DoBitCodedCaps (HDC, HDC, int, int, int) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     static char szAppName[] = "DevCaps2" ;
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;

	 wndclass.cbSize        = sizeof (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  = szAppName ;
     wndclass.lpszClassName = szAppName ;
	 wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;

     hwnd = CreateWindow (szAppName, NULL,
                          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 msg, WPARAM wParam, LPARAM lParam)
     {
     static char   szDevice[32], szWindowText[64] ;
     static int    n, cxChar, cyChar, 
		           nCurrentDevice = IDM_SCREEN,
                   nCurrentInfo   = IDM_BASIC ;
     static DWORD  dwNeeded, dwReturned ;
     static LPPRINTER_INFO_5 pinfo5 ;

     DWORD         i ;
     HDC           hdc, hdcInfo ;
     HMENU         hMenu ;
     PAINTSTRUCT   ps ;
     TEXTMETRIC    tm ;
     HANDLE        hPrint ;

     switch (msg)
          {
          case WM_CREATE :
               hdc = GetDC (hwnd) ;
               SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
               GetTextMetrics (hdc, &tm) ;
               cxChar = tm.tmAveCharWidth ;
               cyChar = tm.tmHeight + tm.tmExternalLeading ;
               ReleaseDC (hwnd, hdc) ;

               lParam = 0 ;
                                                  // fall through
          case WM_WININICHANGE :
               if (lParam != 0 && lstrcmp ((PSTR) lParam, "devices") != 0)
                    return 0 ;

               hMenu = GetSubMenu (GetMenu (hwnd), 0) ;

               while (GetMenuItemCount (hMenu) > 1)
                    DeleteMenu (hMenu, 1, MF_BYPOSITION) ;

               // Get a list of all local and remote printers
               // 
               // First, find out how large an array we need; this
               //   call will fail, leaving the required size in dwNeeded
               EnumPrinters (PRINTER_ENUM_LOCAL, 
                             NULL, 5, (LPBYTE) "", 0, &dwNeeded, &dwReturned) ;

               // Next, allocate space for PRINTER_INFO_5 array
               if (pinfo5)
                    HeapFree (GetProcessHeap (), 0, pinfo5) ;
               pinfo5 = (LPPRINTER_INFO_5) HeapAlloc (GetProcessHeap (), 
                                              HEAP_NO_SERIALIZE, dwNeeded) ;

               // Last, fill allocated PRINTER_INFO_5 array
               if (!pinfo5 || !EnumPrinters (PRINTER_ENUM_LOCAL, 
                                    NULL, 5, (LPBYTE) pinfo5, dwNeeded, 
                                    &dwNeeded, &dwReturned))
                    {
                    MessageBox (hwnd, "Could not enumerate printers!",
                                                  NULL, MB_ICONSTOP) ;
                    DestroyWindow (hwnd) ;
                    return 0 ;
                    }

               for (i = 0, n = IDM_SCREEN + 1 ; i < dwReturned ; i++, n++)
                    {
                    AppendMenu (hMenu, n % 16 ? 0 : MF_MENUBARBREAK, n, 
                                                  pinfo5->pPrinterName) ;
                    pinfo5++ ;
                    }

               AppendMenu (hMenu, MF_SEPARATOR, 0, NULL) ;
               AppendMenu (hMenu, 0, IDM_DEVMODE, "Properties") ;

               wParam = IDM_SCREEN ;
                                                  // fall through
          case WM_COMMAND :
               hMenu = GetMenu (hwnd) ;

               if (wParam < IDM_DEVMODE)          // IDM_SCREEN & Printers
                    {
                    CheckMenuItem (hMenu, nCurrentDevice, MF_UNCHECKED) ;
                    nCurrentDevice = wParam ;
                    CheckMenuItem (hMenu, nCurrentDevice, MF_CHECKED) ;
                    }
               else if (wParam == IDM_DEVMODE)    // "Properties" selection
                    {
                    GetMenuString (hMenu, nCurrentDevice, szDevice,
                                   sizeof (szDevice), MF_BYCOMMAND) ;

                    if (OpenPrinter (szDevice, &hPrint, NULL))
                         {
                         PrinterProperties (hwnd, hPrint) ;
                         ClosePrinter (hPrint) ;
                         }
                    }
               else                               // info menu items
                    {
                    CheckMenuItem (hMenu, nCurrentInfo, MF_UNCHECKED) ;
                    nCurrentInfo = wParam ;
                    CheckMenuItem (hMenu, nCurrentInfo, MF_CHECKED) ;
                    }
               InvalidateRect (hwnd, NULL, TRUE) ;
               return 0 ;

          case WM_INITMENUPOPUP :
               if (lParam == 0)
                    EnableMenuItem (GetMenu (hwnd), IDM_DEVMODE,
                         nCurrentDevice == IDM_SCREEN ?
                              MF_GRAYED : MF_ENABLED) ;
               return 0 ;

          case WM_PAINT :
               strcpy (szWindowText, "Device Capabilities: ") ;
          
               if (nCurrentDevice == IDM_SCREEN)
                    {
                    strcpy (szDevice, "DISPLAY") ;
                    hdcInfo = CreateIC (szDevice, NULL, NULL, NULL) ;
                    }
               else
                    {
                    hMenu = GetMenu (hwnd) ;
                    GetMenuString (hMenu, nCurrentDevice, szDevice,
                                   sizeof (szDevice), MF_BYCOMMAND) ;
                    hdcInfo = CreateIC (NULL, szDevice, NULL, NULL) ;
                    }

               strcat (szWindowText, szDevice) ;
               SetWindowText (hwnd, szWindowText) ;

               hdc = BeginPaint (hwnd, &ps) ;
               SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;

               if (hdcInfo)
                    {
                    switch (nCurrentInfo)
                         {
                         case IDM_BASIC :
                              DoBasicInfo (hdc, hdcInfo, cxChar, cyChar) ;
                              break ;

                         case IDM_OTHER :
                              DoOtherInfo (hdc, hdcInfo, cxChar, cyChar) ;
                              break ;

                         case IDM_CURVE :
                         case IDM_LINE :
                         case IDM_POLY :
                         case IDM_TEXT :
                              DoBitCodedCaps (hdc, hdcInfo, cxChar, cyChar,
                                   nCurrentInfo - IDM_CURVE) ;
                              break ;
                         }
                    DeleteDC (hdcInfo) ;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -