|
-
May 3rd, 2013, 07:15 AM
#1
[RESOLVED] Changing Fonts at Runtime.
Hi, I'd like an opinion on the following code...
vb.net Code:
Private Sub Face(ByVal a As Integer)
'Style 0 regular, 1 Bold, 2 Italic, 3 Bold Italic.
If a = 1 Or a = 2 Then
Label2.Font = GFR(My.Resources.MTCORSVA, 48, 2)
Label3.Font = GFR(My.Resources.MTCORSVA, 34, 2)
Label4.Font = GFR(My.Resources.MTCORSVA, 34, 2)
Else
Label2.Font = GFR(My.Resources.albanyzc, 36, 3)
Label3.Font = GFR(My.Resources.albanyc, 28, 0)
Label4.Font = GFR(My.Resources.albanyc, 28, 0)
End If
End Sub 'Set Type face.
Private Function GFR(ByVal FontResource As Byte(), _
ByVal Size As Integer, _
ByVal Style As FontStyle) As Drawing.Font
Dim Fc As New Drawing.Text.PrivateFontCollection
Fc.AddMemoryFont(Runtime.InteropServices.Marshal. _
UnsafeAddrOfPinnedArrayElement(FontResource, 0), _
FontResource.Length)
Return New Drawing.Font(Fc.Families(0), Size, Style)
End Function 'Get Font from Resource
The font files are in Resources, and also in C:\Windows\Fonts.
I've been using this code for a couple of years or so, but every now and then something in the project causes the Labels to malfunction, in that they're just the outline of where they should be with no text, and crossed out in red, two red lines from opposite corners, corner to corner. (That's to the best of my memory, it doesn't happen all that often and my memory isn't what it once was).
Strangely, if I run the project again, even immediately after closing it when it malfunctioned, it'll display correctly. This problem occurs fairly often on my wife's laptop but quite rarely on my PC. This project is called from the Start Menu so all sorts of other things are likely to be happening at the same time, so I'm trying to eliminate this part of the equation.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 3rd, 2013, 07:59 AM
#2
Registered User
Re: Changing Fonts at Runtime.
how can i change the fonts at runtime
-
May 3rd, 2013, 01:40 PM
#3
Re: Changing Fonts at Runtime.
So, if the fonts are already registered on the machine, why are we going to Resources for them? The only candidate for the cross you describe would be the GDI equivalent of "I can't draw that" which could have any number of causes some of which may have nothing to do with your program unfortunately. However, there is indeed a fairly obvious candidate in AddMemoryFont. Let's just say I wouldn't do it that way myself!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 3rd, 2013, 04:06 PM
#4
Re: Changing Fonts at Runtime.
Take a look at this article.
-
May 3rd, 2013, 07:22 PM
#5
Re: Changing Fonts at Runtime.
 Originally Posted by dunfiddlin
So, if the fonts are already registered on the machine, why are we going to Resources for them? The only candidate for the cross you describe would be the GDI equivalent of "I can't draw that" which could have any number of causes some of which may have nothing to do with your program unfortunately.
There's a bit of history associated with embedding the fonts, my original intention was to make the program independent of the Windows\Fonts folder. However, (I think it was) jmc insisted the there's no way to display a font unless it's in that folder, so I added them.
However, there is indeed a fairly obvious candidate in AddMemoryFont. Let's just say I wouldn't do it that way myself!
I'm afraid I can't see anything obviously wrong with AddMemoryFont.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 4th, 2013, 06:53 AM
#6
Re: Changing Fonts at Runtime.
I think I may've shed some light on my problem.
I was still trying to get the problem to show, stepping through the code for displaying the texts...
Here is the relevant snippet:
vb.net Code:
Private Sub Display()
'ListBox Nos.
'1: Today. 2: Tomorrow. 3: Next week 4: Event.
Dim a As Integer, z, y, x, w As String, Lb As Object
If Nf Then
Face(3)
Label2.Text = ""
Label3.Text = "There are no entries in today's diary"
Label4.Text = ""
Else
For i = 1 To 3
Lb = Me.Controls("listbox" & i.ToString)
While Lb.Items.Count > 0
w = ""
x = ""
y = ""
z = Lb.Items(0)
a = Val(Mid(z, 1, 1))
z = Mid(z, 3)
Lb.Items.RemoveAt(0)
Face(a) 'Select type face.
Say(a, w, x, y) 'Get next texts to display.
Label2.Text = w
Label3.Text = z & x
Label4.Text = y
End While
Next
End If
End Sub 'Read texts
When I got to (Line 20) z = Lb.Items(0), Form1 was displayed complete with the crossed-out text ( I see that it's actually all in red, not just the cross), well that's the first time I've been able to make it happen 'to order' so to speak. When I say 'stepping through' you may wonder how the form displays... What I usually do; is step through the code until something goes wrong, then move the breakpoint to the line that fails and when the program gets to that point and stops, I click 'continue' to see what happens.
I got an error message saying VB couldn't find the SymbolCache and that it was looking at some (I thought) FilePath... starting with f:... via a torturous route to (if I remember correctly) ... Help\Symbol.
Anyway... I spent a bit of time trying to find the files manually, without any success, despite having seen them being downloaded when to code was converted from VS2008 to VS2010. (I think it was then... I've mentioned previously, my memory's not what it was).
Eventually I discovered how to specify a folder for the SymbolCache and did it. Problem then was, how to get the files there... I re-installed (repaired) VS2010 and found that the files were where I'd specified... Phew!
So, I re-ran the code, stepping to and through where it'd previously failed and this time it was ok.
What I don't understand now is: If the program runs correctly most of the time, why does it only fail sometimes? If the problem was vb not finding files, why didn't fail all the time?
I thought that once a program had been 'Built' everything that it needs to run would be 'Built-in', including the files it needs, so now that the files are on my PC and the code seems to be working, I wouldn't expect to have to put those Symbol files on any other machine (My wife's laptop) running the 'Built' program.
Maybe I have to 'Publish' the program and install it on our laptops to get it to run correctly? Does that copy those files to the same folder?
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 4th, 2013, 11:06 AM
#7
Re: Changing Fonts at Runtime.
I'm afraid I can't see anything obviously wrong with AddMemoryFont.
The moment Runtime.InteropServices.Marshal appears you know that the function/sub is not truly compatible with VB.Net and that a lot of management is going n behind the scenes to allow you to use it in this context. This makes it rather more vulnerable to failure.
I thought that once a program had been 'Built' everything that it needs to run would be 'Built-in', including the files it needs, so now that the files are on my PC and the code seems to be working, I wouldn't expect to have to put those Symbol files on any other machine (My wife's laptop) running the 'Built' program.
What makes you think that? The vast majority of the files that a VB program uses are external to the application. Most are system files and can therefore be expected to be in place by default (though you still can't run Framework 4 on a system that only has Framework 3!), of course.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 10th, 2013, 07:50 PM
#8
Re: Changing Fonts at Runtime.
My problem has been that for ages, every now and then, mostly when I needed (wanted) to change fonts in my display labels, Label2, Label3 & Label4, and upon start-up, which is when the 'Diary' is called, the program would crash. More often on my wife's laptop but once in a while on my desktop PC.
I've tried asking for help and I've had answers that I don't fully understand... Although the 're-learning' process is progressing, albeit slowly.
Earlier today my brain must've kicked into gear, as it does from time to time, because I realized there's more than one way to skin a cat, and there's a very simple way out of my problem:
All I had to do was add three more labels to my form... (Labels 7,8 & 9) These I made exactly the same size as, and located in exactly the same places as, the corresponding labels 2,3 & 4. The three new labels have one set of Fonts installed and the originals have the other. Every time I enter text I enter the same text into each set so now all I have to do is manage which set of labels is visible and which not. A bonus seems to be a faster text and font change, but more importantly, for me anyway, is that the program hasn't crashed once on my wife's laptop since I installed the new version, so brownie points have been gained.
I realise that this hasn't taught me where my code was at fault, but it's solved my problem. So I'm going to mark it as 'Resolved' anyway.
Poppa.
Along with the sunshine there has to be a little rain sometime.
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
|