2014-10-11 40 views
0

我想旋轉顯示中的文本。我試圖通過拖動矩形來說明字符串的位置,以說明DrawString方法的區域。RotateTransformed DrawString的位置鼠標

我正在使用RotateTransform和TranslateTransform,它工作正常。但是,拖動時,鼠標光標不會轉換爲停留在正在拖動的矩形的底角。

我錯過了什麼......? ;-)

下面是一些示例代碼,吸引到基地Windows窗體,可以直接粘貼到Windows窗體

Public Class Form1 
Dim drawingShape As Boolean = False 
Dim mDown, mPos As Point 
Dim bitMap1 As Bitmap 
Dim txt As String = "Hello folks, here is some text" 
Dim angle As Integer = 0 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    bitMap1 = New Bitmap(Me.Width, Me.Height) 
End Sub 

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint 
    If drawingShape Then 
     e.Graphics.RotateTransform(angle, Drawing2D.MatrixOrder.Append) 
     e.Graphics.TranslateTransform(mDown.X, mDown.Y, Drawing2D.MatrixOrder.Append) 
     Dim rect As New Rectangle(0, 0, mPos.X - mDown.X, mPos.Y - mDown.Y) 
     e.Graphics.DrawRectangle(Pens.Red, rect) 
     e.Graphics.DrawString(txt, New Font("Calibri", 12), Brushes.Green, rect) 
     e.Graphics.ResetTransform() 
    End If 
End Sub 

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown 
    mDown = e.Location 
    drawingShape = True 
End Sub 

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove 
    mPos = e.Location 
    Me.Refresh() 
End Sub 

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp 
    Dim g As Graphics = Graphics.FromImage(bitMap1) 
    g.RotateTransform(angle, Drawing2D.MatrixOrder.Append) 
    g.TranslateTransform(mDown.X, mDown.Y, Drawing2D.MatrixOrder.Append) 
    Dim rect As New Rectangle(0, 0, mPos.X - mDown.X, mPos.Y - mDown.Y) 
    g.DrawString(txt, New Font("Calibri", 12), Brushes.Blue, rect) 
    'g.ResetTransform() 
    g.Dispose() 
    Me.BackgroundImage = bitMap1 
    drawingShape = False 
    angle += 45 
End Sub 

末級

+0

'xxxTransform'變換相關Graphics對象不是整個應用的方面。您需要手動轉換鼠標位置。例如,當angle = 90時,MDown == Rect Left,所以只有在向左移動時纔會更高。以正常的角度(角度= 0)收集矩形可能更容易(少代碼),然後只轉換成品;也許使用臨時bmp,直到用戶說OK。使用TextRender而不是DrawString,文本看起來可能更好。 – Plutonix 2014-10-11 11:38:54

+0

[這個答案](http://stackoverflow.com/q/23623490/1070452)是我剛剛談論的一個例子,只是一個基本的矩形 – Plutonix 2014-10-11 12:08:05

回答

0

你可以使用「Cursor.X and ‘Cursor.Y將鼠標定位在您想要的位置。

0

感謝您的輸入。我最終有它的工作,更新的代碼如下。我使用了System.Windows.Vector的矢量例程和System.Windows.Media.Matrix的矩陣。這些提供矩陣乘法功能。不知道這是否被認爲是好的做法,因爲像Point這樣的類與System.Drawing類發生衝突。

注意:這些載體類需要程序通過創建描述由於鼠標按鈕被點擊的拖動運動矢量來引用WindowsBase

程序工作。因爲這個矢量是在拖動文本顯示框的同時創建的 - 即該框已被旋轉以顯示它 - 矢量將被轉換回它將繪製正常水平矩形時的情況。該水平矩形(「標準化矩形」)是隨後旋轉以實現當前拖動的文本顯示框。

的代碼只需要一個基本的形式來運行

Public Class Form1 
Dim drawingShape As Boolean = False     ' True during mouse-drag 
Dim mDown, mPos As Point       ' Mouse-Button-Down, and current mouse position 
Dim bitMap1 As Bitmap        ' Background display to hold drawn items 
Dim txt As String = "Hello folks, here is some text" 
Dim angle As Integer = 0       ' Angle to rotate text through (0 = along X-axis) 
Dim rect As Rectangle        ' Rectangle holding text - which is then transformed 
Dim vecRectDiag As New Windows.Vector    ' Vector TopLeft-BotRight of rect holding text 
Dim mxIdentWin As System.Windows.Media.Matrix  ' Identity matrix for resetting before rotating 
Dim mxNegAngle As System.Windows.Media.Matrix  ' Matrix to transform mouse cursor vector back to horizontal 
Dim botRight As Point        ' Bottom-right corner of rect rectangle 
Dim mVecWin As New Windows.Vector     ' Vector showing mouse movement since mouse-button-down 
Dim pen1red, pen2yel, pen3turq, pen4lime, pen5corn As Pen 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    bitMap1 = New Bitmap(Me.Width, Me.Height) 
    mxIdentWin = New Windows.Media.Matrix(1, 0, 0, 1, 0, 0)   ' Identity matrix 
End Sub 

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint 
    If drawingShape Then 
     e.Graphics.TranslateTransform(-mDown.X, -mDown.Y, Drawing2D.MatrixOrder.Append) 
     e.Graphics.RotateTransform(angle, Drawing2D.MatrixOrder.Append) 
     e.Graphics.TranslateTransform(mDown.X, mDown.Y, Drawing2D.MatrixOrder.Append) 
     e.Graphics.DrawRectangle(Pens.Red, rect) 
     e.Graphics.DrawString(txt, New Font("Calibri", 12), Brushes.Green, rect) 
     e.Graphics.ResetTransform() 
    End If 
End Sub 

Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown 
    mDown = e.Location     ' Location of mouse button click 
    drawingShape = True 
    mxNegAngle = mxIdentWin    ' Reset the negative-rotate matrix... 
    mxNegAngle.Rotate(-angle)   ' ... and re-rotate with the new angle 
End Sub 

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove 
    mPos = e.Location 
    mVecWin = New Windows.Vector(mPos.X - mDown.X, mPos.Y - mDown.Y)  ' Vector showing the real mouse movement 
    vecRectDiag = Windows.Vector.Multiply(mVecWin, mxNegAngle)    ' Vector to the normalised horizontal rectangle - which now needs transforming 
    botRight = New Point(CInt(mDown.X + vecRectDiag.X), CInt(mDown.Y + vecRectDiag.Y))  ' Bottom right of normalised horizontal rectangle 
    rect = New Rectangle(mDown.X, mDown.Y, botRight.X - mDown.X, botRight.Y - mDown.Y) 
    If drawingShape Then Me.Refresh() 
End Sub 

Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp 
    Dim g As Graphics = Graphics.FromImage(bitMap1) 
    g.TranslateTransform(-mDown.X, -mDown.Y, Drawing2D.MatrixOrder.Append) 
    g.RotateTransform(angle, Drawing2D.MatrixOrder.Append) 
    g.TranslateTransform(mDown.X, mDown.Y, Drawing2D.MatrixOrder.Append) 
    g.DrawString(txt, New Font("Calibri", 12), Brushes.Blue, rect) 
    g.ResetTransform() 
    g.Dispose() 
    Me.BackgroundImage = bitMap1 
    drawingShape = False 
    angle += 45 
End Sub 

End Class