C ++ developing GUI classes?

I have to say that I am quite inexperienced when it comes to C ++, don't be too harsh for me.

Stumbled upon the win32 API wonders recently and decided to practice it (I would rather not use MFC / wxWidgets / etc at this point, just for educational purposes).

Well my real question is: How do you code your win32 GUI file into classes correctly. What I mean is that you can create a class that keeps individual window handles, makes it easier to modify window properties, has a more simplified version of CreateWindow (). But as I understand it, for your created windows you will need a messagepump function and a few callback functions. How does this work when combining them with classes?

Can anyone point me in the right direction? I don't mind reading (a lot) the example code if commented out.

PS: I'm also having trouble finding good (read: simple) documentation on using "resource files" for my window decorations. Bonus points for this! :)

+1


source to share


9 replies


The biggest problem I faced when I was using the Win32 API (moved to Linux and cross platform since then) was callbacks. Especially winproc one, AKA is the message pump. I found this one , which should be a good hint. I did what this page suggests when I rolled my own packaging.



+1


source


I am programming in C ++ for life. I like C ++.

However, your life will be much easier if you make your Windows GUI something .Net like C #. Win32 is very low-level and you will be building a lot of things that you get for free with .Net libraries. Win32 no longer surprises. :-)



If you want to learn C ++, choose something other than a GUI for it.

+4


source


I personally would use MFC instead of reinventing the wheel here. However, if you insist on the need to have an application object that is created when the program starts and contains a message loop and a mechanism for forwarding messages to the desired window objects.

The way MFC does it, at least. I'm not sure if the MFC source code is available for download, but if you have access to the Visual C ++ installation discs (any version), you should be able to install the source code on your computer to view it.

+2


source


Look at MFC or ATL / WFC. If you want to reinvent the wheel, the wheel itself is the best reference for doing this, especially since the source code is readily available.

+1


source


I would suggest reading up Paula Dilascia Windows ++ . It walks you through the entire process of creating a C ++ class library on top of the Windows API. It was written for 16-bit Windows, but all the concepts presented in the book still apply. Plus, you can get it really cheap since it's "outdated".

And make sure you learn about message hacks ( #include <windowsx.h>

), they won't let you pull out too much hair .; -)

+1


source


The only reason I would recommend not reinventing the wheel is not being an expert on C ++ and Win32 API. Attempting to study two unrelated topics at once will not be productive. If you want to get better in C ++, write a library for a subject that you know a lot about. If you want to explore the Win32 API, please request it raw to understand how it works before you create (or use) a wrapper for it.

0


source


The Reliable Software website has a pretty good tutorial on Windows C ++ APIs.

0


source


The best way to find out is to go and read Charles Petzold's original book. It does a good job of showing how to set up a basic message loop and how to write instructions to route various events to handlers. The real problem here is that by reinventing everything you're going to spend hours and hours writing and debugging the hanlding window code instead of writing your own application.

Unless you have a good reason to do this, you would be much better off using someone else like MFC.

The only reason I see all of this is to understand how it works before switching to MFC or something similar. At least this way you will see how it works under the covers before you can forget it forever.

0


source


Many years ago I developed a set of classes to encapsulate the API (various reasons we couldn't use MFC). I've learned a lot from the MFC source code.

The big clue is that each window has a UserInfo data member - you can use it for whatever you want. What you want to use for it, it is a pointer to the class the this .

Now, another tricky thing is that the message handler callback function cannot be a normal class member function since Windows uses C calls, not C ++. Therefore, your callbacks must be static. However, since you have stored the this pointer , it's just a matter of getting the userinfo class, casting it back to your class with that pointer, and then calling any (non-static) class functions you need.

If you plan it right, inheritance will work well, including all inheritance that windows themselves show (i.e. Edit is a Control is a window).

0


source







All Articles