C# WinForms FontDialog can't be initialized with some fonts?
I am using VS C# 2008 Express.
I am adding the capability to store and retrieve a user's 开发者_高级运维font selection to my app using the standard FontDialog control.
During my testing I have noticed that certain fonts do not initialize the dialog fully -- the name does not appear in the "Font" box, and the sample is blank.
These are fonts in which part of the name appears in the "Font Style" list on the dialog: "Arial Black", "Segoe UI Light", "Segoe UI Semibold", etc. (Although, interestingly, "Arial Narrow" does work as expected.)
I see the same problem whether I initialize the dialog's font in code or if I set the properties of the FontDialog control to one of the above problem fonts in the IDE.
It is easy to reproduce by just creating a simple form, adding a FontDialog control launched by a button, and setting its font property to "Arial Black" and size to 16.
Any way to resolve this?
Added information: This problem exhibits on a Windows 7 64-bit system. On Vista and XP, Arial Black and Arial Narrow are listed as separate fonts, rather than just Arial with Black and Narrow styles. So it seems Microsoft is doing something different with the common font dialog in Win7.
I can confirm that behavior, you should wait and see if someone offers a definitive explanation for why this is correct, and if nobody offers one, report it through Microsoft Connect.
Here's how to reproduce the problem, use LINQPad and execute the following program:
void Main()
{
using (var dlg = new FontDialog())
{
dlg.Font = new Font("Arial Black", 16);
dlg.Font.Dump("before");
dlg.ShowDialog();
dlg.Font.Dump("between");
dlg.ShowDialog();
dlg.Font.Dump("after");
}
}
use F4 to add references to System.Drawing and System.Windows.Forms, and hit Ctrl+. on Font and FontDialog to add the required using clauses
Then observe that:
- Arial Black is not chosen the first time it is shown
- If you, during that first time, pick Arial, and then pick Black in the font style picker, click OK, the dialog comes back up with no font selected.
- The only thing different between the 3 dumps of the font (provided you picked Arial, Black, 16 both times) is that the size is slightly off after the dialogs (15.75pt vs. 16pt). Also, the
OriginalFontName
is left asnull
after the dialogs. The font name, however, is the same, "Arial Black".
The reason I feel this is a bug is that if the same dialog pops up twice, with the second time showing the results of picking values the first time, it should show the same information.
精彩评论