Menu

How Do I write code in a Windows application?

2010-11-29
2012-09-26
  • Michael Jacob

    Michael Jacob - 2010-11-29

    Dear Sirs;
    I am new to programming and forums and I need help.
    How do I write code in a windows application and get the practice code to
    appear in the window?
    When I use the console application or an empty source everything works jsut
    fine.
    Any help would be greatly appreciated.
    Thanks,
    Neferkamichael1

     
  • Steve A

    Steve A - 2010-11-30

    By the question you ask, I assume you mean: "how do I write windows gui
    programs in C/C++" ?
    If that is the case, then, I also assume by our statement, that you have
    written the standard C introductory console mode apps, such as the console
    mode "hello world":

    #include <stdio.h>
    
    int main()
    {
       printf("hello world");
    
      return 0;
    }
    

    You will find that windows gui programming requires considerably more code to
    display to the screen, as conpared to console mode apps. Here is a simple
    windows gui "hello world" type app:

    #include <windows.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        static char szAppName[]="HelloWin";
        HWND        hwnd;
        MSG            msg;
        WNDCLASS    wndclass;
    
        if(!hPrevInstance)
        {
            wndclass.style    = CS_HREDRAW | CS_VREDRAW;
            wndclass.lpfnWndProc    = WndProc;
            wndclass.cbClsExtra    = 0;
            wndclass.cbWndExtra    = 0;
            wndclass.hInstance    = hInstance;
            wndclass.hIcon    = LoadIcon(NULL, IDI_APPLICATION);
            wndclass.hCursor    = LoadCursor(NULL, IDC_ARROW);
            wndclass.hbrBackground    = (HBRUSH) GetStockObject(WHITE_BRUSH);
            wndclass.lpszMenuName    = NULL;
            wndclass.lpszClassName    = szAppName;
    
            RegisterClass(&wndclass);
        }
    
        hwnd = CreateWindow(szAppName, "The Hello Program",
                            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
                            CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    
        ShowWindow(hwnd,nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    } /* ------- end WinMain ------- */
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC    hdc;
        PAINTSTRUCT    ps;
        RECT    rect;
    
        switch(message)
        {
            case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);
                GetClientRect(hwnd, &rect);
                DrawText(hdc, "Hello, Windows!", -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                EndPaint(hwnd, &ps);
                return 0;
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    } /*--------- end WndProc ---------*/
    

    You can find C/C++ GUI programming tutorials on the web.
    Simply Google: "C/C++"+"gui programming"
    include the quotes.
    I would recommend Charles Petzold's book: "Programming Windows (5th Edition)":
    http://www.amazon.com/Programming-Windows-Microsoft-Charles-Petzold/dp/157231
    995X/ref=sr_1_1?s=books&ie=UTF8&qid=1291133446&sr=1-1

    Get a new or "clean" used copy for cheap.
    Considered by many to be one of the best "Windows, GUI, C/C++" tutorials
    available.

    Steve

     
  • Steve A

    Steve A - 2010-11-30

    Well,
    that post came out totally screwed up !

     
  • Michael Jacob

    Michael Jacob - 2010-11-30

    Sarbayo,
    Thank you for the response.
    As a beginner I don't enough at the present time to ask the right questions.
    Since my post I have read more and practiced writing examples. ?How to I write
    the the code and have it execute in the client area of a window. Thank you for
    the example and directions to tutorials.
    Neferkamichael1

     
  • Michael Jacob

    Michael Jacob - 2010-11-30

    Sarbayo,
    I dontknow hwhat I did wrong. I copied your example to a source file and when
    I compile it I get a undefined reference to 'GetStockObject@4', Id returned 1
    exit status. Any advice.
    Thanks

     
  • Steve A

    Steve A - 2010-11-30

    Sorry for the delay getting back to you.
    The solution is quite simple.
    What is needed is to tell the linker where to find that function
    (GetStockObject()).
    In Dev-C++,
    1) click on the Projects tab,
    2) click "Project Options"
    3) click "Parameters"
    4) under "Linker", where it says: "Add Library or Object",
    click that button,
    5) scan across the list until you find "libgdi32.a"
    that is the library that contains GetStockObject.
    Double click on "libgdi32.a".
    6) that will place "libgdi32.a" in the linker list,
    7) click on the OK button to close that window,
    8) Under FILE, click on "Save All", (just for good measure),
    9) Under Execute, click on "Rebuild All"
    10) now, if there are no more errors, click on RUN.

    That should be it.

    Sometimes, how you create the project will determine what files you need to
    tell the linker about.
    Also, whether or not it's a C or a C++ project.

    Hope this helps,
    Steve

     
  • DARC

    DARC - 2011-06-14

    I'm not a true windows programmer, but I think you can use message boxes too.
    Unfortunately I dont know all the headers and files and syntax needed to do
    the job.

     

Log in to post a comment.