You are on page 1of 12

Robot.

h #pragma once namespace Struktur{ static struct Dimension { int width; int height; }; static struct Point { int x; int y; }; }

class Robot { public: Robot(void); ~Robot(void); static static static static static static static static static static static static static static static void setMouseTo(int,int); Struktur::Dimension getScreenRect(); Struktur::Point getMousePosition(); void pressMouseButton(int); void releaseMouseButton(int); void clickMouseButton(int); bool isMouseButtonDown(int); bool isMouseButtonUp(int); bool isKeyDown(int); bool isKeyUp(int); bool isKeyToogleOn(int); bool isKeyToogleOff(int); void pressKey(int); void releaseKey(int); void clickKey(int);

private: static const SHORT lowBit; static const SHORT hiBit; };

namespace Enumeration{ enum MouseButtons { LEFT = 1, MIDDLE = 2, RIGHT = 3 }; enum Keys { VK_LEFTMOUSEBUTTON = 0x01, VK_RIGHTMOUSEBUTTON = 0x02,

VK_CANCELKEY = 0x03, VK_MIDDLEMOUSEBUTTON = 0x04, VK_XMOUSEBUTTON1 = 0x05, VK_XMOUSEBUTTON2 = 0x06, VK_BACKSPACEKEY = 0x08, VK_TABKEY = 0x09, VK_CLEARKEY = 0x0C, VK_ENTERKEY = 0x0D, VK_SHIFTKEY = 0x10, VK_CONTROLKEY = 0x11, VK_ALTKEY = 0x12, VK_PAUSEKEY = 0x13, VK_CAPSLOCKKEY = 0x14, VK_ESCAPEKEY = 0x1B, VK_KANAMODE = 0x15, VK_HANGULMODE = 0x15, VK_JUNJAMODE = 0x17, VK_FINALMODE = 0x18, VK_HANJAMODE = 0x19, VK_KANJIMODE = 0x19, VK_CONVERTIME = 0x1C, VK_NONCONVERTIME = 0x1D, VK_ACCEPTIME = 0x1E, VK_MODECHANGEREQUEST = 0x1F, VK_SPACEKEY = 0x20, VK_PAGEUPKEY = 0x21, VK_PAGEDOWNKEY = 0x22, VK_ENDKEY = 0x23, VK_HOMEKEY = 0x24, VK_LEFTARROWKEY = 0x25, VK_UPARROWKEY = 0x26, VK_RIGHTARROWKEY = 0x27, VK_DOWNARROWKEY = 0x28, VK_SELECTKEY = 0x29, VK_PRINTKEY = 0x2A, VK_EXECUTEKEY = 0x2B, VK_PRINTSCREENKEY = 0x2C, VK_INSERTKEY = 0x2D, VK_DELETEKEY = 0x2E, VK_HELPKEY = 0x2F, VK_0KEY = 0x30, VK_1KEY = 0x31, VK_2KEY = 0x32, VK_3KEY = 0x33, VK_4KEY = 0x34, VK_5KEY = 0x35, VK_6KEY = 0x36, VK_7KEY = 0x37, VK_8KEY = 0x38, VK_9KEY = 0x39, VK_AKEY = 0x41, VK_BKEY = 0x42, VK_CKEY = 0x43, VK_DKEY = 0x44, VK_EKEY = 0x45, VK_FKEY = 0x46, VK_GKEY = 0x47, VK_HKEY = 0x48, VK_IKEY = 0x49, VK_JKEY = 0x4A, VK_KKEY = 0x4B, VK_LKEY = 0x4C, VK_MKEY = 0x4D, VK_NKEY = 0x4E, VK_OKEY = 0x4F,

VK_PKEY = 0x50, VK_QKEY = 0x51, VK_RKEY = 0x52, VK_SKEY = 0x53, VK_TKEY = 0x54, VK_UKEY = 0x55, VK_VKEY = 0x56, VK_WKEY = 0x57, VK_XKEY = 0x58, VK_YKEY = 0x59, VK_ZKEY = 0x5A, VK_LEFTWINDOWSKEY = 0x5B, VK_RIGHTWINDOWSKEY = 0x5C, VK_APPLICATIONKEY = 0x5D, VK_COMPUTERSLEEPKEY = 0x5F, VK_0NUMPADKEY = 0x60, VK_1NUMPADKEY = 0x61, VK_2NUMPADKEY = 0x62, VK_3NUMPADKEY = 0x63, VK_4NUMPADKEY = 0x64, VK_5NUMPADKEY = 0x65, VK_6NUMPADKEY = 0x66, VK_7NUMPADKEY = 0x67, VK_8NUMPADKEY = 0x68, VK_9NUMPADKEY = 0x69, VK_MULTIPLYKEY = 0x6A, VK_ADDKEY = 0x6B, VK_SEPARATORKEY = 0x6C, VK_SUBTRACTKEY = 0x6D, VK_DECIMALKEY = 0x6E, VK_DIVIDEKEY = 0x6F, VK_F1KEY = 0x70, VK_F2KEY = 0x71, VK_F3KEY = 0x72, VK_F4KEY = 0x73, VK_F5KEY = 0x74, VK_F6KEY = 0x75, VK_F7KEY = 0x76, VK_F8KEY = 0x77, VK_F9KEY = 0x78, VK_F10KEY = 0x79, VK_F11KEY = 0x7A, VK_F12KEY = 0x7B, VK_F13KEY = 0x7C, VK_F14KEY = 0x7D, VK_F15KEY = 0x7E, VK_F16KEY = 0x7F, VK_F17KEY = 0x80, VK_F18KEY = 0x81, VK_F19KEY = 0x82, VK_F20KEY = 0x83, VK_F21KEY = 0x84, VK_F22KEY = 0x85, VK_F23KEY = 0x86, VK_F24KEY = 0x87, VK_NUMLOCKKEY = 0x90, VK_SCROLLLOCKKEY = 0x91, VK_LEFTSHIFTKEY = 0xA0, VK_RIGHTSHIFTKEY = 0xA1, VK_LEFTCONTROLKEY = 0xA2, VK_RIGHTCONTROLKEY = 0xA3, VK_LEFTMENUKEY = 0xA4, VK_RIGHTMENUKEY = 0xA5, VK_LEFTALTKEY = 0xA4, VK_RIGHTALTKEY = 0xA5,

VK_BROWSER_BACKKEY = 0xA6, VK_BROWSER_FORWARDKEY = 0xA7, VK_BROWSER_REFRESHKEY = 0xA8, VK_BROWSER_STOPKEY = 0xA9, VK_BROWSER_SEARCHKEY = 0xAA, VK_BROWSER_FAVORITESKEY = 0xAB, VK_BROWSER_HOMEKEY = 0xAC, VK_VOLUME_MUTEKEY = 0xAD, VK_VOLUME_DOWNKEY = 0xAE, VK_VOLUME_UPKEY = 0xAF, VK_MEDIA_NEXT_TRACKKEY = 0xB0, VK_MEDIA_PREVIOUS_TRACKKEY = 0xB1, VK_MEDIA_STOPKEY = 0xB2, VK_MEDIA_PLAY_PAUSEKEY = 0xB3, VK_START_MAILKEY = 0xB4, VK_LAUNCH_MEDIA_SELECTKEY = 0xB5, VK_LAUNCH_APPLICATION1KEY = 0xB6, VK_LAUNCH_APPLICATION2KEY = 0xB7, VK_OEM_1KEY = 0xBA, VK_OEM_PLUSKEY = 0xBB, VK_OEM_COMMAKEY = 0xBC, VK_OEM_MINUSKEY = 0xBD, VK_OEM_PERIODKEY = 0xBE, VK_OEM_2KEY = 0xBF, VK_OEM_3KEY = 0xC0, VK_OEM_4KEY = 0xDB, VK_OEM_5KEY = 0xDC, VK_OEM_6KEY = 0xDD, VK_OEM_7KEY = 0xDE, VK_OEM_8KEY = 0xDF, VK_OEM_102KEY = 0xE2, VK_ATTNKEY = 0xF6, VK_CRSELKEY = 0xF7, VK_EXSELKEY = 0xF8, VK_EREOFKEY = 0xF9, VK_PLAYKEY = 0xFA, VK_ZOOMKEY = 0xFB, VK_NONAMEKEY = 0xFC, VK_PA1KEY = 0xFD, VK_OEM_CLEARKEY = 0xFE }; }

Robot.cpp
#include "stdafx.h" #include "Robot.h"

Robot::Robot(void) { } Robot::~Robot(void) { }

void Robot::setMouseTo(int x, int y){ SetCursorPos(x,y); } Struktur::Dimension Robot::getScreenRect(void){ RECT desktop; const HWND hDesktop = GetDesktopWindow(); GetWindowRect(hDesktop, &desktop); Struktur::Dimension dim; dim.width=desktop.right; dim.height=desktop.bottom; } return dim;

Struktur::Point Robot::getMousePosition(){ Struktur::Point p; POINT cursorPos; GetCursorPos(&cursorPos); p.x=(int)cursorPos.x; p.y=(int)cursorPos.y; return p; } void Robot::pressMouseButton(int mb){ if(mb==1){ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); }else if(mb==2){ mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); }else if(mb==3){ mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); } }

void Robot::releaseMouseButton(int mb){ if(mb==1){ mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); }else if(mb==2){ mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); }else if(mb==3){ } } void Robot::clickMouseButton(int mb){ if(mb==1){ mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); }else if(mb==2){ mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);

}else if(mb==3){ mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); } } const SHORT Robot::lowBit = 1; //Toggle state prfen const SHORT Robot::hiBit = (1 << 15); //Test ob Taste gerade gedrckt ist oder nicht; bool Robot::isMouseButtonDown(int mb){ SHORT ButtonState; if(mb==1){ ButtonState = GetKeyState(VK_LBUTTON); }else if(mb==2){ ButtonState = GetKeyState(VK_MBUTTON); }else if(mb==3){ ButtonState = GetKeyState(VK_RBUTTON); }else { return false; } return (ButtonState & Robot::hiBit); } bool Robot::isMouseButtonUp(int mb){ SHORT ButtonState; if(mb==1){ ButtonState = GetKeyState(VK_LBUTTON); }else if(mb==2){ ButtonState = GetKeyState(VK_MBUTTON); }else if(mb==3){ ButtonState = GetKeyState(VK_RBUTTON); }else { return false; } return !(ButtonState & Robot::hiBit); } bool Robot::isKeyDown(int key){ SHORT ButtonState; ButtonState = GetKeyState(key); return (ButtonState & Robot::hiBit); } bool Robot::isKeyUp(int key){

