Differenze tra le versioni di "Spostare con il mouse il testo in una DrawingArea"
Da Gambas-it.org - Wikipedia.
Riga 18: | Riga 18: | ||
<FONT color=gray>' ''Gestisce l'evento dello spostamento del mouse sulla DrawingArea''</font> | <FONT color=gray>' ''Gestisce l'evento dello spostamento del mouse sulla DrawingArea''</font> | ||
'''Public''' Sub DrawingArea1_MouseMove() | '''Public''' Sub DrawingArea1_MouseMove() | ||
− | + | ||
+ | DrawingArea1.Mouse = 18 | ||
x = Mouse.X | x = Mouse.X | ||
y = Mouse.Y | y = Mouse.Y | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseUp() | ||
+ | |||
+ | DrawingArea1.Mouse = Mouse.Default | ||
'''End''' | '''End''' | ||
Riga 39: | Riga 47: | ||
− | Il '''secondo''' codice, più complesso, consente di spostare il testo senza sbalzi e spostamenti indesiderati rispetto alla posizione del testo e del mouse: |[[#Note|1]]| | + | Il '''secondo''' codice consente di aggiungere più stringhe testuali alla ''DrawingArea'': |
+ | Public Struct STRUTTURA | ||
+ | xx As Integer | ||
+ | yy As Integer | ||
+ | tx As String | ||
+ | End Struct | ||
+ | Private sstt As New STRUTTURA[] | ||
+ | Private st As STRUTTURA | ||
+ | Private i As Integer | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseDown() | ||
+ | |||
+ | st = New STRUTTURA | ||
+ | Inc i | ||
+ | st.xx = Mouse.X | ||
+ | st.yy = Mouse.Y | ||
+ | st.tx = "abc " & CStr(i) | ||
+ | sstt.Push(st) | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseMove() | ||
+ | |||
+ | DrawingArea1.Mouse = 18 | ||
+ | st.xx = Mouse.X | ||
+ | st.yy = Mouse.Y | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_MouseUp() | ||
+ | |||
+ | DrawingArea1.Mouse = Mouse.Default | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | '''Public''' Sub DrawingArea1_Draw() | ||
+ | |||
+ | DrawingArea1.Refresh | ||
+ | |||
+ | For Each st In sstt | ||
+ | With Paint | ||
+ | .Font.Size = 22 | ||
+ | .DrawText(st.tx, st.xx, st.yy) | ||
+ | End With | ||
+ | Next | ||
+ | Paint.End | ||
+ | |||
+ | '''End''' | ||
+ | |||
+ | |||
+ | |||
+ | Il '''terzo''' codice, più complesso, consente di spostare il testo senza sbalzi e spostamenti indesiderati rispetto alla posizione del testo e del mouse: |[[#Note|1]]| | ||
Private bOk As Boolean <FONT color=gray>' ''Booleano attivo se puntatore è in area scritta''</font> | Private bOk As Boolean <FONT color=gray>' ''Booleano attivo se puntatore è in area scritta''</font> | ||
Private bDis As Boolean <FONT color=gray>' ''Booleano attivo se dati provengono da Form_Open''</font> | Private bDis As Boolean <FONT color=gray>' ''Booleano attivo se dati provengono da Form_Open''</font> |
Versione delle 08:04, 28 lug 2018
Mostreremo due codici per spostare con il mouse un testo all'interno di una DrawingArea.
Il primo codice più semplice e basilare per effettuare uno spostamento del testo nella DrawingArea:
Private x As Integer Private y As Integer ' Gestisce l'evento della pressione del tasto del mouse sulla DrawingArea: Public Sub DrawingArea1_MouseDown() x = Mouse.X ' prende la coordinata x del punto ove si trova il mouse in quel momento y = Mouse.Y ' prende la coordinata y del punto ove si trova il mouse in quel momento End ' Gestisce l'evento dello spostamento del mouse sulla DrawingArea Public Sub DrawingArea1_MouseMove() DrawingArea1.Mouse = 18 x = Mouse.X y = Mouse.Y End Public Sub DrawingArea1_MouseUp() DrawingArea1.Mouse = Mouse.Default End Public Sub DrawingArea1_Draw() DrawingArea1.Refresh With Paint .Font.Size = 22 .DrawText("abc", x, y) ' è il testo che andremo a spostare nella DrawingArea .End End With End
Il secondo codice consente di aggiungere più stringhe testuali alla DrawingArea:
Public Struct STRUTTURA xx As Integer yy As Integer tx As String End Struct Private sstt As New STRUTTURA[] Private st As STRUTTURA Private i As Integer Public Sub DrawingArea1_MouseDown() st = New STRUTTURA Inc i st.xx = Mouse.X st.yy = Mouse.Y st.tx = "abc " & CStr(i) sstt.Push(st) End Public Sub DrawingArea1_MouseMove() DrawingArea1.Mouse = 18 st.xx = Mouse.X st.yy = Mouse.Y End Public Sub DrawingArea1_MouseUp() DrawingArea1.Mouse = Mouse.Default End Public Sub DrawingArea1_Draw() DrawingArea1.Refresh For Each st In sstt With Paint .Font.Size = 22 .DrawText(st.tx, st.xx, st.yy) End With Next Paint.End End
Il terzo codice, più complesso, consente di spostare il testo senza sbalzi e spostamenti indesiderati rispetto alla posizione del testo e del mouse: |1|
Private bOk As Boolean ' Booleano attivo se puntatore è in area scritta Private bDis As Boolean ' Booleano attivo se dati provengono da Form_Open Private xIn As Integer ' Memorizza la posizione del testo al momento che agiamo sul tasto sinistro del mouse Private yIn As Integer ' Idem Private xDif As Integer ' Memorizza la distanza tra la posizione del puntatore e quella del testo Private yDif As Integer ' Idem Private x As Integer ' Memorizza la posizione del puntatore Private y As Integer ' Idem Private iAlt As Integer ' Memorizza l'altezza del testo Private iLarg As Integer ' Memorizza la larghezza del testo Private sTesto As String ' Memorizza la scritta del testo Public Sub Form_Open() ' Inserisce il testo al centro dell'area di disegno dando le giuste indicazioni a DrawingArea1_Draw: sTesto = "Testo di prova" bDis = True bOk = True DrawingArea1.Refresh End Public Sub DrawingArea1_MouseDown() ' Quando si preme il tasto sinistro passa la posizione del mouse alle variabili x e y: x = Mouse.X y = Mouse.Y ' Ottiene la differenza fra le posizioni x e y del testo rispetto a quelle del mouse: xDif = xIn - x yDif = yIn - y ' Si accerta che il booleano OK sia su false: bOk = False ' Se il mouse si trova nell'area della scritta cambia aspetto e porta il booleano OK a vero: If x >= xIn And x <= (xIn + iLarg) Then If y <= yIn And y >= (yIn - iAlt) Then DrawingArea1.Mouse = Mouse.SizeAll bOk = True Endif Endif End Public Sub DrawingArea1_MouseMove() ' Se il booleano OK è vero passa alle variabili la posizione e attiva il disegno: If bOk Then x = Mouse.X y = Mouse.Y DrawingArea1.Refresh Endif End Public Sub DrawingArea1_Draw() ' Se il booleano "bOK" è vero, passa a controllare il booleano eventualmente attivato da "Form_Open". ' Se quest 'ultimo è vero crea il testo scritto al centro dell'area di disegno, ' altrimenti disegna il testo sulle indicazioni del mouse: If bOk Then With Paint .Font.Size = ridimensiona(22) If bDis Then iLarg = .Font.RichTextWidth(sTesto) x = (DrawingArea1.W / 2) - (iLarg / 2) xIn = x iAlt = .Font.RichTextHeight(sTesto) y = (DrawingArea1.H / 2) + (iAlt / 2) yIn = y bDis = False Endif .DrawText(sTesto, x + xDif, y + yDif) .End End With Endif End Public Sub DrawingArea1_MouseUp() ' Quando si rilascia il bottone del mouse cambia l'aspetto del mouse a quello di default ' e memorizza nelle variabili la posizione iniziale della scritta per essere pronti al nuovo trascinamento: DrawingArea1.Mouse = Mouse.Default xIn = x + xDif yIn = y + yDif End Public Sub ridimensiona(size As Float) As Float ' Proporziona il testo nell'area di disegno: If Paint.Device = DrawingArea1 Then Return (size * 0.744) Else Return size Endif End
Note
[1] Il codice è stato realizzato dall'utente Gianluigi del forum www.gambas-it.org.