Results 1 to 12 of 12

Thread: C++ and Windows?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I'm currently learning C++ (tech yourself C++ in 21 days) and it's going quite well!! I am actually suprised (and quite happy!!!) to find that my VB experience is helping me out as far as understading C++, despite the fact that most people on these forums seem to think that VB knowledge will not help you in learning C++.

    Anyway, back to the question..

    Because this book is strickly C++ (not Visual C++) it does not go over the subject of Win32 and C++. So I just wanted to find out from you experts out there what I have to look forward to after I master C++ and then move to C++ and Win32. Is it just a matter of calling Win32 API functions like you would in VB and just tie it in with the C++ code?

    Any help would be appreciated.. In fact, if you could give me a CPP file that creates a form with a command button on it that pops up a message box in Visual C++, I would greatly appreciate it!!

    Thanks,

    Dan

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Here ya go:
    Code:
    #include <windows.h>
    
    HWND ghWnd_Main;
    HWND ghWnd_ButtonA;
    HWND ghWnd_ButtonB;
    
    LONG_PTR CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        switch(uMsg) {
            case WM_CLOSE:
                DestroyWindow(ghWnd_Main); // Destroy the main window
                return 0;
    
            case WM_DESTROY:
                PostQuitMessage(0); // Quit the application
                return 0;
    
            case WM_COMMAND:
                if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_ButtonA) {
                    MessageBox(hWnd, "Button A clicked!", "Button", MB_OK);
                }
                if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == ghWnd_ButtonB) {
                    MessageBox(hWnd, "Button B clicked!", "Button", MB_OK);
                }
                return 0;
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        WNDCLASSEX wcxMyClass;
    
        wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
        wcxMyClass.style = 0; // Extra style information
        wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
        wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
        wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
        wcxMyClass.hInstance = hInstance; // Instance handle for the class
        wcxMyClass.hIcon = NULL; // Default icon - use system
        wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
        wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
        wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
        wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
        wcxMyClass.hIconSm = NULL; // Small icon
        
        RegisterClassEx(&wcxMyClass); // Register the class
    
        ghWnd_Main = CreateWindow("TESTCLASS", "My test window", \
                                  WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, \
                                  NULL, NULL, hInstance, NULL);
        if(ghWnd_Main) {
            ghWnd_ButtonA = CreateWindow("Button", "Button A", \
                                      WS_CHILD | WS_VISIBLE, \
                                      50, 50, 200, 40, ghWnd_Main, NULL, hInstance, NULL);
            ghWnd_ButtonB = CreateWindow("Button", "Button B", \
                                      WS_CHILD | WS_VISIBLE, \
                                      280, 50, 200, 40, ghWnd_Main, NULL, hInstance, NULL);
            ShowWindow(ghWnd_Main, nCmdShow); // Show using the specific show style
        }
    
        /////////////////////////
        // Handle message loop //
        /////////////////////////
        MSG msg;
        while(GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg); // Translate keycode messages
            DispatchMessage(&msg); // Send to the window
        }
    
        UnregisterClass("TESTCLASS", hInstance); // Unregister our window class
        return msg.wParam; // msg.wParam contains the code passed to PostQuitMessage()
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks, but I think I'm having a problem here.. Is there any special setup required in my VC++ environment or can I just past your code into a .cpp file and compile it?

    I started a blank project and added a .cpp file and pasted your code into it.. I then tried to build it and it generated 3 errors:


    --------------------Configuration: Cpp1 - Win32 Debug--------------------
    Compiling...
    Cpp1.cpp
    c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(7) : error C2143: syntax error : missing ';' before '__stdcall'
    c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(7) : error C2501: 'LONG_PTR' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\test1\cpp1.cpp(34) : error C2440: '=' : cannot convert from 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long
    )'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    Cpp1.exe - 3 error(s), 0 warning(s)
    any ideas what this is?

    Thanks,

    Dan


  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oops...change LONG_PTR to LRESULT (I was using the latest version of the headers)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Welp, I think we're getting closer, it's down to 2 errors now..

    --------------------Configuration: Cpp3 - Win32 Debug--------------------
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/Cpp3.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Cpp3.exe - 2 error(s), 0 warning(s)
    Any ideas?

    It compiles okay, but gives the above errors when trying to build..

    thanks,

    dan

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Technically, that's only one error...but...

    The problem is not with the source. You didn't use the correct project type - it must be Win32 Application. (Not Console or MFC). Windows programs have WinMain as their entry-point, as opposed to _main.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Okay, I created a Win32 Application but now it's saying "Cannot execute program" when I try to run it..

    Do I need to add anything else to the project in order to run it? I just created a win32 app and pasted your code right into a .cpp file..

    Thanks,

    Dan

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Thanks! Works like a charm.. Don't know why mine didn't work.. Ohwell..

    Dan

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Also look at the Platform SDK at http://msdn.microsoft.com/library for loads of information on programming for Win32.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Guest
    To narrow it down, you can go to the Visual C++ library which is info for using VC++.

  12. #12
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    sometime i find that if i compile and get an error then change something i get an error to do with linking, if so goto Build on the menu then click clean and then compile and it should work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width