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

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