Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


C++ Taking a Screenshot
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Mon Nov 23, 2009 7:21 am    Post subject: C++ Taking a Screenshot Reply with quote

How do I take a Screen shot of a specific HWND?
So I can use getPixel().

I am trying to make a Bot so thats why I need to know how to get a Screen shot of the HWND and then use GetPixel() as a while back they told me that I should get a Screen shot of the HWND first before using GetPixel because GetPixel is slow if you use it on the Active Screen.

PS: I need the code in C++ no C, C#, VB, .NET.. ect.
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Mon Nov 23, 2009 8:10 am    Post subject: Reply with quote

why do you need to take a screenshot for pixels detection?
just read the pixel from the current position and compare it to what u want
and when it's not equal do whatever u wish to do
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Mon Nov 23, 2009 8:38 am    Post subject: Reply with quote

_DoR wrote:
why do you need to take a screenshot for pixels detection?
just read the pixel from the current position and compare it to what u want
and when it's not equal do whatever u wish to do

Well, I need to get Pixels from a specific HWND, taking screen shots and then using getPixel is faster than GetPixel on an Active Screen.

EDIT: And I have no use for Pixels outside the Bot Client so GetPixel Would be faster. And with the Screenshot technique I can minimize the Bot Client.
Back to top
View user's profile Send private message
Stylo
Grandmaster Cheater Supreme
Reputation: 3

Joined: 16 May 2007
Posts: 1073
Location: Israel

PostPosted: Mon Nov 23, 2009 9:08 am    Post subject: Reply with quote

you can get the pixel from your game's hWnd when your bot is minimized too
and i don't think that taking a screen shot is faster..
instead you're just pointing to a pixel on your screen and get it's color
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Tue Nov 24, 2009 4:10 am    Post subject: Reply with quote

_DoR wrote:
you can get the pixel from your game's hWnd when your bot is minimized too
and i don't think that taking a screen shot is faster..
instead you're just pointing to a pixel on your screen and get it's color

Hmm I dunno I'll try GetPixel().
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Thu Nov 26, 2009 4:09 am    Post subject: Reply with quote

Well I got another Problem now, I can't get the Handle of a Flash Control :S

Code:
   HWND MainHandle = FindWindow(L"WindowsForms10.Window.8.app.0.33c0d9d", NULL);
   if(!MainHandle)
      return 10;

   HWND Child1 = FindWindowEx(MainHandle, NULL, L"WindowsForms10.SysTabControl32.app.0.33c0d9d", NULL);
   if(!Child1)
      return 1;

   HWND Child2 = FindWindowEx(Child1, NULL, L"WindowsForms10.Window.8.app.0.33c0d9d", NULL);
   if(!Child2)
      return 2;

   HWND Child3 = FindWindowEx(Child2, NULL, L"WindowsForms10.Window.8.app.0.33c0d9d", NULL);
   if(!Child3)
      return 3;

   HWND Child4 = FindWindowEx(Child3, NULL, L"Shell Embedding", NULL);
   if(!Child4)
      return 4;

   HWND Child5 = FindWindowEx(Child4, NULL, L"Shell DocObject View", NULL);
   if(!Child5)
      return 5;

   HWND Child6 = FindWindowEx(Child5, NULL, L"Internet Explorer_Server", NULL);
   if(!Child6)
      return 6;

   HWND hWnd = FindWindowEx(Child6, NULL, L"MacromediaFlashPlayerActiveX", NULL);
   if(!hWnd)
      return 7;

has exited with code 7 (0x7).

I can get the Handle of "Internet Explorer_Server" but not "MacromediaFlashPlayerActiveX".
Maybe I have to get the Handle of the "Shell Embedding" instead.
"Shell Embedding" is the Internet Explorer Control in my Forms.

EDIT: Only "MacromediaFlashPlayerX" will work Sad but I can't get the Handle.

EDIT2: nvm got it Razz. had to add 1 thing.
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Sun Nov 29, 2009 2:18 am    Post subject: Reply with quote

Is there any better way than GetPixel? It's really slow compared to Scar Divi and AutoIt.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Nov 29, 2009 3:09 am    Post subject: Reply with quote

You shouldn't be having too much trouble with GetPixel() unless you're trying to read a massive amount of pixels


This also sounds fast and logical enough in my head, maybe worth a try.

GetDC(), create the DC
CreateCompatibleDC(), create the memory DC
CreateDIBSection(), a nice DIB for you access directly

select your dib section into the memory DC.

BitBlt your DC to your memory DC. You don't have to blit the entire screen.


CreateDIBSection() provides you with a pointer to the bits, so you can just do something like:

Code:
COLORREF ReadPixel(int x, int y)
{
   DWORD* p = bits + (y * WIDTH + x);
   return *p;
}
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Sun Nov 29, 2009 4:12 am    Post subject: Reply with quote

I am reading like 800*500 Pixels.
slovach wrote:
You shouldn't be having too much trouble with GetPixel() unless you're trying to read a massive amount of pixels


This also sounds fast and logical enough in my head, maybe worth a try.

GetDC(), create the DC
CreateCompatibleDC(), create the memory DC
CreateDIBSection(), a nice DIB for you access directly

select your dib section into the memory DC.

BitBlt your DC to your memory DC. You don't have to blit the entire screen.


CreateDIBSection() provides you with a pointer to the bits, so you can just do something like:

Code:
COLORREF ReadPixel(int x, int y)
{
   DWORD* p = bits + (y * WIDTH + x);
   return *p;
}


Thanks I'll try that out in a moment.

EDIT:
Ok I have some Questions.
What do you mean with:
Quote:
select your dib section into the memory DC.


Code:
BOOL BitBlt(
  HDC hdcDest, //My HDC, yes?
  int nXDest,     //Client x
  int nYDest,     //Client y
  int nWidth,     //Client width
  int nHeight,    //Client Height
  HDC hdcSrc,  //Whats this?
  int nXSrc,      //And this?
  int nYSrc,      //And this?
  DWORD dwRop  //And this?
);


And
Code:
COLORREF ReadPixel(int x, int y)
{
   DWORD* p = bits + (y * WIDTH + x);
   return *p;
}

What does it return? the Color like "8828926" or "255,255,255"?

Thanks in advance!
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Nov 29, 2009 5:12 am    Post subject: Reply with quote

Use SelectObject() to select an object into the DC
http://msdn.microsoft.com/en-us/library/aa932923.aspx
Code:
SelectObject(memDc, dib);





Quote:
BOOL BitBlt(
__in HDC hdcDest,
__in int nXDest,
__in int nYDest,
__in int nWidth,
__in int nHeight,
__in HDC hdcSrc,
__in int nXSrc,
__in int nYSrc,
__in DWORD dwRop
);

Parameters

hdcDest [in]

A handle to the destination device context.
nXDest [in]

The x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nYDest [in]

The y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nWidth [in]

The width, in logical units, of the source and destination rectangles.
nHeight [in]

The height, in logical units, of the source and the destination rectangles.
hdcSrc [in]

A handle to the source device context.
nXSrc [in]

The x-coordinate, in logical units, of the upper-left corner of the source rectangle.
nYSrc [in]

The y-coordinate, in logical units, of the upper-left corner of the source rectangle.
dwRop [in]

A raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color.

The following list shows some common raster operation codes...

...long list, you'll probably be hard pressed to find a reason for anything other than SRCCOPY right now.

read and change to taste.
Code:
BitBlt(dc, 0, 0, width, height, memDc, 0, 0, SRCCOPY);



finally, COLORREF is a typedef for DWORD.
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Sun Nov 29, 2009 5:29 am    Post subject: Reply with quote

I got so far:
Code:
#include <windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;


HDC hDCCom;
BYTE* bitPointer;
HWND hWnd = GetDesktopWindow();
int x,y;
int redValue, greenValue, blueValue;

int main()
{

   HDC hdcScreen = GetDC(hWnd);
   RECT rect;
   GetWindowRect(hWnd, &rect);
   hDCCom = CreateCompatibleDC(hdcScreen);
   HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, 800,495);
   SelectObject(hDCCom, hBitmap);
   BitBlt(hDCCom, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hdcScreen, 0, 0, SRCCOPY);   



   BITMAPINFO bi;
   bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
   bi.bmiHeader.biWidth = 1280;
   bi.bmiHeader.biHeight = 768;
   bi.bmiHeader.biPlanes = 1;
   bi.bmiHeader.biBitCount = 32;
   bi.bmiHeader.biCompression = BI_RGB;
   bi.bmiHeader.biSizeImage = 1280 * 4 * 768;
   bi.bmiHeader.biClrUsed = 0;
   bi.bmiHeader.biClrImportant = 0;

   HBITMAP hBitmap2 = CreateDIBSection( hDCCom, &bi, DIB_RGB_COLORS, (void**) (&bitPointer), NULL, NULL);
   SelectObject(hDCCom, hBitmap2);
   BitBlt(hDCCom, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hdcScreen, 0, 0, SRCCOPY);

   ReleaseDC(hWnd,hdcScreen);

return 0;
}


But ReadPixel() gives me an error (I have removed it from the code for now) cannot convert from DWORD to int or something like that. I don't understand how ReadPixel works either.

All I would need to know now is how to read and then I'm done Very Happy.

PS: This is not my original code, I made a new Project for this so it is easier to know whats what Razz.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sun Nov 29, 2009 6:42 pm    Post subject: Reply with quote

oops, BitBlt should have the memory DC as the destination, i had it backwards.


this nasty thing should be right, i'll leave it to you to turn it into something presentable though.
look through it and change the sizing, etc.


Code:
#include <Windows.h>

DWORD* bits = NULL; // :\

COLORREF ReadPixel(int x, int y);

