|Download the project files here, youíll notice there are two projects this time, first is the font demo, the other is the bitmap demo. Open up the font project file for starters. This program is exactly the same as the last line plotting demo for the most part, the difference lies in the globalís (concepts the same, they just refer to a font and not a pen), and the initialisation/cleanup routines. Compile it and run it, just so you can see what it does.
Yeah the fonts ugly, but it was the first one i came across and you can replace it with whatever you like, assuming youíve read the init/cleanup comments:
DrawText(hdc,"OSI SI TEH OWN",-1,&rcClient,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
The first two lines are important as the RECT function is prevalent in direct x too, in a window the area you can draw on is known as the Ďclient areaí obviously the size of this could be different every time, the first two lines just get the size of the client area. The AddFontResource function, as its name suggests loads a specified font into the system font table, once you load it you can create it within your app, which i do on the next line with the CreateFont function:
LPCTstr lpszFace // font name (not filename)
Lot of parameters, youíll notice ive set most of them off so feel free to experiment. The rest is pretty much explained by the comments, although the DrawText function could use some explaining:
HDC hDC, //handle to the DC
LPCTSTR lpString //text to output
int nCount, //number of characters to display
LPRECT lpRect, //formatting dimensions
UINT uFormat //additional options
uFormat values are: DT_BOTTOM, DT_CENTER, DT_LEFT, DT_NOCLIP, DT_RIGHT, DT_SINGLELINE, DT_TOP and DT_VCENTER, once again experiment with the flags. The cleanup routine is pretty much a rewind version of the initialisation.
When you understand that. Onto the bitmap demo, fire up the project file and read through it, unlike the font app this program has a left mouse button handler that could use some explaining
you know the first and last statements, the new bugger is the one in the middle, BitBlt moves the bitmap from its dc to the window dc:
HDC hdcDest, //handle to destination (window) DC
int nXDest, //x cord of upper left corner
int nYDest, //y cord of upper left corner
int nWidth, //width of destination bitmap
int nHeight, //height of destination bitmap
HDC hdcSrc, //handle to dc that contains the bitmap
int nXSrc, //x cord of source upper left corner
int nYSrc, //y cord of upper left corner
DWORD dwRop //which raster operation to perform
Basically it defines how the bitmap gets coped, here ive used SRCCOPY which would overwrite the current contents of the DC, if anything was there, other raster op flags include SRCAND (copies one bmp over the other using AND), SRCPAINT (uses OR to combine the two) and SRCINVERT (uses XOR to combine and as its name suggests, inverts the image). The function that needs explaining in the initialisation routine is LoadImage:
HINSTANCE hinst, //handle to an instance
LPCTSTR lpszName, //name of the image (with extension)
UINT uType //type of image
int cxDesired, //width you want
int cyDesired, //height you want
UINT fuLoad //load flags
Once again the cleanup is self explanatory.
Direct x next time (albeit completely unimpressive), promise, make sure you have the sdk.
This article was originally written by Pigsbig78