PDA

View Full Version : API



Aeronautics
27-02-2003, 04:50 PM
I'm learning a bit about API now (thanks to alistairs' tutorials), and found a problem I cannot solve.

I know how to make a window, and create objects (like buttons) on it, with a specific size and position. I also know how to change the position of an object in the window.
But how do I change other properties, like color, font, caption?

Alimonster
27-02-2003, 07:55 PM
But how do I change other properties, like color, font, caption?
Everything comes down to windows, handles and messages. Your button is a window, which means that you can (slightly unintuitively) use "for windows" functions, passing in the appropriate handle to your button object.

To get/set the caption, you can use GetWindowText (use a string to store the result, but set its size beforehand using SetLength based on the value from SetWindowTextLength [you might need a +1 for the NUL, can't remember]) and SetWindowText (or SetDlgItemText if you're dealing with a button on another dialogue window).

A lot of functions in Windows are shortcuts that send messages to a window identified by a particular handle. In this case, you can use the Windows messages WM_GETTEXT and WM_SETTEXT (use SendMessage or PostMessage to chuck them onto the queue, filling out the lparam and wparam based on what the win32.hlp file says for that particular message).

I've not tried the other two (changing font and colour). However, Windows sends a message WM_CTLCOLORBTN message when it needs the colour of a button, apparently. You'd want to add a case for this message in your window proc. You'd do a switch based on the lParam:


case WM_CTLCOLORBTN:
begin
if lParam = YER_BUTTON_HANDLE_HERE then
begin
// do stuff here (don't know what :()
end;
...

The checking of lParam is pretty straightforward. The same message will be passed for each relevant button on your window, so you have to know which one you're dealing with (perhaps you'd want a case statement for many buttons). As per the help file, the lParam gives you the handle of the button. The wParam gives you a DC - I'm not sure how they fit together, though, or even if that's the right message. I think it is.

You should be able to change the font of the button by sending a WM_SETFONT message. You'd fill out your LOGFONT structure as usual, use CreateFontIndirect to get an HFONT, and use that HFONT as the wparam. SendMessage it away.

HTH. If anyone has more specific knowledge about this, please feel free to jump in,