Mouse Usage & coloring the selected area

Witch Doctor
01-20-2005, 06:40 AM
I have a picture object in which I have drawn a graph. I want to look a only selected section of the data. I would use the mousebutton_down to start the selection. Then holding the mouse button down move it over until a Mouse button _up event to end the selection. The problem is how do I shade/change the color of the area that I have selected?

I suppose I could draw a bunch of vertical lines but this does not seem like the best approach to accomplish the task.

Thank you in advance for your help

zelg37
01-21-2005, 11:06 PM
I have a picture object in which I have drawn a graph. I want to look a only selected section of the data. I would use the mousebutton_down to start the selection. Then holding the mouse button down move it over until a Mouse button _up event to end the selection. The problem is how do I shade/change the color of the area that I have selected?

I suppose I could draw a bunch of vertical lines but this does not seem like the best approach to accomplish the task.

Thank you in advance for your help
It sounds like you have a good grasp of how to use the mouse to define the origin and extent of your rectangular, selected area.

However, you are right that drawing lines is a bit too coarse a way to darken an area.

Probably the best way to shade is adjsut the colors of individual pixels. The VB's in-built way to do this is through PSet and Point, however the API calls GetPixel and SetPixel do the same thing only faster.

But in either case you start at the pixel in the upper left hand corner and loop through all the pixels in the selection area. After getting the color of the pixel, split it's RGB color value into Red Green Blue values (each being between 0 to 255). Then the you add to each value to lighten or subtract to darken (remember to check not to go below 0 or above 255). Then
set the pixels's new RGb color value at the X,Y location of the pixel being adjusted.

Here's one PDF article that the GetPixel/SetPixel APIs to create code for ShadeRegion and ChangeShade functions:
http://www.ftponline.com/archives/premier/mgznarch/vbpj/1997/03mar97/hitt.pdf

There's a working example of using Direct Memory Access (DMA) for doing fast pixel darkening and lightening attached to this post:
http://www.xtremevbtalk.com/showpost.php?p=859095

Another example of fast DMA pixel manipulation is David Brebner's ImagePro2 example attached to this page:
http://www.ur.co.nz/static/default_typeid_3_dataid_367.htm

ByteMe
01-21-2005, 11:25 PM
What type of area do u want to select? Would the area be a particular shape or is it irregular shaped?

herilane
01-22-2005, 10:28 AM
The easiest way would be to draw a rectangle on the picturebox using vbInvert pen mode. It won't be as pretty as darkening/lightening, but it will be simple and fast.

passel
01-22-2005, 02:10 PM
What I was going to post (and might as well anyway), is similiar to what herilane posted.
I usually use vbXorPen whereas Helen's example used vbInvert.
Also, (as a side note), there was a typo in the first comment of Helen's example.


'If you want an outline instead of a filled rectangle, change BF to F in each Line statement.

should be
"change BF to B" rather than "change BF to F"

In my example, using XorPen, you can change the constant SHADECOLOR to see what
effect it has on the shaded area. If you set it to &HFFFFFF (or vbWhite) you should
get the same result as using vbInvert. &HA5A5A5 inverts every other bit so is less
"inverting" than &HFFFFFF.

Option Explicit
Dim lx As Single, ly As Single, bx As Single, by As Single
Private Const SHADECOLOR As Long = &HA5A5A5

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
lx = X: ly = Y: bx = X: by = Y
Picture1.DrawMode = vbXorPen
Picture1.Line (bx, by)-(lx, ly), SHADECOLOR, BF
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Picture1.Line (bx, by)-(lx, ly), SHADECOLOR, BF
lx = X: ly = Y
Picture1.Line (bx, by)-(lx, ly), SHADECOLOR, BF
End If
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.Line (bx, by)-(lx, ly), SHADECOLOR, BF
Picture1.DrawMode = vbCopyPen
End Sub


Also, I'm assuming that the DrawMode was the default vbCopyPen and set it
back to that when the mouse is released. But if I was changing the drawmode
for other reasons, in my code, I would probably save the current DrawMode
in a variable in the MouseDown, and restore it in the MouseUp.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum