📄 childviewtx.cpp
字号:
// ChildView.cpp : implementation of the CChildView class
//
#include "stdafx.h"
#include "Draw.h"
#include "ChildViewTx.h"
#include <accctrl.h>
#include <aclapi.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#define _USE_API_SEMAPHORE
#ifdef _USE_API_SEMAPHORE
HANDLE g_sem;
#else
CSemaphore g_sem (1, 1, "JAMSA MEDIA");
#endif
const char *szFileEvent = {"JAMSA"};
const char *szMemFile = {"JAMSAFILE"};
/////////////////////////////////////////////////////////////////////////////
// CChildView
CChildView::CChildView()
{
m_hMap = CreateFileMapping (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, szMemFile);
if (m_hMap != NULL)
m_sfBuf = (BYTE *) MapViewOfFile (m_hMap, FILE_MAP_WRITE, 0, 0, 0);
else
m_sfBuf = NULL;
#ifdef _USE_API_SEMAPHORE
g_sem = CreateSemaphore (NULL, 1, 1, "JAMSAMEDIA");
WaitForSingleObject (g_sem, INFINITE);
#else
CSingleLock semLock (&g_sem);
semLock.Lock ();
#endif
m_bTracking = false;
m_hevMemFile = CreateEvent (NULL, FALSE, TRUE, szFileEvent);
if (m_hevMemFile == NULL)
{
AfxMessageBox ("MemFile event is NULL");
}
m_ptLineStart.x = -1;
m_ptLineStart.y = -1;
if (!StartReceiver ())
{
CString Message;
Message.Format (_T("Could not start receiver process. Error %d"), GetLastError());
AfxMessageBox (Message);
}
#ifdef _USE_API_SEMAPHORE
ReleaseSemaphore (g_sem, 1, NULL);
#else
// Note that the CSingleLock object will unlock
// the semaphore when it goes out of scope.
semLock.Unlock ();
#endif
}
CChildView::~CChildView()
{
if (m_hevMemFile != NULL)
CloseHandle (m_hevMemFile);
if (m_pi.hProcess)
::PostThreadMessage (m_pi.dwThreadId, WM_QUIT, 0, 0);
}
BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}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;
}
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
DoMouseEvent (WM_LBUTTONDOWN, nFlags, point);
}
void CChildView::OnLButtonUp(UINT nFlags, CPoint point)
{
DoMouseEvent (WM_LBUTTONUP, nFlags, point);
}
void CChildView::OnRButtonDown(UINT nFlags, CPoint point)
{
DoMouseEvent (WM_RBUTTONDOWN, nFlags, point);
}
void CChildView::OnRButtonUp(UINT nFlags, CPoint point)
{
DoMouseEvent (WM_RBUTTONUP, nFlags, point);
}
void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
DoMouseEvent (WM_MOUSEMOVE, nFlags, point);
}
void CChildView::DoMouseEvent(UINT message, UINT nFlags, POINT &point)
{
CWindowDC dc (this);
static int nPoints = 0;
static unsigned char *buf = m_sfBuf;
CString Message;
#ifndef _USE_API_SEMAPHORE
CSingleLock semLock (&g_sem);
#endif
switch (message)
{
case WM_LBUTTONDOWN:
m_ptLineStart = point;
break;
case WM_LBUTTONUP:
m_bTracking = false;
m_ptLineStart.x = -1;
m_ptLineStart.y = -1;
break;
case WM_RBUTTONUP:
#ifdef _USE_API_SEMAPHORE
WaitForSingleObject (g_sem, INFINITE);
#else
semLock.Lock();
#endif
m_ptLast.x = -1;
m_ptLast.y = -1;
buf = m_sfBuf;
memcpy (buf, &nPoints, sizeof (int));
#ifdef _USE_API_SEMAPHORE
ReleaseSemaphore (g_sem, 1, NULL);
#else
semLock.Unlock ();
#endif
SetEvent (m_hevMemFile);
nPoints = 0;
break;
case WM_RBUTTONDOWN:
dc.SetPixel (point, 0);
#ifdef _USE_API_SEMAPHORE
// SignalObjectAndWait (g_sem, g_sem, INFINITE, 0);
WaitForSingleObject (g_sem, INFINITE);
#else
semLock.Lock();
#endif
if (m_ptLast.x >= 0)
{
DoMouseLine (dc, m_ptLast, point);
memcpy (buf, &point, sizeof (POINT));
buf += sizeof (POINT);
++nPoints;
}
else
{
memcpy (buf, &nPoints, sizeof (int));
buf += sizeof (int);
}
#ifdef _USE_API_SEMAPHORE
ReleaseSemaphore (g_sem, 1, NULL);
#else
semLock.Unlock();
#endif
m_ptLast = point;
break;
case WM_MOUSEMOVE:
if (nFlags & MK_RBUTTON)
{
DoMouseEvent (WM_RBUTTONDOWN, nFlags, point);
break;
}
if (!(nFlags & MK_LBUTTON))
return;
if (m_bTracking)
{
POINT pt;
pt = point;
if (m_ptLineStart.x >= 0)
{
DoMouseLine (dc, m_ptLineStart, m_ptLineEnd);
DoMouseLine (dc, m_ptLineStart, pt);
}
}
m_ptLineEnd = point;
m_bTracking = true;
break;
}
}
void CChildView::DoMouseLine(CWindowDC &dc, POINT &ptLineStart, POINT &ptLineEnd)
{
CPen *penOld, Pen;
Pen.CreatePen (PS_SOLID, 2, (COLORREF) 0x00ffffff);
int nOldMode = dc.SetROP2 (R2_XORPEN);// R2_NOT);
dc.MoveTo (ptLineStart);
penOld = dc.SelectObject (&Pen);
dc.LineTo (ptLineEnd);
dc.SetROP2 (nOldMode);
dc.SelectObject (penOld);
Pen.DeleteObject ();
Pen.CreatePen (PS_SOLID, 1, (COLORREF) 0x0);
}
BOOL CChildView::StartReceiver()
{
STARTUPINFO si;
memset ((char *) &si, '\0', sizeof (STARTUPINFO));
memset ((char *) &m_pi, '\0', sizeof (PROCESS_INFORMATION));
si.cb = sizeof (STARTUPINFO);
CWindowDC dc(GetDesktopWindow());
int nWidth = GetDeviceCaps (dc.m_hDC, HORZRES) / 2;
int nDepth = GetDeviceCaps (dc.m_hDC, VERTRES) / 2;
si.dwXSize = nWidth;
si.dwYSize = nDepth;
si.dwX = nWidth;
si.dwY = 0;
si.dwFlags = STARTF_USESIZE | STARTF_USEPOSITION;
int nBytes = GetCurrentDirectory (0, NULL);
if (!nBytes)
return (FALSE);
++nBytes;
char *dir = new char [nBytes];
GetCurrentDirectory (nBytes, dir);
if (dir == NULL)
return (FALSE);
char *s = strrchr (dir, '\\');
if (s == NULL)
{
delete [] dir;
return (FALSE);
}
*s = '\0';
CString strDir = dir;
delete [] dir;
#ifdef _DEBUG
CString ProgPath = strDir + "\\Redraw\\Debug\\Redraw.exe";
#else
CString ProgPath = strDir + "\\Redraw\\Release\\Redraw.exe";
#endif
CString ProgParam = ProgPath;
CString StartPath = strDir + "\\Redraw";
BOOL bResult = CreateProcess((LPCTSTR) ProgPath,
(char *) (LPCSTR) ProgParam,
(LPSECURITY_ATTRIBUTES) NULL,
(LPSECURITY_ATTRIBUTES) NULL,
false,
DETACHED_PROCESS,
(LPVOID) NULL,
(LPCTSTR) StartPath,
(LPSTARTUPINFO) &si,
(LPPROCESS_INFORMATION) &m_pi);
return (bResult);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -