Bitblt beginner question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I have a simple task that I can't seem to work out. No previous
experience in C++.

OK, what I want to do is when a mouse enters a window draw an image (from
resourses) at a certain location over that form, such as the center of it.

When the mouse leaves the form i want the image to be disposed.

Also I need to fire an event when the mouse moves over the image that has
been drawn.

Can anyone help?

thanks.
 
jordan,

I suggest to take a look at the PictureBox control. It fires all the mouse
events.
 
thanks, but I don't want to embed a control on the Form, as I will be wanting
to perform the same action on a number of controls. I also want to add
opacity to the displayed image.
 
jordan,

To find out when the mouse enters/leaves the form hook on
MouseEnter/MouseLeave events.



To draw the images you use Graphics.DrawImage overloads. In order to keep
the image on the form you need to hook on form's Paint event or override
OnPaint virtual method. If you don't do that the image won't repaint if
covered part gets uncovered for example or when you minimize/maximize.



In order to fire the event when the mouse moves over the image you need to
handle form's MouseMove event and hittest against the image rectangle.



As you can see a lot of things need to be implemented. If you consider using
ImageBox as I suggested, most of the work is already done for you.
 
Event hookings aren't the problem, everthing is custom drawn.

Basically I have created a layout framework that impliments framing, drag &
dropping of containers / tab pages / window layouts, etc.

Each container has a PlugIn as content which the shell knows little about.

The final bit im trying to impliment is Drop Hints. When a user drags a
plugin over another plugin they can dock it above, below, left, right, or
tabbed. So I want to highlight with images the drop zones.
 
jordan,

You've done a great job as far as I can see. What is the problem with the
drawing exactly? You can do this on DragEnter event, refresh the drop zones
if needed on DragOver and clear on DragLeave.
 
I tried as you suggested, have the container draw on enter but because the
container is housing a Plugin dll in its controls collection the image drawn
is done behind the plugin so not visible.

The desired behaviour I'm trying to achieve is that such as found in .Net
Studio 2005 when dragging Tool Windows.
 
jordan,

Now I see what the problem is.

Each control has its own device context and all the parent drawing gets
clipped form its children.

One possible solution is to remove parent's windows style WS_CLIPCHILDREN.

To do that in the parent control you need to add that piece of code

private const int WS_CLIPCHILDREN = 0x02000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style &= ~WS_CLIPCHILDREN;
return cp;
}
}

When you do that you will be able to draw over all children control. Though,
It might mess up with the rest of the application functionality.

What VS does is to get HDC to the entire screen and draw directly there. It
uses raster operations to draw inversible lines in order to restore the
screen easily. The evidence for this is that when you drag a window over the
task bar it draws the outline even there.
Unfortunately this is not possible to do without using PInvoke and win32 API
calls. If you don't mind go ahead and use'em.
The only managed solution that I see is to use
ControlPaint.DrawReversibleFrame, ControlPaint.DrawReversibleLine or
ControlPaint.FillReversibleRectangle. Three of them use screen cordinates
and draw directly on the screen's device context. DrawReversibleFrame should
do exactly what you are after, but there is a big limitation on the styles
of the frame. I believe you'll get better results with
FillReversibleRectangle.
 
Thanks, will give the suggestions a shot.

This is by the subject is BtiBlt as I assumed that I would have to use that
method to draw on the screen. I currently use PatBlt to draw a hatched
rectangle when a container is being dragged but cannot work out how to use
BitBlt.

The times I have managed to get it to draw something it just draws black.
Mainly it throws a Win32 error (InteropServices.Marshal.GetLastWin32Error())
of 8 when I try to call GetHbitmap() on a bitmap in the app's resources.

I have seen examples of BitBlt in C# on the web, but these are mainly
drawing images created in memory such as single colour lines or for capturing
images from the screen. Nothing exists for drawing a resource image to
screen.
 
Back
Top