2014-04-24 12 views
4

我想在vb.net 2010的菜單項的左側邊距中添加一個數字,但似乎只能將其設置爲圖像。所以,我一直在嘗試使用Graphics.DrawString()來創建一個我想要的號碼的圖像。我嘗試了各種方法,但是我無法將結果圖像看作菜單項本身的文本 - 有沒有辦法做到這一點?這是我目前的代碼(分配一個圖片來衡量文本,然後重新分配正確的大小是關於這個版本3 - 非常醜陋,但我不知道如何度量)。在vb.net中繪製文本以匹配菜單項

mnuItem = New ToolStripMenuItem 
numPeople = CInt(Math.Ceiling(Rnd() * 20)) 

' Calculate the size of the text 
qImg = New Bitmap(1, 1) 
sf.Alignment = StringAlignment.Center 
sf.LineAlignment = StringAlignment.Center 
gr = Graphics.FromImage(qImg) 
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias 
sz = gr.MeasureString(numPeople, mnuItem.Font, New Point(0, 0), sf) 
w = CInt(Math.Ceiling(sz.Width)) 
h = CInt(Math.Ceiling(sz.Height)) 
m = Math.Max(w, h) 

' Now allocate an image of the correct size 
qImg = New Bitmap(m, m) 
gr = Graphics.FromImage(qImg) 
gr.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias 
gr.DrawString(numPeople, mnuItem.Font, Brushes.Black, New RectangleF((m - w)/2, (m - h)/2, w, h), sf) 

mnuItem.Image = qImg 

這裏有一對夫婦的這是什麼給出一些例子 - 注意如何模糊邊緣的文字(圖像)相比,菜單項文本:

Example menu with fuzzy textExample menu with fuzzy text

我已經嘗試了所有的TextRenderingHint選項,其中一些比其他更好,但沒有一個給出菜單文本的清晰外觀。有沒有辦法更接近那種外觀?

+0

您是否嘗試過其他刷或字體來的DrawString()? – TimG

+0

「DrawString」的筆刷選擇似乎僅限於更改顏色。至於字體,我推測如果我想讓它匹配菜單文本,使用菜單的字體將是最好的選擇 - 是不是這種情況? – dsl101

+0

我還沒有嘗試System.Drawing.Brush類的所有功能。看起來像Brushes.Black將與Brush或SolidBrush相同,但你永遠不知道。這聽起來像是你用TextRenderer獲得了勝利者。榮譽。 – TimG

回答

1

你有沒有創建自己的ToolStripMenuItem試過的頂部?

Class CustomToolStripMenuItem 
    Inherits System.Windows.Forms.ToolStripMenuItem 
    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) 
     MyBase.OnPaint(e) 
     e.Graphics.DrawString("11", Me.Font, System.Drawing.Brushes.Black, New System.Drawing.PointF(0, 0)) 
    End Sub 
End Class 

我不會做任何計算來正確顯示它,但我認爲你可以做到這一點。看來像這樣我的系統上(請參閱「另存爲」選項)

enter image description here

+0

奇妙 - 作品一種享受。我也能夠在mnuItem.tag中傳遞值,並且結合來自其他代碼的MeasureString調用,所有內容都完美地排列起來。謝謝! – dsl101

1

這讓我難住了一段時間,我斷定這是一個錯誤,因爲只要添加背景顏色,文本就會按照您的意願顯示。

我有你的解決方案。它使用來自Professional Colors類的漸變畫筆在繪製文本之前在位圖的背景中繪製矩形。

Dim mnuItem = TestToolStripMenuItem 
Dim numPeople = CInt(Math.Ceiling(Rnd() * 20)) 

Dim qImg = New Bitmap(mnuItem.Height, mnuItem.Height) 
Using gr = Graphics.FromImage(qImg) 
    Dim sz = gr.MeasureString(numPeople, mnuItem.Font) 

    Dim w = CInt(Math.Ceiling(sz.Width)) 
    Dim h = CInt(Math.Ceiling(sz.Height)) 

    Dim linGrBrush As New LinearGradientBrush(_ 
     New Point(0, 0), _ 
     New Point(qImg.Width, 0), _ 
     ProfessionalColors.ToolStripGradientBegin, _ 
     ProfessionalColors.ToolStripGradientEnd) 

    gr.FillRectangle(linGrBrush, New Rectangle(0, 0, qImg.Width, qImg.Height)) 
    gr.DrawString(numPeople, mnuItem.Font, Brushes.Black, New Point((qImg.Width/2) - (w/2), (qImg.Height/2) - (h/2))) 
End Using 

mnuItem.Image = qImg 

enter image description here

您將需要Imports System.Drawing.Drawing2D線在類

+0

真的很有趣的方法,我試了一下,但我發現了一個問題(不能把屏幕截圖在這裏的評論)。雖然它看起來比我管理的好得多,但從mnuItem.Height x mnuItem.Height生成的qImg略小於菜單本身 - 不知道在圖像最後分配給mnuItem時是否發生縮小。但是它在文字的邊緣留下了一些漸變不太齊整的邊框。最後,我使用了下面的@ sallushan的方法,因爲它更少代碼和完全乾淨的文本。 – dsl101

+0

是的,我注意到發佈後(它在我的配色方案中並不明顯,因爲它是灰色陰影)這是因爲菜單上的漸變的標準寬度是30像素,創建的位圖的大小是24像素,輕微不匹配。這可以得到補償,但@sallushan有一個更好的解決方案(我已upvoted它) –