home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / object / selobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.6 KB  |  120 lines

  1. /*
  2. Function(s) demonstrated in this program: SelectObject
  3. Compiler version:5.1
  4. Description: This example creates a object (a pen), selects the object into
  5.              the hDC, and checks the return value.
  6. */
  7.  
  8. #include <windows.h>
  9. #include "selobj.h"
  10.  
  11. static char szAppName [] = "SELOBJ";
  12. static char szFuncName [] = "SelectObject";
  13.  
  14. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  15. HANDLE      hInstance;
  16. HANDLE      hPrevInstance;
  17. LPSTR       lpszCmdLine;
  18. int         nCmdShow;
  19. {
  20.  
  21.    HWND        hWnd;
  22.    WNDCLASS    wndclass;
  23.    MSG         msg;
  24.    HMENU       hMenu;
  25.  
  26.    if (!hPrevInstance)
  27.       {
  28.       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  29.       wndclass.lpfnWndProc   = WndProc;
  30.       wndclass.cbClsExtra    = 0;
  31.       wndclass.cbWndExtra    = 0;
  32.       wndclass.hInstance     = hInstance;
  33.       wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  34.       wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  35.       wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  36.       wndclass.lpszMenuName  = (LPSTR)"Select";
  37.       wndclass.lpszClassName = szAppName;
  38.  
  39.       if (!RegisterClass (&wndclass))
  40.          return FALSE;
  41.       }
  42.  
  43.    hMenu = LoadMenu ( hInstance, (LPSTR)"Select" );
  44.  
  45.    hWnd = CreateWindow (szAppName,            /* window class name       */
  46.                   szFuncName,                 /* window caption          */
  47.                   WS_OVERLAPPEDWINDOW,        /* window style            */
  48.                   CW_USEDEFAULT,              /* initial x position      */
  49.                   0,                          /* initial y position      */
  50.                   CW_USEDEFAULT,              /* initial x size          */
  51.                   0,                          /* initial y size          */
  52.                   NULL,                       /* parent window handle    */
  53.                   hMenu,                      /* window menu handle      */
  54.                   hInstance,                  /* program instance handle */
  55.                   NULL);                      /* create parameters       */
  56.  
  57.    ShowWindow (hWnd, nCmdShow);
  58.    UpdateWindow (hWnd);
  59.  
  60.    while (GetMessage(&msg, NULL, 0, 0))
  61.       {
  62.       TranslateMessage(&msg);
  63.       DispatchMessage(&msg);
  64.       }
  65.  
  66.    return (msg.wParam);     
  67. }
  68.  
  69. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  70. HWND     hWnd;
  71. unsigned iMessage;
  72. WORD     wParam;
  73. LONG     lParam;
  74. {
  75.    HANDLE   hObject;
  76.    HDC      hDC;
  77.    HPEN     hPen;
  78.  
  79.    switch(iMessage)
  80.    {
  81.    case WM_COMMAND:
  82.       switch ( wParam )
  83.          {
  84.          case IDM_SELECT:
  85.             hDC = GetDC ( hWnd );
  86.             hPen = CreatePen ( 0, 2, (DWORD)0xFF00FF );
  87.             if ( !hPen )
  88.                {
  89.                MessageBox ( hWnd, (LPSTR)"Pen was not created",
  90.                   (LPSTR)szFuncName, MB_OK );
  91.                return (0L);
  92.                }
  93.             
  94.             hObject = SelectObject ( hDC, hPen );
  95.             if ( hObject )
  96.                MessageBox ( hWnd, (LPSTR)"The object (a pen) was selected",
  97.                   (LPSTR)szFuncName, MB_OK );
  98.             else
  99.                MessageBox ( hWnd, (LPSTR)"The object (a pen) was not selected",
  100.                   (LPSTR)szFuncName, MB_OK );
  101.  
  102.             DeleteObject ( hObject );
  103.             ReleaseDC ( hWnd, hDC );
  104.             break;
  105.  
  106.          default:
  107.             return DefWindowProc (hWnd, iMessage, wParam, lParam);
  108.          }
  109.       break;
  110.  
  111.    case WM_DESTROY:
  112.       PostQuitMessage(0);
  113.       break;
  114.  
  115.    default:
  116.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  117.    }
  118.    return (0L); 
  119. }
  120.