Posted December 01, 2002 12:00AM by Robert Smith
Tagged: Coding, VB, Imaging
Originally published December 2002 on DoItIn.net using VB7.0/2002. Updated for VB7.1 February 2005
Links for compiled demo versions, all required resources and source code are included at the end of this article. Plus, get the complete eBook in Adobe Acrobat 7 format ... all here.
29) Offer more options with menusOur toolbars offer the most common functions, leaving the menus to round out the functionality. I've added a mnuSource and mnuResult, both menus have the same menuItem options: 
The hookups send the clicks to the property methods based on the sender: Private Sub mniEditorRotate90_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceRotate90.Click, mniResultRotate90.Click
If sender Is mniSourceRotate90 Then
svImageSource.RotateDisplay(RotateFlipType.Rotate90FlipNone)
Else
svImageResult.RotateDisplay(RotateFlipType.Rotate90FlipNone)
End If
End Sub
Private Sub mniEditorRotate270_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceRotate270.Click, mniResultRotate270.Click
If sender Is mniSourceRotate270 Then
svImageSource.RotateDisplay(RotateFlipType.Rotate270FlipNone)
Else
svImageResult.RotateDisplay(RotateFlipType.Rotate270FlipNone)
End If
End Sub
Private Sub mniEditorFlip_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceFlip.Click, mniResultFlip.Click
If sender Is mniSourceFlip Then
svImageSource.RotateDisplay(RotateFlipType.RotateNoneFlipY)
Else
svImageResult.RotateDisplay(RotateFlipType.RotateNoneFlipY)
End If
End Sub
Private Sub mniEditorInformation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceInformation.Click, mniResultInformation.Click
Dim oID As svDisplayImageDetail
If sender Is mniSourceInformation Then
oID = svImageSource.CurrentImageDetail
Else
oID = svImageResult.CurrentImageDetail
End If
MsgBox("Page: " & oID.PageNumber.ToString & " of " & _
oID.PagesInFile.ToString & vbCrLf & _
"width: " & oID.Width.ToString & vbCrLf & _
"height: " & oID.Height.ToString & vbCrLf & _
"current zoom: " & oID.CurrentZoom.ToString & vbCrLf & _
"max zoom: " & oID.MaxZoom.ToString, MsgBoxStyle.OKOnly And _
MsgBoxStyle.Information, Me.Text)
oID = Nothing
End Sub
Private Sub mniEditorMirror_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceMirror.Click, mniResultMirror.Click
If sender Is mniSourceMirror Then
svImageSource.RotateDisplay(RotateFlipType.RotateNoneFlipX)
Else
svImageResult.RotateDisplay(RotateFlipType.RotateNoneFlipX)
End If
End Sub
Private Sub mniEditorZoomToFit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceZoomToFit.Click, mniResultZoomToFit.Click
If sender Is mniSourceZoomToFit Then
svImageSource.ZoomToFit()
Else
svImageResult.ZoomToFit()
End If
End Sub
You'll just need a couple of switches in the File menu; one to turn the source editor AllowDrop on and off and the other to force new images in both editors to fit to the size of the control. AllowDrop directly sets the property of the source editor, and the size to fit option requires a module level flag variable "m_SizeToFitOnLoad" Private Sub mniFileAllowDrop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mniFileAllowDrop.Click
svImageSource.AllowDrop = Not svImageSource.AllowDrop
CType(sender, MenuItem).Checked = svImageSource.AllowDrop
End Sub
Private Sub mniFileFitOnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mniFIleFitOnLoad.Click
CType(sender, MenuItem).Checked = Not CType(sender, MenuItem).Checked
m_SizeToFitOnLoad = CType(sender, MenuItem).Checked
End Sub
Add a line at the end of both ImageLoaded Events to optionally force the display based on that variable: If m_SizeToFitOnLoad Then [editor].ZoomToFit()
You don't want those menus enabled unless there is an image loaded so flip the enabled property to False in the corresponding "ClearXXXViewport" method and to True in the editor's ImageLoaded event. To add a little more polish, drop two ContextMenu controls on the main form, name them "ctxSource" and "ctxResult" and set the ContextMenu property sheet item of each editor control to the related ctx. You can add as many options as you like, including duplicating all of the menu and toolbar options, I'm just adding a single Image Information item to each. Similar to the regular menus, in each ImageEditor's ClearXXXViewport method, set the related context menu's Visible property to False and in each editor's ImageLoaded event set the visibility to True; use Visible instead of Enabled so the context menus don't show up at all on right clicks unless the editor has a loaded image. Wiring up the context menus is as simple has adding them to the previous menu click handlers: Private Sub mniEditorInformation_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mniSourceInformation.Click, mniResultInformation.Click, _
ctxSource_Information.Click, ctxResult_Information.Click
Dim oID As svDisplayImageDetail
If sender Is mniSourceInformation Or sender Is ctxSource_Information Then
oID = svImageSource.CurrentImageDetail
Else
oID = svImageResult.CurrentImageDetail
End If
MsgBox("Page: " & oID.PageNumber.ToString & " of " & _
oID.PagesInFile.ToString & vbCrLf & _
"width: " & oID.Width.ToString & vbCrLf & _
"height: " & oID.Height.ToString & vbCrLf & _
"current zoom: " & oID.CurrentZoom.ToString & vbCrLf & _
"max zoom: " & oID.MaxZoom.ToString, _
MsgBoxStyle.OKOnly And MsgBoxStyle.Information, Me.Text)
oID = Nothing
End Sub
And with that, the app and its nicely reusable control is complete - On time and ready for a tough code review thanks to the step by step information in this article. Congratulations, here's hoping it helps keep your company in your neighborhood ;-) Next: Links to the code Robert Smith
Kirkland, WA added to smithvoice march 2005
jump to: |