Lynie Posted February 7, 2009 Share Posted February 7, 2009 (edited) Is it possible to use GDI to draw something in another handle then your program's handle? Thanks Edited February 7, 2009 by Lynie Link to comment Share on other sites More sharing options...
Richard Robertson Posted February 7, 2009 Share Posted February 7, 2009 Yes. I've painted in windows that don't belong to my program before. Link to comment Share on other sites More sharing options...
BrettF Posted February 7, 2009 Share Posted February 7, 2009 Quick 'n' dirty example: expandcollapse popup#include <GDIPlus.au3> ;Run program $iPID = Run("Notepad") ;Wait for it to become active Sleep (500) ;get the window handle $hWnd = _GetHwndFromPID($iPID) ;get the control handle $hControl = ControlGetHandle($hWnd, "", "[CLASS:Edit; INSTANCE:1]") ;start GDI and draw the string _GDIPlus_Startup() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hControl) $hBrush = _GDIPlus_BrushCreateSolid(0x7F00007F) $hFormat = _GDIPlus_StringFormatCreate() $hFamily = _GDIPlus_FontFamilyCreate("Arial") $hFont = _GDIPlus_FontCreate($hFamily, 12, 2) $tLayout = _GDIPlus_RectFCreate(140, 110, 100, 20) _GDIPlus_GraphicsDrawStringEx($hGraphic, "Hello world", $hFont, $tLayout, $hFormat, $hBrush) ; Clean up resources _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() ;Wait... Sleep (4000) ;Function to convert PID to HWND Func _GetHwndFromPID($PID) $hWnd = 0 $winlist = WinList() Do For $i = 1 To $winlist[0][0] If $winlist[$i][0] <> "" Then $iPID2 = WinGetProcess($winlist[$i][1]) If $iPID2 = $PID Then $hWnd = $winlist[$i][1] ExitLoop EndIf EndIf Next Until $hWnd <> 0 Return $hWnd EndFunc ;==>_GetHwndFromPID You will probably need to hook the window for the likes of repainting when certain actions such as resizing, repainting of the window occurs etc. Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Lynie Posted February 8, 2009 Author Share Posted February 8, 2009 (edited) Thanks! Edited February 8, 2009 by Lynie Link to comment Share on other sites More sharing options...
Lynie Posted February 8, 2009 Author Share Posted February 8, 2009 It worked fine with Notepad, but when I tried it with FreeCell it doesn't work. Link to comment Share on other sites More sharing options...
BrettF Posted February 8, 2009 Share Posted February 8, 2009 Well if I understand it correctly, the minute that window gets redrawn, the text will cease to be there, hence the need to hook the window. Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Lynie Posted February 8, 2009 Author Share Posted February 8, 2009 Well if I understand it correctly, the minute that window gets redrawn, the text will cease to be there, hence the need to hook the window.Cheers,BrettThanks for the reply. But I already thought about that I just redraw the line in a loop (while 1). But it doesn't work :s Link to comment Share on other sites More sharing options...
Richard Robertson Posted February 9, 2009 Share Posted February 9, 2009 Did you check the handle to make sure it returned valid? Link to comment Share on other sites More sharing options...
Lynie Posted February 9, 2009 Author Share Posted February 9, 2009 Did you check the handle to make sure it returned valid?Well it should since I just run it like BretF's example. Link to comment Share on other sites More sharing options...
BrettF Posted February 9, 2009 Share Posted February 9, 2009 Try alter the sleep time.... Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Lynie Posted February 9, 2009 Author Share Posted February 9, 2009 (edited) I have the script to do the same thing in C++. Exactly the same works fine with notepad, but doesn't work in FreeCell or any other process :s. expandcollapse popup#include <windows.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; break; } return DefWindowProc(hwnd, msg, wparam, lparam); } INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { WNDCLASSEX wc; MSG msg; HWND hwnd, hTarget; HDC hdc; ZeroMemory(&wc, sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.lpfnWndProc = WndProc; wc.lpszClassName = "BASIC"; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); RegisterClassEx(&wc); hwnd = CreateWindowEx(NULL, "BASIC", "NoScope", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 200, 200, 250, 300, NULL, NULL, hInstance, NULL); hTarget = FindWindow(NULL, "FreeCell"); Sleep(500); hdc = GetDC(hTarget); if(!hTarget) return 1; if(!hdc) return 1; while(true) { Sleep(1); Rectangle(hdc, 5, 5, 20, 20); GetMessage(&msg, NULL, NULL, NULL); TranslateMessage(&msg); DispatchMessage(&msg); if(msg.message == WM_QUIT) break; } } EDIT: Works in wordpad too Edited February 9, 2009 by Lynie Link to comment Share on other sites More sharing options...
Lynie Posted February 9, 2009 Author Share Posted February 9, 2009 Try alter the sleep time....Thanks, I tried that and when I opened up counter strike I saw a rectangle for like 1 second. So I must be getting close(I guess) Link to comment Share on other sites More sharing options...
BrettF Posted February 9, 2009 Share Posted February 9, 2009 Like I said you need to hook the window to redraw it. Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
Lynie Posted February 10, 2009 Author Share Posted February 10, 2009 Like I said you need to hook the window to redraw it.Could you give a small example on how to do that please? Link to comment Share on other sites More sharing options...
Valik Posted February 10, 2009 Share Posted February 10, 2009 As tempting as it is to let you get banned from your game for modifying it, I'm closing this. You're not going to leech on users who are too silly to realize helping you is a waste of their time. As a result I'm closing this thread. Link to comment Share on other sites More sharing options...
Recommended Posts