Results 1 to 13 of 13

Thread: Device Contexts v Picture Boxes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118

    Device Contexts v Picture Boxes

    Hi. I am new to the Windows API and have heard that using excessive amounts of Picture boxes means a great overhead that will kill (slow down) a graphical application.

    I know you can use device contexts (DCs) instead of picture boxes when "blitting" (i assume that word means painting or drawing) images to the screen.

    When I try this approach I create a DC using the CreateCompatibleDC function. I then use LoadImage to load an image and associate that image with a handler.

    I then use SelectObject to associate the DC and the handler I made using LoadImage.

    No errors but nothing happens. Obviously I am not actually drawing the image to the screen. I attempt to use BitBlt to do this but no luck (although I can use bitblt fine when using picture boxes). Could any1 please help me in this matter?

    Thanks for any help b4 hand.

    Daniel

    PS I tried to give some help to others in this thread but my knowledge is so limited that I decided against it, I didnt want to make you all laugh

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    When you use CreateCompatibleDC(), it'll create a memory DC and then you'll have to select your image in that memory DC - not your actual visible picture box DC. After you select the image (HBITMAP) in your MEMORY DC using SelectObject(), you can then whatever you want on that image (picture) by using the DC handle of the memory. It will won't show up on the screen because the image is in the memory. You'll have to use BitBlt() with the handle of memory DC as the source DC and the destination DC as, for example, the picture DC.
    You also might have to change the AutoRedraw property and/or refresh the picture box using .Refresh().
    Baaaaaaaaah

  3. #3
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    Try this (bear with me):


    Private Declare Function LoadImage _
    Lib "user32" Alias "LoadImageA" ( _
    ByVal hInst As Long, _
    ByVal Filename As String, _
    ByVal un1 As Long, _
    ByVal Width As Long, _
    ByVal Height As Long, _
    ByVal opmode As Long) As Long

    Private Declare Function CreateCompatibleDC _
    Lib "gdi32" (ByVal hdc As Long) As Long

    Private Declare Function SelectObject _
    Lib "gdi32" (ByVal hdc As Long, _
    ByVal hObject As Long) As Long

    Private Declare Function BitBlt _
    Lib "gdi32" ( _
    ByVal hDestDC As Long, _
    ByVal X As Long, _
    ByVal Y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal hSrcDC As Long, _
    ByVal xSrc As Long, _
    ByVal ySrc As Long, _
    ByVal dwRop As Long _
    ) As Long
    Private Const LR_LOADFROMFILE = &H10

    Private Sub draw()
    Dim hBitmap As Long
    Dim hdcBitmap As Long
    'load bitmap
    'on LoadImage, don't worry about hInst or un1
    'LR_LOADFROMFILE tells comp to load from file and not resource
    hBitmap = LoadImage(0, somename, 0, someWidth, someHeight, LR_LOADFROMFILE)
    'create device context
    hdcBitmap = CreateCompatibleDC(Me.hdc)
    'associate them
    SelectObject hdcBitmap, hBitmap
    'draw it
    BitBlt Me.hdc, someX, someY, someWidth, someHeight, hdcBitmap, 0, 0, vbSrcCopy
    End Sub

    on BitBlt, xSrc and ySrc can be set to 0 as far as I know, the vbSrcPaint, is one of several methods used for painting (blitting), there are other for things like transparancy, but vbSrcCopy just does straight blitting. Tell me if this works.
    PS: make sure form is set to AutoRedraw: True and pixels if that's measurement you define in the functions.

  4. #4
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    By the way, that function blits to the form, seeing as that was the hdc that was defined as the destination dc in BitBlt ("me.hdc").

  5. #5
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    This is great..
    I am new to this as well.

    This stuff is hard to wrap your head around. .

    Your code u posted.. I dont think its complete,
    OR.. i dont have a clue. (prob no clue)


    Whats LoadImage from?


    Seahag

  6. #6
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    LoadImage is one of the API functions privately declared at the top of the code, it returns simply a Long value. Then, a handle, or 'device context' is created for the bitmap using CreateCompatibleDC where the dc of the form is passed to it. The dc and the long value are associated with selectobject, and then the bitblt function is called.

  7. #7
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    Ok.. my bad

    I see it. .
    I got it to work..

    NOw.. What can i do with that..
    is it possible to combine 4 pics together
    on that DC to create or paste together pics..
    Or is that up to me to Blit them to a back buffer??

  8. #8
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    btw: you have to release and/or delete the dc's at the end.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  9. #9
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    You can release the hdc and hBitmap objects using the following API calls:
    Private Declare Function DeleteDC _
    Lib "gdi32" (ByVal hdc As Long) As Long

    Private Declare Function DeleteObject _
    Lib "gdi32" (ByVal hObject As Long) As Long

    However, obviously, you can define more than one variable in a sub, like: hbitmap1, hbitmap2, hdc1, hdc2 and so on; just associate 1 with 1 and so on. Of course, then you'd have to blt each picture with it's own bitblt function (or loop it, i think). I'm actually fairly new to this too, and I'm not sure how or if you could store multiple pics on a buffer and then blt them all at once. If anyone else know about this, let me know.
    jmiller

  10. #10
    Fanatic Member SeaHag's Avatar
    Join Date
    Jul 2001
    Location
    Lake Huron
    Posts
    901
    I have an example here that uses 1 for to create
    3 different DC`s.. (back buffer, front buffer .. and so on) or at least i think thats what it does.
    Is that possible..

    Confused


    seahag

  11. #11
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    put them in an array. all of the dc/hanles are long variables. so put them in an array and controll them from there
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118
    To all who replied

    Thanks v much for your help guys Im getting a bit better wiv this now. Actually if any of you are interested I am also starting to use DirectX8 which I highly recommend.

    The link to a great page is below (sorry it is so long). Wen u get to that page click the very first beginner tutorial at the top of the page.

    U will be so amazed at what this tutorial does. Dont get me wrong its not stupidly simple but DirectX is something u need to keep playing with to understand since it is mainly just function calls and you need to know function names.

    Despite that this tutorial is great and keeps things as simple as poss. Wen copying the source code one line has een commentd out by accident. So u will need to deal with that 1. Also the program uses a bitmap and a wav file so u need to put both with the right names in the project directory b4 the program works.

    http://www.planetsourcecode.com/vb/s...earch&lngWId=1

    I am just amazed at how powerful DirectX is and how short the code is. If this helps u guys in any way I am glad I have helped u.

    Take care and hope u keep going well.

    Dan

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    118
    By the way

    If any1 is interested in the DirectX thing but has problems with this link I gave out or anything else that I might be able to help u wiv just let me know at [email protected]

    Thanks once again every1 u r great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width