home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 August / VPR9708A.ISO / OLS / WIN32 / Comwp362 / COMWP362.LZH / SAMPLES.LZH / SAMPLE2.C < prev    next >
C/C++ Source or Header  |  1997-05-25  |  6KB  |  220 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. LONG FAR PASCAL WndProcW(HWND, UINT, WPARAM, LPARAM);
  47. int WmPaintProc(HWND, WPARAM, LPARAM);
  48.  
  49. int PASCAL WinMain(HINSTANCE hInstance,
  50.                    HINSTANCE hPrevInstance,
  51.                    LPSTR lpszCmdParam,
  52.                    int nCmdShow)
  53. {
  54.     static TCHAR szAppName[] = _TEXT("Sample2");
  55.     HWND         hwnd;
  56.     MSG          msg;
  57.     WNDCLASS     wndclass;
  58.  
  59.     if (!hPrevInstance) {
  60.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  61.         wndclass.lpfnWndProc   = WndProc;
  62.         wndclass.cbClsExtra    = 0;
  63.         wndclass.cbWndExtra    = 0;
  64.         wndclass.hInstance     = hInstance;
  65.         wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  66.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  67.         wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  68.         wndclass.lpszMenuName  = NULL;
  69.         wndclass.lpszClassName = szAppName;
  70.  
  71.         RegisterClass(&wndclass);
  72.     }
  73.  
  74.     hwnd = CreateWindow(
  75.                 szAppName, _TEXT("Sample2 for ComWin"),
  76.                 WS_OVERLAPPEDWINDOW,
  77.                 CW_USEDEFAULT, CW_USEDEFAULT,
  78.                 CW_USEDEFAULT, CW_USEDEFAULT,
  79.                 NULL, NULL, hInstance, NULL);
  80.  
  81.     SetTimer(hwnd, 1, 5000, NULL);
  82.     ShowWindow(hwnd, nCmdShow);
  83.     UpdateWindow(hwnd);
  84.  
  85.     while (GetMessage(&msg, NULL, 0, 0)) {
  86.         TranslateMessage(&msg);
  87.         DispatchMessage(&msg);
  88.     }
  89.     return msg.wParam ;
  90. }
  91.  
  92. LONG FAR PASCAL WndProc(HWND hwnd,
  93.                         UINT uMsg,
  94.                         WPARAM wParam,
  95.                         LPARAM lParam)
  96. {
  97.  
  98. #if !defined(WIN32)
  99.     static TCHAR szFormat[] = _TEXT("%04X:%08lX:%s\n");
  100. #else
  101.     static TCHAR szFormat[] = _TEXT("%08lX:%08lX:%s\n");
  102. #endif
  103.  
  104.     switch (uMsg) {
  105.         case WM_ACTIVATE:
  106.             cwFprintf(NULL, szFormat, wParam, lParam,
  107.                       (LPTSTR)_TEXT("WM_ACTIVATE"));
  108.             break;
  109.  
  110.         case WM_CHAR:
  111.             cwFprintf(NULL, szFormat, wParam, lParam,
  112.                       (LPTSTR)_TEXT("WM_CHAR"));
  113.             break;
  114.  
  115.         case WM_CLOSE:
  116.             cwFprintf(NULL, szFormat, wParam, lParam,
  117.                       (LPTSTR)_TEXT("WM_CLOSE"));
  118.             break;
  119.  
  120.         case WM_CREATE:
  121.             cwFprintf(NULL, szFormat, wParam, lParam,
  122.                       (LPTSTR)_TEXT("WM_CREATE"));
  123.  
  124.             break;
  125.  
  126.         case WM_DESTROY:
  127.             cwFprintf(NULL, szFormat, wParam, lParam,
  128.                       (LPTSTR)_TEXT("WM_DESTROY"));
  129.             PostQuitMessage(0);
  130.             break;
  131.  
  132.         case WM_KEYDOWN:
  133.             cwFprintf(NULL, szFormat, wParam, lParam,
  134.                       (LPTSTR)_TEXT("WM_KEYDOWN"));
  135.             break;
  136.  
  137.         case WM_KEYUP:
  138.             cwFprintf(NULL, szFormat, wParam, lParam,
  139.                       (LPTSTR)_TEXT("WM_KEYUP"));
  140.             break;
  141.  
  142.         case WM_KILLFOCUS:
  143.             cwFprintf(NULL, szFormat, wParam, lParam, 
  144.                       (LPTSTR)_TEXT("WM_KILLFOCUS"));
  145.             break;
  146.  
  147.         case WM_LBUTTONDOWN:
  148.             cwFprintf(NULL, szFormat, wParam, lParam,
  149.                       (LPTSTR)_TEXT("WM_LBUTTONDOWN"));
  150.             break;
  151.  
  152.         case WM_PAINT:
  153.             cwFprintf(NULL, szFormat, wParam, lParam,
  154.                       (LPTSTR)_TEXT("WM_PAINT"));
  155.             WmPaintProc(hwnd, wParam, lParam);
  156.             break;
  157.  
  158.         case WM_RBUTTONDOWN:
  159.             cwFprintf(NULL, szFormat, wParam, lParam,
  160.                       (LPTSTR)_TEXT("WM_RBUTTONDOWN"));
  161.             break;
  162.  
  163.         case WM_SETFOCUS:
  164.             cwFprintf(NULL, szFormat, wParam, lParam,
  165.                       (LPTSTR)_TEXT("WM_SETFOCUS"));
  166.             break;
  167.  
  168.         case WM_SIZE:
  169.             cwFprintf(NULL, szFormat, wParam, lParam,
  170.                       (LPTSTR)_TEXT("WM_SIZE"));
  171.             break;
  172.  
  173.         case WM_SYSCOMMAND:
  174.             cwFprintf(NULL, szFormat, wParam, lParam,
  175.                       (LPTSTR)_TEXT("WM_SYSCOMMAND"));
  176.             break;
  177.  
  178.         case WM_TIMER:
  179.             cwFprintf(NULL, szFormat, wParam, lParam,
  180.                       (LPTSTR)_TEXT("WM_TIMER"));
  181.             break;
  182.  
  183.     }
  184.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  185. }
  186.  
  187. int WmPaintProc(HWND hwnd, WPARAM wParam, LPARAM lParam)
  188. {
  189.     HDC         hdc;
  190.     PAINTSTRUCT ps;
  191.     RECT        rc;
  192.     HFONT       hFont, hFontOld;
  193.     LOGFONT     lf;
  194.  
  195.     hdc = BeginPaint(hwnd, &ps);
  196.  
  197.     SetTextColor(hdc, GetSysColor(COLOR_WINDOWTEXT));
  198.     SetBkColor(hdc, GetSysColor(COLOR_WINDOW));
  199.  
  200.     memset(&lf, 0, sizeof(lf));
  201.     lstrcpy(lf.lfFaceName, _TEXT("Times New Roman"));
  202.     lf.lfHeight = -24;
  203.     lf.lfItalic = 1;
  204.     lf.lfWeight = FW_BOLD;
  205.     lf.lfUnderline = 1;
  206.     hFont    = CreateFontIndirect(&lf);
  207.     hFontOld = SelectObject(hdc, hFont);
  208.  
  209.     GetClientRect(hwnd, &rc);
  210.  
  211.     DrawText(hdc, _TEXT("ComWin ver 3.62"), -1, &rc,
  212.                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  213.  
  214.     SelectObject(hdc, hFontOld);
  215.     DeleteObject(hFont);
  216.  
  217.     EndPaint(hwnd, &ps);
  218.     return 0;
  219. }
  220.