| psalm62 (23) | |
|
Hi, I am a beginner C++ programmer. I know only as much in C++ than any beginner would -- not very much. I am trying to learn more to create my own program so, using the given Windows Application template from Microsoft Visual Studio 2010 C++, I was going over the code. The first line of code is HINSTANCE hInst // current instance -- what does HINSTANCE hInst do? How does it help/work in the program? Thanks -- | |
|
|
|
| freddie1 (847) | |
|
Most if not all the variables used in Windows programming that are prefixed with an 'h' symbol are 'HANDLES'. These are essentially virtual memory pointers (sometimes called opaque pointers) that are needed many times for use as arguements to Windows Api functions. I haven't looked at your example, but the hInstance variable of which you are speaking is likely used in WinMain() to initialize the WNDCLASSEX::hInstance member of the WNDCLASSEX struct. The number will be meaningless to you, but highly meaningful to the operating system itself in identifying the location in virtual memory of the running instance. It is also needed in CreateWindowEx() calls. Also, it can be obtained in a number of ways in addition to retrieval from the WinMain() parameter list, for example, GetModuleHandle(). You are entering a strange new world, and at least for awhile, are going to have to accept some of these things 'in good faith'. | |
|
Last edited on
|
|
| psalm62 (23) | |
|
I used to be a web developer working in some of the languages such as HTML, CSS and a little bit of Javascript before devoting some of my time to C++, so I think I've entered that 'strange new world' a little while ago ") ... Anywho, those languages were easy compared to C++ programming. It just takes a lot of learning and practice, but I know I'll get there. I would like to know how to manipulate the Windows Application, like adding my own icon, adding/taking away the close, minimize and restore button -- just learn everything about the window; I'm not really concerned about adding content at this moment. I'm not sure where to find the code or what the code called in the already written for this in the program... Any help will be helpful... ") Thanks for the reply -- | |
|
|
|
| freddie1 (847) | |
|
If you look at a couple of the theads currently active you'll see references to Charles Petzold's "Programming Windows" books. These books cover the exact sort of material you are looking for, i.e., what all those strange variables are such as hInstance, hWnd, hIcon, etc. While its likely possible to get up to speed without Petzold's books by just spending a lot of time doing internet searches, your time will be much better spent by just getting the standard text book on this subject. I think its important to realize that doing Win32 coding is getting as close to bare metal as you can get with Windows. Its the lowest level possible. All the technologies you just listed above are several layers of abstraction above where you are at with Win32. | |
|
|
|
| psalm62 (23) | |
|
I'll try the book you mentioned, thanks. I'm trying to start somewhere, and if I can learn how to make my own interface with a file menu or similar to that, I can then begin to add content and soon have my own program. I'll mess with the code and try different things and learn that way I guess. I appreciate your help -- | |
|
|
|
| freddie1 (847) | |||||||
Here's about the simplest (except for my message cracker scheme) "Hello, World!" type Win32 GUI program that will put up a 'File' menu ...
You'll need to know how to create a project in your IDE and add the resource file (*.rc) and header file. | |||||||
|
Last edited on
|
|||||||
| psalm62 (23) | |
|
Wow, thanks! What language is that -- c++? Because I'm trying to follow a tutorial online on C++ icon changing and so on, and he's typing his code (By the way, he's making a WIndows app) in DevC++ but when I type it in visual studio, it doesn't work... I'll copy & paste your code and test it. Very much appreciated! | |
|
|
|
| psalm62 (23) | |
| Actually, I can't test your code because your I don't have your files that you used in your headers... I'll just have to surf through there and find the 'file' code... | |
|
|
|
| freddie1 (847) | |
|
Yes, that's C++. My code should work. There are only three files, e.g., Main.cpp (the top one), Mnu01.h (middle one), and Mnu01.rc (bottom one). You can name your Visual Studio Project whatever you want, but you should copy the code as is and name it as described above. Put the three files in the same directory Visual Studio makes for the project, and use the 'Add Files' right click option to include the three files in the project. The only thing anyway near non-standard in any of the above code is my message cracker scheme I use. The purpose of that is to create seperate message handling functions in place of a large Window Procedure with a massive switch statement. I can easily reverse that for you if you want. | |
|
|
|
| freddie1 (847) | |
| Just tested with VC9 (Visual Studio 2008 Pro). No errors or warnings. Tomorrow I'll post a simplified version without the message crackers. It'll be easier to follow. I would have done that at first, but the above was the way I had it. | |
|
|
|
| freddie1 (847) | |||
|
Here's an altered version with no file dependencies. In other words, you don't need Mnu01.h or Mnu01.rc. All you need is this file - Main.cpp. What I did was replace the complicated message cracking scheme with a simple WndProc and switch construct, and I created the menu from code directly inside the WM_CREATE case instead of relying on the resource script ...
| |||
|
Last edited on
|
|||
| psalm62 (23) | |||
|
I'm getting red lines under IDM_FILE_OPEN, "&Open"); IDM_FILE_EXIT, "E&xit"); "&File"); This happened before but I typed AppendMenu(hMenu.... under switch (message) and I got the same result -- red lines. My output box says there's an undeclared identifier. As I look back at the code, I see you had switch (msg), same thing I had.
But I added
| |||
|
|
|||
| freddie1 (847) | |||
I didn't test that last program in Visual Studio (rather in Code::Blocks), and low and behold, I forgot the TCHAR macros on the strings. CodeBlocks wouldn't have hollered on that. So this might help ...
Other than that, I don't know what the problem is. Afterall IDM_FILE_OPEN and IDM_FILE_EXIT are defined??? | |||
|
|
|||
| psalm62 (23) | ||
|
Yes! It worked. I believe that was the problem -- you forgot the TCHAR because it was telling me that the header TCHAR wasn't defined I think. So I deleted it and added #include "stdafx.h" but it still didn't work. The code you posted worked -- it looks great, but it's small; that should be an easy fix... How would I create another file popup next to 'file', like MUSIC? I tried coping and pasting the following:
and replacing IDM_FILE_OPEN to IDM_MUSIC_OPEN, ... but got those red lines again... | ||
|
|
||
| freddie1 (847) | ||||
Them red lines are a bugger, ain't they? Try this ...
| ||||
|
Last edited on
|
||||
| psalm62 (23) | ||
It worked -- what did you add to it? I noticed this line on the top:
Why are those number next to them? How do they help the program work? | ||
|
|
||
| freddie1 (847) | |
|
You might want to grab the code again as I fixed a couple small glitches. Those numbers are termed equates. They simply associate an id number with a text string that is more readable and understandable than a number in isolation, i.e., a mystery number which doesn't mean anything to you. Their significance will become somewhat clearer to you when you ask your next question, which is, ... "OK, that works. But when I click on a menu selection, nothing happens?" I believe you said in your first post that you were only starting at programming, so I don't know how far along you are. But this type of coding is somewhat advanced. Most folks who start it get a copy of Charles Petzold's book, "Programming Windows", I believe 4th or 5th edition. There are a whole lot of architectural issues that need to be expolained, and they are not really C++ issues as you would find in C++ oriented books. They are rather issues relating to how Windows was put together and its Api. There are a lot of tutorials out there too if the Petzold books are too expensive for you. One of them is the Forger's Win32 Api Tutorial. Do a search on that. Another is one I wrote, which is here ... http://www.jose.it-berater.org/smfforum/index.php?topic=3389.0 | |
|
|
|
| psalm62 (23) | |
|
Yes, I was going to ask how to make the file menu work. I know others put NULL in their codes and I'm assuming that is what makes your file menu not link. I will check out the link provided, it seems to have good programming help on there... One question: you had "e&xit"? | |
|
|
|
| freddie1 (847) | |||
|
The '&' symbol is what is termed (I think) a 'keyboard accelerator'. Run the program below and hold down the [ALT] key and then touch the 'F' key while keeping the [ALT] key held down. That should open up the 'File' menu. Then, still holding the [ALT] key down, touch the 'x' key. That will trigger the ... File >> Exit command, once you've built and run the below program ....
Note how every addition to the program has resulted in WinMain() staying exactly the same; everything added went into fnWndProc(), i.e., the Window Procedure. That's how it works. If you continued and made a real program out of this that really did something, you would end up with a program with a WinMain() exactly the same, and several thousands of horrid lines of code in a rambling WndProc(). Mark my words. That's why the original program I posted didn't look like this. It was designed to be extensible and completely modular. But its a lot harder for a beginner to grasp. | |||
|
|
|||
| psalm62 (23) | ||
|
Does it make a difference that in the code there's ("&OPEN") and then ("E&xit")? Does it matter that the keyboard accelerator is between the 'e' and 'x'? I see you added
Nice... | ||
|
|
||