Posted by [V3N0M] on Thursday, January 29, 2009
Labels:

Ever wonder how programs like Paint or Calculator are made? Well, learn how to create a basic application using this step-by-step guide.how to make a window

STEPS:

  • Get a compiler. A compiler transforms your raw source code (which you will soon write) into an executable application. For the purpose of this tutorial, get DEV-CPP IDE. 
  • After installing DEV-CPP, open it. You will be presented with a window with a text area where you will write your source code.
  • Get ready to write a program to display text in a textbox. Before you begin writing the source, keep in mind that Win32 applications don't behave in the same way as other languages, such as JAVA.
  • In the main screen of DEV-CPP, go to File -> New -> Project. You will be presented with another screen. Choose the little picture which says "Windows Application" and set the language as "C", not "C++." At the text box where it says "Name", enter "SimpleProgram." Now, DEV-CPP will ask you where you wish to save it. Save the file in any directory, but just be sure to remember it. As soon as you are done with that, you will be presented with a template on the source screen. Do Ctrl+A and then Backspace. The reason we are doing this is so that we can begin anew.
  • At the begin of your source, type "#include <windows.h>" (without the quotes). This includes the windows library so that you can make an application. Directly beneath that, write:

 #include "resource.h" And then type: const char g_szClassName[] = "myWindowClass";

  • Write one method to handle all the messages and write another method where we will handle the messages from the resources. Don't worry if this is confusing. It will become clear later on. Now, save your source as SimpleProg.c. We will be leaving it as is for the moment.
  • Make a Resource Script. A Resource Script is a piece of source code which defines all your controls (e.g: TextBox, Buttons, etc.) You will incorporate your Resource Script into your program and Voila! You will have a program. Writing the Resource Script isn't hard, but can be time consuming 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, etc. In your DEV-CPP main screen,

go to File -> New -> Resource File.

DEV-CPP will ask you "Add resource file to current Project?" Click YES. At the top of your resource script, type

#include "resource.h", and also type #include <afxres.h> This takes care of all the controls.

  • Make your first control: a simple menu. Type:
    • 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 call it whatever you want, however. The BEGIN part is self explanatory. The POPUP "&File" makes a new menu catagorey called File. The & sign allows the user of your application to type Ctrl+F on the keyboard and quickly access your menu :) The MENUITEM "E&xit", ID_FILE_EXIT adds a menuitem to the File catagorey. You must, however, define the menuitem by doing ID_FILE_EXIT.
  • Now for the button part. Your button will be inside a dialog, so we must make the dialog 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 defines your dialog. The four numbers after the word "DIALOG" determine x-pos, y-pos, width, and height of the dialog. Don't worry too much about the Style part for now. The MENU IDR_THEMENU puts our old menu into the program. The CAPTION speaks for itself as does the font. The DEFPUSHBUTTON creates our button named "Hello!" and we define it by saying ID_HELLO and give it x-pos and y-pos and width and height coordinates.
  • That's it! We're done with our resource script. Only one more thing remains. We have to assign values to all the things we defined in our resource script (e.g. IDR_THEMENU, etc.) Save the resource file as SimpleProg.rc
  • Go to 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 defined controls, we give them numbers. It doesn't matter too much on which numbers you give your controls, but you should make them organized. For example, don't define a control by giving it a random number like 062491 or something. So type:
    • "#define IDR_THEMENU 100"
    • "#define ID_FILE_EXIT 200"
    • "#define IDD_SIMPLECONTROL 300"
    • "#define ID_HELLO 400"
  • Save this file as resource.h Do you remember we did "#include "resource.h""? Well, this is why we did it. We needed to assign values.
  • Get back to the source, our SimpleProg.c or whatever you called it. Type:

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

  • Don't worry too much with all the technical stuff here. Just know that this parts returns the dialog to our message handling procedure called SimpleProc.
  • Type:

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:MessageBox(NULL,"Hey", "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 ID_HELLO (our button), we make a message box saying hello. Also, in the case where we go to File and Exit, we close the window in case ID_FILE_EXIT.
  • Make sure that your SimpleProc comes before the int WINAPI WINMAIN part! This is important if you want your program to work.
  • Press F9 to compile and run your program!

[V-LINKED]

posted by V3N0M . WIKIHOW . ALL RIGHTS RESERVED .

Subscribe in a reader

0 comments: