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

📄 childview.cpp

📁 Synaptics触摸板应用开发包(SDK)
💻 CPP
字号:
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChildView

CChildView::CChildView()
{
  m_eGraphSource = eRaw;

  m_iNewBackgroundNeeded = 1;
  m_iSequence = 0;

  m_LogName = "SynView.log";
  m_LogFile = 0;
}

CChildView::~CChildView()
{

}


BEGIN_MESSAGE_MAP(CChildView,CWnd )
  //{{AFX_MSG_MAP(CChildView)
  ON_WM_PAINT()
  ON_WM_CREATE()
  ON_WM_SIZE()
  ON_WM_ERASEBKGND()
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
  if (!CWnd::PreCreateWindow(cs))
    return FALSE;

  cs.dwExStyle |= WS_EX_CLIENTEDGE;
  cs.style &= ~WS_BORDER;
  cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
    ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

  return TRUE;
}

int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
  if (CWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
  
  SelectDevice(0); // Select the default device.

  return 0;
}

void CChildView::SelectDevice(DWORD hDevice)
{
  m_Device->Select(hDevice);

  m_Device->SetSynchronousNotification(this);

  if (m_Device.GetLongProperty(SP_DeviceType) == SE_DeviceTouchPad)
  {
    Contexts[0].SetParams(8192, 0);
    Contexts[1].SetParams(8192, 0);
    Contexts[2].SetParams(256, 0);

    m_eGraphSource = eRaw;
  }
  else if (m_Device.GetLongProperty(SP_DeviceType) == SE_DeviceStyk)
  {
    Contexts[0].SetParams(256, -256);
    Contexts[1].SetParams(256, -256);
    Contexts[2].SetParams(64, -64);

    m_eGraphSource = eRaw;
  }
  else // Assume mouse, place zero in middle of graph area
  {
    Contexts[0].SetParams(32, -32);
    Contexts[1].SetParams(32, -32);
    Contexts[2].SetParams(8, -8);
    m_eGraphSource = eMickeys;
  }

  Contexts[3].SetParams(1024, 0);
}

long CChildView::OnSynDevicePacket(long lSeq) 
{
  m_Device->LoadPacket(m_Packet);

  if (m_LogFile)
    fprintf(m_LogFile, "x: %.4d, y: %.4d, z: %.4d, dx: %.4d, dy: %.4d\n",
      m_Packet.GetLongProperty(SP_XRaw), 
      m_Packet.GetLongProperty(SP_YRaw), 
      m_Packet.GetLongProperty(SP_ZRaw),
      m_Packet.GetLongProperty(SP_XMickeys), 
      m_Packet.GetLongProperty(SP_YMickeys));

  PacketData sPacket(&m_Packet);

  m_Undrawn.Add(sPacket);
  InvalidateGraphArea();

  return 0;
}

void CChildView::OnSize(UINT nType, int cx, int cy)
{
  RECT ClientRect;
  GetClientRect(&ClientRect);

  RECT Rect = ClientRect;

  if (0) 
  {
    Rect.bottom = ClientRect.top + (ClientRect.bottom - ClientRect.top)/3;
    Contexts[0].SetRect(Rect);

    Rect.top = Rect.bottom + 1;
    Rect.bottom = ClientRect.top + 2*(ClientRect.bottom - ClientRect.top)/3;
    Contexts[1].SetRect(Rect);

    Rect.top = Rect.bottom + 1;
    Rect.bottom = ClientRect.bottom;
    Contexts[2].SetRect(Rect);
  }
  else
  {
    Contexts[0].SetRect(ClientRect);
    Contexts[1].SetRect(ClientRect);
    Contexts[2].SetRect(ClientRect);
  }

  Contexts[3].SetRect(ClientRect);
}

void CChildView::PaintBackGround(CDC *cdc, GraphContext *Context)
{
  FillRect(cdc->m_hDC, &Context->m_Rect, 
    (HBRUSH) GetStockObject(WHITE_BRUSH));

  HFONT hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
  HFONT hFontLast = (HFONT) cdc->SelectObject(hFont);

  for (int i = 1; i <= Context->m_divisions; i++)
  {
    int y  = Context->m_Rect.top +
      i*(Context->m_Rect.bottom - Context->m_Rect.top)/
      (Context->m_divisions + 1);

    cdc->MoveTo(Context->m_Rect.left, y);
    cdc->LineTo(Context->m_Rect.right, y);

    char label[16];
    sprintf(label, "%d", Context->m_Top - 
      i*(Context->m_Top - Context->m_Bottom)/(Context->m_divisions + 1));
    cdc->TextOut(Context->m_Rect.right - 32, y - 16, label, strlen(label));
  }

  cdc->SelectObject(hFontLast);
}

