26282 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
BT Tower is just a
relic? Wrong: It
relays 18,000hrs of
telly daily
Curiosity
plunges its drill
into Mars AGAIN,
seeks life-giving
sample
Our new 1.5TB
lappie drive isn"t
thick, it"s just
the densest - HGST
A backdoor into
Skype for the Feds?
You"re joking...
BYOD beyond the
noise
Six things you
should know before
you roll out Office
365
Vodafone revenues
hit as customers in
Europe hang onto
their cash
Space dogs and
Dragons: A brief
history of reentry
tech
Your hot
peer-on-peer code
wrestling could net
$800k from Samsung
Win a Nexus 7 with
reed.co.uk and The
Register
Slashdot
Australia Makes
Asian Language
Learning a Priority
Web of Tax Shelters
Saved Apple
Billions, Inquiry
Finds
German Researchers
Hit 40 Gbps On
Wireless Link
The Hunt For
LulzSec"s Missing
Sixth Member
Latvian Police Raid
Teacher"s Home for
Uploading $4.00
Textbook
EFF Resumes
Accepting Bitcoin
Donations After Two
Year Hiatus
Google Drops XMPP
Support
Motion To Delay
Sanctions Against
Prenda Lawyers
Denied
NSA Data Center the
Focus of Tax
Controversy
Viruses In Mucus
Protect From
Infection
Article viewer

Direct X Part 3: GDI Fonts/Bitmaps



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:C
Search OSI about C.More articles by dimport.
 viewed 10474 times send this article printer friendly

Digg this!
    Rate this article :
One more lesson before we get to the good stuff, honest, today I’m going to cover font and bitmap resources w/gdi as most of the concepts apply under direct x, and they're a damn site easier to understand this way.

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:


 

 
 bool Prog_Init()
 
 {
 
    RECT rcClient;
 
    GetClientRect(hWndMain,&rcClient);
 
    
 
    AddFontResource("Ass.ttf");
 
    hfntNew=CreateFont(-44,0,0,0,0,0,0,0,0,0,0,0,0,"Astigma");
 
    HDC hdc=GetDC(hWndMain);
 
    hfntOld=(HFONT)SelectObject(hdc,hfntNew);
 
    SetBkMode(hdc,TRANSPARENT);
 
    SetTextColor(hdc,RGB(0,0,0));
 
    DrawText(hdc,"OSI SI TEH OWN",-1,&rcClient,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
 
    ReleaseDC(hWndMain,hdc);
 
    return(true);//return success
 
 }
 
 

 

 


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:


 

 
 HFONT CreateFont(
 
 int nHeight,
 
 int nWidth,
 
 int nEscapement,
 
 int nOrientation,
 
 int fnWeight,
 
 DWORD fdwItalic,
 
 DWORD fdwUnderline,
 
 DWORD fdwStrikeout,
 
 DWORD fdwCharSet,
 
 DWORD fdwOutputPrecision,
 
 DWORD fdwClipPrecision,
 
 DWORD fdwQuality,
 
 DWORD fdwPitchAndFamily,
 
 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:


 

 
 int DrawText(
 
 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


 

 
 case WM_LBUTTONDOWN:
 
 {
 
    HDC hdc=GetDC(hWndMain);
 
    BitBlt(hdc,LOWORD(lParam)-BITMAPWIDTH/2,HIWORD(lParam)- BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMem,0,0,SRCCOPY)
 
    ReleaseDC(hWndMain,hdc);
 
    return(0);
 
 }
 
 

 

 


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:

 

 
 BOOL BitBlt
 
 (
 
 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:

 

 
 HANDLE 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

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
#include<stdio.h> main(){ printf(”the source file name is %s\n”,__FILE__); } actually __FILE__ is a macro which stands for the file name the programme is kept in and the compiler does the rest .. for you ..
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006