Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
App. WinForms / WPF .NET
Menu con immagini in vb.net
martedì 23 agosto 2005 - 18.56
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
ghost77
Profilo
| Newbie
8
messaggi | Data Invio:
mar 23 ago 2005 - 18:56
Ciao a tutti,
chi mi sa dire come sia possibile inserire delle immagini nei menu con VB.net (Win Forms)? Con VB6 si usava il subclassing e in .net? Inoltre, chi mi sa dire dove posso trovare componenti free per vb.net (incluso codice sorgente)?
Grazie a tutti per la cortese attenzione.
Cteniza
Profilo
| Guru
1.509
messaggi | Data Invio:
mar 23 ago 2005 - 19:03
Ti do un piccolo consiglio, non fare mai paragoni con vb6 che a volte diventa fuorviante.
E' ovvio che alcune cose si facevano meglio e per altre bisognava soffrire.
Dire con vb6 si faceva così non porta nulla alla discussione.
Veniamo a noi
Questo esempio ti mostra come fare quello che chiedi.
Tutto il trucco diventa agire sul drawitem e sul measureitem, naturalmente le immagini che richiama da disco devi impostarle PRIMA di eseguire il programma perchè altrimenti va subito in errore.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents Label1 As System.Windows.Forms.Label
Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.MenuItem2 = New System.Windows.Forms.MenuItem
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
Me.MenuItem1.Text = "File"
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.OwnerDraw = True
Me.MenuItem2.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(224, 72)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Example will show you how to draw image in menu item. You can draw any image in me" & _
"nuitem but you have to set owner draw property of particular menuitem to true. " & _
""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 121)
Me.Controls.Add(Me.Label1)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Draw Image in Menu Item"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
MsgBox("Hey!!! Its a image")
End Sub
Private Sub MenuItem2_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem2.DrawItem
'To draw in menuitems background
e.DrawBackground()
'getting bounds of drawing area or say menuitem area
Dim r As Rectangle = e.Bounds
'loading image from current executable directory by
'Image.FromFile(Application.StartupPath & "\test.jpg")
'and drawing the image in particular area by
'e.Graphics.DrawImage(Image Object, Rectangle Object)
e.Graphics.DrawImage(Image.FromFile(Application.StartupPath & "\test.jpg"), r)
End Sub
Private Sub MenuItem2_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem2.MeasureItem
'Image type of variable to load image in order to get image bounds.
Dim img As Image
'Loading Image from file to object
img = Image.FromFile(Application.StartupPath & "\test.jpg")
'gettting aspects or bound of image.
e.ItemHeight = img.Height
e.ItemWi
ghost77
Profilo
| Newbie
8
messaggi | Data Invio:
mer 24 ago 2005 - 12:53
Grazie per l'esempio, però è stato troncato dal forum...me lo potresti fornire in allegato?
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !