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

📄 viewsp.cs

📁 一个远程终端软件的源码
💻 CS
📖 第 1 页 / 共 2 页
字号:
//  Copyright (C) 2005, 2007 Rocky Lo. All Rights Reserved.
//
//  This file is part of the VNC system.
//
//  The VNC system is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// If the source code for the VNC system is not available from the place 
// whence you received this file, check http://www.uk.research.att.com/vnc or contact
// the authors on vnc@uk.research.att.com for information on obtaining it.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Vnc.Viewer
{
  internal class ViewSp : View
  {
    private const byte CursorDelta = 100; // TODO: Find an optimal value.
    private const UInt16 InputDelta = 5000; // TODO: Find an optimal value.
    private const UInt16 MouseIdleDelta = 1000; // TODO: Find an optimal value.
    private const UInt16 LowSpeed = 3; // TODO: Find an optimal value.
    private const UInt16 NormalSpeed = 5; // TODO: Find an optimal value.
    private const UInt16 HighSpeed = 7; // TODO: Find an optimal value.

    private Timer cursorTimer = new Timer();
    private int xSpeed = 0;
    private int ySpeed = 0;

    private Timer inputTimer = new Timer();
    private TextBox inputBox = new TextBox();

    private Timer mouseIdleTimer = new Timer();

    private MenuItem mouseAccelModeMenu = new MenuItem();
    private EventHandler mouseAccelModeHdr = null;

    [DllImport("coredll.dll")]
    private static extern short GetKeyState(int keyCode);

    private bool IsCapsLocked()
    {
      return ((ushort)GetKeyState((int)Keys.CapsLock) & 0xffff) != 0;
    }

    private void LeftClicked(object sender, EventArgs e)
    {
      if(isSetSingleWinPending)
        SetSingleWin(mouseX, mouseY);
      else
      {
        OnMouseEvent(mouseX, mouseY, true, rightBtnDown);
        OnMouseEvent(mouseX, mouseY, false, rightBtnDown);
      }
    }

    private void RightClicked(object sender, EventArgs e)
    {
      if(isSetSingleWinPending)
        SetSingleWin(mouseX, mouseY);
      else
      {
        OnMouseEvent(mouseX, mouseY, leftBtnDown, true);
        OnMouseEvent(mouseX, mouseY, leftBtnDown, false);
      }
    }

    private void CancelExitFullScrn()
    {
      timer.Enabled = false;
      InvalidateTapHoldCircles();
      // Send the "delayed" mouse event.
      OnMouseEvent(mouseX, mouseY, leftBtnDown, rightBtnDown);
    }

    private void CursorTicked(object sender, EventArgs e)
    {
      // This is what we call friction.
      if(xSpeed > 0)
        xSpeed--;
      else if(xSpeed < 0)
        xSpeed++;
      if(ySpeed > 0)
        ySpeed--;
      else if(ySpeed < 0)
        ySpeed++;

      if(xSpeed == 0 && ySpeed == 0)
        return;
      else
      {
        if(connOpts.ViewOpts.SendMouseLocWhenIdle)
          ResetMouseIdleTimer();
      }

      Rectangle usable = UsableRect;

      mouseX += xSpeed;
      int tempX = mouseX;
      mouseX = Math.Max(Math.Min(mouseX, usable.Right - 1), usable.Left);
      tempX -= mouseX;
      mouseY += ySpeed;
      int tempY = mouseY;
      mouseY = Math.Max(Math.Min(mouseY, usable.Bottom - 1), usable.Top);
      tempY -= mouseY;

      if(tempX == 0 && tempY == 0)
      {
        // Erase the "old" cross and draw the new one.
        Rectangle rect = new Rectangle();
        rect.X = (xSpeed > 0)? mouseX - xSpeed : mouseX;
        rect.Y = (ySpeed > 0)? mouseY - ySpeed : mouseY;
        rect.X -= BigCircleRadius / 2;
        rect.Y -= BigCircleRadius / 2;
        rect.Width = Math.Abs(xSpeed) + BigCircleRadius;
        rect.Height = Math.Abs(ySpeed) + BigCircleRadius;
        Invalidate(rect);
      }
      else
      {
        if(hScrlBar.Visible && tempX != 0)
        {
          if((hScrlBar.Value <= 0 && tempX <= 0) || (hScrlBar.Value >= hScrlBar.Maximum + 1 - hScrlBar.LargeChange && tempX >= 0))
            xSpeed = 0;
          else
            hScrlBar.Value = Math.Max(0, Math.Min(hScrlBar.Value + tempX, hScrlBar.Maximum + 1 - hScrlBar.LargeChange));
        }
        if(vScrlBar.Visible && tempY != 0)
        {
          if((vScrlBar.Value <= 0 && tempY <= 0) || (vScrlBar.Value >= vScrlBar.Maximum + 1 - vScrlBar.LargeChange && tempY >= 0))
            ySpeed = 0;
          else
            vScrlBar.Value = Math.Max(0, Math.Min(vScrlBar.Value + tempY, vScrlBar.Maximum + 1 - vScrlBar.LargeChange));
        }
        Invalidate();
      }
    }

    protected override void Ticked(object sender, EventArgs e)
    {
      if(!timer.Enabled)
      {
        // I am not entirely sure whether this will ever occur.
        // One possibility is that the Timer event is queued
        // before OnMouseUp or OnMouseMove disables the timer.
        return;
      }

      if(tapHoldCnt > NumTapHoldCircles)
      {
        // We won't get a KeyUp after going back to window mode. Set the flag here.
        rightBtnDown = false;

        timer.Enabled = false;
        ToggleFullScrn();
      }
      else
      {
        tapHoldCnt++;
        DrawTapHoldCircles(tapHoldCnt, App.Blue);
      }
    }

    private void InputTicked(object sender, EventArgs e)
    {
      // We have to set Enabled to false before hiding inputBox.
      // Otherwise the input mode will not be set correctly after exiting extended input mode.
      inputBox.Enabled = false;
      inputBox.Visible = false;
      inputTimer.Enabled = false;
      Focus();
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
      base.OnKeyDown(e);
      if(e.Handled)
        return;

      if(timer.Enabled && e.KeyCode != Keys.F2)
        CancelExitFullScrn();

      switch(e.KeyCode)
      {
        case Keys.F1:
          leftBtnDown = true;
          // If isSetSingleWinPending, we just keep track of the state.
          if(isSetSingleWinPending)
            return;
          OnMouseEvent(mouseX, mouseY, leftBtnDown, rightBtnDown);
          break;
        case Keys.F2:
          if(timer.Enabled) // Repeated KeyDown in this case.
            return;
          rightBtnDown = true;
          // Refer to the comment of isSetSingleWinPending.
          if(isSetSingleWinPending)
            return;
          timer.Enabled = true; // Tap-and-Hold active.
          tapHoldCnt = 0;
          break;
        case Keys.Up:
          if(connOpts.ViewOpts.MouseAccelMode)
            ySpeed -= LowSpeed;
          else
          {
            switch(connOpts.ViewOpts.MouseSpeed)
            {
              case MouseSpeed.Low:
                ySpeed = -LowSpeed;
                break;
              case MouseSpeed.High:
                ySpeed = -HighSpeed;
                break;
              default:
                ySpeed = -NormalSpeed;
                break;
            }
          }
          break;
        case Keys.Down:
          if(connOpts.ViewOpts.MouseAccelMode)
            ySpeed += LowSpeed;
          else
          {
            switch(connOpts.ViewOpts.MouseSpeed)
            {
              case MouseSpeed.Low:
                ySpeed = LowSpeed;
                break;
              case MouseSpeed.High:
                ySpeed = HighSpeed;
                break;
              default:
                ySpeed = NormalSpeed;
                break;
            }
          }
          break;
        case Keys.Left:
          if(connOpts.ViewOpts.MouseAccelMode)
            xSpeed -= LowSpeed;
          else
          {
            switch(connOpts.ViewOpts.MouseSpeed)
            {
              case MouseSpeed.Low:
                xSpeed = -LowSpeed;
                break;
              case MouseSpeed.High:
                xSpeed = -HighSpeed;
                break;
              default:
                xSpeed = -NormalSpeed;
                break;
            }
          }
          break;
        case Keys.Right:
          if(connOpts.ViewOpts.MouseAccelMode)
            xSpeed += LowSpeed;
          else
          {
            switch(connOpts.ViewOpts.MouseSpeed)
            {
              case MouseSpeed.Low:
                xSpeed = LowSpeed;
                break;
              case MouseSpeed.High:
                xSpeed = HighSpeed;
                break;
              default:
                xSpeed = NormalSpeed;
                break;
            }
          }
          break;
        case Keys.F8:
          inputTimer.Enabled = true;
          inputBox.Text = String.Empty;

⌨️ 快捷键说明

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