⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tty.c

📁 WINDOWS CE 通信指南(附光盘) 本书配套光盘内容包括:本书的英文版电子书;SDK for Windows CE
💻 C
📖 第 1 页 / 共 2 页
字号:

        case SB_ENDSCROLL:
          break;
      }
      break;

    case WM_DESTROY:
      PostQuitMessage (0);
      break;
  }

  return (DefWindowProc (hWnd, uMsg, wParam, lParam));
}


/***********************************************************************

FUNCTION: 
  MainWndProc

PURPOSE: 
  Processes messages sent to the main window.
      
***********************************************************************/
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam,
                              LPARAM lParam)
{
  HDC hDC;
  RECT rect;
  HBRUSH hBrush;
  PAINTSTRUCT ps;

  switch (uMsg)
  {
    case WM_CREATE:
      // Create command bar and insert the menu.
      hCmdBarWnd = CommandBar_Create (hInst, hWnd, 1);
      CommandBar_InsertMenubar (hCmdBarWnd, hInst, IDR_MAIN_MENU, 0);
      CommandBar_AddAdornments (hCmdBarWnd, 0, 0);
      return 0;

    case WM_PAINT:
      hDC = BeginPaint (hWnd, &ps);

      // Draw the gray background for the button bar.
      hBrush = (HBRUSH)GetStockObject (LTGRAY_BRUSH);
      GetClientRect (hWnd, &rect);
      rect.bottom = BUTTONBAR_HEIGHT;
      FillRect (hDC, &rect, hBrush);
      DeleteObject (hBrush);

      // Draw the black background for rest of the area.
      hBrush = (HBRUSH)GetStockObject (BLACK_BRUSH);
      rect.top = BUTTONBAR_HEIGHT - 1;
      FillRect (hDC, &rect, hBrush);
      DeleteObject (hBrush);

      EndPaint (hWnd, &ps);
      return 0;

    case WM_SETFOCUS:
      // Give the focus to the terminal window.
      SetFocus (hTermWnd);
      return 0;

    case WM_SIZE:
      GetClientRect (hWnd, &rect);
      rect.top = CommandBar_Height (hCmdBarWnd);
      rect.top += BUTTONBAR_HEIGHT;
      SetWindowPos (hTermWnd, HWND_TOP, rect.left, rect.top,
                    rect.right - rect.left, rect.bottom - rect.top, 0);
      ShowWindow (hTermWnd, SW_SHOWNORMAL);
      return 0;

    case WM_COMMAND:
      switch (LOWORD(wParam))
      {
        case ID_FILE_EXIT:
          DestroyWindow (hWnd);
          break;

        case ID_EDIT_CLEARBUFFER:
          ClearScreen ();
          break;

        case ID_SETTINGS:
          DialogBox (hInst, MAKEINTRESOURCE(IDD_COMMUNICATIONS),
                     hWnd, CommDlgProc);
          break;

        case ID_HELP_ABOUTTTY:
          MessageBox (hWnd, 
                      TEXT("Window CE TTY Sample Application\r\n")
                      TEXT("              (c) Microsoft 1999"),
                      TEXT("About TTY"), 
                      MB_OK);
          break;

        case ID_DTR_BTN:
          if (SendMessage (hDTRWnd, BM_GETCHECK, 0, 0))
            EscapeCommFunction (hPort, SETDTR);
          else
            EscapeCommFunction (hPort, CLRDTR);
          
          SetFocus (hTermWnd);
          break;

        case ID_RTS_BTN:
          if (SendMessage (hRTSWnd, BM_GETCHECK, 0, 0))
            EscapeCommFunction (hPort, SETRTS);
          else 
            EscapeCommFunction (hPort, CLRRTS);

          SetFocus (hTermWnd);
          break;

        case ID_DSR_BTN:
        case ID_CTS_BTN:
        case ID_DCD_BTN:
        case ID_RING_BTN:
          SetFocus (hTermWnd);
          break;

        default:
          MessageBox (hWnd, 
                      TEXT("Unhandled WM_COMMAND message"),
                      TEXT("ERROR"), 
                      MB_OK);
          break;
      }
      return 0;

    case WM_DESTROY:
    {
      // Clear all events to be monitored for the port. 
      SetCommMask (hPort, 0);

      // Close the serial port.
      PortClose (hPort);

      PostQuitMessage (0);
      return 0;
    }
  }

  return (DefWindowProc (hWnd, uMsg, wParam, lParam));
}

/***********************************************************************

FUNCTION: 
  InitApplication

PURPOSE: 
  Initialize and register window class.

***********************************************************************/
BOOL InitApplication (HINSTANCE hInstance)
{
  WNDCLASS  wc;

  wc.style = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc = (WNDPROC)MainWndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = (HICON)NULL;
  wc.hCursor = NULL;
  wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  wc.lpszMenuName = 0;
  wc.lpszClassName = szAppName;

  if (!RegisterClass (&wc))
    return FALSE;
  
  wc.style = CS_VREDRAW | CS_HREDRAW;
  wc.lpfnWndProc = (WNDPROC)TermWndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = (HICON)NULL;
  wc.hCursor = NULL;
  wc.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH);
  wc.lpszMenuName = 0;
  wc.lpszClassName = TEXT("TermClass");

  return RegisterClass (&wc);
}

