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

📄 mttty.c

📁 用于串口的测试调试
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-----------------------------------------------------------------------------
    This is a part of the Microsoft Source Code Samples. 
    Copyright (C) 1995 Microsoft Corporation.
    All rights reserved. 
    This source code is only intended as a supplement to 
    Microsoft Development Tools and/or WinHelp documentation.
    See these sources for detailed information regarding the 
    Microsoft samples programs.

    MODULE: mttty.c

    PURPOSE: Program entry point and window management

    FUNCTIONS:
        WinMain            - Program entry point
        VersionCheck       - Checks OS version to make sure we can run
        InitializeApp      - Global program initialization and window class
                             creation
        MTTTYWndProc       - main window procedure
        CmdDispatch        - Carries out menu commands
        OpenTTYChildWindow - Creates the tty child window
        ScrollTTYVert      - Scroll TTY window vertically
        ScrollTTYHorz      - Scrolls TTY window horizontally
        PaintTTY           - Paints the TTY window
        MoveTTYCursor      - moves the tty cursor
        SetTTYFocus        - responds to tty window getting focus
        KillTTYFocus       - responds to tty window losing focus
        SizeTTY            - responds to tty window size changes
        TTYChildProc       - window procedure for TTY child window

-----------------------------------------------------------------------------*/

#include <windows.h>
#include "mttty.h"

/*
    Prototypes for functions called only within this file
*/
BOOL InitializeApp( HINSTANCE, int);
int WINAPI MTTTYWndProc( HWND, UINT, WPARAM, LPARAM );
int WINAPI TTYChildProc( HWND, UINT, WPARAM, LPARAM );
void CmdDispatch( int, HWND, LPARAM );
void OpenTTYChildWindow( HWND );
BOOL ScrollTTYVert( HWND, WORD, WORD );
BOOL ScrollTTYHorz( HWND, WORD, WORD );
BOOL VersionCheck();
BOOL PaintTTY( HWND );


/*-----------------------------------------------------------------------------

FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 
PURPOSE: Start application and process all window messages

PARAMETERS:
    hInstance     - this apps hinstance
    hPrevInstance - previous instance of this app - always NULL
    lpCmdLine     - command line parameters
    nCmdShow      - code for showing window

RETURN:
    1 for success
    0 for failure to start app

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

/*-----------------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    MSG msg;

    if (!VersionCheck()) {
        MessageBox(NULL, "MTTTY can't run on this version of Windows.", NULL, MB_OK);
        return 0;
    }

    if (!InitializeApp(hInstance, nShowCmd)) {
        MessageBox(NULL, "MTTTY couldn't start!", NULL, MB_OK);
        return 0;
    }

    while (GetMessage(&msg, NULL, 0, 0)) {
        if (!TranslateAccelerator( ghwndMain, ghAccel, &msg )) {
            TranslateMessage( &msg ) ;
            DispatchMessage( &msg ) ;
        }
    }

    return 1;
}


/*-----------------------------------------------------------------------------

FUNCTION: VersionCheck(void)

PURPOSE: Verifies that the correct version of Windows is running

RETURN:
    TRUE  - success version for running this app
    FALSE - correct version not verified

HISTORY:   Date:      Author:     Comment:
           11/20/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL VersionCheck()
{
    gOSV.dwOSVersionInfoSize = sizeof(gOSV);
    if (!GetVersionEx(&gOSV))
        return FALSE;

    if (gOSV.dwPlatformId == VER_PLATFORM_WIN32s)
        return FALSE;

    return TRUE ;
}


/*-----------------------------------------------------------------------------

FUNCTION: InitializeApp(HINSTANCE, int)

PURPOSE: GlobalInitialize, Register window classes 
         and create main window

PARAMETERS:
    hInst    - HINSTANCE of this app
    nShowCmd - code for showing this window

RETURN:
    TRUE  - successful inititialization of this app
    FALSE - failure to init app

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
BOOL InitializeApp(HINSTANCE hInst, int nShowCmd)
{
    WNDCLASS wc = {0};

    GlobalInitialize();     // get all global variables initialized to defaults

    //
    // setup program's main window class
    //
    wc.lpfnWndProc      = (WNDPROC) MTTTYWndProc;
    wc.hInstance        = hInst;
    wc.hIcon            = LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON));
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.lpszMenuName     = MAKEINTRESOURCE(IDR_MTTTYMENU);
    wc.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1) ;
    wc.lpszClassName    = "MTTTYClass";

    if (!RegisterClass(&wc)) {
        GlobalCleanup();
        return FALSE;
    }

    //
    // setup program's tty child window class
    //
    wc.lpfnWndProc      = (WNDPROC) TTYChildProc;
    wc.hInstance        = hInst;
    wc.hCursor          = LoadCursor(NULL, IDC_IBEAM);
    wc.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszClassName    = "MTTTYChildClass";
    wc.lpszMenuName     = NULL;
    wc.hIcon            = NULL;

    if (!RegisterClass(&wc)) {
        GlobalCleanup();
        return FALSE;
    }

    //
    // create main window
    //
    ghwndMain = CreateWindow("MTTTYClass", "Multi-threaded TTY",
            WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
            STARTXWINDOW, STARTYWINDOW,
            MAXXWINDOW, MAXYWINDOW,
            NULL, NULL, hInst, NULL);

    if (ghwndMain == NULL) {
        GlobalCleanup();
        return FALSE;
    }

    ShowWindow( ghwndMain, nShowCmd ) ;   
    UpdateWindow( ghwndMain ) ;

    ghInst = hInst;
    ghAccel = LoadAccelerators( hInst, MAKEINTRESOURCE( IDR_MTTTYACCELERATOR) ) ;

    return TRUE;
}


/*-----------------------------------------------------------------------------

FUNCTION: MTTTYWndProc(HWND, UINT, WPARAM, LPARAM)

PURPOSE: Window Procedure for main window

PARAMETERS:
    hwnd    - window handle
    message - window message
    wParam  - window message parameter (depends on message)
    lParam  - window message parameter (depends on message)

RETURN:
    If message is process, return value is 0
    If message is not processed, then it is passed to DefWindowProc
    and the return value from that function is returned
    
HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
int WINAPI MTTTYWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) 
    {
        case WM_CREATE:
            //
            // since main window is created, I can now open all other windows
            //
            InitTTYInfo();
            OpenTTYChildWindow(hwnd);
            OpenSettingsToolbar(hwnd);
            OpenStatusToolbar(hwnd);
            ChangeConnection(hwnd, CONNECTED(TTYInfo));
            break;

        case WM_DESTROY: 
            //
            // since main windows is being destroyed, so same to other windows
            //
            DestroyTTYInfo();
            DestroyWindow(ghWndToolbarDlg);
            DestroyWindow(ghWndStatusDlg);
            DestroyWindow(ghWndTTY);
				
            GlobalCleanup();
            PostQuitMessage(0);
            break;

        case WM_GETMINMAXINFO:
            {
                //
                // make sure that main window doesn't get smaller than
                // the minimum child windows.
                //
                LPMINMAXINFO lpTemp;
                POINT        ptTemp;

                lpTemp = (LPMINMAXINFO) lParam;
      
                ptTemp.x = (long) lpTemp->ptMinTrackSize.x;
                ptTemp.y = (long) gcyMinimumWindowHeight;

                lpTemp->ptMinTrackSize = ptTemp;
            }
        
            break;

        case WM_SIZE:
            {
                //
                // main window size has changed,
                // so I need to change the positions of child windows
                //
                WORD wTop;
                WORD wHeight;
                WORD wWidth = LOWORD(lParam);

                //
                // put Settings window at top
                //
                wHeight = SETTINGSFACTOR*gwBaseY;
                wTop = 0;
                MoveWindow(ghWndToolbarDlg, 0,wTop, wWidth, wHeight, TRUE);

                //
                // put Status window at bottom
                //
                wHeight = STATUSFACTOR*gwBaseY;
                wTop = HIWORD(lParam) - wHeight;
                MoveWindow(ghWndStatusDlg, 0, wTop, wWidth, wHeight, TRUE);

                //
                // put TTTY window right in the middle
                // height = whole window - height of two previous windows
                //
                wHeight = HIWORD(lParam) - ((STATUSFACTOR + SETTINGSFACTOR)*gwBaseY);
                wTop = SETTINGSFACTOR*gwBaseY;
                MoveWindow(ghWndTTY, 0, wTop, wWidth, wHeight, TRUE);
            }
         
            break;

      case WM_COMMAND:       
            CmdDispatch(LOWORD(wParam), hwnd, lParam);        
            break;

      case WM_CHAR:
            SetFocus(ghWndTTY);
            SendMessage(ghWndTTY, WM_CHAR, wParam, lParam);
            break;

      case WM_CLOSE:
            if (DisconnectOK()) {
                if (CONNECTED(TTYInfo)) {
                    if (TRANSFERRING(TTYInfo))
                        TransferFileTextEnd();
                    BreakDownCommPort();
                }
                DestroyWindow(hwnd);
            }
            break;

      default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }

    return 0L;
}

/*-----------------------------------------------------------------------------

FUNCTION: CmdDispatch(int, HWND)

PURPOSE: Responds to menu selections

PARAMETERS:
    iMenuChoice - ID of menu choice (from resource file)
    hwnd        - window handle of menu owner

HISTORY:   Date:      Author:     Comment:
           10/27/95   AllenD      Wrote it

-----------------------------------------------------------------------------*/
void CmdDispatch(int iMenuChoice, HWND hwnd, LPARAM lParam)
{
    static char szFileName[MAX_PATH] = {0};

    switch (iMenuChoice) 
    {
        case ID_HELP_ABOUTMTTTY:
            CmdAbout(hwnd);
            break;

        case ID_TRANSFER_SENDFILETEXT:
            {
                char * szFilter = "Text Files\0*.TXT\0";
                OPENFILENAME ofn = {0};

                ofn.lStructSize = sizeof(OPENFILENAME);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFilter = szFilter;
                ofn.lpstrFile = szFileName;
                ofn.nMaxFile = MAX_PATH;
                ofn.lpstrTitle = "Send File";
                ofn.Flags = OFN_FILEMUSTEXIST;

                if (!GetOpenFileName(&ofn))
                    break;
                
                if (TRUE)
                    TransferFileTextStart(szFileName);
            }
            break;

        case ID_TRANSFER_RECEIVEFILETEXT:
            {
                char * szFilter = "Text Files\0*.TXT\0";
                OPENFILENAME ofn = {0};

                ofn.lStructSize = sizeof(OPENFILENAME);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFilter = szFilter;
                ofn.lpstrFile = szFileName;
                ofn.nMaxFile = MAX_PATH;
                ofn.lpstrTitle = "Receive File";
                ofn.Flags = OFN_OVERWRITEPROMPT;

                if (!GetSaveFileName(&ofn))
                    break;

                ReceiveFileText(szFileName);
            }
            break;

        case ID_TRANSFER_ABORTSENDING:
            // was abort sent from the abort button?
            if (LOWORD(lParam) == IDC_ABORTBTN) {
                // am I in a transfer repeat?
                if (REPEATING(TTYInfo))
                    TransferRepeatDestroy();
                // am I in a normal recieve state, then stop sending
                else if (gdwReceiveState == RECEIVE_TTY)
                    TransferFileTextEnd();
                // if I am not in a normal receive state, then stop capturing
                else
                    gfAbortTransfer = TRUE;
            }
            else
                // transfer abort was sent by transfer thread   
                TransferFileTextEnd();
            break;

        case ID_TRANSFER_SENDREPEATEDLY:
            {
                DWORD dwFreq;
                char * szFilter = "Text Files\0*.TXT\0";
                OPENFILENAME ofn = {0};

                ofn.lStructSize = sizeof(OPENFILENAME);
                ofn.hwndOwner = hwnd;
                ofn.lpstrFilter = szFilter;
                ofn.lpstrFile = szFileName;
                ofn.nMaxFile = MAX_PATH;
                ofn.lpstrTitle = "Send File Repeatedly";
                ofn.Flags = OFN_FILEMUSTEXIST;

                if (!GetOpenFileName(&ofn))
                    break;

                dwFreq = GetAFrequency();
                
                TransferRepeatCreate(szFileName, dwFreq);
            }
            break;

        case ID_TRANSFER_ABORTREPEATEDSENDING:
            TransferRepeatDestroy();
            break;

        case ID_TTY_CLEAR:
            ClearTTYContents();
            InvalidateRect(ghWndTTY, NULL, TRUE);
            break;

        // The following correspond to menu choices and buttons in the settings dlog
        case IDC_FONTBTN:
        case IDC_COMMEVENTSBTN:
        case IDC_FLOWCONTROLBTN:
        case IDC_TIMEOUTSBTN:
            SendMessage(ghWndToolbarDlg, WM_COMMAND, (WPARAM) iMenuChoice, (LPARAM) GetDlgItem(ghWndToolbarDlg, iMenuChoice));
            break;

        case ID_FILE_CONNECT:
            if (SetupCommPort() != NULL)
                ChangeConnection(hwnd, CONNECTED(TTYInfo));
            break;

        case ID_FILE_DISCONNECT:
            if (BreakDownCommPort())
                ChangeConnection(hwnd, CONNECTED(TTYInfo));
            break;

        case ID_FILE_EXIT:
            PostMessage(hwnd, WM_CLOSE, 0, 0);
            break;

⌨️ 快捷键说明

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