File:  [WindowsNT SDKs] / q_a / samples / printer / printer.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:29:19 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-jun-1992, HEAD
Microsoft Windows NT Build 297 06-28-1992

/******************************************************************************\
*
*  PROGRAM:     PRINTER.C
*
*  PURPOSE:     To demonstrate the printing of graphics & fonts in Win32.
*               Also allows a user to enumerate all system printers
*               (and graphically lists all info returned by EnumPrinters),
*               print various graphics & fonts to each one, and to query
*               the device capablities for each printer and the display.
*
*  FUNTIONS:    WinMain()          - register class, create windows,
*                                    msg loop
*               MainWndProc()      - processes main window msgs
*               InvalidateClient() - invalidates client area, excluding
*                                    toolbar
*
\******************************************************************************/

#include <windows.h>
#include "lookup.h"
#include "printer.h"
#include "paint.h"
#include "toolbar.h"
#include "vars.h"



/******************************************************************************\
*
*  FUNCTION:    WinMain (standard WinMain INPUTS/RETURNS)
*
*  GLOBAL VARS: hInst    - current program instance handle
*               hWndMain - handle of the main standard window
*
*  LOCAL VARS:  wc  - class initial;ization struct
*               msg - msg to get/dispatch
*
*  COMMENTS:    Standard WinMain: registers a class, creates a window,
*               creates a toolbar for the window, does a msg loop, then
*               terminates.
*
\******************************************************************************/