/***********************************************************************

FUNCTION: 
  InitInstance

PURPOSE: 
  Create and display the main window.

***********************************************************************/
BOOL InitInstance (HINSTANCE hInstance, int nCmdShow)
{
  RECT rect;
  DWORD dwStyle,
        dwError;

  hMainWnd = CreateWindowEx (0, 
                             szAppName,
                             szTitle,
                             WS_CLIPCHILDREN,
                             0,0,
                             CW_USEDEFAULT, 
                             CW_USEDEFAULT,
                             NULL, 
                             NULL,
                             hInstance, 
                             NULL);
  if (!hMainWnd)
  {
    dwError = GetLastError ();
    return FALSE;
  }

  ShowWindow (hMainWnd, nCmdShow);
  UpdateWindow (hMainWnd);

  GetClientRect (hMainWnd, &rect);
  rect.top = CommandBar_Height (hCmdBarWnd) + 1;

  // Window style
  dwStyle = WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX;

  hDTRWnd = CreateWindow (TEXT("button"), TEXT("DTR"), dwStyle, 
                          rect.left + 5, rect.top, 50, BUTTON_HEIGHT,
                          hMainWnd, (HMENU)ID_DTR_BTN, hInstance, NULL);
              
  hRTSWnd = CreateWindow (TEXT("button"), TEXT("RTS"), dwStyle,
                          rect.left + 65, rect.top, 50, BUTTON_HEIGHT, 
                          hMainWnd, (HMENU)ID_RTS_BTN, hInstance, NULL);
  
  // Create the following check box for monitoring a set of events.
  // Because these checkboxes are used for monitoring events only, 
  // make all of them disabled upon creation.

  // Window style
  dwStyle = WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | WS_DISABLED;

  hDSRWnd = CreateWindow (TEXT("button"), TEXT("DSR"), dwStyle, 
                          rect.left + 125, rect.top, 50, BUTTON_HEIGHT, 
                          hMainWnd, (HMENU)ID_DSR_BTN, hInstance, NULL);

  hCTSWnd = CreateWindow (TEXT("button"), TEXT("CTS"), dwStyle, 
                          rect.left + 185, rect.top, 50, BUTTON_HEIGHT, 
                          hMainWnd, (HMENU)ID_CTS_BTN, hInstance, NULL);

  hDCDWnd = CreateWindow (TEXT("button"), TEXT("DCD"), dwStyle, 
                          rect.left + 245, rect.top, 50, BUTTON_HEIGHT, 
                          hMainWnd, (HMENU)ID_DCD_BTN, hInstance, NULL);

  hRINGWnd = CreateWindow (TEXT("button"), TEXT("RING"), dwStyle, 
                          rect.left + 305, rect.top, 60, BUTTON_HEIGHT, 
                          hMainWnd, (HMENU)ID_RING_BTN, hInstance,NULL);
  
  rect.top += BUTTONBAR_HEIGHT - 1;

  // Terminal window style
  dwStyle = WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE | WS_BORDER;

  // Create the terminal window.
  hTermWnd = CreateWindowEx (0, 
                             TEXT("TermClass"),
                             TEXT("H/PC TTY"),
                             dwStyle,
                             rect.left, 
                             rect.top, 
                             rect.right - rect.left,
                             rect.bottom - rect.top,
                             hMainWnd, 
                             NULL,
                             hInstance, 
                             NULL);
  if (!hTermWnd)
  {
    dwError = GetLastError ();
    return FALSE;
  }

  SetFocus (hTermWnd);

  return TRUE;
}



/***********************************************************************

FUNCTION: 
  WinMain

PURPOSE: 
  The WinMain function of the application. It is called by the system as
  the initial entry point for this WindowsCE-based application.

***********************************************************************/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
                    LPTSTR lpCmdLine, int nCmdShow)
{
  MSG msg;
  DWORD dwCommModemStatus;

  hInst = hInstance;

  if (!hPrevInstance)
  {
    if (!InitApplication (hInstance))
      return 0;
  }

  if (!InitInstance (hInstance, nCmdShow))
  {
    if (hTermWnd)
      DestroyWindow (hTermWnd);
    
    goto ExitMain;
  }

  // Initialize the screen.
  if (!InitScreenSettings (hTermWnd))
  {
    MessageBox (hMainWnd, 
                TEXT("Unable to allocate screen buffer"),
                TEXT("Fatal Error"), 
                MB_OK);

    DestroyWindow (hTermWnd);
    goto ExitMain;
  }

  // Clear the screen.
  ClearScreen ();

  // Assign the port name. If the port name is NULL, assign the default
  // value as "COM1:".
  if (*lpCmdLine == TEXT('\0')) 
    lpszDevName = TEXT("COM1:");
  else
    lpszDevName = lpCmdLine;

  // Initialize the port.
  if (!PortInitialize (lpszDevName))
  {
    DestroyWindow (hTermWnd);
    goto ExitMain;
  }
  
  // Set the state of the DTR and RTS check boxes.
  SendMessage (hDTRWnd, BM_SETCHECK, TRUE, 0);
  SendMessage (hRTSWnd, BM_SETCHECK, TRUE, 0);

  // Initialize the indicator lights.
  GetCommModemStatus (hPort, &dwCommModemStatus);
  SetLightIndicators (dwCommModemStatus);

  while (GetMessage (&msg, NULL, 0, 0))
  {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }

ExitMain:

  if (hFont)
    DeleteObject ((HGDIOBJ)hFont);
  
  return 1;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -