Adding Watermark to ListBox

dipique
08-13-2004, 12:33 PM
I want to add a watermark to a listbox- big gray sort of transparent characters diagonal across the background. Any suggestions?

Dan

dipique
08-16-2004, 09:37 AM
*bumping*

Any ideas?

Dan

Iceplug
08-16-2004, 10:45 AM
Can you set a background image for the listbox?

dipique
08-16-2004, 10:57 AM
Can you set a background image for the listbox?

It doesn't have a property like that, as far as I know. I looked and didn't see one.

Dan

dipique
08-23-2004, 07:43 AM
*Final Bump*

lebb
08-23-2004, 08:47 AM
What do you mean? Listboxes have a BackgroundImage property. Have you tried it?

dipique
08-23-2004, 01:27 PM
What do you mean? Listboxes have a BackgroundImage property. Have you tried it?

What do you mean? Enclosed is a screenshot with my property list, in which there is no backgroundimage property.

Dan

lebb
08-23-2004, 01:33 PM
Not in the property list; at runtime. MSDN should have details.

dipique
08-23-2004, 02:33 PM
Not in the property list; at runtime. MSDN should have details.



'This is my code. What am I doing wrong?

Dim cdMain As OpenFileDialog = New OpenFileDialog
cdMain.Filter = "BMP|*.bmp|JPG|*.jpg"
cdMain.ShowDialog()
Dim sFN As String = cdMain.FileName
ListBox1.BackgroundImage = Image.FromFile(sFN)



Dan

lebb
08-23-2004, 02:49 PM
Yeah, I've just been looking at the same thing, and seems to be not as simple as I'd expected. That will work with a picturebox, but not with a listbox. I'm guessing that the text area is getting painted over the top of the background image. The best thing I can think of is to subclass it and handle the painting yourself. Someone with more experience with .NET controls may have a better suggestion, though.

dipique
08-24-2004, 07:19 AM
Yeah, I've just been looking at the same thing, and seems to be not as simple as I'd expected. That will work with a picturebox, but not with a listbox. I'm guessing that the text area is getting painted over the top of the background image. The best thing I can think of is to subclass it and handle the painting yourself. Someone with more experience with .NET controls may have a better suggestion, though.

For a guru, you're not very comforting;) Do you know a good webpage with info about .Net subclassing? The .Net MSDN library is... a bit young right now.

Dan

Iceplug
08-24-2004, 08:05 AM
You may want to look at OwnerDraw for a listbox.
Set the listbox DrawMode to OwnerDrawVariable (not sure what the difference is right now between OwnerDrawFixed and OwnerDrawVariable, though).

Add an image to your form's declarations.


Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight = 14
e.ItemWidth = ListBox1.Width - 4
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
If e.Index = 0 AndAlso (e.State And DrawItemState.Selected) = DrawItemState.None Then
e.Graphics.DrawImage(pict, New Rectangle(0, 0, 50, 50))
End If

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Blue, 0, e.Index * e.Bounds.Height)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Red, 0, e.Index * e.Bounds.Height)
End If
End Sub

That should give you some starting point. :)

dipique
08-24-2004, 10:05 AM
Here's my code and my image. What am I doing wrong? :mad:



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 ListBox1 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
Me.ListBox1.Location = New System.Drawing.Point(8, 8)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(560, 251)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(576, 421)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private pict As Image

Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight = 14
e.ItemWidth = ListBox1.Width - 4
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
If e.Index = 0 AndAlso (e.State And DrawItemState.Selected) = DrawItemState.None Then
e.Graphics.DrawImage(pict, New Rectangle(0, 0, 50, 50))
End If

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Blue, 0, e.Index * e.Bounds.Height)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Red, 0, e.Index * e.Bounds.Height)
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cdMain As OpenFileDialog = New OpenFileDialog
cdMain.Filter = "Bitmaps|*.bmp|JPeg Files|*.jpg|All Files|*.*"
cdMain.ShowDialog()
If cdMain.FileName <> vbNullString Then
pict = Image.FromFile(cdMain.FileName)
End If
End Sub
End Class



Dan

Iceplug
08-29-2004, 07:12 PM
I haven't worked out all the details of ownerdrawing your own listbox, but I'm thinking that you have to draw part of the background image onto the listbox.

So, I came up with this to draw a strip of the picture to the listbox.
Looks good so far... but doesn't work with some of the pictures on my computer (I can't figure that part out)

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem


e.Graphics.DrawImage(pict, e.Bounds.Left, e.Bounds.Top, New Rectangle(0, e.Index * e.Bounds.Height, pict.Width, e.Bounds.Height), GraphicsUnit.Pixel)


If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Blue, 0, e.Index * e.Bounds.Height)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), ListBox1.Font, Brushes.Red, 0, e.Index * e.Bounds.Height)
End If
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum