www.pudn.com > mini_remote.zip > DesktopViewerDlg.cpp


// DesktopViewerDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "DesktopViewer.h" 
#include "DesktopViewerDlg.h" 
#include "..\VideoCodec.h" 
 
typedef struct _MOUSE_KEYBOARD 
{ 
	DWORD dCtrlID; 
	BOOL bMove; 
	int  x; 
	int  y; 
	UINT uChar; 
	UINT nFlags; 
}MOUSEKEYCTRL,*PMOUSEKEYCTRL; 
 
#define _MOUSE_MOVE			0x21 
#define _MOUSE_LBUTTONDOWN	0x22 
#define _MOUSE_LBUTTONUP		0x23 
#define _MOUSE_RBUTTONDOWN	0x24 
#define _MOUSE_RBUTTONUP		0x25 
#define _MOUSE_LDBLCLK		0x26 
#define _MOUSE_RDBLCLK		0x27 
#define _KEY_DOWN				0x31 
#define _KEY_UP				0x32 
 
BOOL isTranslating=FALSE; 
 
void RemoteDesk(void *p) 
{ 
	CDesktopViewerDlg *dlg=(CDesktopViewerDlg*)p; 
 
	WSADATA data; 
	WSAStartup(MAKEWORD(2,1),&data); 
	SOCKET hSocket = socket(PF_INET,SOCK_STREAM,0); 
	SOCKET m_hSocket; 
	if(hSocket != INVALID_SOCKET) 
	{ 
		SOCKADDR_IN sockAddr; 
		memset(&sockAddr,0,sizeof(sockAddr)); 
		sockAddr.sin_family = AF_INET; 
		sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); 
		sockAddr.sin_port = htons((u_short)dlg->dwExtinationPort); 
		bind(hSocket,(SOCKADDR*)&sockAddr, sizeof(sockAddr)); 
		if (listen(hSocket,1)==SOCKET_ERROR) 
			return; 
		m_hSocket = accept(hSocket,NULL,NULL); 
		closesocket(hSocket); 
		 
		isTranslating=TRUE; 
		if (m_hSocket==INVALID_SOCKET) 
			return; 
		else 
		{ 
			MOUSEKEYCTRL ctrl; 
			memset(&ctrl,0,sizeof(MOUSEKEYCTRL)); 
			if (recv(m_hSocket,(char *)&ctrl,sizeof(MOUSEKEYCTRL),NULL)==SOCKET_ERROR) 
			{ 
				closesocket(m_hSocket); 
				return; 
			} 
			dlg->s=m_hSocket; 
			dlg->RemoteScreenX=ctrl.x; 
			dlg->RemoteScreenY=ctrl.y; 
 
			BYTE *data = new BYTE[500000]; 
			while(1) 
			{ 
				if (isTranslating == FALSE) 
				{ 
					delete data; 
					closesocket(m_hSocket); 
					return; 
				} 
				memset(data,0,500000); 
				int nRet=recv(m_hSocket,(char *)data,500000,NULL); 
				if(nRet==SOCKET_ERROR) 
				{ 
					closesocket(m_hSocket); 
					return; 
				} 
				CString str; 
				str.Format("远程桌面 -收到数据:%d(KB)...",nRet/1024); 
				dlg->SetWindowText(str); 
 
				C_LZW lw; 
				HANDLE hUnLZW = ( HANDLE ) lw.Decode((char *)data,nRet); 
				 
				BYTE *p2; 
				p2 = (LPBYTE)GlobalLock(hUnLZW); 
 
				RECT rect; 
				HDC dc; 
				HWND hWnd=dlg->GetSafeHwnd (); 
				::GetClientRect(hWnd,&rect); 
				dc=::GetDC (hWnd); 
				int color = (1<<((LPBITMAPINFOHEADER)p2)->biBitCount); 
				if(color>256) 
					color = 0; 
 
				StretchDIBits(dc, 0, 0, rect.right,rect.bottom,0,0, 
					((LPBITMAPINFOHEADER)p2)->biWidth,  
					((LPBITMAPINFOHEADER)p2)->biHeight, 
					(LPBYTE)p2+(sizeof(BITMAPINFOHEADER)+color*sizeof(RGBQUAD)), 
					(LPBITMAPINFO)p2,DIB_RGB_COLORS, SRCCOPY); 
				dlg->EndWaitCursor(); 
				GlobalFree(hUnLZW); 
				Sleep(500); 
			} 
		} 
	} 
} 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDesktopViewerDlg dialog 
 
CDesktopViewerDlg::CDesktopViewerDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDesktopViewerDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDesktopViewerDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CDesktopViewerDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDesktopViewerDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDesktopViewerDlg, CDialog) 
	//{{AFX_MSG_MAP(CDesktopViewerDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_LBUTTONDBLCLK() 
	ON_WM_MOUSEMOVE() 
	ON_WM_RBUTTONDBLCLK() 
	ON_WM_RBUTTONDOWN() 
	ON_WM_RBUTTONUP() 
	ON_WM_KEYDOWN() 
	ON_WM_KEYUP() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDesktopViewerDlg message handlers 
 
BOOL CDesktopViewerDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
 
	SetWindowText("远程桌面 -正在连接中..."); 
	t=NULL; 
	isTranslating = FALSE; 
 
	HKEY hKey; 
	DWORD ext_port; 
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MiniRemote",0L,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS) 
	{ 
		DWORD dwType; 
		DWORD dwSize=sizeof(DWORD); 
		if (RegQueryValueEx(hKey,"Extination_Port",NULL,&dwType,(BYTE *)&ext_port,&dwSize) == ERROR_SUCCESS) 
			dwExtinationPort = ext_port; 
		RegCloseKey(hKey); 
	} 
	else 
		dwExtinationPort = 65521; 
 
	///dwExtinationPort = 12332; 
 
	t=AfxBeginThread((AFX_THREADPROC) RemoteDesk,this); 
	SetTimer(0,10000,NULL); 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CDesktopViewerDlg::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(); 
	} 
	ShowWindow(SW_SHOWMAXIMIZED); 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CDesktopViewerDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CDesktopViewerDlg::OnOK()  
{ 
} 
 
void CDesktopViewerDlg::OnTimer(UINT nIDEvent)  
{ 
	if (!isTranslating) 
	{ 
		if (t!=NULL) 
		{ 
			TerminateThread(t->m_hThread,0); 
			TerminateThread(t->m_hThread,1); 
			CloseHandle(t->m_hThread); 
			t=NULL; 
		} 
		SetWindowText("远程桌面 -连接超时..."); 
	} 
	KillTimer(0); 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CDesktopViewerDlg::OnDestroy()  
{ 
	CDialog::OnDestroy(); 
	isTranslating=FALSE; 
} 
 
void CDesktopViewerDlg::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	CDialog::OnLButtonDown(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_LBUTTONDOWN; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	 
} 
 
void CDesktopViewerDlg::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	CDialog::OnLButtonUp(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_LBUTTONUP; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	 
} 
 
void CDesktopViewerDlg::OnLButtonDblClk(UINT nFlags, CPoint point)  
{ 
	CDialog::OnLButtonDblClk(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_LDBLCLK; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	 
} 
 
void CDesktopViewerDlg::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	CDialog::OnMouseMove(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_MOVE; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
} 
 
void CDesktopViewerDlg::OnRButtonDblClk(UINT nFlags, CPoint point)  
{	 
	CDialog::OnRButtonDblClk(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_RDBLCLK; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
 
} 
 
void CDesktopViewerDlg::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	CDialog::OnRButtonDown(nFlags, point); 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_RBUTTONDOWN; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
 
} 
 
void CDesktopViewerDlg::OnRButtonUp(UINT nFlags, CPoint point)  
{ 
	CDialog::OnRButtonUp(nFlags, point); 
 
	CRect rect; 
	GetClientRect(&rect); 
	int x=point.x*RemoteScreenX/rect.Width()+1; 
	int y=point.y*RemoteScreenY/rect.Height()+1; 
	CString str; 
	str.Format("%d %d",x,y); 
 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_MOUSE_RBUTTONUP; 
	ctrl.bMove=TRUE; 
	ctrl.x=x; 
	ctrl.y=y; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	 
} 
 
void CDesktopViewerDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_KEY_DOWN; 
	ctrl.uChar=nChar; 
	ctrl.nFlags=nFlags; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags); 
} 
 
void CDesktopViewerDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)  
{ 
	MOUSEKEYCTRL ctrl; 
	ctrl.dCtrlID=_KEY_UP; 
	ctrl.uChar=nChar; 
	ctrl.nFlags=nFlags; 
	send(s,(char *)&ctrl,sizeof(MOUSEKEYCTRL),0); 
	CDialog::OnKeyUp(nChar, nRepCnt, nFlags); 
}