Kohr Posted October 3, 2006 Share Posted October 3, 2006 I use the following function to draw 4 lines (make a box with a width of 3 and red color) around objects. The problem is that I will do a Loop of 100 and inside the loop I call the function roughly 30 times. Right around the 88th time in the loop the lines that are drawn from the function quit showing up as size 3 color red and end up being size 1 color black. Everything else about the function is still accurate (line x/y coords). I have been trying to figure this out and I was hoping someone could give me any suggestions as to why a DllCall would only partially work after running for a while. I am using DllOpen, DllCall, DllClose inside the function so this gets done maybe 2400+ times. I tried moving the DllOpen outside the function but that didn't help. Kohr expandcollapse popupFunc fDrawBox($Window, $Control, $LineColor, $LineWidth, $OffsetX = 0, $OffsetY = 0, $LeftIn = 0, $RightIn = 0) $size = WinGetPos($Window) ;ConsoleWrite("Window-X:" & $size[0] & " Y:" & $size[1] & " W:" & $size[2] & " H:" & $size[3] & @CRLF) $ControlPos = ControlGetPos($Window, "", $Control) If $ControlPos = 0 Then MsgBox(0, "Cannot find Object", "Verify the POWER application is open.") Return 0 EndIf ;ConsoleWrite("Control-X:" & $ControlPos[0] & " Y:" & $ControlPos[1] & " W:" & $ControlPos[2] & " H:" & $ControlPos[3] & @CRLF) $ControlPos[0] = $ControlPos[0] + $size[0] + $OffsetX $ControlPos[1] = $ControlPos[1] + $size[1] + $OffsetY $dllUser32 = DllOpen("user32.dll") $dllGdi32 = DllOpen("gdi32.dll") For $i = 1 To 4 Switch $i Case 1;top side $x1 = $ControlPos[0] + $LeftIn $y1 = $ControlPos[1] $x2 = $ControlPos[0] + $ControlPos[2] - $RightIn $y2 = $ControlPos[1] Case 2;left side $x1 = $ControlPos[0] + $LeftIn $y1 = $ControlPos[1] $x2 = $ControlPos[0] + $LeftIn $y2 = $ControlPos[1] + $ControlPos[3] Case 3;right side $x1 = $ControlPos[0] + $ControlPos[2] - $RightIn $y1 = $ControlPos[1] $x2 = $ControlPos[0] + $ControlPos[2] - $RightIn $y2 = $ControlPos[1] + $ControlPos[3] Case 4;bottom side $x1 = $ControlPos[0] + $LeftIn $y1 = $ControlPos[1] + $ControlPos[3] $x2 = $ControlPos[0] + $ControlPos[2] - $RightIn $y2 = $ControlPos[1] + $ControlPos[3] EndSwitch $hd = DllCall($dllUser32, "int", "GetDC", "hwnd", 0) $pen = DllCall($dllGdi32, "int", "CreatePen", "int", 0, "int", $LineWidth, "int", $LineColor) DllCall($dllGdi32, "int", "SelectObject", "int", $hd[0], "int", $pen[0]) DllCall($dllGdi32, "int", "MoveToEx", "hwnd", $hd[0], "int", $x1, "int", $y1, "int", 0) DllCall($dllGdi32, "int", "LineTo", "hwnd", $hd[0], "int", $x2, "int", $y2) DllCall($dllUser32, "int", "ReleaseDC", "hwnd", 0, "int", $hd[0]) Next DllClose($dllUser32) DllClose($dllGdi32) ;~ If $LineColor <> "0x0000ff" Then ;~ fLog("Color:" & $LineColor & " LineWidth:" & $LineWidth) ;~ EndIf EndFunc ;==>fDrawBox AutoIt LinksAutoIt CrapsGrid_PixelSearchAdvancedPixelGrab Link to comment Share on other sites More sharing options...
BitRot Posted October 3, 2006 Share Posted October 3, 2006 I have been trying to figure this out and I was hoping someone could give me any suggestions as to why a DllCall would only partially work after running for a while.That is not the problem. That you are allocating resources without releasing them is. Add the following line just under the "ReleaseDC" -line, and all will (probably) be well : DllCall($dllGdi32, "int", "DeleteObject", "int", $pen[0]) Link to comment Share on other sites More sharing options...
Zedna Posted October 3, 2006 Share Posted October 3, 2006 That is not the problem. That you are allocating resources without releasing them is. Add the following line just under the "ReleaseDC" -line, and all will (probably) be well : DllCall($dllGdi32, "int", "DeleteObject", "int", $pen[0]) Before DeleteObject you must do SelectObject to old object because Selected object can't be deleted. Look for example at my Radar project in my signature. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Kohr Posted October 4, 2006 Author Share Posted October 4, 2006 Your suggestions worked great. Now to try it on a bigger run which loops about 1300 times so this function will run about 39,000 times. Kohr AutoIt LinksAutoIt CrapsGrid_PixelSearchAdvancedPixelGrab Link to comment Share on other sites More sharing options...
BitRot Posted October 4, 2006 Share Posted October 4, 2006 Before DeleteObject you must do SelectObject to old objectbecause Selected object can't be deleted.Ehhmmm ... Does the deleting of the DC it was selected in not automatically un-select it ? I'm asking because I tried the line I suggested just above as well as just below the line deleting the DC. Above it did not help, but below it seemed to work allright. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now