Draw / display a box

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
weklica
Newbie
Posts: 6
Joined: Thu May 27, 2010 10:19 pm

Draw / display a box

Post by weklica » Fri Jun 18, 2010 2:38 pm

I have a script looking in an imaginary box where I say the x and y start positions might be 0,0 their end positions might be 100,100. This will create a box for various functions. Is there a way to light that same square up? Perhaps, I could just tell a picture (colored box) to show up in the given area. Or, maybe there is some way in JAVA? not sure, hoping that perhaps macro 12 has this ability...if so, I can't find a topic to illustrate line drawing or anything. Thanks in advance.

One more thing .... I have thought about just having the mouse move around the perimeter. This would at least illustrate where the box was for user feedback, but a semi-lucent box that would fade in and out would be so much more pleasing to the eyes.
Jesse

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Fri Jun 18, 2010 3:19 pm

You can use the Windows GDI functions. You can easily draw a rectangle on the screen. For inspiration take a look at the DrawLine subroutine in this post:

http://www.mjtnet.com/forum/viewtopic.php?t=6030
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Jun 18, 2010 3:29 pm

Sounds to me like you want the windows API drawing functions. You access these using Macro Scheduler's LibFunc> function. I've posted several sample scripts over the past couple of years but if you don't know what to search for you're not likely to find them.

Here's a couple of subroutines I've created to do drawing. I don't have time to make you a working sample right now and I don't have these commented for you but you can probably figure out how to use them. You can find some of the samples I've already posted by searching the forum for "GetDC". I really need to get these (and others) commented with simple samples and posted in Scripts and Tips.

Hopefully I can get a simpler sample put together later today unless someone else does it first....

Code: Select all

//DrawLine Usage:
//GoSub>DrawLine,WindowHandle,PenSize,PenColor,XStart,YStart,XEnd,YEnd

SRT>DrawLine
  LibFunc>user32,GetDC,HDC,%DrawLine_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawLine_var_2%,%DrawLine_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  Libfunc>gdi32,MoveToEx,mtres,HDC,%DrawLine_var_4%,%DrawLine_var_5%,0
  LibFunc>gdi32,LineTo,ltres,hdc,%DrawLine_var_6%,%DrawLine_var_7%
  LibFunc>gdi32,DeleteObject,DOres,Penres
  LibFunc>user32,ReleaseDC,RDCres,HDC_1,HDC
END>DrawLine

Code: Select all

//DrawRectangle Usage:
//GoSub>DrawRectangle,WindowHandle,PenSize,PenColor,SolidColor,ULXLocation,ULYLocation,LRXLocation,LRYLocation

SRT>DrawRectangle
  LibFunc>user32,GetDC,HDC,%DrawRectangle_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawRectangle_var_2%,%DrawRectangle_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  LibFunc>gdi32,CreateSolidBrush,SBres,%DrawRectangle_var_4%
  LibFunc>gdi32,SelectObject,SOres,hdc,sbres
  LibFunc>gdi32,Rectangle,recres,hdc,%DrawRectangle_var_5%,%DrawRectangle_var_6%,%DrawRectangle_var_7%,%DrawRectangle_var_8%
  LibFunc>gdi32,DeleteObject,DOres,Penres
  LibFunc>gdi32,DeleteObject,DOres,sbres
  LibFunc>user32,ReleaseDC,RDCres,HDC_1,HDC
END>DrawRectangle

fightcancer
Macro Veteran
Posts: 252
Joined: Fri Apr 15, 2005 8:32 am

Re: Draw / display a box

Post by fightcancer » Thu Apr 01, 2021 7:57 pm

I could never get MS to draw a rectangle. Is there any sample script I could look at to see what values might follow the GoSub> command? TIA!

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Draw / display a box

Post by JRL » Fri Apr 02, 2021 2:02 am

Wow. It took 11 years to post a sample. I feel bad.
See the comments.

Code: Select all

//Dialog to put a rectangle in
Dialog>Dialog1
object Dialog1: TForm
  BorderIcons = [biSystemMenu, biMinimize]
  Caption = 'Rectangle Example'
  ClientHeight = 211
  ClientWidth = 476
  Position = poScreenCenter
end
EndDialog>Dialog1

//handler to exit when the dialog is closed
AddDialogHandler>Dialog1,,OnClose,Quit

Show>Dialog1

SRT>Quit
  Exit>0
