home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 December / VPR9712A.ISO / OLS / WIN32 / COMWP380 / comwp380.exe / SAMPLES.EXE / SAMPLE2.C < prev    next >
C/C++ Source or Header  |  1997-08-25  |  6KB  |  219 lines

  1. /*--------------------*
  2.  * デバッグ端末使用例 *
  3.  *--------------------*
  4.  */
  5. //#define  UNICODE
  6. //#define _UNICODE
  7. #include <windows.h>
  8. #include <tchar.h>
  9. #include <string.h>
  10. #include "comwin.h"
  11.  
  12. #if !defined(UNICODE)
  13.  
  14. #define LPTSTR LPSTR
  15.  
  16. #else
  17.  
  18. #define cwFputs   cwFputsW
  19. #define cwFprintf cwFprintfW
  20.  
  21. LONG FAR PASCAL cwFprintfW( LPVOID lpFile, LPTSTR lpFormat, ... )
  22. {
  23.     HGLOBAL hStr;
  24.     LPTSTR lpStr;
  25.     va_list argptr;
  26.     LONG lRet;
  27.  
  28.     hStr = GlobalAlloc(GMEM_MOVEABLE, 0x400 * sizeof(TCHAR));
  29.     lpStr = GlobalLock(hStr);
  30.     if (lpStr == NULL) {
  31.         return -1;
  32.     }
  33.     va_start(argptr, lpFormat);
  34.     _vsntprintf(lpStr, 0x400 - 1, lpFormat, argptr);
  35.     va_end(argptr);
  36.     lRet = cwFputs(lpStr, lpFile);
  37.     GlobalUnlock(hStr);
  38.     GlobalFree(hStr);
  39.  
  40.     return lRet;
  41. }
  42.  
  43. #endif
  44.  
  45. LONG FAR PASCAL WndProc(HWND, UINT, WPARAM, LPARAM);
  46. int WmPaintProc(HWND, WPARAM, LPARAM);
  47.  
  48. int PASCAL WinMain(HINSTANCE hInstance,
  49.                    HINSTANCE hPrevInstance,
  50.                    LPSTR lpszCmdParam,
  51.                    int nCmdShow)
  52. {
  53.     static TCHAR szAppName[] = _TEXT("Sample2");
  54.     HWND         hwnd;
  55.     MSG          msg;
  56.     WNDCLASS     wndclass;
  57.  
  58.     if (!hPrevInstance) {
  59.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  60.         wndclass.lpfnWndProc   = WndProc;
  61.         wndclass.cbClsExtra    = 0;
  62.         wndclass.cbWndExtra    = 0;
  63.         wndclass.hInstance     = hInstance;
  64.         wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  65.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  66.         wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  67.         wndclass.lpszMenuName  = NULL;
  68.         wndclass.lpszClassName = szAppName;
  69.  
  70.         RegisterClass(&wndclass);
  71.     }
  72.  
  73.     hwnd = CreateWindow(
  74.                 szAppName, _TEXT("Sample2 for ComWin"),
  75.                 WS_OVERLAPPEDWINDOW,
  76.                 CW_USEDEFAULT, CW_USEDEFAULT,
  77.                 CW_USEDEFAULT, CW_USEDEFAULT,
  78.                 NULL, NULL, hInstance, NULL);
  79.  
  80.     SetTimer(hwnd, 1, 5000, NULL);
  81.     ShowWindow(hwnd, nCmdShow);
  82.     UpdateWindow(hwnd);
  83.  
  84.     while (GetMessage(&msg, NULL, 0, 0)) {
  85.         TranslateMessage(&msg);
  86.         DispatchMessage(&msg);
  87.     }
  88.     return msg.wParam ;
  89. }
  90.  
  91. LONG FAR PASCAL WndProc(HWND hwnd,
  92.                         UINT uMsg,
  93.                         WPARAM wParam,
  94.                         LPARAM lParam)
  95. {
  96.  
  97. #if !defined(WIN32)
  98.     static TCHAR szFormat[] = _TEXT("%04X:%08lX:%s\n");
  99. #else
  100.     static TCHAR szFormat[] = _TEXT("%08lX:%08lX:%s\n");
  101. #endif
  102.  
  103.     switch (uMsg) {
  104.         case WM_ACTIVATE:
  105.             cwFprintf(NULL, szFormat, wParam, lParam,
  106.                       (LPTSTR)_TEXT("WM_ACTIVATE"));
  107.             break;
  108.  
  109.         case WM_CHAR:
  110.             cwFprintf(NULL, szFormat, wParam, lParam,
  111.                       (LPTSTR)_TEXT("WM_CHAR"));
  112.             break;
  113.  
  114.         case WM_CLOSE:
  115.             cwFprintf(NULL, szFormat, wParam, lParam,
  116.                       (LPTSTR)_TEXT("WM_CLOSE"));
  117.             break;
  118.  
  119.         case WM_CREATE:
  120.             cwFprintf(NULL, szFormat, wParam, lParam,
  121.                       (LPTSTR)_TEXT("WM_CREATE"));
  122.  
  123.             break;
  124.  
  125.         case WM_DESTROY:
  126.             cwFprintf(NULL, szFormat, wParam, lParam,
  127.                       (LPTSTR)_TEXT("WM_DESTROY"));
  128.             PostQuitMessage(0);
  129.             break;
  130.  
  131.         case WM_KEYDOWN:
  132.             cwFprintf(NULL, szFormat, wParam, lParam,
  133.                       (LPTSTR)_TEXT("WM_KEYDOWN"));
  134.             break;
  135.  
  136.         case WM_KEYUP:
  137.             cwFprintf(NULL, szFormat, wParam, lParam,
  138.                       (LPTSTR)_TEXT("WM_KEYUP"));
  139.             break;
  140.  
  141.         case WM_KILLFOCUS:
  142.             cwFprintf(NULL, szFormat, wParam, lParam, 
  143.                       (LPTSTR)_TEXT("WM_KILLFOCUS"));
  144.             break;
  145.  
  146.         case WM_LBUTTONDOWN:
  147.             cwFprintf(NULL, szFormat, wParam, lParam,
  148.                       (LPTSTR)_TEXT("WM_LBUTTONDOWN"));
  149.             break;
  150.  
  151.         case WM_PAINT:
  152.             cwFprintf(NULL, szFormat, wParam, lParam,
  153.                       (LPTSTR)_TEXT("WM_PAINT"));
  154.             WmPaintProc(hwnd, wParam, lParam);
  155.             break;
  156.  
  157.         case WM_RBUTTONDOWN:
  158.             cwFprintf(NULL, szFormat, wParam, lParam,
  159.                       (LPTSTR)_TEXT("WM_RBUTTONDOWN"));
  160.             break;
  161.  
  162.         case WM_SETFOCUS:
  163.             cwFprintf(NULL, szFormat, wParam, lParam,
  164.                       (LPTSTR)_TEXT("WM_SETFOCUS"));
  165.             break;
  166.  
  167.         case WM_SIZE:
  168.             cwFprintf(NULL, szFormat, wParam, lParam,
  169.                       (LPTSTR)_TEXT("WM_SIZE"));
  170.             break;
  171.  
  172.         case WM_SYSCOMMAND:
  173.             cwFprintf(NULL, szFormat, wParam, lParam,
  174.                       (LPTSTR)_TEXT("WM_SYSCOMMAND"));
  175.             break;
  176.  
  177.         case WM_TIMER:
  178.             cwFprintf(NULL, szFormat, wParam, lParam,
  179.                       (LPTSTR)_TEXT("WM_TIMER"));
  180.             break;
  181.  
  182.     }
  183.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  184. }
  185.  
  186. int WmPaintProc(HWND hwnd, WPARAM wParam, LPARAM lParam)
  187. {
  188.     HDC         hdc;
  189.     PAINTSTRUCT ps;
  190.     RECT        rc;
  191.     HFONT       hFont, hFontOld;
  192.     LOGFONT     lf;
  193.  
  194.     hdc = BeginPaint(hwnd, &ps);
  195.  
  196.     SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  197.     SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
  198.  
  199.     memset(&lf, 0, sizeof(lf));
  200.     lstrcpy(lf.lfFaceName, _TEXT("Times New Roman"));
  201.     lf.lfHeight = -24;
  202.     lf.lfItalic = 1;
  203.     lf.lfWeight = FW_BOLD;
  204.     lf.lfUnderline = 1;
  205.     hFont    = CreateFontIndirect(&lf);
  206.     hFontOld = SelectObject(hdc, hFont);
  207.  
  208.     GetClientRect(hwnd, &rc);
  209.  
  210.     DrawText(hdc, _TEXT("ComWin ver 3.80"), -1, &rc,
  211.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  212.  
  213.     SelectObject(hdc, hFontOld);
  214.     DeleteObject(hFont);
  215.  
  216.     EndPaint(hwnd, &ps);
  217.     return 0;
  218. }
  219.