Drawing & Animation I[3]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 sonicdater 的 blog

 

at this point you can really impress your family and friends by using words like hdc, dwrop and bitblt in casual conversation and are likely to be the life of the party but if your goal is to actually write a game we'll need to go on. let's try a simple project to see how this all works.

bitblt test

this sample project demonstrates the use of the bitblt function to draw a bitmap from a picturebox to a form. this sample project is found in bitblttest.zip .

in the project we have a form (frmbitblt), a picturebox with a small picture (picbitblt) and a command button (cmdbitblt).

note: since the bitblt function expects the parameters to be in pixels, the scalemode property of the picturebox and the form has been changed from 1-twips to 3-pixels.

the code for this small program is very simple:

option explicit



declare function bitblt lib "gdi32" alias "bitblt" _

(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 sub cmdbitblt_click()

me.cls
bitblt me.hdc, 0, 0, picbitblt.scalewidth, _
       picbitblt.scaleheight, picbitblt.hdc, 0, 0, vbsrccopy

end sub

 

when the button is clicked, the bitblt function is called to draw the picture of the picbitblt picture box onto the form. we specify the hdestdc parameter of the bitblt function to be that of the current form (me.hdc). then we specify the upper left x and y coordinates of the destination to be 0. the width and height of the picture on the destination is defined to be the scalewidth and scaleheight of the picture box. this ensures that we will not be copying the border of the picture box, but only the elements inside the picture box. the hsrcdc is set to the source hdc, which is of course the picbitblt picturebox. we specify that the drawing should start at 0,0. the width and height of the source will then be the same as the width and height of the destination area.

press the command button and observe how the picture is drawn onto the form.

 

the observant reader might want to protest a little bit now about the use of an api function, instead of the native vb paintpicture method. after all, paintpicture uses bitblt to do its work so if they both do the same thing, why use the api? the reason for this is simple; the paintpicture method is extremely slow, compared to the speed that can be achieved by using the bitblt function directly. a quick test using each to copy the same picture shows that paintpicture takes almost 10 times as long to copy a picture! in a game, where speed in serving up your graphics is a big issue, i'm sure you will agree this would yield unacceptable performance.

本文关键:Drawing & Animation
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top