Sign in to follow this  

Device Contexts! :oD

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Well I took your advice and had some faith in my search powers and before I knew it I was understanding stuff I once thought I never could ;o) Petzold's windows book works out at about £30 for me which is a little too dear right now after Christmas so between now and then I'm going to be relying on my 'search fu' as one poster called it, to progress.

Ok with that said have a look at this function:
case WM_PAINT:
{
if(g_hBMP)
{
GetObject(g_hBMP,
sizeof(DIBSECTION),
&dibSection);
HDC hdc = GetDC(hDlg);
HDRAWDIB hdd = DrawDibOpen();
DrawDibDraw(hdd, hdc, 50, 10, 95, 99,
&dibSection.dsBimh,
dibsection.dsBm.bmBits, 0, 0,
dibSection.dsBmih.biWidth,
dibSection.dsBmih.biHeight, 0);
DrawDibClose(hdd);
ReleaseDC(hDlg, hdc);
}
} break;

Believe it or not I'm not too bad with all this after having done a load of searching the Microsoft Website about it. Here's what's going on according to my present understanding:

a)
GetObject(g_hBMP,
sizeof(DIBSECTION),
&dibSection);

The GetObject function gets the details of an existing bitmap file. In this case the if statement checks to see if one is in existence and has been initialised by seeing if a handle to it exists. It then prepares a section of memory according to the size of the data type DIBSECTION which is needed to hold all the required data about the bitmap. Then it takes the address of a variable in this case a buffer which is filled with all the required data for use later. OK!

b)
HDC hdc         = GetDC(hDlg);

The next part gets a handle to a 'Device Context' using the GetDC function and passing a handle to the Dialog Box as the parameter.

c)
HDRAWDIB hdd    = DrawDibOpen();

Required to open the DrawDib library. Not too sure about this one but without it being opened and a handle to the instance being created the drawing functions later cannot be used.

d)
DrawDibDraw(hdd, hdc, 50, 10, 95, 99,
&dibSection.dsBimh,
dibsection.dsBm.bmBits, 0, 0,
dibSection.dsBmih.biWidth,
dibSection.dsBmih.biHeight, 0);

This is where the graphic is drawn - I assume - within the context of the Dialog Box, which is probably where those numbers are related to - the confines of the Dialog Box otherwise I imagine they would have no reference to anything except a standard arbitrary point which would most likely be the upper left part of the screen display. Not useful when you want to draw a graphic within a dialog box boundary - is this where the power of the HDC comes in? The rest is basically graphic specific stuff being read from the buffer 'dibsection' which was filled before with the GetObject function.

e)The rest is just clean up stuff!

So - I sort of worked my questions into my description of what's going on which was probably not the best idea so to recap here are my main questions numbered below:

1)I know the term 'Device Context' can refer to a printer or to a monitor screen or that sort of device - but it seems here a Dialog Box is being considered as the 'Device'. Seems rather bizarre, but is this assumption correct? Is that where all that stuff mentioned in part d) comes in? The numbers and offsets are drawn relevant to the device they are in - in this case the dialog box's boundarys? So could the same code used to draw stuff within a dialog box be used to draw stuff to a printer through the GDI? I'd be interested to know if this is using the same stuff here. Seems reminiscent of the early days in windows and OS's in general where they used the same functions to draw stuff to screens as to printers to save memory usage.

Actually that's it, I only had 1 question afterall - so far :o) Thanks ;o)

Share this post


Link to post
Share on other sites
You can read an explanation of what a device context is here: About Device Contexts (Windows).

When you process WM_PAINT, you should use BeginPaint and EndPaint to validate the update region. You will also get a DC from that. So another way to write your code would be:

case WM_PAINT: {
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hDlg, &ps);
if(hDC != NULL) {
if(g_hBMP) {
// draw bitmap
}

EndPaint(hDlg, &ps);
}
} return 0;



You should also return 0 after WM_PAINT or DefDialogProc or what other default handler you call after the break will handle the same message.

Share this post


Link to post
Share on other sites

This topic is 2539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this