END>Quit

//draw a red rectangle in dialog1 with a 5 pixel wide border
//a white center and drawn from position 40,40 to 160,160
GoSub>DrawRectangle,Dialog1.handle,5,255,16777215,40,40,160,160
Wait>1

//We loop to keep the rectangle active.  If the dialog is minimised
//or goes off the desktop the rectangle goes away.  They are not
//automatically redrawn.  If you want it to stay visible, it must
//be redrawn continuously
Label>Loop
  Wait>0.01
  //Draw a green rectangle in dialog1 with an 8 pixel wide border
  //a light blue center apositioned from 50,50 to 150,150
  GoSub>DrawRectangle,Dialog1.handle,8,111111,16777180,50,50,150,150
Goto>Loop


//DrawRectangle Usage:
//GoSub>DrawRectangle,WindowHandle,PenSize,PenColor,SolidColor,ULXLocation,ULYLocation,LRXLocation,LRYLocation
SRT>DrawRectangle
  LibFunc>user32,GetDC,HDC,%DrawRectangle_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawRectangle_var_2%,%DrawRectangle_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  LibFunc>gdi32,CreateSolidBrush,SBres,%DrawRectangle_var_4%
  LibFunc>gdi32,SelectObject,SOres,hdc,sbres
  LibFunc>gdi32,Rectangle,recres,hdc,%DrawRectangle_var_5%,%DrawRectangle_var_6%,%DrawRectangle_var_7%,%DrawRectangle_var_8%
  LibFunc>gdi32,DeleteObject,DOres,Penres
  LibFunc>gdi32,DeleteObject,DOres,sbres
  LibFunc>user32,ReleaseDC,RDCres,HDC_1,HDC
END>DrawRectangle
Note that rectangles are filled. If you want a border that is empty in the middle, you'll want to draw four lines.

Code: Select all

//Dialog to put a rectangle in
Dialog>Dialog1
object Dialog1: TForm
  BorderIcons = [biSystemMenu, biMinimize]
  Caption = 'Rectangle Example'
  ClientHeight = 211
  ClientWidth = 476
  Position = poScreenCenter
  object Label1: TLabel
    Left = 65
    Top = 88
    Width = 32
    Height = 13
    Caption = 'Empty Center'
  end
end
EndDialog>Dialog1

//handler to exit when the dialog is closed
AddDialogHandler>Dialog1,,OnClose,Quit

Show>Dialog1

SRT>Quit
  Exit>0
END>Quit


//We loop to keep the rectangle active.  If the dialog is minimised
//or goes off the desktop the rectangle goes away.  They are not
//automatically redrawn.  If you want it to stay visible, it must
//be redrawn continuously
Label>Loop
  Wait>0.01
  //Draw a green rectangle in dialog1 with an 8 pixel wide border
  //and positioned from 50,50 to 150,150
  GoSub>DrawLine,Dialog1.handle,8,111111,50,50,150,50
  GoSub>DrawLine,Dialog1.handle,8,111111,150,50,150,150
  GoSub>DrawLine,Dialog1.handle,8,111111,150,150,50,150
  GoSub>DrawLine,Dialog1.handle,8,111111,50,150,50,50
Goto>Loop

//DrawLine Usage:
//GoSub>DrawLine,WindowHandle,PenSize,PenColor,XStart,YStart,XEnd,YEnd
SRT>DrawLine
  LibFunc>user32,GetDC,HDC,%DrawLine_var_1%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawLine_var_2%,%DrawLine_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  Libfunc>gdi32,MoveToEx,mtres,HDC,%DrawLine_var_4%,%DrawLine_var_5%,0
  LibFunc>gdi32,LineTo,ltres,hdc,%DrawLine_var_6%,%DrawLine_var_7%
  LibFunc>gdi32,DeleteObject,DOres,Penres
  LibFunc>user32,ReleaseDC,RDCres,HDC_1,HDC
END>DrawLine

fightcancer
Macro Veteran
Posts: 252
Joined: Fri Apr 15, 2005 8:32 am

Re: Draw / display a box

Post by fightcancer » Wed Apr 07, 2021 6:51 pm

Thank you!

What are the chances I could get MS to draw a shape in a browser like Chrome? I've tried GetWindowHandle> and Let>WIN_USEHANDLE=1, but nothing works when passing the handle to the SRT with GoSub.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts