Related
Hi gents.
I want to ask you,how can I use external font loaded from ttf file in my app,developed under Embedded Visual C++?
I need to easily change color and size of the font and using the DrawText() function make an output into current DC.
Can someone make an example for me please?
Thank you.
Here's the bare bones of it ,you'll have to flesh it out to suit. It might not be perfect but it works.
Global Variable
Code:
HFONT g_hfont;
Forward declaration of EnumFontProc()
Code:
int CALLBACK EnumFontsProc(LOGFONT *lplf, TEXTMETRIC *lptm, DWORD dwType, LPARAM lpData);
In WM_PAINT print it out. I'll use Wingdings. The name in the EnumFonts must match the actual name of the font as declared in the TTF file.
Code:
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
EnumFonts(hdc,TEXT("Wingdings"),(FONTENUMPROC) EnumFontsProc,NULL);
SelectObject(hdc,g_hfont);
DrawText(hdc,TEXT("12345"),5, &rt, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
DeleteObject(g_hfont);
EndPaint(hWnd, &ps);
break;
In EnumFontsProc set the font to the first font given. Returning 0, ends the enumeration, non zero, give me the next.
To change the size do it here by changing lplf->lfHeight and lplf->lfWidth
Code:
int CALLBACK EnumFontsProc(LOGFONT *lplf, TEXTMETRIC *lptm, DWORD dwType, LPARAM lpData)
{
g_hfont = CreateFontIndirect(lplf);
return 0;
}
To load and release your font add the code below to the message handling sections.
Code:
WM_CREATE:
AddFontResource(TEXT("\\Storage Card\\myfont.TTF"));
WM_DESTROY:
RemoveFontResource(TEXT("\\Storage Card\\myfont.TTF"));
Here's the proof it works, (with Wingdings anyway!)
Thanks man,I will try it ASAP.
Well,is it possible to read the ttf file directly from exe resource also?
I am still learning C++,so some things are a bit unclear for me. Can you please post this source?
This stuff is explained fully at:
http://msdn.microsoft.com/en-us/library/aa911455.aspx
I've just taken a few shortcuts with it.
The zip file contains the main .cpp file of the above project. It does not include the AddFontResource or RemoveFontResource statements.
Create a shell WinMo application and replace the main program .cpp file with it.
AddFontResource can only accept the path to a True Type font TTF file as an argument.
The thing you have got to get your head around are CALLBACK routines, they are used during most enumeration routines, and are widely used in Win32 for all sorts of things. A Win32 app's WndProc it itself a callback routine. In the case of EnumFonts, you pass the address of the callback routine to the enumerating function, which will call your function once for every instance it finds that matches your request. You terminate the sequence by returning 0 or it will carry on until there are no instances left. You have to decide what to do with the results. In my case I take the first Wingding font I'm offered, then quit.
Be aware that there is little, or no error handling in this code. I cobbled it together pretty quickly to prove the point.
I remember reading somewhere that if you drop the .TTF file in the Windows directory, and do a soft reset, then Windows will pick it up as a resident font, so the AddFontResource and RemoveFontResource functions are not required. EnumFonts() should know about it, and present it accordingly.
Well,thank you very much for that,but can you please provide me full project source,including all the files? With working project,I can better understand the font loading principle.
Unfortunately,I am too busy nowadays to discover something,that I don't understand well,so every time save is a big plus for me. Thank you very much again.
Attached,
But, it is a VS 2008 Smart Device project. EVC won't read it.
You will have to create a shell hello world project in EVC and copy the modified code in the panels above from TestFont.CPP.
Hello.
Actually i've got it working,but I still cannot discover,how to resize the font.
Where should I exactly put the lfWidth and lfHeight?
I am now a bit confused of that.
This is my code:
Functions and declarations of HFONT,CALLBACK,WM_CREATE,WM_DESTROY are present as you described.
void ShowNumber(HDC hdc, int Value,int xrect,int yrect,int wrect,int hrect,HBITMAP source)
{
EnumFonts(hdc,TEXT("Sam's Town"),(FONTENUMPROC) EnumFontsProc,NULL);
SelectObject(hdc,g_hfont);
TCHAR szText[MAX_LOADSTRING];
int width,height;
rtText.right=wrect;rtText.bottom=hrect;width=wrect-xrect;height=hrect-yrect;
TransparentImage(hdc,xrect,yrect,width,height,source,xrect,yrect,width,height,RGB(255,0,255));
SetBkMode(hdc,TRANSPARENT);
if(Value<10) wsprintf (szText, TEXT ("0%d"),Value);else wsprintf (szText, TEXT ("%d"),Value);
rtText.left=xrect ;rtText.top=yrect ;SetTextColor(hdc,RGB(0,0,0)); DrawText(hdc,szText,2,&rtText,DT_SINGLELINE | DT_LEFT | DT_TOP);
DeleteObject(g_hfont);
}
int CALLBACK EnumFontsProc(LOGFONT *lplf, TEXTMETRIC *lptm, DWORD dwType, LPARAM lpData)
{
g_hfont = CreateFontIndirect(lplf);
return 0;
}
Click to expand...
Click to collapse
This doesn't work:
lplf.lfWidth=0;
g_hfont = CreateFontIndirect(lplf);
Click to expand...
Click to collapse
Actually I want to put one more parameter into ShowNumber() function,which will tell the function the size of the font.
You are nearly there...........
The code to set the font size will have to go in here.......
Code:
int CALLBACK EnumFontsProc(LOGFONT *lplf, TEXTMETRIC *lptm, DWORD dwType, LPARAM lpData)
{
lplf->lfHeight=14;
lplf->lfWidth=8;
g_hfont = CreateFontIndirect(lplf);
return 0;
}
Or whatever your values are, I just picked 8 and 14 as examples. If these values are passed as variables to another function above, store them as global variables, and use them here.
As lplf is passed to this function as a pointer to a LOGFONT structure, you have to use the -> operator to access its members. lplf.lfHeight won't work as you mentioned, that is for predeclared or static structures.
These values have to be changed here in the EnumFontsProc() routine before the font is created, as we are passing that pointer to create the font.
Good Luck!!
P.S. While we are here, let's tidy it up a bit.
Replace:
Code:
if(Value<10) wsprintf(szText,TEXT ("0%d"),Value);else wsprintf (szText,TEXT("%d"),Value);
with
Code:
wsprintf(szText, TEXT ("02%d"),Value);
A very big thanks for that.
I am still beginner in C++,so something can look very comic for others.
My beloved programming language is Pascal.
I really didn't think,that -> is an operator,I've just mentioned,you wrote it as something else(something like "to")
You are more than welcome.
Some features in C/C++ are not immediately obvious, but the real battle is fighting your way through the Win32 programming model, trying to get it to do what you want. Sometimes the simplest of things become maddeningly complicated, but the effort to do it properly is usually worth the effort.
Glad you got it working, best wishes, stephj.
Hello a bit later.
First of all,thank your for your explanation and now I am using this method successfuly.
I want to ask you now,if there is a way to get fixed pitch of the font,that is truetype. With standart font I can achieve with this...
lf.lfPitchAndFamily = (tm.tmPitchAndFamily & 0xf0) | TMPF_FIXED_PITCH;
I am using font,that displays numbers in "digital" mode(7-segment display) and I get the number "1" very narrow in comparison with others(actually it is narrow on any kind display,but is displayed on right segments,so "01" has adequate spacing on hard display). Now I get "0l_" instead of "0_l",that's not preferable.
Thanks in advance for reactions.
I don't think there the above method will work as each character has it own width associated with it.
A simpler approach, a bit of a pain, but which should work, is to draw/print the numeric value out one character at a time using DrawText(). Set a RECT structure up to hold the coordinates of the top left and bottom right of the area for each character.
Setting uFormat to DT_RIGHT will force the character to right align in the RECT area, so the '1' should appear correctly. Move the left and right values along a fixed amount for each character. A bit of trial and error will be involved first until you get the values just right.
Good luck, stephj.
I see some posts where voluminous data (a log for instance) is put in a scrollable text box. The idea is to isolate long pieces of info so that the post is not 200 lines long - you just see the main post and can scroll through the data if you need to see it.
However, I can't see how to do that. I see how to format text, insert a URL, indent text, justify, etc., etc., but not the box I am looking for. Any help? Thanks.
I think you're talking about the code box. Click on the hash icon to do that.
missparker76 said:
I think you're talking about the code box. Click on the hash icon to do that.
Click to expand...
Click to collapse
If you click "QUOTE" on someone's post you can see the BB code they used.
Or just point us to a post if you still can't figure it, and we'll take a look.
Dave
Thanks! I couldn't identify the hash icon, but from the "code" I figured out if I use a bb tag of "code" and "/code", I could get it. See:
Code:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Now, just a small inconvenience. Does it have to say "code" at the top? It seems to add that automatically.
Code:
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
Line 1
I wonder if this will work.
Click go advance when you reply then its the # symbol on the top of the input box.
Gahh Its Lee said:
Click go advance when you reply then its the # symbol on the top of the input box.
Click to expand...
Click to collapse
I see! When I looked before, I didn't understand what code hashes meant. Thanks much!
As Dave Shaw mentions above, vBulletin boards use what are called BB Codes to format the text within a post.
Have a look at the vBulletin Community Forum BB Code page for a full list of codes. Straight from the horses mouth!
Be aware that not all codes may be available, or usable on XDA-DEVS. Their use is controlled by the security settings, as defined by the board administrator(s).
stephj said:
As Dave Shaw mentions above, vBulletin boards use what are called BB Codes to format the text within a post.
Have a look at the vBulletin Community Forum BB Code page for a full list of codes. Straight from the horses mouth!
Be aware that not all codes may be available, or usable on XDA-DEVS. Their use is controlled by the security settings, as defined by the board administrator(s).
Click to expand...
Click to collapse
Hehe. Most should be available, except anything that allows code embedding in a page, like html, as they are always off by default.
We've actually got our own list at http://forum.xda-developers.com/misc.php?do=bbcode too (same list as that I think )
hello,
I'm programming a WP7-App, i have a letter (for example the "A").
How can i recognize the input and match the correct character ?
sorry for my bad english
You can cast chars to ints.
see: http://www.asciitable.com/
In the table in that link, note that 'A' corresponds to a decimal number 65. When you cast your char 'A' to an int, it will turn into 65.
Similarly, (int)'a' is 97. And 'a' - ch, where ch = 'b', is equal to -1. Doing subtraction of chars automatically converts to integers. Casting the whole string to lowercase and then checking (currentChar - 'a' < 26) is a great way to check if you're looking at an alphabetic character (a through z).
Thanks for the answer...
the "A" is only an example caracter.
in the real world example i use japanese caracters.
the user shall be paint the correct caracter in my app, like this on the picture...
CB.NET said:
Thanks for the answer...
the "A" is only an example caracter.
in the real world example i use japanese caracters.
the user shall be paint the correct caracter in my app, like this on the picture...
Click to expand...
Click to collapse
That will be an insanely difficult task. Actually this in general takes years of study in order to accomplish
Handwriting recognition is one of the hardest things to accomplish.
If you do want to give it a shot, my suggestion:
Crop and rescale the images, and than determine patterns for each letter, thus an A can be build from 3 linear formules, check if the drawing matches this structure. You can than compute the derivatives of the drawings and from those derivatives cross check them with a database to determine which letter it is.
But this is extremely difficult, we tried to read digits in a sudoku puzzle which was already quite a difficult task to accomplish (and we tried to reference it against a database with images, as well as checking several characteristic points in a figure etc) this went OK with printed letters, but with handwritten it was a disaster. Not trying to discourage you, maybe there are libraries out there which you can use, but I would reconsider what you are trying to accomplish and determine an approach for yourself.
CB.NET said:
Thanks for the answer...
the "A" is only an example caracter.
in the real world example i use japanese caracters.
the user shall be paint the correct caracter in my app, like this on the picture...
Click to expand...
Click to collapse
I highly recommend making a class of "Cases" this way if it detects "A" it uses the "A" case select opposed to making a ton of "if" and "else" statements.
Better yet... You always could use an if/else statement or have an array of listed recognized items...
Here is something that might help: http://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox.
I am trying to figure out how to calculate dewpoint from temperature and humidity. The formula is ridiculously long, but there is a simple formula that gives an approximation, which is good enough for my purposes.
Td = T - ((100 - RH)/5).
Td = Dewpoint in degrees Celcius
T = Temperature in degrees Celcius
RH = Humidity
Since my phone is in degrees F. I had to convert to degrees C. This is what I did, but it doesn't work, I think because of the % symbol attached to the humidity variable: $int((#WCTEMP#-32)*5/9))-(100-#WCHUM#)/5)$. The problem is, it just displays exactly what I typed, it doesn't do the math. Can anyone point a noob in the right direction? It is probably obvious, but I am still trying to figure out zooper.
I have seen other requests for a dewpoint variable on the forums, so there is a demand out there. Any help would be appreciated, thanks!
Do you own Tasker?
You could do an https get on yahoo weather api and get all the info from there.
Its not to terribly difficult. Probably on the intermediate level.
Sent from my Nexus 5 using Tapatalk
I was just considering purchasing that tonight. I will give that a shot! Thanks Mr Biggzz
MrBiggzz said:
Do you own Tasker?
You could do an https get on yahoo weather api and get all the info from there.
Its not to terribly difficult. Probably on the intermediate level.
Sent from my Nexus 5 using Tapatalk
Click to expand...
Click to collapse
Its a great app!
Sent from my Nexus 5 using Tapatalk
@robgross how are you making out?
MrBiggzz said:
@robgross how are you making out?
Click to expand...
Click to collapse
I downloaded Tasker but haven't had time to learn it yet...but I will! I do know that ZW sees #WCHUM# as text, so it will not formulate it. I tried using #WCHUMN# to see if it would dispay the value as a number, as #WCTEMPN# does...but that didnt work. Thanks for following up!
Christmas in May!
Dave Handler shares a file with you, please click the link to download: http://dl.dropboxusercontent.com/1/view/qqiv1skjchq7ifu/Tasker Projects/Yahoo_Weather.prj.xml
In it are two tasks Input Zip code which uses a scene to well, get the zip!
The other task is Get Yahoo Weather. That uses a variable that gets set when you set the zip code.
Set your zip and the run the weather task and then check the variables tabs. You'll see every thing that is supplied including the five day forecast.
You'll have to figure what you want and pass it on to Zooper. Every variable for weather is global!
Enjoy!
p.s. everything is in standard units. Fahrenheit, mph and inches. This only thing I didn't do is give you an indicator if the barometric pressure is falling, stable or rising. If you need it I can put it in there!
Sent from my Nexus 5 using Tapatalk
Mr. Biggs, thanks for this, but I can't seem to open this link, it says it's not valid both in Tasker and a browser.
MrBiggzz said:
@robgross how are you making out?
Click to expand...
Click to collapse
MrBiggzz, I figured out how to use Tasker! It's a lot easier to use than I thought, and the possibilities are endless. Thanks for the tip!
I know its an old thread and i was seaching in this thread too; and now i solved by myself. I post here the solution for tasker if someone needs it:
You need to set up or read out from web 2 variables (%temp = temperature and %hum = humidity), once you have set the 2 variables you need to add a code java scriplet to the task to calculate dewpoint and absolute humidity:
function taupunkt() {
if ( (temp == null || temp == 0) ||
(hum == null || hum == 0) ) {
return;
}
var a1=7.45;
var b1=235;
tp=temp;
fw=hum;
tp=tp*1;
fw=fw*1;
x1=(a1*tp)/(b1+tp);
e1=6.1*Math.exp(x1*2.3025851);
e2=e1*fw/100;
x2=e2/6.1;
x3=0.434292289*Math.log(x2);
taux=(235*x3)/(7.45-x3)*100;
taux=Math.floor(taux)/100;
feux=(216.7*e2)/(273.15+tp)*100;
feux=Math.round(feux)/100;
var TAUX = taux;
setGlobal("TAUX", TAUX );
var FEUX = feux;
setGlobal("FEUX", FEUX );
exit ();
}
taupunkt()
The java scriplet calculate the dewpoint and the absolute humdity and stores it in the variable %TAUX (dewpoint) and %FEUX (absolute humidity). Now you can make a popup alert or whatever you want and display the dewpoint calling %TAUX and the absolute humidity %FEUX
So I'm using the #TMU_TRACK_PERC# line in the progress bar and I want a way to display how much time is left in the song.
This is what I've come up with:
Code:
$((TMU_TRACK_LENGTH#-#TMU_PLAYTIME#)/100)$
Of course, this returns the result in the format m.ss instead of m:ss (where m = minutes and ss = seconds), but oh well.
Now the problem I run into is that when the song is paused, instead of showing me the track length, it shows me "Unknown". So I thought I could solve this with conditionals like so:
Code:
$#TMU_TRACK_LENGTH#=Unknown?:$((TMU_TRACK_LENGTH#-#TMU_PLAYTIME#)/100)$
But all it gives me is $.
How do I fix this?
Just delte the $ in the middle.
CopKiller76 said:
Just delte the $ in the middle.
Click to expand...
Click to collapse
But now when the song plays it just gives me this:
-((TMU_TRACK_LENGTH#-#TMU_PLAYTIME#)/100)
minions301 said:
So I'm using the #TMU_TRACK_PERC# line in the progress bar and I want a way to display how much time is left in the song.
This is what I've come up with:
Code:
$((TMU_TRACK_LENGTH#-#TMU_PLAYTIME#)/100)$
Of course, this returns the result in the format m.ss instead of m:ss (where m = minutes and ss = seconds), but oh well.
Now the problem I run into is that when the song is paused, instead of showing me the track length, it shows me "Unknown". So I thought I could solve this with conditionals like so:
Code:
$#TMU_TRACK_LENGTH#=Unknown?:$((TMU_TRACK_LENGTH#-#TMU_PLAYTIME#)/100)$
But all it gives me is $.
How do I fix this?
Click to expand...
Click to collapse
Easier solution is to just use #TMU_REMAINING_TIME#. No need to do complicated math and way easier to handle in conditionals
kwerdenker said:
Easier solution is to just use #TMU_REMAINING_TIME#. No need to do complicated math and way easier to handle in conditionals
Click to expand...
Click to collapse
Haha. Oh. Yeah, that would be easier. I didn't know that existed! I searched for something like that but didn't find it. Thanks!
minions301 said:
Haha. Oh. Yeah, that would be easier. I didn't know that existed! I searched for something like that but didn't find it. Thanks!
Click to expand...
Click to collapse
Sure, no problem. Here is a dump of all available variables for future reference. You might find it useful
MU_PLAYTIME - Pretty string indicating how long current track is playing
MU_PLAYTIME_MILLIS - Total playing time in milliseconds
MU_TRACK_PERC - Total track percent played
MU_REMAINING_TIME - Pretty string indicating how track remaining time
MU_REMAINING_MILLIS - Total remaining time in milliseconds
MU_TRACK_NUM - Track number on album
MU_TRACK_LENGTH - Pretty string indicating track length
MU_TRACK_LENGTH_MILLIS - Total track length in milliseconds
MU_ALBUM - Album name
MU_ARTIST - Artist name
MU_TRACK - Track name
kwerdenker said:
Sure, no problem. Here is a dump of all available variables for future reference. You might find it useful
MU_PLAYTIME - Pretty string indicating how long current track is playing
MU_PLAYTIME_MILLIS - Total playing time in milliseconds
MU_TRACK_PERC - Total track percent played
MU_REMAINING_TIME - Pretty string indicating how track remaining time
MU_REMAINING_MILLIS - Total remaining time in milliseconds
MU_TRACK_NUM - Track number on album
MU_TRACK_LENGTH - Pretty string indicating track length
MU_TRACK_LENGTH_MILLIS - Total track length in milliseconds
MU_ALBUM - Album name
MU_ARTIST - Artist name
MU_TRACK - Track name
Click to expand...
Click to collapse
Awesome, thanks!