void CChildView::PaintUnifiedBackGround(CDC *cdc, GraphContext *Context)
{
  FillRect(cdc->m_hDC, &Context->m_Rect, 
    (HBRUSH) GetStockObject(WHITE_BRUSH));

  HFONT hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
  HFONT hFontLast = (HFONT) cdc->SelectObject(hFont);
  COLORREF ColorLast = cdc->GetTextColor();
  UINT AlignLast = cdc->GetTextAlign();

  cdc->SetTextAlign(TA_RIGHT | TA_TOP);

  for (int i = 1; i <= Context->m_divisions; i++)
  {
    int y  = Context->m_Rect.top +
      i*(Context->m_Rect.bottom - Context->m_Rect.top)/
      (Context->m_divisions + 1);

    cdc->MoveTo(Context->m_Rect.left, y);
    cdc->LineTo(Context->m_Rect.right, y);

    char label[16];

    GraphContext *tContext = Context;
    sprintf(label, "%d", tContext->m_Top - 
      i*(tContext->m_Top - tContext->m_Bottom)/(tContext->m_divisions + 1));
    cdc->SetTextColor(RGB(0, 255, 0));
    cdc->TextOut(tContext->m_Rect.left + 32, y - 16, label, strlen(label));

    tContext++;
    sprintf(label, "%d", tContext->m_Top - 
      i*(tContext->m_Top - tContext->m_Bottom)/(tContext->m_divisions + 1));
    cdc->SetTextColor(RGB(0, 0, 255));
    cdc->TextOut((tContext->m_Rect.right - tContext->m_Rect.left)/2, 
      y - 16, label, strlen(label));
  
    tContext++;
    sprintf(label, "%d", tContext->m_Top - 
      i*(tContext->m_Top - tContext->m_Bottom)/(tContext->m_divisions + 1));
    cdc->SetTextColor(RGB(255, 0, 0));
    cdc->TextOut(tContext->m_Rect.right - 4, y - 16, label, strlen(label));  
  }

  cdc->SelectObject(hFontLast);
  cdc->SetTextColor(ColorLast);
  cdc->SetTextAlign(AlignLast);
}

void CChildView::PaintGraph(CDC *cdc, GraphContext *Context, HPEN hpen,
  unsigned int position, int value)
{
  POINT point;
  point.x = Context->m_Rect.left + (position % 
    (Context->m_Rect.right - Context->m_Rect.left));
  point.y = Context->m_Rect.top + (value - Context->m_Top)*
    (Context->m_Rect.bottom - Context->m_Rect.top)/
    (Context->m_Bottom - Context->m_Top);

  if (Context->m_Last.x < point.x)
  {
    HPEN hlast = (HPEN) cdc->SelectObject(hpen);

    cdc->MoveTo(Context->m_Last.x, Context->m_Last.y);
    cdc->LineTo(point.x, point.y);

    cdc->SelectObject(hlast);
  }
  else
  {
    if (Context == Contexts)
      PaintUnifiedBackGround(cdc, Context);

    cdc->MoveTo(Context->m_Rect.left, Context->m_Rect.bottom);
    cdc->LineTo(Context->m_Rect.right, Context->m_Rect.bottom);
  }

  Context->m_Last = point;
}


void CChildView::OnPaint() 
{
  CPaintDC dc(this);

  if (m_iNewBackgroundNeeded)
    OnEraseBkgnd(&dc);

  for (PacketData *pPacket = m_Undrawn.Read(); pPacket; 
    pPacket = m_Undrawn.Read())
  {

    if (m_eGraphSource == eMickeys &&
        !GetX(pPacket) && !GetY(pPacket))
      break; // Only paint non-zero relative packets.

    HPEN hpen = CreatePen(PS_SOLID, 0, RGB(0, 255, 0));
    PaintGraph(&dc, Contexts, hpen, m_iSequence, GetX(pPacket));
    DeleteObject(hpen);

    hpen = CreatePen(PS_SOLID, 0, RGB(0, 0, 255));
    PaintGraph(&dc, Contexts + 1, hpen, m_iSequence, GetY(pPacket));
    DeleteObject(hpen);

    hpen = CreatePen(PS_SOLID, 0, RGB(255, 0, 0));
    PaintGraph(&dc, Contexts + 2, hpen, m_iSequence, GetZ(pPacket)); 
    DeleteObject(hpen);

    m_iSequence += 1;
  }
}

BOOL CChildView::OnEraseBkgnd(CDC* pDC) 
{
  PaintUnifiedBackGround(pDC, Contexts);

  m_iNewBackgroundNeeded = 0;

  return TRUE;
}

FILE *CChildView::OpenLogFile(CString &Filename)
{
  if (m_LogFile)
    fclose(m_LogFile);
  m_LogFile = 0;

  m_LogName = Filename;

  if (!Filename.IsEmpty())
    return m_LogFile = fopen(Filename, "a+");

  
  return m_LogFile;
}

⌨️ 快捷键说明

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