|
-
Aug 1st, 2002, 08:42 PM
#1
Thread Starter
Lively Member
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
-
Aug 1st, 2002, 10:55 PM
#2
PowerPoster
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().
-
Aug 1st, 2002, 11:01 PM
#3
Addicted Member
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.
-
Aug 1st, 2002, 11:04 PM
#4
Addicted Member
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").
-
Aug 2nd, 2002, 09:12 AM
#5
Fanatic Member
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
-
Aug 2nd, 2002, 09:52 AM
#6
Addicted Member
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.
-
Aug 2nd, 2002, 10:00 AM
#7
Fanatic Member
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??
-
Aug 2nd, 2002, 02:14 PM
#8
Fanatic Member
btw: you have to release and/or delete the dc's at the end.
-
Aug 2nd, 2002, 03:14 PM
#9
Addicted Member
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
-
Aug 2nd, 2002, 03:59 PM
#10
Fanatic Member
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
-
Aug 2nd, 2002, 11:29 PM
#11
Fanatic Member
put them in an array. all of the dc/hanles are long variables. so put them in an array and controll them from there
-
Aug 4th, 2002, 05:21 PM
#12
Thread Starter
Lively Member
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
-
Aug 4th, 2002, 05:26 PM
#13
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|