sábado, 30 de julio de 2011

9.1 Empezamos escribiendo el código del control

Crearemos nuestra clase y añadiremos una imagen relacionada al control (un eje cartesiano)


En la clase ejescartesiano copiamos el siguiente código:

' Gambas class file
'ZONA 1 Donde definimos la herencia y la orden EXPORT
INHERITS DrawingArea
EXPORT

'ZONA 2 Añadimos las propiedades que aparecerán en el cuadro de propiedades
PUBLIC CONST _Properties AS String = "*,C,B,Ap1,Ap2,colorejes"

'ZONA 3 Definimos las variables tanto Públicas como Privadas de la clase
PROPERTY C AS Float ' distancia del nuevo eje U'x medido segun eje unidades Ux (el del propio control DrawingArea
PRIVATE hC AS Float

PROPERTY B AS Float ' distancia del nuevo eje U'y medido segun unidades Uy
PRIVATE hB AS Float

PROPERTY ap1 AS Float ' Inicio del intervalo de representación eje U'x
PRIVATE hap1 AS Float

PROPERTY ap2 AS Float 'Fin del intervalo de representación eje U'x
PRIVATE hap2 AS Float

PROPERTY colorejes AS Float 'es el color en van a dibujarse los ejes
PRIVATE hcolorejes AS Float


PRIVATE FUNCTION C_Read() AS Float
RETURN hC
END

PRIVATE SUB C_Write(Value AS Float)
hC = value
END

PRIVATE FUNCTION B_Read() AS Float
RETURN hB
END

PRIVATE SUB B_Write(Value AS Float)
hb = value
END

PRIVATE FUNCTION ap1_Read() AS Float
RETURN hap1
END

PRIVATE SUB ap1_Write(Value AS Float)
hap1 = value
END

PRIVATE FUNCTION ap2_Read() AS Float
RETURN hap2
END

PRIVATE SUB ap2_Write(Value AS Float)
hap2 = value
END

PRIVATE FUNCTION colorejes_Read() AS Float
RETURN hcolorejes
END

PRIVATE SUB colorejes_Write(Value AS Float)
hcolorejes = value
END

'ZONA 4 Definirmos las subrutinas y funciones que podran ser p úblicas o privadas.
PUBLIC FUNCTION pasarUpxaUx(upx AS Float) AS Float
DIM conversion AS Float
DIM xdist AS Float
DIM escalado AS Float
xdist = ME.Width
escalado = xdist / (ME.ap2 - ME.ap1)
TRY conversion = ME.c + (upx) * escalado * 0.95
IF ERROR THEN
Message(("Se ha producido un error al convertir la coordenadas, seguramente no estan definidos los intervalos"))
RETURN 0
ELSE
RETURN conversion
ENDIF
END

PUBLIC FUNCTION pasarUpyaUy(upy AS Float) AS Float
DIM conversion AS Float
DIM xdist AS Float
DIM escalado AS Float
xdist = ME.Width
escalado = xdist / (ME.ap2 - ME.ap1)
TRY conversion = ME.B - (Upy) * escalado * 0.95
IF ERROR THEN
Message(("Se ha producido un error al convertir la coordenadas, ancho de la zona de dibujo erroneo"))
RETURN 0
ELSE

RETURN conversion
ENDIF
END

'-------------------------------------------------------------------------
' psar de ux a upx
'-------------------------------------------------------------------------

PUBLIC FUNCTION pasarUxaUpx(ux AS Float) AS Float
DIM conversion AS Float
DIM xdist AS Float
DIM escalado AS Float
xdist = ME.Width
escalado = xdist / (ME.ap2 - ME.ap1)
TRY conversion = (ux - ME.c) / escalado * 0.95

IF ERROR THEN
Message(("Se ha producido un error al convertir la coordenadas, ancho de la zona de dibujo erroneo"))
RETURN 0
ELSE

RETURN conversion
ENDIF
END

PUBLIC FUNCTION pasarUyaUpy(uy AS Float) AS Float
DIM conversion AS Float
DIM xdist AS Float
DIM escalado AS Float
xdist = ME.Width
escalado = xdist / (ME.ap2 - ME.ap1)
TRY conversion = (- uy + ME.b) / (escalado * 0.95)
IF ERROR THEN
Message(("Se ha producido un error al convertir la coordenadas, ancho de la zona de dibujo erroneo"))

RETURN 0
ELSE

RETURN conversion
ENDIF
END

PUBLIC SUB dibujarejeup()

DIM font0inicial AS String
DIM font1final AS String
' Para escribir de menor tamaño el ap1 y ap2
font0inicial = "<font size=" & Chr$(34) & "2" & Chr$(34) & "> "
font1final = "</font>"
Draw.Begin(ME)

Draw.ForeColor = ME.colorejes
Draw.Line(0, ME.pasarUpyaUy(0), ME.Width, ME.pasarUpyaUy(0))
Draw.Line(ME.pasarupxaux(0), 0, ME.pasarUpxaUx(0), ME.Height)
'escribo ap1 y ap2 eje y
Draw.ForeColor = Color.blue
Draw.RichText(font0inicial & Format$(ME.ap2, "0.00") & font1final, ME.pasarupxaux(0) + 5, ME.pasarupyauy(ME.ap2))
Draw.Line(ME.pasarupxaux(0) + 10, ME.pasarUpyaUy(ME.ap2), ME.pasarupxaux(0) - 10, ME.pasarUpyaUy(ME.ap2))
Draw.RichText(font0inicial & Format$(ME.ap1, "0.00") & font1final, ME.pasarupxaux(0) + 5, ME.pasarupyauy(ME.ap1) - 25)
Draw.Line(ME.pasarupxaux(0) + 10, ME.pasarUpyaUy(ME.ap1), ME.pasarupxaux(0) - 10, ME.pasarUpyaUy(ME.ap1))

'escribo ap1 y ap2 eje x
Draw.RichText(font0inicial & Format$(ME.ap1, "0.00") & font1final, ME.pasarupxaux(ME.ap1), ME.pasarupyauy(0) + 5)
Draw.Line(ME.pasarupxaux(ME.ap1), ME.pasarUpyaUy(0) + 10, ME.pasarupxaux(ME.ap1), ME.pasarUpyaUy(0) - 10)


Draw.RichText(font0inicial & Format$(ME.ap2, "0.00") & font1final, ME.pasarupxaux(ME.ap2) - 25, ME.pasarupyauy(0) + 5)
Draw.Line(ME.pasarupxaux(ME.ap2), ME.pasarUpyaUy(0) + 10, ME.pasarupxaux(ME.ap2), ME.pasarUpyaUy(0) - 10)
Draw.End
END


No hay comentarios:

Publicar un comentario en la entrada