|
VB .NET Helper Newsletter
|
Rod Stephens
|
Feb 13, 2009 11:55 PST
|
A short one this time. Too much work...
Rod
Have a great week and thanks for subscribing!
Rod
RodSte-@vb-helper.com
Books To Keep: http://www.BooksToKeep.com
----------
*** Now Available ***
Beginning Database Design Solutions
http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/
Visual Basic 2008 Programmer's Reference
http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
==========
VB.NET Contents:
1. New HowTo: Make bitmap files containing numbers in circles in Visual
Basic 2005
2. New HowTo: Make a ListView control display icons in its subitems in
Visual Basic 2005
3. New HowTo: Programmatically expand a ComboBox in Visual Basic 2005
==========
++++++++++
<VB.NET>
++++++++++
==========
1. New HowTo: Make bitmap files containing numbers in circles in Visual
Basic 2005
http://www.vb-helper.com/howto_2005_make_number_icons.html
http://www.vb-helper.com/HowTo/howto_2005_make_number_icons.zip
Sometimes I need small images of numbers in circles to annotate an image
for a book or article so I wrote this program to make them. It gives
each a blue gradient background and saves them in separate files. You
can use a similar technique to make files containing other graphics if
you need them.
When you click the program's button, the following code loops through
the numbers 0 through 9. For each it creates a new Bitmap and an
associated Graphics object.
The program sets the Graphics object's TextRenderingHint property to the
text is drawn smoothly. It also sets the SmoothingMode property so the
circle is drawn smoothly.
Next the code fills an ellipse in the Bitmap with the color gradient
(change the brush to use a different background) and draws the number
centered in it. Finally the code saves the Bitmap into a file.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGo.Click
Const WID As Integer = 24
Dim base_path As String = txtDirectory.Text
If Not base_path.EndsWith("\") Then base_path &= "\"
Dim the_font As New Font("Arial", 10, FontStyle.Bold)
For i As Integer = 0 To 9
Dim bm As New Bitmap(WID, WID)
Dim gr As Graphics = Graphics.FromImage(bm)
gr.TextRenderingHint =
Drawing.Text.TextRenderingHint.AntiAliasGridFit
'gr.InterpolationMode = Drawing2D.InterpolationMode.Bicubic
gr.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
gr.Clear(Color.White)
Dim layout_rectangle As New Rectangle(0, 0, WID - 1, WID - 1)
Using br As New LinearGradientBrush(layout_rectangle, _
Color.White, Color.Blue, LinearGradientMode.ForwardDiagonal)
gr.FillEllipse(br, layout_rectangle)
gr.DrawEllipse(Pens.Blue, layout_rectangle)
End Using
Dim string_format As New StringFormat()
string_format.LineAlignment = StringAlignment.Center
string_format.Alignment = StringAlignment.Center
gr.DrawString(i.ToString, the_font, Brushes.White, _
layout_rectangle, string_format)
bm.Save(base_path & i.ToString & ".bmp", _
System.Drawing.Imaging.ImageFormat.Bmp)
Next i
MessageBox.Show("Done")
End Sub
==========
2. New HowTo: Make a ListView control display icons in its subitems in
Visual Basic 2005
http://www.vb-helper.com/howto_2005_listview_with_icons.html
http://www.vb-helper.com/HowTo/howto_2005_listview_with_icons.zip
The program makes a ListViewWithIcons class that is a subclass of
ListView. After the object's handle is created, the code sets the
control's extended styles to allow images.
The AddIconToSubitem subroutine builds a LV_ITEM structure to describe
the changes that it must make to display an icon in a subitem. It fills
in the structure's parameters to indicate where the icon should be
positioned and uses SendMessage to send the LVM_SETITEM message to the
control.
Note that the icon comes from the ImageList stored in the control's
SmallImageList property.
' See http://www.codeproject.com/KB/list/ListViewWithIcons.aspx.
Public Class ListViewWithIcons
Inherits System.Windows.Forms.ListView
Public Declare Function SendMessage Lib "user32.dll" Alias
"SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam
As IntPtr, ByVal lParam As IntPtr) As Integer
Public Declare Function SendMessage Lib "user32.dll" Alias
"SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam
As Int32, ByRef lParam As LV_ITEM) As Boolean
Public Structure LV_ITEM
Public mask As UInt32
Public iItem As Int32
Public iSubItem As Int32
Public state As UInt32
Public stateMask As UInt32
Public pszText As String
Public cchTextMax As Int32
Public iImage As Int32
Public lParam As IntPtr
End Structure
Public Const LVM_FIRST As Int32 = &H1000
Public Const LVM_GETITEM As Int32 = LVM_FIRST + 5
Public Const LVM_SETITEM As Int32 = LVM_FIRST + 6
Public Const LVIF_TEXT As Int32 = &H1
Public Const LVIF_IMAGE As Int32 = &H2
Public Const LVW_FIRST As Integer = &H1000
Public Const LVM_GETEXTENDEDLISTVIEWSTYLE As Integer = LVW_FIRST +
54
Public Const LVS_EX_GRIDLINES As Integer = &H1
Public Const LVS_EX_SUBITEMIMAGES As Integer = &H2
Public Const LVS_EX_CHECKBOXES As Integer = &H4
Public Const LVS_EX_TRACKSELECT As Integer = &H8
Public Const LVS_EX_HEADERDRAGDROP As Integer = &H10
Public Const LVS_EX_FULLROWSELECT As Integer = &H20 ' applies to
report mode only
Public Const LVS_EX_ONECLICKACTIVATE As Integer = &H40
' Change the style to accept images on subitems.
Public Sub New()
' In .NET Framework 2.0 and later this
' must be done after the handle is created.
AddHandler Me.HandleCreated, AddressOf
ListViewWithIcons_HandleCreated
End Sub
' Needed for .NET Framework 2.0 and later.
Private Sub ListViewWithIcons_HandleCreated(ByVal sender As Object,
ByVal e As EventArgs)
' Change the style of listview to accept image on subitems
Dim m As System.Windows.Forms.Message = New Message
m.HWnd = Me.Handle
m.Msg = LVM_GETEXTENDEDLISTVIEWSTYLE
m.LParam = New IntPtr(LVS_EX_GRIDLINES Or LVS_EX_FULLROWSELECT
Or LVS_EX_SUBITEMIMAGES Or LVS_EX_CHECKBOXES Or LVS_EX_TRACKSELECT)
m.WParam = IntPtr.Zero
Me.WndProc(m)
End Sub
' Add an icon to a subitem.
Public Sub AddIconToSubitem(ByVal row As Integer, ByVal col As
Integer, ByVal icon_num As Integer)
Dim lvi As New ListViewWithIcons.LV_ITEM()
lvi.iItem = row ' Row.
lvi.iSubItem = col ' Column.
' lvi.pszText = "Test" ' Text.
' Indicate what we're setting.
' lvi.mask = ListViewWithIcons.LVIF_IMAGE Or
ListViewWithIcons.LVIF_TEXT
lvi.mask = ListViewWithIcons.LVIF_IMAGE
lvi.iImage = icon_num ' Image index in the ImageList.
' Send the LVM_SETITEM message.
ListViewWithIcons.SendMessage(Me.Handle, _
ListViewWithIcons.LVM_SETITEM, 0, lvi)
End Sub
End Class
The program's code creates a ListViewWithIcons control when it starts.
It uses the ListViewMakeColumnHeaders and ListViewMakeRow helper
routines to build the ListView's rows and columns. It then calls the
control's AddIconToSubitem method to give each subitem an icon.
Public Class Form1
Private lvwBooks As ListViewWithIcons
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Make the ListViewWithIcons.
lvwBooks = New ListViewWithIcons()
Me.Controls.Add(lvwBooks)
lvwBooks.SmallImageList = imlIcons
lvwBooks.Dock = DockStyle.Fill
lvwBooks.View = View.Details
' Make the column headers.
ListViewMakeColumnHeaders(lvwBooks, _
"Title", 230, HorizontalAlignment.Left, _
"URL", 220, HorizontalAlignment.Left, _
"ISBN", 130, HorizontalAlignment.Left, _
"Picture", 230, HorizontalAlignment.Left, _
"Pages", 50, HorizontalAlignment.Right, _
"Year", 60, HorizontalAlignment.Right)
' Add data rows.
ListViewMakeRow(lvwBooks, "Ready-to-Run Visual Basic
Algorithms", "http://www.vb-helper.com/vba.htm", "0-471-24268-3",
"http://www.vb-helper.com/vba.jpg", "395", "1998")
ListViewMakeRow(lvwBooks, "Visual Basic Graphics Programming",
"http://www.vb-helper.com/vbgp.htm", "0-472-35599-2",
"http://www.vb-helper.com/vbgp.jpg", "712", "2000")
ListViewMakeRow(lvwBooks, "Advanced Visual Basic Techniques",
"http://www.vb-helper.com/avbt.htm", "0-471-18881-6",
"http://www.vb-helper.com/avbt.jpg", "440", "1997")
ListViewMakeRow(lvwBooks, "Custom Controls Library",
"http://www.vb-helper.com/ccl.htm", "0-471-24267-5",
"http://www.vb-helper.com/ccl.jpg", "684", "1998")
ListViewMakeRow(lvwBooks, "Ready-to-Run Delphi Algorithms",
"http://www.vb-helper.com/da.htm", "0-471-25400-2",
"http://www.vb-helper.com/da.jpg", "398", "1998")
ListViewMakeRow(lvwBooks, "Bug Proofing Visual Basic",
"http://www.vb-helper.com/err.htm", "0-471-32351-9",
"http://www.vb-helper.com/err.jpg", "397", "1999")
ListViewMakeRow(lvwBooks, "Ready-to-Run Visual Basic Code
Library", "http://www.vb-helper.com/vbcl.htm", "0-471-33345-X",
"http://www.vb-helper.com/vbcl.jpg", "424", "1999")
For r As Integer = 0 To lvwBooks.Items.Count - 1
For c As Integer = 0 To lvwBooks.Columns.Count - 1
lvwBooks.AddIconToSubitem(r, c, c)
Next c
Next r
End Sub
' Make the ListView's column headers.
' The ParamArray entries should alternate between
' strings and HorizontalAlignment values.
Private Sub ListViewMakeColumnHeaders(ByVal lvw As _
ListView, ByVal ParamArray header_info() As Object)
' Remove any existing headers.
lvw.Columns.Clear()
' Make the column headers.
For i As Integer = header_info.GetLowerBound(0) To _
header_info.GetUpperBound(0) Step 3
lvw.Columns.Add( _
DirectCast(header_info(i), String), _
DirectCast(header_info(i + 1), Integer), _
DirectCast(header_info(i + 2), _
HorizontalAlignment))
Next i
End Sub
' Make a ListView row.
Private Sub ListViewMakeRow(ByVal lvw As ListView, ByVal item_title
As String, ByVal ParamArray subitem_titles() As String)
' Make the item.
Dim new_item As ListViewItem = lvw.Items.Add(item_title, 1)
' Make the sub-items.
For i As Integer = subitem_titles.GetLowerBound(0) To
subitem_titles.GetUpperBound(0)
new_item.SubItems.Add(subitem_titles(i))
Next i
End Sub
End Class
==========
3. New HowTo: Programmatically expand a ComboBox in Visual Basic 2005
http://www.vb-helper.com/howto_2005_expand_combobox.html
http://www.vb-helper.com/HowTo/howto_2005_expand_combobox.zip
To expand the ComboBox, the code simply sets its DroppedDown property to
True.
cboChoices.DroppedDown = True
==========
Archives:
http://www.topica.com/lists/VBHelper
http://www.topica.com/lists/VB6Helper
http://www.topica.com/lists/VBNetHelper
Post questions at:
http://www.topica.com/lists/VBHelperQA
|
|
 |
|