Example: dlg_three
![[images/dlg_three.gif]](http://www.winprog.org/tutorial/images/dlg_three.gif)
Now don’t get me wrong, this is a
Example: dlg_three
![[images/dlg_three.gif]](http://www.winprog.org/tutorial/images/dlg_three.gif)
Now don’t get me wrong, this is a
Example: ctl_one
![[images/ctl_one.gif]](http://www.winprog.org/tutorial/images/ctl_one.gif)
I realize I’ve already used buttons in previous examples, so you should already be more or less familiar with them, however I figured that since I was using them in this example I might as well add it to the title for the sake of being complete.
One thing to remember about controls is that
Example: dlg_two
![[images/dlg_two.gif]](http://www.winprog.org/tutorial/images/dlg_two.gif)
Now we take a look at
Example: menu_one
![[images/menu_one.gif]](http://www.winprog.org/tutorial/images/menu_one.gif)
This is just a small section to show how to add basic menus to your window. Usually you use a pre-made menu resource. This will be in an .rc file and will be compiled and linked into your .exe. This is rather compiler specific, commercial compilers will have a resource editor that you can use to create your menus, but for this example I will show the text of the .rc file so you can add it in manually. I usually have an .h file as well which is included in both my .rc file and my .c source files. This file contains the identifiers for controls and menu items etc.
For this example you can start with the window code from simple_window and add this code into it as instructed.
First the .h file. Usually called “resource.h”
#define IDR_MYMENU 101 #define IDI_MYICON 201 #define ID_FILE_EXIT 9001 #define ID_STUFF_GO 9002
Not much there, but our menu will be pretty simple. The names and values here are up to you for the choosing. Now we write our .rc file.
#include "resource.h"
IDR_MYMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Stuff"
BEGIN
MENUITEM "&Go", ID_STUFF_GO
MENUITEM "G&o somewhere else", 0, GRAYED
END
END
IDI_MYICON ICON "menu_one.ico"
You will want to add the .rc file to your project or makefile depending on what tools you are using.
You also want to
Example: dlg_one
There’s hardly a windows program out there that doesn’t use dialog boxes. Just go File -> Open in any text editor or any other kind of editor for that matter and voila, you are presented with a dialog box, one that probably allows you to select a file to be opened.
Dialogs aren’t limited to the standard open file ones, they can look like and do whatever you choose. The attractive point of dialogs is that they provide a quick way to arrange and create a GUI (Graphic User Interface) and even some default processing, cutting down on the amount of code you must write.
One thing to remember is that
You may also want to refer to the Appendices at the end of this tutorial for more information on resources with VC++ and BC++.
Before we get any deeper I will cover the topic of resources so that I won’t have to re-write it for each section.You don’t actually need to compile the stuff in this section, it’s as example only.
Resources are pre-defined bits of data stored in binary format inside your executable file. You create resources in a resources script, a file with an extension of “.rc”. comercial compilers will have a visual resource editor which allows you to create resources without manually editing this file but sometimes editing it is the only way to go, especially if your compiler has no visual editor, it sucks, or doesn’t support the exact feature you need.
Unfortunately different compiler suites handle resources differently. I will do the best I can to explain the common features needed to work with resources in general.
The resource editor included with MSVC++ makes it very difficult to edit the resources manually, since it enforces a proprietary format on them, and will totally mangle the file if you save one that you had created by hand. In general you shouldn’t bother with creating .rc files from scratch, but knowing how to modify them manually can be very useful. Another annoyance is that MSVC++ will by default name the resource header file “resource.h” even if you wanted to call it something else. I will go with this for the sake of simplicity in this document, but will show you how to change this in the appendix on compilers.
First lets take a very simple resource script, with a single icon.
#include "resource.h" IDI_MYICON ICON "my_icon.ico"
That’s the entire file.
Always get fatal error RC1004: unexpected end of file found while building. The Sample file as below
#define IDI_MYICON 101
#define ID_FILE_EXIT 401
Understanding the message loop and entire message sending structure of windows programs is essential in order to write anything but the most trivial programs. Now that we’ve tried out message handling a little, we should look a little deeper into the whole process, as things can get very confusing later on if you don’t understand why things happen the way they do.
A message is an integer value. If you look up in your header files (which is good and common practice when investigating the workings of API’s) you can find things like:
#define WM_INITDIALOG 0x0110 #define WM_COMMAND 0x0111 #define WM_LBUTTONDOWN 0x0201
…and so on. Messages are used to communicate pretty much everything in windows at least on basic levels. If you want a window or control (which is just a specialized window) to do something you send it a message. If another window wants you to do something it sends you a message. If an event happens such as the user typing on the keyboard, moving the mouse, clicking a button, then messages are sent by the system to the windows affected. If you are one of those windows, you handle the message and act accordingly.
Each windows message may have up to two parameters,
Example: window_click
Alright, we've got a window, but it doesn't do anything except what
Example: simple_window
Sometimes people come on IRC and ask “How do I make a window?”…Well it’s not entirely that simple I’m afraid. It’s not difficult once you know what you’re doing but there are quite a few things you need to do to get a window to show up; And they’re more than can be simply explained over a chat room, or a quick note.
I always liked to do things first and learn them later…so here is the code to a simple window which will be explained shortly.
#includeconst char g_szClassName[] = "myWindowClass"; // Step 4: the Window Procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "The title of my window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
For most part this is the simplest windows program you can write that actually creates a functional window, a mere 70 or so lines. If you got the first example to compile then this one should work with no problems.
A