How to Create a Windows Application: 4 Steps

Table of contents:

How to Create a Windows Application: 4 Steps
How to Create a Windows Application: 4 Steps
Anonim

Have you always wondered how programs like Paint or Calculator are created? Well, learn how to create a simple Windows application using this step-by-step guide.

Steps

46622 1
46622 1

Step 1. Get a compiler

A compiler turns your raw source code (which you will write shortly) into an executable application. Obtain the DEV-CPP IDE software for the purposes of this tutorial. You can download it here.

46622 2
46622 2

Step 2. Launch DEV-CPP once installed

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

46622 3
46622 3

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

Before you start writing your source code, keep in mind that Win32 applications don't behave the same way as other programming languages, such as JAVA.

46622 4
46622 4

Step 4. On the DEV-CPP main screen, go to File -> New -> Project

You will be presented with another screen. Choose the icon where it says "Windows Application" and set the language as "C", not "C ++". In the text box where it says "Name", enter "ProgramExample". Now DEV-CPP will ask you where you want to save it. Save the file in any folder, but just make sure you remember it. Once this is done, you will be presented with a form on the source code screen. Press Ctrl + A and then Backspace. The reason we are doing this is that this way we can start over.

46622 5
46622 5

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

This includes the Windows library so you can create an application. Immediately below type: #include "resource.h" and then type: const char g_szClassName = "myWindowClass";

46622 6
46622 6

Step 6. Write a method to handle all messages and write another method where we will manage messages from resources

Don't worry if this confuses you. It will become clear later. For now, save your source code as ProgramExample.c. We will leave it as it is for the moment.

46622 7
46622 7

Step 7. Create a "Resource Script"

It is a piece of source code that defines all of your controls (for example: text boxes, buttons, etc.). You'll embed the resource script into your schedule and voila! You will have a schedule. Writing a resource script isn't difficult, but it takes time if you don't have a visual editor. This is because you will need to estimate the exact X and Y coordinates of the controls on the screen. On the DEV-CPP main screen, go to File -> New -> Resource File. DEV-CPP will ask you "Add resource files to current project?" Click on "Yes". At the beginning of the resource script, write #include "resource.h", and also write #include This takes care of all checks.

46622 8
46622 8

Step 8. Create your first control:

a simple menu. Write:

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

  • The "IDR_ILMENU" part defines your menu as ILMENU. You can call it whatever you want, however. The BEGIN part indicates the beginning. POPUP "& File" creates a new menu category called File. The & sign allows the user of your application to press Ctrl + F on the keyboard to quickly access your menu:) The MENUITEM "E & xit", ID_FILE_EXIT adds a menu item to the Files category. However, you have to define the menu object via ID_FILE_EXIT.
46622 9
46622 9

Step 9. Now let's move on to the buttons part

Your button will be placed inside a dialog, so we need to create the dialog first. To do this you need to write:

    IDD_SIMPLECONTROL DIALOG 50, 50, 150, 142 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU MENU IDR_ILMENU CAPTION "Example Program" FONT 8, "MS Sans Serif" BEGIN DEFPUSHBUTTON "Hello!", ID_CIAO, 10, 10, 40, 15 END

  • IDD_SIMPLECONTROL defines your dialog box. The four numbers after the word "DIALOG" determine the x and width coordinates of the dialog box. Don't worry too much about style for now. The IDR_ILMENU MENU puts your old menu into the program. CAPTION is the caption and attributes the character. DEFPUSHBUTTON creates our button called "Hello!" and that we define as ID_HELLO and give it the x and y coordinates, height and width.
  • Done! We are done with the resource script. There is only one more thing left. We need to assign values to all things defined in our resource script (for example IDR_ILMENU, etc.) Save the resource file as ProgramExample.rc.
46622 11
46622 11

Step 10. Go to File -> New -> Source File

Add source file to current project? Yes. You will be presented with a blank screen. To assign values to our defined controls we need to give them numbers. It doesn't really matter what numbers you assign to your checks, but you should do it in a way that keeps them organized. For example, don't define a control by assigning a random number like 062491 or something else. So, write:

    #define IDR_ILMENU 100 #define ID_FILE_EXIT 200 #define IDD_SIMPLECONTROL 300 #define ID_CIAO 400

46622 12
46622 12

Step 11. Save this file as resource.h

Remember we created "#include" resource.h ""? Well, that's the reason we did it. We needed to assign values.

46622 13
46622 13

Step 12. Go back to the source, our ProgramExample.c or whatever you called it

Write:

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

46622 14
46622 14

Step 13. Don't worry too much about the technical stuff here

Just understand that this part returns the dialog box to our message handling procedure called SimpleProc.

46622 15
46622 15

Step 14. Write:

    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_CIAO: Message "NULL," HeyBox " "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;}

  • This part handles the dialog messages. For example in the case of ID_HELLO (our button), we need to create a message box that says hello! Also, in case we go to File and Exit, we close the ID_FILE_EXIT window.
46622 17
46622 17

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

This is important if you want your program to work.

46622 18
46622 18

Step 16. Press F9 to compile and run your program

Advice

  • If you are lost, there are many guides available on the internet.
  • If you feel frustrated, take a break and then come back.
  • This is a beginner's guide, so many parts are not explained. Although it is a beginner's guide, it is recommended that you have SOME experience in the world of programming (for example you understand logical operators like if-else etc.).

Recommended: