// GravMouseDlg.cpp : implementation file // By Adam Rea, Travis Martin and Michael Goldschmidt #include "stdafx.h" #include "GravMouse.h" #include "GravMouseDlg.h" #define TIMER_ID 634376 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CGravMouseDlg dialog CGravMouseDlg::CGravMouseDlg(CWnd* pParent ) : CDialog(CGravMouseDlg::IDD, pParent) { //{{AFX_DATA_INIT(CGravMouseDlg) m_Port = 0; m_Server = _T(""); //}}AFX_DATA_INIT m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CGravMouseDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGravMouseDlg) DDX_Text(pDX, IDC_PORT, m_Port); DDX_Text(pDX, IDC_SERVER, m_Server); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGravMouseDlg, CDialog) //{{AFX_MSG_MAP(CGravMouseDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CONNECT, OnConnect) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGravMouseDlg message handlers BOOL CGravMouseDlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CGravMouseDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } HCURSOR CGravMouseDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CGravMouseDlg::OnConnect() { // get the dialog data UpdateData(TRUE); SOCKADDR_IN sin; LPPROTOENT pr = NULL; int m_nListenPort = 14001; WORD wVersionRequested; wVersionRequested = MAKEWORD( 2, 2 ); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); s = socket( AF_INET, SOCK_STREAM, 0 ); if ( s == INVALID_SOCKET ) { int i = WSAGetLastError(); AfxMessageBox( _T("Cannot create socket") ); return; } sin.sin_family = AF_INET; sin.sin_port = ntohs( m_Port ); sin.sin_addr.S_un.S_addr = inet_addr( m_Server.GetBuffer( m_Server.GetLength() ) ); connect(s, (LPSOCKADDR)&sin, sizeof(sin)); send(s, "I hope this works", 17, 0); SetTimer(TIMER_ID, // timer identifier 30, // interval NULL); // no timer callback } void CGravMouseDlg::OnTimer(UINT nIDEvent) { struct recvtype { ULONG x; ULONG y; } recvbuf; // check the event ID if(nIDEvent == TIMER_ID) { ZeroMemory( &recvbuf, sizeof( recvtype ) ); int ret = recv( s, (char*) &recvbuf, sizeof( recvtype ), 0 ); if( ret < 1 ) OnCancel(); INPUT input; ZeroMemory(&input, sizeof(input)); input.type = INPUT_MOUSE; input.mi.dx = recvbuf.x; input.mi.dy = recvbuf.y; input.mi.dwFlags = MOUSEEVENTF_MOVE; SendInput( 1, &input, sizeof(INPUT)); CDialog::OnTimer(nIDEvent); } } void CGravMouseDlg::OnCancel() { closesocket(s); CDialog::OnCancel(); }