SHORT ButtonState; ButtonState = GetKeyState(key); } return !(ButtonState & Robot::hiBit);

bool Robot::isKeyToogleOn(int key){ SHORT ButtonState; ButtonState = GetKeyState(key); } return (ButtonState & Robot::lowBit);

bool Robot::isKeyToogleOff(int key){ SHORT ButtonState; ButtonState = GetKeyState(key); } return !(ButtonState & Robot::lowBit);

void Robot::pressKey(int vk){ keybd_event(vk, 0, 0, 0); } void Robot::releaseKey(int vk){ keybd_event(vk, 0, KEYEVENTF_KEYUP, 0); } void Robot::clickKey(int vk){ keybd_event(vk, 0, 0, 0); keybd_event(vk, 0, KEYEVENTF_KEYUP, 0); }

Screenshot.h
#pragma once #include <vector> class Screenshot { public: Screenshot(void); ~Screenshot(void); static CBitmap *getScreenshot(bool); static void getScreenshotToClipboard(bool); static std::vector<std::vector<int>> _getScreenshot(bool); static void _getScreenshot(std::vector<std::vector<int>>*,bool); private: static CBitmap *bmpdesktop; };

Screenshot.cpp
#include "stdafx.h" #include "Screenshot.h"

CBitmap *Screenshot::bmpdesktop; Screenshot::~Screenshot(void){ delete(bmpdesktop); }

CBitmap* Screenshot::getScreenshot(bool zeiger){ CDC dc; CWnd * wnd = CWnd::GetDesktopWindow(); const BOOL FullWnd = true; CDC memDC; CBitmap bm; CRect r; CString s; if(FullWnd) { /* full window */ HDC hdc = ::GetWindowDC(wnd->m_hWnd); dc.Attach(hdc); } /* full window */ else { /* client area only */ HDC hdc = ::GetDC(wnd->m_hWnd); dc.Attach(hdc); } /* client area only */

memDC.CreateCompatibleDC(&dc); if(FullWnd){ wnd->GetWindowRect(&r); }else{ wnd->GetClientRect(&r); } wnd->GetWindowText(s); CSize sz(r.Width(), r.Height()); bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY); if(zeiger){ POINT pt; HWND hwnd; HCURSOR hCursor; DWORD threadID; GetCursorPos(&pt); hwnd = WindowFromPoint(pt); threadID = GetWindowThreadProcessId(hwnd, NULL);

AttachThreadInput(GetCurrentThreadId(), threadID, TRUE); hCursor = GetCursor(); AttachThreadInput(GetCurrentThreadId(), threadID, FALSE); DrawIcon(memDC, pt.x, pt.y, hCursor); } bmpdesktop=&bm; memDC.SelectObject(oldbm); bm.Detach(); // make sure bitmap not deleted with CBitmap object return bmpdesktop; } std::vector<std::vector<int>> Screenshot::_getScreenshot(bool zeiger){ CDC dc; CWnd * wnd = CWnd::GetDesktopWindow(); const BOOL FullWnd = true; CDC memDC; CBitmap bm; CRect r; CString s; if(FullWnd) { /* full window */ HDC hdc = ::GetWindowDC(wnd->m_hWnd); dc.Attach(hdc); } /* full window */ else { /* client area only */ HDC hdc = ::GetDC(wnd->m_hWnd); dc.Attach(hdc); } /* client area only */

memDC.CreateCompatibleDC(&dc); if(FullWnd){ wnd->GetWindowRect(&r); }else{ wnd->GetClientRect(&r); } wnd->GetWindowText(s); CSize sz(r.Width(), r.Height()); bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY); if(zeiger){ POINT pt; HWND hwnd; HCURSOR hCursor; DWORD threadID; GetCursorPos(&pt);

hwnd = WindowFromPoint(pt); threadID = GetWindowThreadProcessId(hwnd, NULL); AttachThreadInput(GetCurrentThreadId(), threadID, TRUE); hCursor = GetCursor(); AttachThreadInput(GetCurrentThreadId(), threadID, FALSE); DrawIcon(memDC, pt.x, pt.y, hCursor); } BITMAP BitmapParam; bm.GetBitmap(&BitmapParam); int h=BitmapParam.bmHeight,w=BitmapParam.bmWidth; std::vector<std::vector<int>> pic ( w, std::vector<int> ( h,0 ) ); int x=0,y=0; for(x=0;x<w;x++){ for(y=0;y<h;y++){ COLORREF colm = memDC.GetPixel(x,y); pic[x][y]=(int)colm; } }

memDC.SelectObject(oldbm); bm.Detach(); // make sure bitmap not deleted with CBitmap object return pic; } void Screenshot::_getScreenshot(std::vector<std::vector<int>> *pic,bool zeiger){ pic->clear(); CDC dc; CWnd * wnd = CWnd::GetDesktopWindow(); const BOOL FullWnd = true; CDC memDC; CBitmap bm; CRect r; CString s; if(FullWnd) { /* full window */ HDC hdc = ::GetWindowDC(wnd->m_hWnd); dc.Attach(hdc); } /* full window */ else { /* client area only */ HDC hdc = ::GetDC(wnd->m_hWnd); dc.Attach(hdc); } /* client area only */

memDC.CreateCompatibleDC(&dc);

if(FullWnd){ wnd->GetWindowRect(&r); }else{ wnd->GetClientRect(&r); } wnd->GetWindowText(s); CSize sz(r.Width(), r.Height()); bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY); if(zeiger){ POINT pt; HWND hwnd; HCURSOR hCursor; DWORD threadID; GetCursorPos(&pt); hwnd = WindowFromPoint(pt); threadID = GetWindowThreadProcessId(hwnd, NULL); AttachThreadInput(GetCurrentThreadId(), threadID, TRUE); hCursor = GetCursor(); AttachThreadInput(GetCurrentThreadId(), threadID, FALSE); } DrawIcon(memDC, pt.x, pt.y, hCursor);

BITMAP BitmapParam; bm.GetBitmap(&BitmapParam); int h=BitmapParam.bmHeight,w=BitmapParam.bmWidth; pic->resize(w); for(int i=0;i<w;i++){ pic->at(i).resize(h); } int x=0,y=0; for(x=0;x<w;x++){ for(y=0;y<h;y++){ COLORREF colm = memDC.GetPixel(x,y); } } memDC.SelectObject(oldbm); bm.Detach(); // make sure bitmap not deleted with CBitmap object } void Screenshot::getScreenshotToClipboard(bool zeiger){ CDC dc; CWnd * wnd = CWnd::GetDesktopWindow(); BOOL FullWnd = true; CDC memDC; CBitmap bm; pic->at(x).at(y)=(int)colm;

CRect r; CString s; if(FullWnd) { /* full window */ HDC hdc = ::GetWindowDC(wnd->m_hWnd); dc.Attach(hdc); } /* full window */ else { /* client area only */ HDC hdc = ::GetDC(wnd->m_hWnd); dc.Attach(hdc); } /* client area only */

memDC.CreateCompatibleDC(&dc); if(FullWnd){ wnd->GetWindowRect(&r); }else{ wnd->GetClientRect(&r); } wnd->GetWindowText(s); CSize sz(r.Width(), r.Height()); bm.CreateCompatibleBitmap(&dc, sz.cx, sz.cy); CBitmap * oldbm = memDC.SelectObject(&bm); memDC.BitBlt(0, 0, sz.cx, sz.cy, &dc, 0, 0, SRCCOPY); /*CPoint pp; pp.x=0; pp.y=0; memDC.SetPixelV(pp, RGB(255,0,0));*/ if(zeiger){ POINT pt; HWND hwnd; HCURSOR hCursor; DWORD threadID; GetCursorPos(&pt); hwnd = WindowFromPoint(pt); threadID = GetWindowThreadProcessId(hwnd, NULL); AttachThreadInput(GetCurrentThreadId(), threadID, TRUE); hCursor = GetCursor(); AttachThreadInput(GetCurrentThreadId(), threadID, FALSE); } DrawIcon(memDC, pt.x, pt.y, hCursor);

wnd->OpenClipboard(); ::EmptyClipboard(); ::SetClipboardData(CF_BITMAP, bm.m_hObject); CloseClipboard(); memDC.SelectObject(oldbm); bm.Detach(); // make sure bitmap not deleted with CBitmap object }

You might also like