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

📄 vt102em.cpp

📁 使用BorlandC++4.5编译的一个MUD客户端程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    for (int i = lastRow; i >= firstRow; i--)
        _PutTextOneLine(hdc, i);
    
    SetBkMode(hdc, oldBkMode); // Restore old drawing modes
    SetBkColor(hdc, oldColor);  
    SelectObject(hdc, oldFont); // Unselect font
}

// ------------------------------------------------------------------------
// _PutCursor - _Paint helper function
//
// This assumes that all other painting is finished, and it may Xor or
// whatever.
//
void VT102Emulator::_PutCursor(HDC hdc)
{
    HFONT oldFont = (HFONT)SelectObject(hdc, _font);
    COLORREF oldColor = SetBkColor(hdc, _cursorBackground);
    int oldBkMode = (int)SetBkMode(hdc, OPAQUE); // Bright background!
    
    Pos curPos(_screen.Cursor());
    _TextToDevice(curPos); // Convert to pixel coords
    SetTextColor(hdc, _cursorColor);
    TextOut(hdc,
            curPos.X(),
            curPos.Y(),
            &_screen.TextElement(_screen.Cursor()),
            1);
    
    SetBkMode(hdc, oldBkMode); // Restore old drawing modes
    SetBkColor(hdc, oldColor);  
    SelectObject(hdc, oldFont); // Unselect font
}

// ------------------------------------------------------------------------
// _Register - Register this class
//
// To be called only AFTER WinMain assigns appropriate
// values to static members of Main and only if no previous
// instances of the program exist (a previous instance would
// have already performed the registration).
//
void VT102Emulator::_Register()
{
    WNDCLASS wndclass; // Structure used to register Windows class.
    
    wndclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc   = Global::WndProc;
    wndclass.cbClsExtra    = 0;
    
    // Reserve extra bytes for each instance of the window;
    // we will use these bytes to store a pointer to the C++
    // (VT102Emulator) object corresponding to the window.
    // the size of a 'this' pointer depends on the memory model.
    
    wndclass.cbWndExtra    = sizeof(VT102Emulator *);
    wndclass.hInstance     = Global::Instance();
    wndclass.hIcon         = LoadIcon(Global::Instance(), "vt");
    wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wndclass.lpszMenuName  = NULL;
    wndclass.lpszClassName = _szClassName;
    
    // If we can't register the class, quit now and get out
    if (! RegisterClass(&wndclass))
        scream_and_die(EC_FatalError, ERR_RegisterClass);
}

// ------------------------------------------------------------------------
// InitScreen - load the monospace font, set up the screen, etc.
//
void VT102Emulator::_InitScreen()
{
    
    _font = (HFONT)GetStockObject(OEM_FIXED_FONT);
    if (NULL == _font)
        scream_and_die(EC_FatalError, ERR_LoadFont);
    
    _cursorPen = (HPEN)GetStockObject(NULL_PEN);
    
    // Get a DC, and select the font into it so we can get its metrics:
    
    HDC hdc = GetDC(Handle()); // Get a device context for a second
    
    // TextMetric won't be right unless I select the font into the GC!
    HFONT oldFont = (HFONT)SelectObject(hdc, _font);
    
    TEXTMETRIC fontMetric; // Get font info
    if (0 == GetTextMetrics(hdc, &fontMetric))
        scream_and_die(EC_FatalError, ERR_GetTextMetric); 
    
    SelectObject(hdc, oldFont); // Restore old font, just in case
    
    // Set the grid size from the text metrics
    _fontSize.Width() = fontMetric.tmMaxCharWidth;
    _fontSize.Height() = fontMetric.tmHeight + 1;
    
    ReleaseDC(Handle(), hdc); // Done with this now
    
    // Well, now that we know how big the fonts are, size the window to
    // fit the fonts, and center on screen.
    
    RECT screen_area;
    GetWindowRect(GetDesktopWindow(), &screen_area);
    int win_width = (_screen.Width() + 1) * _fontSize.Width();
    int win_height = (_screen.Height() + 2) * _fontSize.Height();
    int win_x = (screen_area.right - win_width) / 2;
    int win_y = (screen_area.bottom - win_height) / 2;
    SetWindowPos(Handle(), (HWND)NULL,
                 win_x, win_y, win_width, win_height,
                 SWP_NOZORDER | SWP_NOACTIVATE);

    // Add special controls to the top of the system menu.  First
    // one inserted will be on the bottom...

    // Separator
    InsertMenu(GetSystemMenu(Handle(), FALSE),
               0,
               MF_BYPOSITION | MF_SEPARATOR,
               -1,
               NULL);
    
    InsertMenu(GetSystemMenu(Handle(), FALSE),
               0,
               MF_BYPOSITION | MF_STRING,
               VT_SYSMENU_DEBUG,
               "Toggle Debug Mode");
    
    InsertMenu(GetSystemMenu(Handle(), FALSE),
               0,
               MF_BYPOSITION | MF_STRING,
               VT_SYSMENU_CONSOLE,
               "Console Mode");
    
    DrawMenuBar(Handle());
}

// ------------------------------------------------------------------------
// WndProc-like function for the main window.  This function is called
//   indirectly by the "real" WndProc
//
LRESULT VT102Emulator::WndProc (UINT iMessage,
                                WPARAM wParam,
                                LPARAM lParam)
{
    int     repeat;
    LRESULT ret_val = 0;         // Return value
    int     call_defwndproc = 1; // 
    switch (iMessage)
    {
      case WM_CREATE: // Ignore
        call_defwndproc = 0;
        ret_val = 0;
        break;
        
      case WM_PAINT: // Paint yourself!
        _Paint();
        call_defwndproc = 0;
        ret_val = 0;
        break;
        
      case WM_CHAR: // Regular char events
        repeat = (lParam & 0xFF);
        while(repeat--)
            process_incoming(CharKeyEvent(wParam).AsString());
        
        call_defwndproc = 0;
        ret_val = 0;
        break;

      case WM_COMMAND:
        process_incoming(AcceleratorKeyEvent(wParam).AsString());
        call_defwndproc = 0;
        ret_val = 0;
        break;
        
      case WM_DESTROY:
        PostQuitMessage (0);
        call_defwndproc = 0;
        ret_val = 0;
        break;

      case WM_SIZE:
        if (_initialized)
        {
            _Size(wParam, LOWORD(lParam), HIWORD(lParam));
            call_defwndproc = 0;
            ret_val = 0;
        };
        break;

      case WM_SYSCOMMAND:  // System menu command
        ret_val = _SysMenuCommand(wParam & 0xFFF0);
        if (0 == ret_val)
            call_defwndproc = 0;
        break;
        
      default:
        // Pass message to inheriting classes
        if (0 != _Dispatch(iMessage, wParam, lParam, ret_val))
            call_defwndproc = 0;

        break;
    }
    if (call_defwndproc)
        ret_val = DefWindowProc(Handle(), iMessage, wParam, lParam);

    return ret_val; // Handled the message
}

// ------------------------------------------------------------------------
// _Size - called when the window is resized
//
// Parameters:
//   type - type of event (see WM_SIZE event)
//   width - new window width (pixels?)
//   height - ""         height
//
// Notes:
//   On a resize, pretend we sent a TIOCGWINSZ signal to VT.  The cool thing
//   about this version is that we don't have to worry about accidentally
//   resizing asynchronously, because we didn't catch a signal.  
//
void VT102Emulator::_Size(WPARAM type, int width, int height)
{
    switch (type)
    {
        // Ignore minimize, because width==height==0, so all the VT window
        // sizes get screwed up (i.e., input window height = 1 line).
        // Additionally, we don't care if we are hidden or revealed.
        // who cares, right?  We only draw in response to expose events
        // anyway.  <shrug>
      case SIZE_MINIMIZED:        // We are minimized
      case SIZE_MAXHIDE:          // Something else hid us
      case SIZE_MAXSHOW:          // Something else was resized
        break;

      default:                    // We have been resized in some way
        width /= _fontSize.Width();   // Truncated to smaller int!  How nice!
        height /= _fontSize.Height();

        if (width < 15)       // These size parameters are enforced by vt
            width = 15;
        if (height < 4)
            height = 4;

        // Did the size change?  (Size does not change on minimize/restore)
        if (width != _screen.Width() || height != _screen.Height())
        {
            // If this call fails, the xalloc will pass by it
            _screen.Resize(width, height); // Resize screen buffer
            resize_screen(height, width); // Tell vt about it!
        };
        break;
    };
}

// ------------------------------------------------------------------------
// _SysMenuCommand - handle mods to the sysmenu
//
// Parameters:
//   command - the menu command id
//
// Returns:
//   zero if the command is processed, nonzero otherwise
//
LRESULT VT102Emulator::_SysMenuCommand(WPARAM command)
{
    LRESULT ret_val = 1;
    switch (command)
    {
      case VT_SYSMENU_DEBUG:
        ret_val = 0;
        if (debug)
            debug = 0;
        else
            debug = 1;
        break;

      case VT_SYSMENU_CONSOLE:
        ret_val = 0;
        if (! console_mode)       // Avoid reentrancy problems
            console();
        break;
        
      default:                    // Not my problem
        break;
    };
    return ret_val;
}

// ------------------------------------------------------------------------
// This is called for all events that are not routed to the above functions
// Classes that handle the message should return nonzero, and set the
// value of return_value_ret to an appropriate value, otherwise they
// should return zero.
//
int VT102Emulator::_Dispatch(UINT msg,
                             WPARAM wParam,
                             LPARAM lParam,
                             LRESULT & return_value_ret)
{
    (void)wParam; // Silence 'not used' warning
    (void)lParam;
    
    int ret_code = 0;
    
    switch (msg)
    {
      case VT_SOCKET_MSG:
        _DoSocketEvent(WSAGETSELECTEVENT(lParam), WSAGETSELECTERROR(lParam));
        return_value_ret = 1; 
        ret_code = 1; // Message handled
        break;
        
      default:
        break;
     };
    return ret_code; // Not handled by default
};

void VT102Emulator::_DoSocketEvent(int event, int error_code)
{
    (void) event;            // Not used for now 
    (void) error_code;
    
    if (io_check(0,0))       // Poll sockets.  Input?
    {
        while (!io_cycle())  // Process buffered input.
            ;
        
        curs_input();        // Put cursor back in input window.
    };
};


// EOF //






@2.2log@Added debug toggle and console mode switch to system menu.@text@d3 1a3 1// $Id: vt102em.cpp 2.1 1995/10/24 15:52:51 tsurace Exp tsurace $d5 3d628 1a628 1    LRESULT ret_val;d632 1d640 1@2.1log@Roll.@text@d3 4a6 4// $Id: vt102em.cpp 1.8 1995/10/19 00:58:27 tsurace Beta tsurace $// $Log: vt102em.cpp $// Revision 1.8  1995/10/19  00:58:27  tsurace// Changed title.d8 3d50 1d67 4d83 1a83 1static const char _TITLE[] = "VT-Win 1.0 $State: Beta $";d220 1a220 1    if (SOCKET_ERROR == WSAAsyncSelect (s, Handle(), MY_SOCKET_MSG, events))d468 22d491 1d502 3a504 2    int repeat;    LRESULT ret_val = 0;d508 1d514 1d521 1a521 1            _KeyDown(KeyEvent(wParam, lParam));d523 7d535 1d543 1d547 6d556 3a558 5        if (0 == _Dispatch(iMessage, wParam, lParam, ret_val))        {            // Not handled, call default proc            ret_val = DefWindowProc(Handle(), iMessage, wParam, lParam);        };d561 3a567 39// _KeyDown - default keystroke handler//// Classes that override this might want to call the base class version.//// Parameters://   ev - keystroke event//void VT102Emulator::_KeyDown(const KeyEvent & ev){    char the_char;    int  use_this_char = 0;    if (ev.Extended())    {        if (ev.VKeyCode() == '\027') // Get escape key        {            the_char = '\027';            use_this_char = 1;        };    }    else    {        the_char = (char)ev.Ascii();        use_this_char = 1;    };    

⌨️ 快捷键说明

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