Draw a rectangle with transparency

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
JimmyBoy
Newbie
Posts: 6
Joined: Mon Feb 08, 2010 2:09 pm

Draw a rectangle with transparency

Post by JimmyBoy » Mon May 29, 2017 4:11 pm

Hi,

I am simply trying to draw a rectangle on the screen, that has some transparency.

I can get this working in VB.NET with the following code

Code: Select all

Public Class Class1

    Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr

    Public Sub ShowBoundary(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer)


        Dim hdc As IntPtr = GetDC(IntPtr.Zero)

        Try
            Using g As Graphics = Graphics.FromHdc(hdc)

                Using brder As New Pen(Color.Blue)

                    ' Paint boundaries rectangle
                    Using myBrush As New System.Drawing.SolidBrush(Color.FromArgb(128, System.Drawing.Color.White))
                        g.FillRectangle(myBrush, New Rectangle(X1, Y1, X2 - X1, Y2 - Y1))
                    End Using

                    Using myBrush As New System.Drawing.SolidBrush(Color.FromArgb(128, System.Drawing.Color.Black))
                        g.FillRectangle(myBrush, New Rectangle(X1 + 1, Y1 + 1, (X2 - X1) - 2, (Y2 - Y1) - 2))
                    End Using

                End Using

            End Using
        Finally
            ReleaseDC(IntPtr.Zero, hdc)
        End Try

    End Sub
I would essentially like to import the above VB.NET code into MS with LibFunc calls instead. I have "borrowed" some code here from JRL (i think) which gets 90% of the work done, but can only get a solid rectangle (no transparency).

Code: Select all

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

  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
I have modified this to allow me to use CreateHatchBrush instead, which could work as a compromise.

Code: Select all

RGB>255,255,255,colWhite
RGB>128,128,128,colGrey
RGB>0,0,0,colBlack
RGB>255,0,0,colRed

Let>OPAQUE=2
Let>TRANSPARENT=1

Let>HS_BDIAGONAL=3
Let>HS_CROSS=4
Let>HS_DIAGCROSS=5
Let>HS_FDIAGONAL=2
Let>HS_HORIZONTAL=0
Let>HS_VERTICAL=1

GoSub>DrawRectangle,0,1,%colWhite%,%colBlack%,10,10,100,100

SRT>DrawRectangle
  LibFunc>user32,GetDC,HDC,%DrawRectangle_var_1%
  Libfunc>gdi32,SetBkMode,sbmres,hdc,%TRANSPARENT%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawRectangle_var_2%,%DrawRectangle_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  LibFunc>gdi32,CreateHatchBrush,SBres,%HS_DIAGCROSS%,%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
I am struggling with translating the VB.NET code to MS, specifically with trying to get FromArgb working as it does in VB.NET, which gives me the transparency I want.

If anyone could point me in the right direction, or code examples of how I might get this implemented, I would be very grateful.

On a sidenote, say I draw a rectangle, what is the best way for me to "erase" the rectangle once I no longer want it present on the screen?
Using this appears to work, but wonder if anyone has a better / alternate option just clear the area the rectangle was drawn on.

Code: Select all

LibFunc>user32,InvalidateRect,invres,0,0,0
Thanks in advance.

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

Re: Draw a rectangle with transparency

Post by Marcus Tettmar » Tue May 30, 2017 7:08 am

Hi,

Googling FromARGB it looks like it is a .Net method. You therefore will not be able to use it in Macro Scheduler.

VB.Net and Macro Scheduler are about as different to each other as you can get - you can't convert VB.Net code to Macro Scheduler code. My advice would be to use each for their strengths. If you need to use Macro Scheduler's automating abilities why not have your VB.Net app run a Macro Scheduler compiled macro, or consider using the SDK, and then you can continue to do the other stuff like this graphics stuff inside of VB.Net.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

JimmyBoy
Newbie
Posts: 6
Joined: Mon Feb 08, 2010 2:09 pm

Re: Draw a rectangle with transparency

Post by JimmyBoy » Tue May 30, 2017 7:16 am

Hi Marcus

Thanks for the reply.

I appreciate VB.net and MS are two completely different beasts, maybe my wording of 'import vb.net script' was a little misleading. What I have been doing is calling the vb.net code with rectangle parameters from MS, to draw the required rectangle on screen, but when trying to draw say 10 rectangles (10 calls to the vb.net app) it can become a little slow (to be expected).

I got inspired after reading JRL's code which almost does exactly what I want, bar some transparency, but draws so much faster than calling vb.net I wanted to try and get it working.

To rephrase the question, is there anyway to modify the below code to have some transparency (i.e. 50%) to the filled rectangle?

Not thought about the SDK, will give that some more thought.

Code: Select all

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

  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

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

Re: Draw a rectangle with transparency

Post by JRL » Tue May 30, 2017 2:38 pm

Does this example not do what you need?

viewtopic.php?f=9&t=8245&p=36647#p36647

The draw rectangle example is meant to draw a filled rectangle. The only way I know to get an empty rectangle is to draw four individual lines. This example provides a semi transparent dialog to draw on and uses a single pixel wide black line. The selection box line can be any color you choose and can be multiple pixels wide. The drawn on dialog can be completely transparent (set AlphaBlendValue = 1 not 0).

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Draw a rectangle with transparency

Post by ocnuybear » Tue May 10, 2022 1:32 am

Just to add to JimmyBoy's code, I needed to draw a square as well, but without using dialogs as MS needs to run javascript when mouse is hovering over chrome & by drawing this way, one does not have to constantly activate the chrome underneath the dialogs:

Code: Select all

RGB>255,0,0,colRed
Let>Line_width=3
Let>High=1080
Let>Wide=1920

Let>XLeft_start=0
Let>YLeft_start=0
Let>XLeft_stop=XLeft_start+Line_width
Let>YLeft_stop=YLeft_start+High

Let>XRight_start=XLeft_start+Wide
Let>XRight_start=XRight_start-Line_width
Let>YRight_start=YLeft_start
Let>XRight_stop=XLeft_stop+Wide
Let>XRight_stop=XRight_stop-Line_width
Let>YRight_stop=YLeft_stop

Let>XTop_start=XLeft_start
Let>YTop_start=YLeft_start
Let>XTop_stop=XLeft_start+Wide
Let>YTop_stop=YLeft_start+Line_width

Let>XBottom_start=XTop_start
Let>YBottom_start=YTop_start+High
Let>YBottom_start=YBottom_start-Line_width
Let>XBottom_stop=XTop_stop
Let>YBottom_stop=YTop_stop+High
Let>YBottom_stop=YBottom_stop-Line_width

GoSub>DrawRectangle,0,1,%colRed%,%colRed%,XLeft_start,YLeft_start,XLeft_stop,YLeft_stop
GoSub>DrawRectangle,0,1,%colRed%,%colRed%,XTop_start,YTop_start,XTop_stop,YTop_stop
GoSub>DrawRectangle,0,1,%colRed%,%colRed%,XRight_start,YRight_start,XRight_stop,YRight_stop
GoSub>DrawRectangle,0,1,%colRed%,%colRed%,XBottom_start,YBottom_start,XBottom_stop,YBottom_stop

SRT>DrawRectangle
  LibFunc>user32,GetDC,HDC,%DrawRectangle_var_1%
  Libfunc>gdi32,SetBkMode,sbmres,hdc,%TRANSPARENT%
  LibFunc>gdi32,CreatePen,Penres,0,%DrawRectangle_var_2%,%DrawRectangle_var_3%
  LibFunc>gdi32,SelectObject,SOPres,hdc,Penres
  LibFunc>gdi32,CreateHatchBrush,SBres,%HS_DIAGCROSS%,%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
Wait>1
LibFunc>user32,InvalidateRect,invres,0,0,0


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