int main()
{   
   BITMAPINFO   bi      = { 0 };   

   bi.bmiHeader.biSize     = sizeof(BITMAPINFO);
   bi.bmiHeader.biWidth    = 1440;
   bi.bmiHeader.biHeight   = -900; //top down
   bi.bmiHeader.biPlanes   = 1;
   bi.bmiHeader.biBitCount = 32;

   HDC hdc         = GetDC(0); //0 is the entire screen
   HDC hdcMem      = CreateCompatibleDC(hdc);
   HBITMAP hDib   = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
   HBITMAP hbmOld   = (HBITMAP)SelectObject(hdcMem, hDib);

   BitBlt(hdcMem, 0, 0, 1440, 900, hdc, 0, 0, SRCCOPY);
   //hdcMem is the destination, hdc is the source

   //we have all the pixel data now, to access it, just make a pointer to it.
   COLORREF c = ReadPixel(0, 0);


//don't forget to release the DCs and stuff, i'm sure you can manage this on your own
   return 0;
}

COLORREF ReadPixel(int x, int y)
{
   DWORD* p = bits + (y * 1440 + x);
   return *p;
}
Back to top
View user's profile Send private message
A_jj74
Cheater
Reputation: 0

Joined: 14 Nov 2009
Posts: 41

PostPosted: Mon Nov 30, 2009 1:45 am    Post subject: Reply with quote

Dude.. your just simply awesome Razz. It scans the entire 800*495 screen in less than a second Smile.

Thank you very much for your help!
The rest I can do myself Smile.

+Rep for you!!

EDIT:
But there is 1 Problem, it uses way to much memory away.
800k to be specific, it nearly Freezes my PC.

Although I use ReleaseDC() and DeleteDC().

EDIT:
I had to delete the BitMaps aswell with DeleteObject() Very Happy.

EDIT:
It's quite difficult for the Program to find the Object I want it to click because it's Rotating =/ I have to find the Color that is being used the most >.<.
Hmm I dunno which Color to pick because none of them work.
My ship just flies past them and even if I make the ship stop right next to the BonusBox it doesn't "find" it. I'm trying since hours to find the right color without success.

I made a Separate Program so I can Check the Value of a Color on the Screen.
Code:
#include <windows.h>
#include <iostream>
using namespace std;

int x,y,x1,y1,i;
POINT pt;

int main()
{
  HDC hdcScreen = GetDC(0);
  COLORREF crPixel = GetPixel(hdcScreen,10,10);
  ReleaseDC(0,hdcScreen);

  int redValue,greenValue,blueValue;

  while(10)
  {
     if(GetAsyncKeyState(VK_LBUTTON))
     {
        Sleep(1);
        if(!GetAsyncKeyState(VK_LBUTTON))
        {
                GetCursorPos(&pt);
                 x = (pt.x);
                  y = (pt.y);
                 HDC hdcScreen = GetDC(0);
                  COLORREF crPixel = GetPixel(hdcScreen,x,y);
                  ReleaseDC(0,hdcScreen);
                  redValue = GetRValue(crPixel);
                  greenValue = GetGValue(crPixel);
                  blueValue = GetBValue(crPixel);
              cout << redValue << " " << greenValue << " " << blueValue << endl; //cout << crPixel << endl;
        }
     }
     if(GetAsyncKeyState(VK_F8))
     {
        Sleep(1);
        if(!GetAsyncKeyState(VK_F8))
        {
           system("cls");
        }
     }
  }
return 0;
}
Back to top
View user's profile Send private message
SuomiPoika
How do I cheat?
Reputation: 0

Joined: 30 Nov 2009
Posts: 1

PostPosted: Mon Nov 30, 2009 9:30 pm    Post subject: Reply with quote

hmm looks hard to me
Back to top
View user's profile Send private message MSN Messenger
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Nov 30, 2009 10:43 pm    Post subject: Reply with quote

The blit will probably need a couple megabytes of memory.

blitting my entire screen jumped up the memory usage by pretty much exactly what I expected, around 5mb. Hardly a problem.


Here's a simple function for cleaning up when you're done. Don't start creating and destroying DC's mid loop as you'll only hurt performance.

Code:
BOOL Cleanup(void)
{
   if(SelectObject(hdcMem, hbmOld))
   {
      if(DeleteObject(hDib))
         if(ReleaseDC(hwnd, hdc))
            if(DeleteDC(hdcMem))
               return TRUE;
   }
   return FALSE;
}



You may have noticed the differences in what GetPixel returns vs the raw pixel data.

Example being, checking the color of the back button on my browser.

Code:
       xxRRGGBB
DIB, 0x000044c0
dec, 00 68 192

       xxBBGGRR
GP,  0x00c04400
dec, 192 68 00


Confused yet?
One returns RGB while the other returns BGR
(endian-ness is another matter...)

So yes, this is likely what is wrong with your color checks / why you're getting strange discrepancies between the two.



If you want to put the raw pixel data in the same format that GetPixel returns, you can just do something like...

Code:
void SwapCol(DWORD* ptr)
{
   *ptr = (*ptr & 0x000000FF) << 16 | (*ptr & 0x0000FF00) | (*ptr & 0x00FF0000) >> 16;
}


and use it like:

Code:
COLORREF c = ReadPixel(30, 60);
SwapCol(&c);

... now do whatever
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites