Thread: OOP help

  1. #1
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24

    OOP help

    I am new to OOP with C and dont really understand it. Could anyone give an example of how do like add a button and use its functions to a windows app?
    --Seph

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    7
    C isn't an object oriented programming language. You're probably looking for C++. The closest thing that C has to an "object", and I use object very loosely, is a struct.

    MSVC++ will give you exactly what you're looking for (if I understood your question).

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I suggest you rethink your question, then repost in the correct forum, either C++ or Windows, but I guess it'll be latter.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    But I have seen programs with buttons and such written in C. I don't want to have to learn C++ just yet to do a windows app with a button. Heres and example: A program that has a button that says Bleh or something and when you press it, a message box comes up and says This is a message. If someone could show me how to do this then that would be great.
    --Seph

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So this is a Windows question then. I'll move the thread to the Windows forum.

    In the meantime, there are some tutorials listed here:
    http://faq.cprogramming.com/cgi-bin/...&id=1031248558
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Seph_31's Avatar
    Join Date
    Nov 2003
    Posts
    24
    Also I was wondering if any1 could post some C code that would be a small windows app with a list box and it pings something then displays the results in the list box
    --Seph

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Even a basic window is fairly involved. You must set up your wndclass structure, register the window, create the window, show/update the window enter your messaging loop and define you callback procedure.


    Here is the source code from Dev C++ template to do just that.
    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    I suggest researching the subject more before asking people to write more programs for you. This should give you an idea of whats involved in windows programming. A button can be considered a child window that you must create, but I am not going to do that for you. (I'm a bit lazy!)

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Ken.com
    C isn't an object oriented programming language. You're probably looking for C++. The closest thing that C has to an "object", and I use object very loosely, is a struct.

    MSVC++ will give you exactly what you're looking for (if I understood your question).
    Not technically true. C can do OOP just as well as C++, just not as easily or in the same way. C OOP involves the use of handles, and methods that take handles as their first parameters. You can even do inheritance and polymorphism using your own vtables, but multiple inheritance is tricky at best.

    The WinAPI is, from a technical point of view, object-oriented, even though it never uses C++ classes.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP flu
    By Hussain Hani in forum General Discussions
    Replies: 15
    Last Post: 06-27-2009, 02:02 AM
  2. Should OOP be any new language priority??
    By Hussain Hani in forum General Discussions
    Replies: 80
    Last Post: 06-13-2009, 10:56 AM
  3. Data Mapping and Moving Relationships
    By Mario F. in forum Tech Board
    Replies: 7
    Last Post: 12-14-2006, 10:32 AM
  4. recommendation for a good OOP book
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2006, 04:28 PM
  5. OOP Theory Question
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2005, 08:37 AM