Thread: Win Api Basics...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Win Api Basics...

    I'm having problems with the basics of windows api programming.
    before you attack me i do have a book, and i did read on-line tutorials on the subject, but they are so blur.
    all i need is a simple explanation on:

    1. what is a Device Context ?

    2. how does the SelectObject() works, what does it mean selecting an object?

    3. when i want to use an object or draw something why do i need to create a compatibleDC, like so:
    Code:
    HDC hDC, memDC;
    HBRUSH hBrush;
    
    hDC = GetDC(hwnd);
    memDC = CreateCompatibleDC(hdc);
    
    hBrush = (HBRUSH) GetStockObject(WHITE_BRUSH);
    SelectObject(memDC, hBrush);
    PatBlt(memDC, 0, 0, 100, 100, PATCOPY);
    
    BitBlt(hDC, 0, 0, 100, 100, memDC, 0, 0, SRCCOPY);
    ReleaseDC(hwnd, hDC);
    DeleteDC(memDC);


    thank you very much,
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I would be interested in the answer also.

    I did some stuff with GetDIBits recently and found that 1 in 20 times it would fail inexplicably and return wrong colours. Eventually, I had to give up and find another way to do what I wanted

    I guess a 'device' is an interface to a hardware element which supports a drawing canvas. But other than that, I don't find the distinction between a device and an HBITMAP very clear.

    I haven't been able to understand what SelectObject actually does neither. Nor do I really understand ReleaseDC - a release from what?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    A device context defines a graphics object and all its attributes and supported functions.

    There are no generic, "just draw" functions in the win32 API due to the fact that Windows applications need permission to access a device (drawing). So you need to get a handle to a device context (asking permission), and later release it (tell OS you're done). You have to release DCs and delete objects after you use them, because they are instantiated in memory.

    DCs have an object associated with them which represent what the DC is being used to do. If you select a PEN into a DC, the DC is being used to draw with a PEN, etc... So basically you have to select objects to use with a DC, because a DC can be used for a number of things. (a DC is sort've generic)

    createCompatibleDC creates a DC in system memory, which is compatible with the DC passed to it. This is mostly used to create an offscreen buffer used to draw on (because if you create a DC in memory that is compatible with the DC you created, it supports the same functionality), and then BitBlt to the "real" DC to prevent flickering when drawing in real-time.



    As for why you "have to" use createCompatibleDC in your example... you don't "have to", but in the example you're using the drawing is being done onto an offscreen (memDC) DC, and then copied (BitBlt) to the "real" DC which is actually visible. If you're drawing in real-time, and you draw directly to the window's DC, you will see the image being drawn, and so it tends to flicker. Coders typically create a DC in memory that they can draw to (hence compatible), and then copy it to the visible DC when it's complete so that the user only sees each frame when it's completely drawn.
    Last edited by BMJ; 09-03-2004 at 12:37 PM.

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by Davros
    Nor do I really understand ReleaseDC - a release from what?
    Your application creates an instance of a special object (a context) that grants you access to a device. The object gets its functionality from the OS, but after it has been instantiated, it's up to YOU to call the release function for the context, which will release anything the context may have created internally while you used it. If you don't release everything you use, you may leak memory.

    Unless you're using a managed platform like .NET, you're in charge of making sure everything is released/deleted properly.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Graphics Device Interface
    GDI Overview
    GDI Objects

    Use the TOC pane to navigate around.

    gg

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    can i selectobject() more than one object, and then draw them both?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    A device context always contains one of each of all the object types - Bitmap, Brush, Font, Pen, Region, Palette.
    You can only have one of each object type selected at a time.
    You can replace any of the objects with one of you own.

    Read the links a second time, you'll pick up alot more.

    gg

  8. #8
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    so i can select two bitmap objects and then draw them both at once?
    if so, how can i manage the place each bitmap will be drawn?
    Last edited by Devil Panther; 09-03-2004 at 02:26 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #9
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    No.

    If you select one bitmap, then select another bitmap, the DC will only have the second bitmap selected.

    A DC can only have one object selected.

  10. #10
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    ok, if i understand right it's like so:

    1. create comaptible dc
    2. select a bitmap into that dc
    3. bitblt that dc to the original dc
    4. select second bitmap in the compatible dc
    5. bitblt the compatible dc to the original


    am i right?


    but still, where do i set the location of each bitmap to be drawn at inside the compatible dc?
    or do i just select the bitmap and with bitblt place it in the right place?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #11
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    The process generally goes like this...

    1) Create a DC. (onscreen)
    2) Create a compatible DC in memory. (offscreen)
    3) Draw everything you want (the bitmaps) to the offscreen DC.
    4) BitBlt() the offscreen DC to the onscreen DC.

    And yes, you BitBlt() the bitmap to whever you want it.

  12. #12
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    And yes, you BitBlt() the bitmap to whever you want it.
    that's one of the thing i understand...


    i create a compatibleDC from the screenDC, so they are both the same size!?

    i now select a bitmap into that compatibleDC

    and now i need to copy the compatibleDC into the screenDC (using bitblt() ), so i specify the start point, width and height of the bitmap. but the numbers are of the compatibleDC to be copied to screenDC, and not the of the object itself (in that case the bitmap).

    this is really weird to me, unless the selectobject() does something to the compatibleDC... i don't know.

    thanks
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #13
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    ...
    ...
    Last edited by Devil Panther; 09-05-2004 at 01:34 PM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  14. #14
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I think I follow that mostly. So...

    How do I extract pixel values from a HBITMAP using GetDIBits?

    int GetDIBits(
    HDC hdc, // handle to DC
    HBITMAP hbmp, // handle to bitmap
    ...
    ...
    );

    The GetBIBits call requires a device. What device do I pass it? Do I create a screen compatible device for this call? Why does it need a device - what does it use it for?

    How do I ensure my HBITMAP is not already selected into a device? How do I unselect it if so? This may have been my problem as I got this method to work, but it would return garbage colours 1 in 20 times or so.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  15. #15
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    i think the above posts explain it pretty well...

    but it's not a real device device, it's device handle from your window's handle:
    Code:
    HWND hwnd;
    HDC hDC;
    
    hDC = GetDC(hwnd);
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  2. Getting Hold of win API Refernce
    By Davros in forum Windows Programming
    Replies: 2
    Last Post: 08-29-2004, 04:01 PM
  3. win api literature
    By DMaxJ in forum Windows Programming
    Replies: 5
    Last Post: 04-30-2004, 03:25 PM
  4. Win API
    By Marky_Mark in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2001, 12:57 PM
  5. Rich edit control example Win API
    By Echidna in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2001, 02:12 AM