Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreGraphics Overlay Window/EasyOverlay/EasyOverlay
Function:
Required plugins for this example: MBS MacCG Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreGraphics Overlay Window/EasyOverlay/EasyOverlay
This example is the version from Thu, 16th Nov 2016.
Project "EasyOverlay.rbp"
FileTypes
Filetype text
End FileTypes
Class Window1 Inherits Window
EventHandler Sub MouseMove(X As Integer, Y As Integer) dim s As string dim i As integer Overlay.clear // Clear the overlay Overlay.refresh // Fire the Paint Event of the overlay // Draw a "X/Y" string on top of it s=str(x)+" X "+str(y) Overlay.TextSize=20 Overlay.TextFont="Helvetica" i=Overlay.StringWidth(s) Overlay.ForeColor=rgb(255,255,255) Overlay.DrawString s,overlay.width/2-i/2,overlay.height-40 Overlay.DrawWindow // Draw the whole thing 'self.title=str(x)+" x "+str(height-y) End EventHandler
EventHandler Sub Open() Overlay=new MyOverlay Overlay.Init left,top,width,height,self // Create Overlay Overlay.refresh // This fire the Paint Event End EventHandler
Property Protected overlay As MyOverlay
Property Protected xClick As Integer
Property Protected yClick As Integer
End Class
MenuBar MenuBar1
MenuItem UntitledMenu1 = ""
MenuItem FileMenu = "&File"
MenuItem FileQuit = "Quit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu0 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class App Inherits Application
EventHandler Sub Open() myEasyOverlay=new EasyOverlayWindow myEasyOverlay.Init 350,200,150,150 myEasyOverlay.Moveable=true myEasyOverlay.IgnoreClicks=false // enable mouse events myTimer=new aTimer myTimer.period=10 myTimer.mode=2 End EventHandler
Sub DrawClock() Dim g As EasyOverlayWindow g=myEasyOverlay g.clear // Clear the OverlayWindow // Draw a Background for the Clock g.foreColor=rgb(200,200,0) g.ForeColortransparency=230 g.FillOval 1,1,100,100 g.ForeColorTransparency=255 g.foreColor=rgb(0,0,0) // Draw a Clock // This is taken from a REALBasic Example: "Cool Clock" made by Made by Jim P. (j-jim@geocities.com) // it used to be in a Paint Event of a Window // No changes in the original code! 'g' was Graphics and is now 'EasyOverlayWindow' thats it. dim xh as integer dim yh as integer dim xm as integer dim ym as integer dim xs as integer dim ys as integer dim s as integer dim m as integer dim h as integer dim xcenter as integer dim ycenter as integer dim today as String dim d as date //center xcenter=50 ycenter=50 //get date stuff d=new date s=d.second m=d.minute h=d.hour today=d.shortdate+" "+d.shorttime //get points for drawing hands xs = cos(s * 3.14/30 - 3.14/2) * 45 + xcenter ys = sin(s * 3.14/30 - 3.14/2) * 45 + ycenter xm = cos(m * 3.14/30 - 3.14/2) * 40 + xcenter ym = sin(m * 3.14/30 - 3.14/2) * 40 + ycenter xh = cos((h*30 + m/2) * 3.14/180 - 3.14/2) * 30 + xcenter yh = sin((h*30 + m/2) * 3.14/180 - 3.14/2) * 30 + ycenter //draw oval g.drawoval 1,1,100,100 // label numbers g.forecolor=rgb(60,60,60) 'change color to dark grey g.drawString "9",xcenter-45,ycenter-12 g.drawString "3",xcenter+40,ycenter-12 g.drawString "12",xcenter-5,ycenter-42 g.drawString "6",xcenter-3,ycenter+30 //draw date g.drawString today, 0, 110 //draw hands g.forecolor=rgb(0,0,255) 'change color to blue g.drawLine xcenter, ycenter-1, xm, ym 'minutes g.drawLine xcenter-1, ycenter, xm, ym 'minutes g.drawLine xcenter, ycenter-1, xh, yh 'hours g.drawLine xcenter-1, ycenter, xh, yh 'hours g.drawLine xcenter, ycenter, xs, ys 'seconds g.DrawWindow //Draw the whole thing into the OverlayWindow End Sub
Property Private myEasyOverlay As EasyOverlayWindow
Property Private myTimer As aTimer
End Class
Class aTimer Inherits Timer
EventHandler Sub Action() app.DrawClock End EventHandler
End Class
Class MyOverlay Inherits EasyOverlayWindow
EventHandler Sub Paint(o As EasyOverlayWindow) dim p As picture o.foreColor=rgb(0,0,0) o.ForeColorTransparency=130 o.fillRect 0,0,o.width,o.height // FillRect o.ForeColor=rgb(255,0,0) // Red o.fillRect 10,10,50,80 o.ForeColorTransparency=127 // set Transparency o.foreColor=rgb(0,255,0) //green o.fillRect o.width/2-15,o.height/2-15,30,30 // another FillRect Centered o.ForeColorTransparency=255 // DrawLine o.LineWidth=4 o.foreColor=rgb(0,100,100) o.DrawLine 90,10,o.width-50,o.height-30 //DrawRect o.LineWidth=20 o.foreColor=rgb(0,0,255) o.DrawRect 50,70,o.width-100,o.height-100 ' //DrawString o.foreColor=rgb(100,0,0) o.TextSize=30 o.TextFont="Courier" o.drawString "DrawString",50,150 //DrawStringWithShadow o.TextSize=20 o.TextFont="Arial" o.foreColor=rgb(255,255,255) o.DrawStringWithShadow "DrawStringWithShadow",40,100,1,127,rgb(10,10,10) //DrawBevelFrame o.lineWidth=5 o.DrawBevelFrame 30,98,230,30,rgb(50,50,50),rgb(200,200,200) //DrawPicture // Create a Picture p = new Picture(80,80,32) p.graphics.foreColor=rgb(100,200,255) p.graphics.fillrect 0,0,80,80 // Create mask p.mask.graphics.foreColor=rgb(255,255,255) p.mask.graphics.FillOval 0,0,80,80 o.DrawPicture p,o.width-150,190 // DrawPicture // DrawOval o.lineWidth=10 o.ForeColor=rgb(255,0,255) o.DrawOval 0,0,o.width-10,o.height-10 //FillOval o.ForeColorTransparency=127 o.ForeColor=rgb(0,200,100) o.FillOval 20,o.height-80,80,40 o.foreColor=rgb(0,0,0) o.ForeColortransparency=127 o.FillRoundRect 30,15,o.width-60,50,10,10 o.LineWidth=4 o.DrawRoundRect 30,15,o.width-60,50,10,10 // Draw "EasyOverlay" horizonal centered Dim i As integer dim s As string o.ForeColorTransparency=255 o.TextFont="Chicago" o.TextSize=30 o.ForeColor=rgb(255,255,0) s="EasyOverlay" i=o.stringWidth(s) o.DrawStringWithShadow s, o.width/2 - i / 2, 20,3,127,rgb(0,0,100) End EventHandler
EventHandler Sub WindowBoundsChanged() me.Update End EventHandler
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 ForeColorTransparency=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.ForeColorTransparency=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