views:

265

answers:

5

Hey,

I ordered Programming Windows Fifth Edition a few days ago, and started working with it.

I'm starting to learn the win32 api, however, I got a question. The windows do not look modern winxp/win vista/win 7 style at all. How do I fix this?

It currently looks like this, crap font and all.

alt text

Thanks in advance!

Machiel

+6  A: 

You didn't, apparently, actually read the book. You're looking for WM_SETFONT. There is a reason the common controls aren't the first thing the book covers.

Billy ONeal
I love getting random downvotes without so much as a freaking comment as to why. NOT!
Billy ONeal
+1 because your point is valid.
Camilo Martin
I think the downvotes are because he's looking at more than fonts, he's looking for a more modern style overall. (Not knowing about the matter myself, I'm guessing there's a different widget-set and possibly an entirely different set of APIs, involved somehow.) Note that I did not leave a downvote.
fennec
@fennec: The images of the buttons he has posted are using the modern style (with the exception of the font). The classic buttons have grey borders, not blue.
Billy ONeal
I haven't searched long enough, I guess, in my enthusiasm, however, I am aware of WM_SETFONT, the problem is putting it to good use. I can't really find a snippet of code in the book that changes all the window's fonts to another font, to be honest.
Machiel
@Machiel: There is no such built in function. Set the font for the given window before showing it. Yes, it's quite verbose, and a major pain in the neck. But that's what dealing with raw win32 is like. If you want the OS to handle most of this for you, you could consider using the "lazy" example in the dialogs section... can't remember the specific page number here, but Petzold implements the entire app in terms of a dialog instead of a window.
Billy ONeal
You're forgiven for not remembering the page number :)
Hans Passant
+1 for realizing the fonts, not the button style, was the issue.
Chris Becke
A: 

You might want to post some screen shots of exactly what differences you are talking about, this would help in figuring out what you need to change.

In general I would say that you probably need to include an approprite manifest with your app so your app uses the latest common controls.

Also, these days most UI is not developed using SDK style code, this is very difficult to program/maintain, instead use some kind of UI library, MFC at the very least.

DevinEllingson
Visual Styles are already enabled. A bit odd. But it's the 5th edition...
Hans Passant
+1  A: 

You might want to check GetThemeSysFont to fill a LOGFONT of an appropriate system font, create it using CreateFontIndirect, and WM_SETFONT to assign it to each control that you create.

To my knowledge there is no way to set a different default for newly created windows in your application. Nor is there a way to set all of the windows that you've already created in a single step (ie. instead of just looping through them all, or assigning individually). The exception is dialog boxes which when created from resources allow the resource to specify the font used for all of the controls on the dialog box.

Brook Miles
No, you don't want to use DEFAULT_GUI_FONT, see http://blogs.msdn.com/b/oldnewthing/archive/2005/07/07/436435.aspx
Anders
Hm, good call. Updated.
Brook Miles
A: 

You need to set the font for each control with WM_SETFONT, you create the font by passing NONCLIENTMETRICS.lfMessageFont to CreateFontIndirect (Use SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ...) to get NONCLIENTMETRICS)

For dialog boxes, you use the pseudo font "MS Shell Dlg" @ 8pt on < Vista and "Segoe UI" @ 9pt on >= Vista

Anders
+2  A: 

To get the font right you should always call this after CreateWindow(Ex):

// hwnd is the handle of the control you just created
::SendMessage(hwnd, WM_SETFONT, (WPARAM)::GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(FALSE, 0));

You probably want to set the modern UI themes as well (XP and beyond). Check out my reply to this post for that. It's very simple.

Update
Above method is officially no longer recommended (see the Remarks section of GetStockObject). I should mention that I've always used it without experiencing any problems with ClearType (as claimed in the OldNewThing post).

Anyway the recommended way to do it now goes as follows:

NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
HFONT hFont = ::CreateFontIndirect(&ncm.lfMessageFont);
::SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
StackedCrooked
I'm sorry, but this is not correct and should not be marked as the answer, see http://blogs.msdn.com/b/oldnewthing/archive/2005/07/07/436435.aspx
Anders
It's not incorrect.
StackedCrooked
@StackedCrooked Did you check my link? It even tells you what the correct API is "It was created during Windows 95 development in the hopes of becoming the new default GUI font, but by July 1994, Windows itself stopped using it in favor of the various fonts returned by the SystemParametersInfo function"
Anders
Yep I did, I read it a few years ago as well, but I was never able to reproduce the claimed problems.
StackedCrooked