![]() ![]() it will return a number representing the color of the pixel on row 15, column 10 from the top-left corner of your leftmost screen. If you paste that entire code block into any module (Worksheet, Workbook, or Code) and call the function like this: MsgBox getScreenPixel(10,15) ![]() ![]() If SelectObject(memDC, memBMP) 0 And BitBlt(memDC, 0, 0, 1, 1, desktopDC, x, y, srccopy) 0 Thenĭon't be alarmed by the first eight lines there.they are just allowing VBA to access built-in Windows functions that allow us to inspect the screen. Public Function getScreenPixel(x As Long, y As Long) As Variantĭim desktopDC As LongPtr: desktopDC = GetDC(0)ĭim memDC As LongPtr: memDC = CreateCompatibleDC(desktopDC)ĭim memBMP As LongPtr: memBMP = CreateCompatibleBitmap(desktopDC, 1, 1) Private Declare PtrSafe Function DeleteDC Lib "gdi32" (ByVal hdc As LongPtr) As Long Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As Long Private Declare PtrSafe Function GetPixel Lib "gdi32" (ByVal hdc As LongPtr, ByVal x As Long, ByVal y As Long) As Long Private Declare PtrSafe Function BitBlt Lib "gdi32" (ByVal hDestDC As LongPtr, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare PtrSafe Function SelectObject Lib "gdi32" (ByVal hdc As LongPtr, ByVal hObject As LongPtr) As Long ![]() Private Declare PtrSafe Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As LongPtr, ByVal nWidth As Long, ByVal nHeight As Long) As LongPtr Private Declare PtrSafe Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As LongPtr) As LongPtr Well, there IS an easy way: Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr This is something I've wanted to be able to do for literally years, but I thought it was impossible in VBA.the internet is full of misinformation, and the few people who WERE able to do it usually did it in an extremely difficult and awkward way that made it hard to adapt their work to suit what I needed (I'm pretty sure a lot of VBA professionals do this on purpose). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |