Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreGraphics Overlay Window/EasyOverlay/Magnify
Function:
Required plugins for this example: MBS MacCG Plugin, MBS Picture Plugin, MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreGraphics Overlay Window/EasyOverlay/Magnify
This example is the version from Fri, 17th Nov 2016.
Project "Magnify.rbp"
FileTypes
Filetype text
End FileTypes
MenuBar MenuBar1
MenuItem UntitledMenu0 = ""
End MenuBar
Class App Inherits Application
EventHandler Sub Open() Overlay=new MagnifyOverlay Overlay.init 300,300,200,200 Overlay.Moveable=true Overlay.IgnoreClicks=false // enable mouse events Overlay.doIt 300,300 Overlay.ForeColor=rgB(200,200,200) Overlay.ForeColorTransparency=180 Overlay.fillOval 10,10,180,180 Overlay.ForeColorTransparency=255 Overlay.TextSize=30 Overlay.ForeColor=rgb(0,0,255) Overlay.DrawString "MBS Plugins",14,82 Overlay.DrawWindow End EventHandler
Property Overlay As MagnifyOverlay
End Class
Class MagnifyOverlay Inherits EasyOverlayWindow
EventHandler Function MouseDown(x As Integer, y As Integer, ModifierKeys As Integer,MouseButton as integer, ClickCount As Integer) As Boolean Return true End EventHandler
EventHandler Sub MouseUp(x As Integer, y As Integer, ModifierKeys As Integer,MouseButton as integer, ClickCount As Integer) clear // Clear context doIt left,top End EventHandler
EventHandler Sub WindowDragAction(X As Integer, Y As Integer) // while dragging the overlay clear // clear contex // prepare and draw round border LineWidth=10 ForeColor=rgb(0,200,0) DrawOval 0,0,200-10,200-10 // prepare and draw crosshair ForeColor=rgb(0,0,0) LineWidth=1 DrawLine 50+50,50+40,50+50,50+60 DrawLine 50+40,50+50,50+60,50+50 drawWindow // draw everything into the overlay window End EventHandler
Sub doIt(x As integer, y As integer) dim p1, p2 As Picture // Hide Cross Clear LineWidth=10 ForeColor=rgb(0,200,0) DrawOval 0,0,200-10,200-10 DrawWindow DelayMBS 0.1 // let system update window p1 = New Picture(200,200,32) p2=ScreenshotRectMBS(x+50,y+50,100,100) // Take a screenshot p1.Graphics.DrawPicture p2,0,0,p1.width,p1.height,0,0,p2.width,p2.height // Scale Screenshot // prepare mask p1.mask.Graphics.ForeColor=rgb(255,255,255) p1.Mask.Graphics.FillRect 0,0,p1.width,p1.height p1.mask.Graphics.ForeColor=rgb(0,0,0) p1.Mask.Graphics.FillOval 0,0,p1.width,p1.height // Draw Mask DrawPicture p1,0,0 // draw the scaled screenshot // prepare and draw a round border LineWidth=10 ForeColor=rgb(0,200,0) DrawOval 0,0,200-10,200-10 DrawWindow //Draw eveything into the overlay window End Sub
End Class
Class EasyOverlayWindow Inherits OverlayWindowMBS
Event MouseDown(x As Integer, y As Integer, ModifierKeys As Integer,MouseButton as integer, ClickCount As Integer) As Boolean End Event
Event MouseDrag(x As Integer, y As Integer, ModifierKeys As Integer,MouseButton as integer) End Event
Event MouseMove(X As Integer, Y As Integer,ModifierKeys As Integer) End Event
Event MouseUp(x As Integer, y As Integer, ModifierKeys As Integer,MouseButton as integer, ClickCount As Integer) End Event
Event Paint(o As EasyOverlayWindow) End Event
Event WindowDragAction(X As Integer, Y As Integer) End Event
EventHandler Function MouseDown(x as double, y as double, ModifierKeys as integer, MouseButton as integer, ClickCount as integer) As boolean systemmousex=System.MouseX systemmousey=System.MouseY MouseClicked=MouseDown(x-left,y-top,ModifierKeys, MouseButton, ClickCount) if Moveable then return true end if End EventHandler
EventHandler Function MouseDragged(x as double, y as double, ModifierKeys as integer, MouseDeltaX as double, MouseDeltaY as double, MouseButton as integer) As boolean if Moveable then left=left+System.MouseX-systemmousex top=top+System.MouseY-systemmousey systemmousey=System.MouseY systemmousex=System.MouseX if MouseDeltaX<>0 or MouseDeltaY<>0 then WindowDragAction left,top end if if MouseClicked then MouseDrag x-left,y-top,ModifierKeys, MouseButton end if End EventHandler
EventHandler Function MouseMoved(x as double, y as double, ModifierKeys as integer, MouseDeltaX as double, MouseDeltaY as double) As boolean MouseMove x-left,y-top,ModifierKeys End EventHandler
EventHandler Function MouseUp(x as double, y as double, ModifierKeys as integer, MouseButton as integer, ClickCount as integer) As boolean if MouseClicked then MouseUp x-left,y-top,ModifierKeys, MouseButton, ClickCount end if End EventHandler
Private Sub AddOval(c as CGContextMBS, center as CGPOintMBS, a as single, b as single) dim pi as single pi=ACos(0)*2.0 c.SaveGState c.TranslateCTM center.x, center.y c.ScaleCTM a,b c.MoveToPoint 1,0 c.AddArc 0, 0, 1, 0.0, 2*pi, false c.ClosePath c.RestoreGState End Sub
Sub Clear() myContext.ClearRect myRect End Sub
Sub DrawBevelFrame(x As Integer, y As Integer, w As Integer, h As integer, TopLeftColor As color, ButtomRightColor As color) dim d,i,LineWidthMerk As integer dim ColorMerk As color ColorMerk=foreColor // Remember ForeColor LineWidthMerk=LineWidth// Remember LineWidth d=ceil(LineWidth/2) LineWidth=1 for i= 1 to LineWidthMerk ForeColor=ButtomRightColor //Draw ButtomRight DrawLine x+w-i, y+i, x+w-i, y+h //right vert DrawLine x+i, y+h-i, x+w-i, y+h-i //down hor ForeColor=TopLeftColor // Draw TopLeft DrawLine x, y+i, x+w-i, y+i //Up hor DrawLine x+i, y, x+i, y+h-i // left vert next ForeColor=ColorMerk // Set ForeColor as it was before LineWidth=LineWidthMerk // set LineWidth as it was before End Sub
Sub DrawLine(x1 As integer, y1 As integer, x2 As integer, y2 As integer) dim ct As CGContextMBS dim yy1 As integer, yy2 ,xx1,xx2 As integer dim c As color 'c=rgb(255,0,0) 'myForeColor xx1=x1'+LineWidth/2 xx2=x2'+LineWidth/2 yy1=height-y1 yy2=height-y2 ct=myContext ct.beginPath ct.moveToPoint xx1,yy1 ct.AddLineToPoint xx2,yy2 ct.strokePath End Sub
Sub DrawOval(x As Integer, y As Integer, w As Integer, h As Integer) dim c as CGContextMBS dim center as CGPointMBS dim yy,l As integer yy=height-y l=lineWidth/2 center=CGMakePointMBS(x+l+w/2,yy-l-h/2) c=myContext c.BeginPath AddOval c,center, w/2,h/2 c.strokePath End Sub
Sub DrawPicture(p As Picture, x As Integer, y As integer) dim r As CGRectMBS dim ci As CGImageMBS ci=CGCreateImageMBS(p,p.mask) r=CGMakeRectMBS(x,height-p.height-y, p.width, p.height) myContext.DrawPicture ci,r End Sub
Sub DrawRect(x As integer, y As integer, w As integer, h As integer) dim r As CGRectMBS dim d As integer d=ceil(LineWidth/2) r=CGMakeRectMBS(x+d,height-h-y+d,w-LineWidth,h-LineWidth) myContext.strokerect r 'dim d As integer ' ' ' 'd=ceil(LineWidth/2) ' 'DrawLine x+d, y+d, x+w, y+d //Up hor ' 'DrawLine x+d, y, x+d, y+h // left vert ' 'DrawLine x+w-d, y, x+w-d, y+h //right vert ' 'DrawLine x+d, y+h-d, x+w, y+h-d //down hor End Sub
Sub DrawRoundRect(x As Integer, y As Integer,w As Integer, h As Integer,arcx As integer,arcy as integer) doDrawRoundRect x+Linewidth/2, y+LineWidth/2, w-lineWidth, h-LineWidth, arcx, arcy,false End Sub
Sub DrawString(Text As string, x As integer, y As Integer) dim c As CGContextMBS c=myContext c.selectFont myTextFont, myTextSize, 1 c.ShowTextAtPoint text,x,height-y-myTextSize End Sub
Sub DrawStringWithShadow(text As string, x As Integer, y As Integer, ShadowSize As integer, ShadowTransparency As Integer, ShadowColor as Color=&c000000) dim colorMerk As Color dim transMerk As Integer ColorMerk=ForeColor transMerk=ForeColorTransparency // set Shadow values ForeColor=ShadowColor Transparency=ShadowTransparency // Draw Shadow DrawString text,x+ShadowSize,y+shadowSize // set String values ForeColor=colorMerk ForeColorTransparency=transMerk // draw string DrawString text,x,y End Sub
Sub DrawWindow() myContext.flush End Sub
Sub FillOval(x As integer, y As integer, w As integer, h As integer) dim c as CGContextMBS dim center as CGPointMBS dim yy,xx As integer yy=height-y xx=width-x center=CGMakePointMBS(x+w/2,yy-h/2) c=myContext c.BeginPath AddOval c,center, w/2,h/2 c.FillPath End Sub
Sub FillRect(x As Integer, y As Integer, w As Integer, h As Integer) dim r As CGRectMBS r=CGMakeRectMBS(x,height-h-y,w,h) myContext.Fillrect r End Sub
Sub FillRoundRect(x As Integer, y As Integer,w As Integer, h As Integer,arcx As integer,arcy as integer) doDrawRoundRect x,y,w,h,arcx,arcy,true End Sub
Function ForeColor() As Color return myForeColor End Function
Sub ForeColor(Assigns c As Color) myContext.setRGBFillColor c.red/255, c.green/255, c.blue/255, ForeColorTransparency/255 myContext.SetRGBStrokeColor c.red/255, c.green/255, c.blue/255, ForeColorTransparency/255 myForeColor=c End Sub
Function ForeColorTransparency() As Integer return myTransparency End Function
Sub ForeColorTransparency(Assigns v As Integer) dim c As color c=ForeColor myContext.setRGBFillColor c.red/255, c.green/255, c.blue/255, v/255 myContext.SetRGBStrokeColor c.red/255, c.green/255, c.blue/255, v/255 myTransparency=v End Sub
Sub Init(wLeft As Integer, wTop As Integer, wWidth As integer, wHeight As integer,AttachedWindow As window=nil) // Create Overlay Window ' if me.Create(wLeft,wTop,wWidth,wHeight)<>0 then beep me.InstallEventHandler if AttachedWindow<>NIL then me.AttachToWindow(AttachedWindow,true) me.show // Create RECT and CONTEXT myRect=CGMakeRectMBS(0,0,me.width,me.height) myContext=me.context myContext.ClearRect myRect // set Defaults foreColor=rgb(0,0,0) ForeColorTransparency=255 LineWidth=1 TextFont="Helvetica" TextSize=12 Width=wWidth Height=wHeight End Sub
Function LineWidth() As Integer Return myLineWidth End Function
Sub LineWidth(Assigns v As Integer) myContext.setLineWidth(v) myLineWidth=v End Sub
Sub Refresh() if myContext<>NIL and myRect<>Nil then Paint me myContext.flush end if End Sub
Function StringWidth(text As String) As Integer dim p As picture dim g As graphics p = New Picture(32,32,32) // Dummy Picture g=p.graphics g.TextFont=TextFont g.TextSize=TextSize return g.stringWidth(text) End Function
Function TextFont() As String return myTextFont End Function
Sub TextFont(Assigns v As string) myTextFont=v End Sub
Function TextSize() As Integer return myTextSize End Function
Sub TextSize(Assigns v As Integer) myTextSize=v End Sub
Sub Update() myRect=CGMakeRectMBS(0,0,width,height) myContext=me.context myContext.ClearRect myRect refresh End Sub
Sub doDrawRoundRect(x As Integer, y As Integer,w As Integer, h As Integer,arcx As integer,arcy as integer,filled As boolean) dim ct As CGContextMBS dim yy, xp, yp As integer yy=height-h-y ct=myContext ct.beginPath xp=x+arcX yp=yy ct.MoveToPoint xp,yp //goto TopLeft xp=xp+w - 2*arcx ct.AddLineToPoint xp,yp // top line xp=xp+arcx ct.AddQuadCurveToPoint xp, yp, xp, yp+arcy // TopRight corner yp=yp+h-arcy ct.AddLineToPoint xp,yp // right line yp=yp+arcy ct.AddQuadCurveToPoint xp, yp, xp-arcx, yp // Bottom/Right Corner xp=xp-w+arcx ct.addLineToPoint xp, yp //bottom line xp=x yp=yp-arcy ct.AddQuadCurveToPoint xp, yp+arcy, xp, yp //BottomLeft corner yp=yy+arcy ct.addLineToPoint xp,yp // left line ct.AddQuadCurveToPoint x, yy, x+arcX, yy // TopLeft corner if filled then ct.fillPath else ct.StrokePath end if End Sub
Note "Info"
EasyOverlay is a easy to use OverlayWindowMBS You can use it almost like a Graphics Class This Methodes and Properties are equal with the Graphics Class: DrawLine x1,y1,x2,y2 DrawOval x, y, width, height FillOval x, y, width, height DrawRect x, y, width, height FillRect x, y, width, height DrawRoundRect x As Integer, y As Integer, width As Integer, height As Integer, arcx As integer, arcy as integer FillRoundRect x As Integer, y As Integer, width As Integer, height As Integer, arcx As integer, arcy as integer DrawPicture p, x, y // No scaling yet! DrawString text, x, y TextSize TextFont StringWidth ForeColor New Methodes: DrawStringWithShadow text As string, x As Integer, y As Integer, ShadowSize As integer, ShadowTransparency As Integer, ShadowColor as Color DrawBevelFrame x As Integer, y As Integer, width As Integer, height As integer, TopLeftColor As color, ButtomRightColor As color Refresh (Fire the Paint Event) Clear (Clear the OverlayWindow) Init left as integer, top as integer, width as integer, height as integer [,w as window] (use init after you created EasyOverlay. If w<>Nil the overlay will be attached to w) Update (use it after you resized the overlay) New Properties: ForeColorTransparency as Integer (0=transparent - 255=opaque) LineWidth as Integer (Use this insteat of PenWidth/PenHeight) Moveable as boolean (if true, the overlay can be draged) Exemples: This Example creates a moveable OverlayWindow // Create a Class name: "TestOverlay" Super: "EasyOverlayWindow" // Add a Propertie "myTestOverlay as TestOverlay" to your 'App Class' // Put this code into the 'Open Event' of your 'App Class' myTestOverlay =new TestOverlay myTestOverlay.EatMouseEvents=true //Otherwise the Mouse Events will not fire myTestOverlay.init 200,200,400,200 myTestOverlay.Moveable=true myTestOverlay.Refresh // Put this code into the 'Paint Event' of 'TestOverlay' o.Transparency=127 o.FillRoundRect 0,0,400,200,10,10 o.ForeColorTransparency=255 o.ForeColor=rgb(0,0,255) o.DrawString "My first EasyOverlayWindow",100,50
Property Private Black As Color
Property Private MouseClicked As Boolean
Property Private MouseWasClicked As Boolean
Property Moveable As Boolean
Property Private myContext As CGContextMBS
Property Private myForeColor As Color
Property Private myLineWidth As Integer
Property Private myRect As CGRectMBS
Property Private myTextFont As String
Property Private myTextSize As Integer
Property Private myTransparency As Integer
Property systemmousex As Integer
Property systemmousey As Integer
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacCG Plugin.




Links
MBS Xojo Plugins