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

  1. /*
  2.  *   TranslateMessage
  3.  *   trnsltmg.c
  4.  *
  5.  *   This program demonstrates the function TranslateMessage.  A virtual-
  6.  *   keystroke message is translated into an ascii character message and
  7.  *   posted to the application queue, to be read the next time the
  8.  *   application calls the GetMessage function.  The character is finally
  9.  *   displayed on the screen.  This sequence is repeated until a
  10.  *   WM_DESTROY message is received (the window is closed by the user).
  11.  * 
  12.  *   After testing this program, try removing the TranslateMessage function.
  13.  *   You will find that the keyboard will appear dead.  None of the
  14.  *   virtual-keystroke messages are translated into character messages.
  15.  *   Mouse input will still be available even Without TranslateMessage.
  16.  *
  17.  */
  18.  
  19. #include <windows.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22.  
  23. #define FIRST_ASCII_CHAR  0
  24. #define LAST_ASCII_CHAR   127
  25.  
  26. /* Forward references */
  27.  
  28. int    PASCAL      WinMain(HANDLE, HANDLE, LPSTR, int);
  29. BOOL            TranslateMsgInit(HANDLE);
  30. void PASCAL     PaintCharacter(HWND);
  31. long    FAR PASCAL TranslateMsgWndProc(HWND, unsigned, WORD, LONG);
  32.  
  33. BYTE chAsciiChar = 255;             /* Current keyboard character */
  34.  
  35. int    PASCAL WinMain(hInstance, hPrevInstance, lpszCmdLine, CmdShow)
  36. HANDLE hInstance, hPrevInstance;
  37. LPSTR lpszCmdLine;
  38. int    CmdShow;
  39. {
  40.   MSG    msg;
  41.   HANDLE hInst;
  42.   HWND   hWnd;
  43.   BOOL   bInit;
  44.  
  45.   if (!hPrevInstance)
  46.     bInit = TranslateMsgInit(hInstance);
  47.  
  48.   hInst = hInstance;
  49.  
  50.   hWnd = CreateWindow((LPSTR)"TranslateMessage",
  51.       (LPSTR)"TranslateMessage",
  52.       WS_OVERLAPPEDWINDOW,
  53.       CW_USEDEFAULT,
  54.       CW_USEDEFAULT,
  55.       CW_USEDEFAULT,
  56.       CW_USEDEFAULT,
  57.       (HWND)NULL,        /* no parent */
  58.   (HMENU)NULL,       /* use class menu */
  59.   (HANDLE)hInstance, /* handle to window instance */
  60.   (LPSTR)NULL        /* no params to pass on */
  61.   );
  62.  
  63. /* Make window visible */
  64.   ShowWindow(hWnd, CmdShow);
  65.   UpdateWindow(hWnd);
  66.  
  67. /* Polling messages from event queue */
  68.   while (GetMessage((LPMSG) & msg, NULL, 0, 0))
  69.   {
  70.     TranslateMessage((LPMSG) & msg);
  71.     DispatchMessage((LPMSG) & msg);
  72.   }
  73.  
  74.   return (int)msg.wParam;
  75. }
  76.  
  77.  
  78. /* Procedure called when the application is loaded for the first time */
  79. BOOL TranslateMsgInit(hInstance)
  80. HANDLE hInstance;
  81. {
  82.   WNDCLASS   rTranslateMsgClass;
  83.  
  84.   rTranslateMsgClass.hCursor        = LoadCursor(NULL, IDC_ARROW );
  85.   rTranslateMsgClass.hIcon          = LoadIcon(hInstance, NULL);
  86.   rTranslateMsgClass.lpszMenuName   = (LPSTR)NULL;
  87.   rTranslateMsgClass.lpszClassName  = (LPSTR)"TranslateMessage";
  88.   rTranslateMsgClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  89.   rTranslateMsgClass.hInstance      = hInstance;
  90.   rTranslateMsgClass.style          = CS_HREDRAW | CS_VREDRAW;
  91.   rTranslateMsgClass.lpfnWndProc    = TranslateMsgWndProc;
  92.  
  93.   if (!RegisterClass((LPWNDCLASS) & rTranslateMsgClass))
  94. /* Initialization failed.
  95.      * Windows will automatically deallocate all allocated memory.
  96.      */
  97.     return FALSE;
  98.  
  99.   return TRUE;        /* Initialization succeeded */
  100. }
  101.  
  102.  
  103. /* Every message for this window will be delevered right here.         */
  104. long    FAR PASCAL TranslateMsgWndProc( hWnd, message, wParam, lParam )
  105. HWND hWnd;
  106. unsigned    message;
  107. WORD wParam;
  108. LONG lParam;
  109. {
  110.   switch (message)
  111.   {
  112.   case WM_PAINT:
  113.     PaintCharacter(hWnd);
  114.     break;
  115.   case WM_CHAR:
  116. /* The value of the key is stored in the low order byte of wParam */
  117.     chAsciiChar = LOBYTE(wParam);
  118.     InvalidateRect(hWnd, (LPRECT)NULL, TRUE);
  119.     break;
  120.   case WM_DESTROY:
  121.     PostQuitMessage(0);
  122.     break;
  123.   }
  124.   return(0L);
  125. }
  126.  
  127.  
  128. void PASCAL PaintCharacter(hWnd)
  129. HWND hWnd;
  130. {
  131.   PAINTSTRUCT ps;
  132.   HDC         hDC;
  133.   char    szOutBuff [80];
  134.  
  135.   hDC = BeginPaint(hWnd, (LPPAINTSTRUCT) & ps);
  136.  
  137.   if ((chAsciiChar >= FIRST_ASCII_CHAR) && (chAsciiChar <= LAST_ASCII_CHAR))
  138.   {
  139.     sprintf(szOutBuff, "The '%c' key was pressed.",  chAsciiChar);
  140.     TextOut(hDC, 30, 30, (LPSTR)szOutBuff, strlen(szOutBuff));
  141.   }
  142.   else
  143.     TextOut(hDC, 30, 30, (LPSTR)"Please press a key", 18);
  144.   EndPaint(hWnd, (LPPAINTSTRUCT) & ps);
  145. }
  146.  
  147.  
  148.