views:

236

answers:

1

I find I can't change the font weight even though I am creating it via win32 pinvoke. I'm using Graphics.DrawString to use the font. I'm stumped and can't find anything on this.

[DllImport("gdi32.dll")]
static extern IntPtr CreateFont(int nHeight, int nWidth, int nEscapement,
   int nOrientation, int fnWeight, uint fdwItalic, uint fdwUnderline, uint
   fdwStrikeOut, uint fdwCharSet, uint fdwOutputPrecision, uint
   fdwClipPrecision, uint fdwQuality, uint fdwPitchAndFamily, string lpszFace);

private void CreateLabelFont()
{
    // Remove -13 magic number later.
    IntPtr hFont = CreateFont(-13, 0, 0, 0, 100, 0, 0, 0, 1, 0, 0, 0, 0, "Arial\0");
    labelFont = Font.FromHfont(hFont);
}
+1  A: 

From MSDN reference for CreateFont:

fnWeight [in] Specifies the weight of the font in the range 0 through 1000. For example, 400 is normal and 700 is bold. If this value is zero, a default weight is used.

This means you fix the weight when you create a font. In case you need different font-weights, you'll have to call CreateFont multiple times.

dirkgently
And the provided code with fnWeight=700 display bold text as expected.
VirtualBlackFox
The way I have it ought to work. I think I might have a problem with the complex graphics transform in effect when I am drawing the font.
P a u l
Then show us some more code. And check the return values.
dirkgently
Yes, just confirmed the code works when there is no transform. I am drawing annotations on an x-y plot under a transform and it seems to be messing with the font. The transform and axis drawing code is involved... it would make for a very long posting.
P a u l
Why not just use two different HFONTs?
dirkgently
Are you applying a transform to the font object by any chance?
dirkgently
Font doesn't seem to have any transform methods. I think whatever graphics transform is active will affect the font. I'd like to read more on this but the info available is very thin. I hope I don't have to do Graphics.ResetTranform just to draw a label but I think this is where its going.
P a u l