Thread: Hi, I need help with 'Device Context' concept

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Smile Hi, I need help with 'Device Context' concept

    Hi,

    I posted this same question on another forum but to be honest I didn't really get the replies I'd hoped for so to avoid hurting someone's feelings I thought I'd post it here instead, within a windows specific forum. The following part of a callback function is within a switch(message) case statement where message is of variable type UINT and WM_PAINT is (as I understand) a message passed by the system which loads a small bitmap graphic in the upper left corner of a Dialog Box. Anyway here it is:

    Code:
    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)
    Code:
    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)
    Code:
    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)
    Code:
    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)
    Code:
    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 Thanks

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by shrink_tubing View Post
    Hi,

    I posted this same question on another forum but to be honest I didn't really get the replies I'd hoped for so to avoid hurting someone's feelings I thought I'd post it here instead, within a windows specific forum. The following part of a callback function is within a switch(message) case statement where message is of variable type UINT and WM_PAINT is (as I understand) a message passed by the system which loads a small bitmap graphic in the upper left corner of a Dialog Box. Anyway here it is:

    Code:
    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)
    Code:
    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)
    Code:
    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)
    Code:
    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)
    Code:
    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 Thanks
    Yes, a device context is an abstraction of a graphical output device, and except for screen-based contexts (such as in your example), an HBITMAP object must be allocated and associated with it. The coordinates of a device context are always relative to the origin (x:0, y:0) of the device (and thus not necessarily the screen itself), although the point of origin depends on the "mapping mode" (default is top-left).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    That's great thanks for the reply

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Device & Rendering Context
    By hannibar in forum Windows Programming
    Replies: 1
    Last Post: 12-16-2005, 05:28 AM
  2. Device context woes
    By samGwilliam in forum Windows Programming
    Replies: 13
    Last Post: 04-16-2005, 10:01 PM
  3. Other Routines For Setting a pixel onto a Device context
    By MicroFiend in forum Game Programming
    Replies: 21
    Last Post: 07-16-2004, 08:09 AM
  4. Replies: 4
    Last Post: 06-30-2004, 03:11 PM
  5. Device Context
    By gvector1 in forum Windows Programming
    Replies: 2
    Last Post: 03-25-2003, 08:38 AM