Quantcast
Viewing all articles
Browse latest Browse all 2703

Mouse Message Fail

I'm totally new with MFC programming and I am doing a server-client chat app (1 Client only). As far as I can tell, I got the first task working (which is to send, accept, and display string message to both the server and client interface).

Now, I was tasked to send and accept Mouse Messages (over LAN Network). To further clarify that, when I clicked the "Control Mouse" Button (me being the Server), I should be able to control the Client's Mouse. Got help from Mr. Google and forums, but I still cannot control the mouse of the client.

Please be nice with me as my way of coding may be bad, as I am new and still learning the do's and dont's of MFC Programming. Here's some parts of the code. Thanks.

Problem: I still cannot control the Client's Mouse =,(
static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam,LPARAM lParam);
    HHOOK hook;
    CDialog* hwnd;

    CCriticalSection c_s;

    UINT ThreadProcess(LPVOID param) 
    {

        CSingleLock lock(&c_s);
        lock.Lock();
        msgString *udata;
        CString m_receive, a, b, ax, ay;
        int iLen,i,in;
        char *pBuf =new char [1025];


        iLen=((CServerChatDlg*)hwnd)->Connect.Receive(pBuf,1025);
        udata = (msgString*)pBuf;

        if(iLen == SOCKET_ERROR)      
      {
      AfxMessageBox(L"Could not Receive");      
      } 

        else     
      { 

        pBuf[iLen]='\0';
        a = CString(udata->cId, 2);
        b = CString(udata->csMsg, 1000);

        if (a=="00")
        {
            ((CServerChatDlg*)hwnd)->RData += b;
            ((CServerChatDlg*)hwnd)->m_Display.SetWindowText(((CServerChatDlg*)hwnd)->RData);
        }

        else if(a="01")
        {
        //Get int within the buffer.. x and y coord.

         for (i=1; i<15; i++)
         { 
             if (b[i] !=' ')
             {
                ax += b[i];
             }
             else
             {
                 break;
             }

             in=i;
         }

         in +=4;

         for (i=in; i<15; i++)
         { 
             if (b[i] !=' ')
             {
                ay += b[i];
             }
             else
             {
                 break;
             }
         }
         int x= atoi((char*)(LPCTSTR)ax);
         int y= atoi((char*)(LPCTSTR)ay);

         ((CServerChatDlg*)hwnd)->MoveMouse(x,y);
         //SetCursorPos(x, y);
        }

        }

        delete pBuf;

        lock.Unlock();
        return 0; 
    }
LRESULT CALLBACK MouseProc (int nCode, WPARAM wParam,LPARAM lParam)
{

    LPARAM LParamTranslated;

    LParamTranslated = ( ( MSLLHOOKSTRUCT* ) lParam )->pt.x ;
    LParamTranslated = LParamTranslated |  ( ( MSLLHOOKSTRUCT* ) lParam )->pt.y << 16 ;
    HWND h = GetForegroundWindow();

    if(nCode < 0) 
    { 
        CallNextHookEx(hook, nCode, wParam, lParam);
        return 0;
    }
    //LPMSG msg = (LPMSG)lParam;
    switch( wParam  )
    {
        case WM_LBUTTONDOWN:
            ((CServerChatDlg*)hwnd)->OnMyMouseLButtonDown(wParam,lParam);
            //::SendMessage( hwnd, UWM_MOUSELBUTTONDOWN, 0 , 0 );
        break;

        case WM_RBUTTONDOWN:
            ((CServerChatDlg*)hwnd)->OnMyMouseRButtonDown(0, 0);
        break;

        case WM_MOUSEMOVE:
            ((CServerChatDlg*)hwnd)->OnMyMouseMove(0, 0);
        break;
    default:    
        break;
    }
    return CallNextHookEx(hook, nCode, wParam, lParam);
//return 1;
}

LRESULT CServerChatDlg::OnMyMouseLButtonDown(WPARAM, LPARAM param)
{
    CSingleLock lock(&c_s);
    lock.Lock();
    char *pBuf =new char [1025];
    CPoint point;
    GetCursorPos(&point);
    CString a;
    a.Format(_T("Left Button at P(%d, %d)"), point.x, point.y);
    Connection.Send(pBuf,a.GetLength());
    return 1;
    lock.Unlock();
}

LRESULT CServerChatDlg::OnMyMouseRButtonDown(WPARAM, LPARAM param)
{
    CSingleLock lock(&c_s);
    lock.Lock();
    char *pBuf =new char [1025];
    CPoint point;
    GetCursorPos(&point);
    CString a;
    a.Format(_T("Right Button at P(%d, %d)"), point.x, point.y);
    Connection.Send(pBuf,a.GetLength());
    return 1;
    lock.Unlock();
}

LRESULT CServerChatDlg::OnMyMouseMove(WPARAM, LPARAM param)
{

    CSingleLock lock(&c_s);
    lock.Lock();
    char *pBuf =new char [1025];
    CPoint point;
    GetCursorPos(&point);
    CString a;
    a.Format(_T("Move at P(%d, %d)"), point.x, point.y);
    Connection.Send(pBuf,a.GetLength());
    return 1;
    lock.Unlock();

}


void CServerChatDlg::OnBnClickedControl()
{
    // TODO: Add your control notification handler code here

    CString ctext;
    m_control.GetWindowText(ctext);

    if (ctext=="Control Mouse")
    {
        hook = SetWindowsHookEx (WH_MOUSE, MouseProc, NULL, GetCurrentThreadId());
        m_control.SetWindowText(_T("Stop"));
    }
    else
    {
        UnhookWindowsHookEx(hook);
        m_control.SetWindowText(_T("Control Mouse"));
    }
}

void CServerChatDlg::MoveMouse(int x, int y)
{
    // TODO: Add your control notification handler code here
    INPUT input={0};
    input.type = INPUT_MOUSE;
    input.mi.dx = (x * (65535.0f / (1280-1)));
    input.mi.dy = (y * (65535.0f / (1024-1)));
    input.mi.mouseData = 0;
    input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);
    input.mi.time = 0;
    input.mi.dwExtraInfo = 0;

    SendInput(1,&input,sizeof(INPUT));
}


Viewing all articles
Browse latest Browse all 2703

Trending Articles