I’m learning to develop windows applications using WINAPI and plain C.
Now I got a bit confused with all those handles and would like to ask if you guys could teach me some good practices to structure and handle controls and windows.
Here’s where I get confused:
Using the IDs declared in the resources for each object, we can get their handles using GetDlgItem(). Now what if we don’t know their parent, which is needed by this function.
One example: We have the main window created at launch. Then we register two new window classes and create a window for each new class and we create a message function for each too.
Now if inside one of the children windows I create a button and inside the other child window I create a text label. Now when we click the button inside of child window A the label in child window B shall be modified to whatever.
The WM_COMMAND for the button is interpreted inside the message loop for child window A. Now what would be the best and more elegant way to access the text label inside the child window B?
I am in the process of learning the WINAPI and just want to learn it right from the start instead of producing Hacked code that someday becomes unreadable and to later have to adapt to a new way of programing.
Is there really no one able to give me some advice?
About a year ago I stumbled on a book at work that was about to be thrown away. It was called
Windows Wisdom: For C and C++ Programmers
ISBN: 0471940046
The book presents a problem, and then provides a number of different approaches and solutions to the problem. If it were published today it would most likely be called “WINAPI Recipes” or “WINAPI Cookbook”. Its that type of book.
The book is out of print but you should be able to find a used copy for cheap.
Instead of regurgitating the contents of the book, I wanted to point you at the source. I believe it may provide the general guidance that you are looking for.
2
First thing to have in mind when writing programs for Windows using the Plain Old Win32 API is that every control on screen is a WINDOW on its own. Which means each have their own “HANDLES” and the like. I think you’re using an easy way out by creating controls on a DialogBox using Win32 RES editor(which is fine for a beginner). But when you go into more advanced programming on Win32, I would suggest you use CreateWindow() function.
To answer your question you could just get the handle of the control like so: GetDlgItem(HWND hParent,int nIDDLGITEM) function, and SetWindowText() over it. Make sure that the Static label has SS_LEFT property.