All time Pageviews

Wednesday 19 February 2014

How to create a windows application

        HOW TO CREATE A WINDOWS APP IN C++

                                         -First point is ti get a compiler,I personally recommend dev-c++ but you can use any one now to create a windows application by using c++ compiler go through  the following steps





  1. 46622 2.jpg
    2
    After installing DEV-CPP, open it. You will be presented with a window with a text area where you will write your source code.
  2. 46622 3.jpg
    3
    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.
  3. 46622 4.jpg
    4
    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.
  4. 46622 5.jpg
    5
    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";
  5. 46622 6.jpg
    6
    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.
  6. 46622 7.jpg
    7
    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.
  7. 46622 8.jpg
    8
    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.
  8. 46622 9.jpg
    9
    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.
  9. 46622 10.jpg
    10
    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
  10. 46622 11.jpg
    11
    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
    
  11. 46622 12.jpg
    12
    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.
  12. 46622 13.jpg
    13
    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_SIMPLECONTROL), NULL, SimpleProc);}
    
  13. 46622 14.jpg
    14
    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.
  14. 46622 15.jpg
    15
    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;}
  15. 46622 16.jpg
    16
    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.
  16. 46622 17.jpg
    17
    Make sure that your SimpleProc comes before the int WINAPI WINMAIN part! This is important if you want your program to work.
  17. 46622 18.jpg
    18
    Press F9 to compile and run your program!

No comments:

Post a Comment