int APIENTRY WinMain (HANDLE hInstance, HANDLE hPrevInstance,
		      LPSTR  lpCmdLine,	int    nCmdShow)
{
  WNDCLASS  wc;
  MSG msg;

  wc.style	   = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = (WNDPROC)MainWndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance	   = hInstance;
  wc.hIcon	   = LoadIcon(hInstance, "printericon");
  wc.hCursor	   = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName  = "PrinterMenu";
  wc.lpszClassName = "PrinterSampleClass";

  if (!RegisterClass(&wc))
    return 0;

  hInst = hInstance;
  CreateWindow ("PrinterSampleClass", "PRINTER Sample App",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hInstance, NULL);

  if (!hWndMain)
      return 0;

  if (!CreateDialog (hInst, (LPTSTR)"PrinterToolbar", hWndMain,
		     (DLGPROC) ToolbarProc))
    MessageBox (NULL, "CreateDialog failed", "Err!", MB_OK);

  ShowWindow   (hWndMain, nCmdShow);
  UpdateWindow (hWndMain);

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



/******************************************************************************\
*
*  FUNCTION:	MainWndProc (standard window procedure INPUTS/RETURNS)
*
*  GLOBAL VARS: hInst             - current program instance handle
*		iMapMode	   - current map mode (eg. MM_TEXT)
*               dwGraphicsOptions - current graphics to draw in client
*                                   area
*
*  LOCAL VARS:	hMenu		 - handle of main menu
*		hMapModesSubMenu - handle of "Map modes" submenu
*		hGraphicsSubMenu - handle of "Graphics" submenu
*		wLastMapMode	 - menu id corresponding to last map mode
*
*  COMMENTS:	The value of the iMapMode and dwGraphicsOptions variables
*               determines the output when the Paint() routine is called
*               in the WM_PAINT procedure.
*
\******************************************************************************/

LONG APIENTRY MainWndProc (HWND hWnd, UINT message,UINT wParam,LONG lParam)
{
  static HMENU hMenu, hMapModesSubMenu, hGraphicsSubMenu;
  static WORD  wLastMapMode;

  switch (message)
  {
    case WM_COMMAND:

      switch (LOWORD(wParam))
      {
	case IDM_HIENGLISH:
	case IDM_HIMETRIC:
	case IDM_LOENGLISH:
	case IDM_LOMETRIC:
	case IDM_TWIPS:
	case IDM_ISOTROPIC:
	case IDM_ANISOTROPIC:
	case IDM_TEXT:

	  /********************************************************************\
          *  Uncheck the last map mode menuitem, check the new map mode
	  *  menuitem, set iMapMode according to menu id, and invalidate
          *  the client area so it'll be redrawn.
	  \********************************************************************/

          CheckMenuItem (hMapModesSubMenu, wLastMapMode,
                          MF_UNCHECKED | MF_BYCOMMAND);
          CheckMenuItem (hMapModesSubMenu, LOWORD(wParam),
                          MF_CHECKED | MF_BYCOMMAND);
	  iMapMode = iLookupWord (iMapModesLkUpW, LOWORD(wParam),
				  MAX_MAPMODES);
	  InvalidateClient ();
	  wLastMapMode = LOWORD(wParam);
	  break;

	case IDM_ARC:
	case IDM_ELLIPSE:
	case IDM_LINETO:
	case IDM_PIE:
	case IDM_POLYBEZIER:
	case IDM_POLYGON:
	case IDM_POLYLINE:
	case IDM_POLYPOLYGON:
	case IDM_RECTANGLE:
	case IDM_ROUNDRECT:
	case IDM_STRETCHBLT:
	case IDM_TEXTOUT:
	{
	  /********************************************************************\
          *  Retrieve the DWORD flag value for the particular menuitem,
          *  toggle (un/check) the menuitem, set/clear the flag in
          *  dwGraphicsOptions, and invalidate the client area so it'll
          *  be redrawn.
	  \********************************************************************/

	  DWORD dwGraphic = dwLookupWord (dwGraphicsLkUpW, LOWORD(wParam),
					  MAX_GRAPHICS);

          if (GetMenuState (hGraphicsSubMenu, LOWORD(wParam),
                                        MF_BYCOMMAND) & MF_CHECKED)
          { dwGraphicsOptions &= ~dwGraphic;
            CheckMenuItem (hGraphicsSubMenu, LOWORD(wParam),
                            MF_UNCHECKED | MF_BYCOMMAND);
          }
          else
          {
	    /******************************************************************\
            *  Clear/uncheck the ENUMFONTS flag/menuitem
	    \******************************************************************/

            dwGraphicsOptions &= ~ENUMFONTS;
            CheckMenuItem (hGraphicsSubMenu, IDM_ENUMFONTS,
                           MF_UNCHECKED | MF_BYCOMMAND);

            dwGraphicsOptions |= dwGraphic;
            CheckMenuItem (hGraphicsSubMenu, LOWORD(wParam),
                            MF_CHECKED | MF_BYCOMMAND);
	  }
	  InvalidateClient ();
	  break;
        }
	case IDM_ALLGRAPHICS:
	{
	  UINT i;

          /********************************************************************\
          *  Clear/uncheck the ENUMFONTS flag/menuitem, set/check all
          *  other graphics flags/menuitems, and invalidate the client
          *  area so it'll be repainted.
          \********************************************************************/

          dwGraphicsOptions = 0;
          CheckMenuItem (hGraphicsSubMenu, IDM_ENUMFONTS,
                          MF_UNCHECKED | MF_BYCOMMAND);

	  for (i = 0; i < MAX_GRAPHICS; i++)
          { CheckMenuItem (hGraphicsSubMenu, IDM_ARC + i,
                            MF_CHECKED | MF_BYCOMMAND);
	    dwGraphicsOptions |= dwLookupWord (dwGraphicsLkUpW,
                                               (WORD) (IDM_ARC+i),
					       MAX_GRAPHICS);
          }
	  InvalidateClient ();
	  break;
        }
	case IDM_NOGRAPHICS:
	{
	  UINT i;

          /********************************************************************\
          *  Clear/uncheck all graphics flags/menuitems, and invalidate
          *  the client area so it'll be repainted.
          \********************************************************************/

	  for (i = 0; i < MAX_GRAPHICS; i++)
            CheckMenuItem (hGraphicsSubMenu, IDM_ARC + i,
			    MF_UNCHECKED | MF_BYCOMMAND);

	  dwGraphicsOptions = 0;

	  InvalidateClient ();
	  break;
        }
	case IDM_ENUMFONTS:
	{
          /********************************************************************\
          *  Set/clear ENUMFONTS flag, toggle (un/check) menuitem, if
          *  checking IDM_ENUMFONTS then uncheck all other items, and
          *  invalidate the client area so it'll be repainted.
          \********************************************************************/

          if (GetMenuState (hGraphicsSubMenu, IDM_ENUMFONTS,
                                        MF_BYCOMMAND) & MF_CHECKED)
          { dwGraphicsOptions = 0;
            CheckMenuItem (hGraphicsSubMenu, IDM_ENUMFONTS,
                            MF_UNCHECKED | MF_BYCOMMAND);
          }
          else
	  { SendMessage (hWnd, WM_COMMAND, IDM_NOGRAPHICS, 0);

            dwGraphicsOptions = ENUMFONTS;
            CheckMenuItem (hGraphicsSubMenu, IDM_ENUMFONTS,
                            MF_CHECKED | MF_BYCOMMAND);
          }
	  InvalidateClient ();
	  break;
        }
      }
      break;

    case WM_PAINT:
    {
      PAINTSTRUCT ps;
      RECT rect;
      HDC  hdc = BeginPaint (hWnd, &ps);

      GetClientRect (hWnd, &rect);

      if (iMapMode == MM_TEXT || iMapMode == MM_ANISOTROPIC)

	SetViewportOrgEx (hdc, 0, TOOLBARHEIGHT, NULL);

      else

	SetViewportOrgEx (hdc, 0, rect.bottom, NULL);

      rect.bottom -= TOOLBARHEIGHT;
      SetMapMode (hdc, iMapMode);
      Paint (hdc, &rect);
      EndPaint (hWnd, &ps);
      break;
    }
    case WM_CREATE:

      hWndMain = hWnd;
      hMenu = GetMenu (hWnd);
      hMapModesSubMenu = GetSubMenu (hMenu, 0);
      hGraphicsSubMenu = GetSubMenu (hMenu, 1);
      SendMessage (hWnd, WM_COMMAND, (DWORD) MAKELONG (IDM_TEXT,0), 0);
      SendMessage (hWnd, WM_COMMAND,
                   (DWORD) MAKELONG (IDM_ALLGRAPHICS,0), 0);
      break;

    case WM_DESTROY:

      PostQuitMessage(0);
      break;

    default:

      return (DefWindowProc(hWnd, message, wParam, lParam));
  }
  return (NULL);
}



/******************************************************************************\
*
*  FUNCTION:    InvalidateClient
*
*  GLOBAL VARS: hWndMain - main app window handle
*
*  COMMENTS:    Eliminates the flashing of the toolbar when we redraw
*
\******************************************************************************/

void InvalidateClient ()
{
  RECT r;

  GetClientRect (hWndMain, &r);

  r.top += TOOLBARHEIGHT;

  InvalidateRect (hWndMain, &r, TRUE);
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.