Monday, June 22, 2009

Custom Ink Control Part II

After completing the previous control, I decided I wanted to extend its functionality, this post will attempt to do that. You will need to complete Custom Ink Control to be able to follow along on this post as it basically just adds on to it.
I want to be able to change the color of the ink used on the control, to do this i will need to add a ColorDialog control to the form. Now create another button on your toolstrip and double-click to open its event. Place the following code after the opening class with the rest of your dimensions:

Dim thisDrawingAttributes As New DrawingAttributes

and this in the button event:

If Me.ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
thisDrawingAttributes.Color = Me.ColorDialog1.Color
inkOverlay.DefaultDrawingAttributes = thisDrawingAttributes
End If

Drawings from the ink perspective have many attributes and all of them are set by default, since we are changing one of them (color) we need to tell the program to use these new defaults. If not, nothing happens and the pen is the default color. Test it out now.

Next I want to increase/decrease the width of the line, this is also done through DrawingAttributes. At first I started creating a splitbutton with pictures of the lines etc like the one in Word but decided i liked the speed and ease of a button instead, like the font size increase/decrease buttons.

Add 2 more buttons to your toolstrip, one to decrease pen width, one to increase it. You can mess around with pen widths to get the increments that you like, but testing showed not a vast difference unless you made large jumps in width so i put my adjustments in at 25.

For the decrease width event:

If thisDrawingAttributes.Width - 25 > 1 Then
thisDrawingAttributes.Width -= 25
inkOverlay.DefaultDrawingAttributes = thisDrawingAttributes
End If

The first line keeps the code from crashing when going below 1, the rest just decrements the width and makes the change visible.

For the increase width event:


thisDrawingAttributes.Width += 25
inkOverlay.DefaultDrawingAttributes = thisDrawingAttributes


Now just format your buttons and update your ToolTipText.

Yes I am aware that my icons stink, they are just some that I had sitting on the comp, will take icon donations :)
I think this turned out pretty well, the toolbar didn't take up as much space as i thought it might. Let me know in the comments.

No comments: