How to create a window (application): 4 steps

Table of contents:

How to create a window (application): 4 steps
How to create a window (application): 4 steps
Anonim

Ever wonder how programs like Paint or a calculator are made? Well, find out then how to create a simple app using this step-by-step guide.

Steps

46622 1
46622 1

Step 1. Get a compiler

The compiler converts the raw source code (which you will write soon) into an executable application. For the purposes of this tutorial, purchase the DEV-CPP IDE. You can download it here.

46622 2
46622 2

Step 2. After installing DEV-CPP, open it

You will be presented with a text area window where you will write your source code.

46622 3
46622 3

Step 3. Prepare to write a program to display text in the text box

Before you start writing the source, keep in mind that Win32 applications do not behave in the same way as other languages such as JAVA.

46622 4
46622 4

Step 4. In the main window of DEV-CPP go to File -> New -> Project menu

You will be prompted for another window. Select the small picture titled "Windows Application" and set the language to "C", not "C ++." In the text box that says "Name", enter "SimpleProgram". Next, DEV-CPP will ask you where you want to save it. Save the file in any directory, but just be sure to save it. Once done with this, you will be presented with a template on the source screen. Press Ctrl + A and then Backspace. The reason why we do this is so that we can start over.

46622 5
46622 5

Step 5. At the beginning of your source text, write "#include" (without quotes)

This includes the windows library so you can build your application. Just below that write: #include "resource.h" And then enter: const char g_szClassName [] = "myWindowClass";

46622 6
46622 6

Step 6. Write one method for processing all messages and write another method where we will process messages from resources

Don't worry if this confuses you. Everything will become clear in the future. Now save your source as SimpleProg.c. For the moment we are leaving everything as it is.

46622 7
46622 7

Step 7. Script the Resource Script

Resource Script is the part of the source code that defines all the controls (for example, TextBox, Buttons, etc.). You include Resource Script in your program and voila! You will have a program. Writing a Resource Script is easy, but it can be time consuming if you don't have a Visual Editor. This is because you will need to calculate roughly the exact X and Y coordinates of the controls, etc. In the main window of DEV-CPP go to File -> New -> Resource File menu. DEV-CPP will ask you: "Add resource file to current Project?". Press YES. At the top of your resource script, enter #include "resource.h", and also type #include This applies to all controls.

46622 8
46622 8

Step 8. Create your first control

simple menu. Enter:

IDR_THEMENU MENU BEGIN POPUP "& File" BEGIN MENUITEM "E & xit", ID_FILE_EXIT END END

The "IDR_THEMENU" part defines your menu as THEMENU. You can name it whatever you want. The BEGIN part speaks for itself. POPUP "& File" creates a new menu category called File. The & symbol allows the user of your application to press Ctrl + F on the keyboard and quickly access the menu. The MENUITEM "E & xit", ID_FILE_EXIT adds a menu item to the File category. You must, however, define the menu item with ID_FILE_EXIT.

46622 9
46622 9

Step 9. Now for the button

Your button will be inside the dialog, so we need to create it first. Do this by typing:

IDD_SIMPLECONTROL DIALOG 50, 50, 150, 142 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU MENU IDR_THEMENU CAPTION "Simple Prog" FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "Hello!", ID_HELLO, 10, 10, 40, 15 END

The IDD_SIMPLECONTROL part defines your dialog. The four digits after the word "DIALOG" define the x-position, y-position, width and height of the dialog box. Don't worry too much about the Style part for now. The MENU IDR_THEMENU part puts our old menu into the program. The CAPTION part speaks for itself, as does the font. The DEFPUSHBUTTON part creates our button called "Hello!" and define it by writing ID_HELLO and giving it the x-position, y-position, width and height coordinates.

46622 10
46622 10

Step 10. That's it

We are done with our resource script. Just one more thing: we have to assign values to all the values that we have defined in our resource script (for example, IDR_THEMENU, etc.). Save the resource file as SimpleProg.rc

46622 11
46622 11

Step 11. Select File -> New -> Source File

Add the source file to the current project? -> Yes. You will be presented with a blank screen. To assign values to our specific controls, we give them numbers. It doesn't really matter which numbers you assign to the controls, but they should be organized. For example, do not define a control by assigning a random number to it (like 062 491 or the like). So enter:

#define IDR_THEMENU 100 #define ID_FILE_EXIT 200 #define IDD_SIMPLECONTROL 300 #define ID_HELLO 400

46622 12
46622 12

Step 12. Save this file as resource.h

Remember how we wrote "#include" resource.h ""? Well, that's why we did it. We needed to assign values.

46622 13
46622 13

Step 13. Go back to the resource, our SimpleProg.c or whatever you named it

Enter:

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {return DialogBox (hInstance, MAKEINTRESOURCE (IDD_SIMPLECONTROL), NULL, SimpleProc);}

46622 14
46622 14

Step 14. Don't worry too much about all the technical details here

Just be aware that these parts return the dialog to our message handling routine called SimpleProc.

46622 15
46622 15

Step 15. Enter

BOOL CALLBACK SimpleProc (HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam) {switch (Message) {case WM_INITDIALOG: return TRUE; case WM_COMMAND: switch (LOWORD (wParam)) {case ID_HELLO: Message Heyox ", NULL," "Hallo!", MB_OK) break; case ID_FILE_EXIT: EndDialog (hWndDlg, 0); break;} break; case WM_CLOSE: EndDialog (hWndDlg, 0); break; default: return FALSE;} return TRUE;}

46622 16
46622 16

Step 16. This part processes the dialog messages

For example, in the case of ID_HELLO (our button), we create a message box with the content "Hello!" In addition, in the case when we go to File and Exit, we close the window in the case of ID_FILE_EXIT.

46622 17
46622 17

Step 17. Make sure your SimpleProc comes before the int part WINAPI WINMAIN

This is important if you want your program to work.

46622 18
46622 18

Step 18. Press F9 to compile and run the program

Advice

  • If you are upset (at any stage of the work), take a rest and come back.
  • This is a tutorial for beginners, so many parts are not explained. Even if this is a beginner's tutorial, it is recommended that you have some programming experience (e.g. knowledge of switch statments, if-else, etc.)
  • If you're confused, there are many tutorials available online.

Warnings

  • Learning Win32 is not an easy task. You need to pay attention to this. This is definitely not for the faint of heart.
  • The line numbering in this tutorial distorts the source code somewhat.

Popular by topic