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

  1. /*
  2.  *   GetWindowText
  3.  *
  4.  *   This program demonstrates the use of the GetWindowText function. The
  5.  *   GetWindowText function copies the given window's caption bar (if one
  6.  *   exists) into the second parameter. GetWindowText returns an integer
  7.  *   specifying the length of the caption. If GetWindowText returns 0, then
  8.  *   there is no caption for the given window.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"
  13. #include <stdio.h>     /* required for sprintf */
  14.  
  15. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  16.  
  17. /* Procedure called when the application is loaded for the first time */
  18. BOOL HelloInit( hInstance )
  19. HANDLE hInstance;
  20. {
  21.     PWNDCLASS   pHelloClass;
  22.  
  23.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  24.  
  25.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  26.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  27.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  28.     pHelloClass->lpszClassName    = (LPSTR)"Sample Application";
  29.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  30.     pHelloClass->hInstance      = hInstance;
  31.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  32.     pHelloClass->lpfnWndProc    = HelloWndProc;
  33.  
  34.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  35.         /* Initialization failed.
  36.          * Windows will automatically deallocate all allocated memory.
  37.          */
  38.         return FALSE;
  39.  
  40.     LocalFree( (HANDLE)pHelloClass );
  41.     return TRUE;        /* Initialization succeeded */
  42. }
  43.  
  44. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  45. HANDLE hInstance, hPrevInstance;
  46. LPSTR lpszCmdLine;
  47. int cmdShow;
  48. {
  49.     MSG   msg;
  50.     HWND  hWnd;
  51.     HMENU hMenu;
  52.     int x;               /* return value from GetWindowText    */
  53.     char buffer[40];           /* buffer that GetWindowText places text */
  54.  
  55.     HelloInit( hInstance );
  56.     hWnd = CreateWindow((LPSTR)"Sample Application",
  57.             (LPSTR)"Sample Application",
  58.             WS_OVERLAPPEDWINDOW,
  59.             CW_USEDEFAULT,
  60.             CW_USEDEFAULT,
  61.             CW_USEDEFAULT,
  62.             CW_USEDEFAULT,
  63.                         (HWND)NULL,        /* no parent */
  64.                         (HMENU)NULL,       /* use class menu */
  65.                         (HANDLE)hInstance, /* handle to window instance */
  66.                         (LPSTR)NULL        /* no params to pass on */
  67.                         );
  68.  
  69.     /* Make window visible according to the way the app is activated */
  70.     ShowWindow( hWnd, cmdShow );
  71.     UpdateWindow( hWnd );
  72.  
  73.     /* get caption bar text if it exists */
  74.     x=GetWindowText(hWnd,buffer,40);
  75.     /* if there is a caption */
  76.     if (x>0)
  77.       /* output the caption    */
  78.       MessageBox(hWnd,(LPSTR)buffer,
  79.              (LPSTR)"THE CAPTION BAR SAYS:",MB_OK);
  80.     else
  81.       /* output "There is no Title Bar */
  82.       MessageBox(hWnd,(LPSTR)NULL,
  83.              (LPSTR)"There is no Title Bar",MB_OK);
  84.  
  85.     /* Polling messages from event queue */
  86.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  87.         TranslateMessage((LPMSG)&msg);
  88.         DispatchMessage((LPMSG)&msg);
  89.         }
  90.  
  91.     return (int)msg.wParam;
  92. }
  93.  
  94. /* Procedures which make up the window class. */
  95. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  96. HWND hWnd;
  97. unsigned message;
  98. WORD wParam;
  99. LONG lParam;
  100. {
  101.     PAINTSTRUCT ps;
  102.  
  103.     switch (message)
  104.     {
  105.     case WM_SYSCOMMAND:
  106.         switch (wParam)
  107.         {
  108.         default:
  109.             return DefWindowProc( hWnd, message, wParam, lParam );
  110.         }
  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.