smithvoice.com
 Y'herd thisun? 

“cASe senSitiviTy is stupid... and it's also just plain dumb. Why do so-smart coders still stick to what was just a bug to begin with? And YOU CAN code by voice, but you have to accept that dogma is not law.”
-smith


from this page

Your own image control and App part 29

TaggedCoding, 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 menus

Our 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:

  • 1) The spec
  • 2) Setting up the workspace
  • 3) Feature 1: Loading an image
  • 4) Custom Exceptions
  • 5) "Fax images" and Multipage TIFFs
  • 6) Custom events
  • 7) Selecting specific fax pages
  • 8) Feature 2: Rotating image displays
  • 9) The most useful tool in GDI+: DrawImage
  • 10) Feature 3: Zooming
  • 11) Handling the unhandleable exception
  • 12) Fixing the squish
  • 13) Zooming to fit the control
  • 14) You're already beating the Pros
  • 15) Feature 4: Cropping
  • 16) Bonus Feature: StickyMouse
  • 17a) Final Cleanup
  • 17b) Passing the current display image
  • 18) Making the application
  • 19) Source and result viewports
  • 20) A better toolbar
  • 21) Hooking the toolbar to the project
  • 22) Adding ImageEditors
  • 23) The toolbar ZoomCombo
  • 24) The final solution
  • 25) Saving to image files
  • 26) An integer-only textbox
  • 27) Passing save options between forms
  • 28) Dealing with that last exception
  • 29) Offer more options with menus
  • 30 The downloads and ebook


  • home     who is smith    contact smith     rss feed π
    Since 1997 a place for my stuff, and it if helps you too then all the better smithvoice.com