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

  1. /*
  2.  *
  3.  *   GetClassName.c
  4.  *
  5.  *   This program demonstrates the use of the GetClassName function.
  6.  *   Given a handle to a window, GetClassName retrieves the class name of
  7.  *   that window. GetClassName is called from WinMain in this application.
  8.  *
  9.  */
  10.  
  11. #include "windows.h"
  12.  
  13. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  14.  
  15. /* Procedure called when the application is loaded for the first time */
  16. BOOL HelloInit( hInstance )
  17. HANDLE hInstance;
  18. {
  19.     PWNDCLASS   pHelloClass;
  20.  
  21.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  22.  
  23.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  24.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  25.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  26.     pHelloClass->lpszClassName    = (LPSTR)"Sample Application";
  27.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  28.     pHelloClass->hInstance      = hInstance;
  29.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  30.     pHelloClass->lpfnWndProc    = HelloWndProc;
  31.  
  32.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  33.         /* Initialization failed.
  34.          * Windows will automatically deallocate all allocated memory.
  35.          */
  36.         return FALSE;
  37.  
  38.     LocalFree( (HANDLE)pHelloClass );
  39.     return TRUE;        /* Initialization succeeded */
  40. }
  41.  
  42. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  43. HANDLE hInstance, hPrevInstance;
  44. LPSTR lpszCmdLine;
  45. int cmdShow;
  46. {
  47.     MSG   msg;
  48.     HWND  hWnd;
  49.     HMENU hMenu;
  50.     char  szClassName[30];    /* buffer to receive Class Name of hWnd */
  51.     int   nSizeBuff;        /*   return value from GetClassName    */
  52.  
  53.     HelloInit( hInstance );
  54.     hWnd = CreateWindow((LPSTR)"Sample Application",
  55.             (LPSTR)"GetClassName",
  56.             WS_OVERLAPPEDWINDOW,
  57.             CW_USEDEFAULT,
  58.             CW_USEDEFAULT,
  59.             CW_USEDEFAULT,
  60.             CW_USEDEFAULT,
  61.                         (HWND)NULL,        /* no parent */
  62.                         (HMENU)NULL,       /* use class menu */
  63.                         (HANDLE)hInstance, /* handle to window instance */
  64.                         (LPSTR)NULL        /* no params to pass on */
  65.                         );
  66.  
  67.     /* Make window visible according to the way the app is activated */
  68.     ShowWindow( hWnd, cmdShow );
  69.     UpdateWindow( hWnd );
  70.  
  71.  
  72. /*** get the class name of the window associated with hWnd ***/
  73.     nSizeBuff = GetClassName(hWnd,(LPSTR)szClassName,30);
  74.  
  75.  
  76.     if (nSizeBuff == 0)
  77.      {
  78.      MessageBox(hWnd,(LPSTR)"is not valid ",
  79.             (LPSTR)"The specified window handle",
  80.             MB_OK);
  81.      return 1;
  82.      }
  83.  
  84.      MessageBox(hWnd,(LPSTR)szClassName,
  85.         (LPSTR)"The class name is:",
  86.         MB_OK);
  87.  
  88.     /* Polling messages from event queue */
  89.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  90.         TranslateMessage((LPMSG)&msg);
  91.         DispatchMessage((LPMSG)&msg);
  92.         }
  93.  
  94.     return (int)msg.wParam;
  95. }
  96.  
  97.  
  98. /* Procedures which make up the window class. */
  99. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  100. HWND hWnd;
  101. unsigned message;
  102. WORD wParam;
  103. LONG lParam;
  104. {
  105.     PAINTSTRUCT ps;
  106.  
  107.     switch (message)
  108.     {
  109.     case WM_SYSCOMMAND:
  110.     return DefWindowProc( hWnd, message, wParam, lParam );
  111.         break;
  112.  
  113.     case WM_DESTROY:
  114.         PostQuitMessage( 0 );
  115.         break;
  116.  
  117.     case WM_PAINT:
  118.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  119.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  120.         break;
  121.  
  122.     default:
  123.         return DefWindowProc( hWnd, message, wParam, lParam );
  124.         break;
  125.     }
  126.     return(0L);
  127. }
  128.