CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Window Error

  1. #1
    Join Date
    Dec 2008
    Posts
    87

    Window Error

    I've been trying to follow a book to get me started on windows programming with C++ and i have an error message when trying to make a blank window:

    CXX0030: Error: expression cannot be evaluated

    I've looked on msdn and the description there didn't help me fix my problem. Here is my source code, any help is appreciated

    EDIT: forgot to mention this error occurs on hwnd

    #include <windows.h>

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    static TCHAR szAppName[] = TEXT("Hello Word");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW || CS_VREDRAW;
    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;

    if(!RegisterClass (&wndclass)) {
    MessageBox (NULL, TEXT("This Program Requires Windows"), szAppName, MB_ICONERROR);
    return 0;
    }
    hwnd = CreateWindow (szAppName, TEXT ("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    }

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Smile Re: Window Error

    Your program so far is fine except that it is incomplete.
    You must define the WndProc function.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Dec 2008
    Posts
    87

    Re: Window Error

    Ah ok cool thanks . I've been trying to run it half way through to make sure that it's all working so far and didn't really realise everything has to be put in. Thanks for the help

  4. #4
    Join Date
    Dec 2008
    Posts
    87

    Re: Window Error

    I'm still getting this error even though i have what i thought was completed code to create a window. Here is the code i now have:


    #include <windows.h>

    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
    static TCHAR szAppName[] = TEXT("Hello Word");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;

    wndclass.style = CS_HREDRAW || CS_VREDRAW;
    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;

    if(!RegisterClass (&wndclass)) {
    MessageBox (NULL, TEXT("This Program Requires Windows"), szAppName, MB_ICONERROR);
    return 0;
    }
    hwnd = CreateWindow (szAppName, TEXT ("The Hello Program"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nShowCmd);
    UpdateWindow(hwnd);
    }

    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    hdc = BeginPaint (hwnd, &ps);
    GetClientRect (hwnd, &rect);
    EndPaint(hwnd, &ps);

    return DefWindowProc (hwnd, message, wparam, lparam);
    }

  5. #5
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Window Error

    this line:
    Code:
    wndclass.style = CS_HREDRAW || CS_VREDRAW;
    Should be:
    Code:
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    You have the logical OR you need the binary OR to combine style flags.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  6. #6
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Smile Re: Window Error

    This line is missing

    Code:
    wndclass.lpfnWndProc = WndProc;
    Also, your WndProc function is not proper.
    It should have a switch statement to check the message parameter

    Code:
    switch (message)
    {
       case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps);
          GetClientRect (hwnd, &rect);
          EndPaint(hwnd, &ps);
    
          break;
    
       default:
          return DefWindowProc (hwnd, message, wparam, lparam);
    }
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  7. #7
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Window Error

    Well you don't need to switch the message in WndProc, you're application will still run, since DefWindowProc will handle all the messages in Window's default way, but if you don't handle WM_DESTROY with a call to PostQuitMessage in there you're process will keep running even after you press the close button.

    Also you have no message pump.

    in WinMain you need:
    Code:
    MSG msg;
    ...WNDCLASSEX and window creation here...
    BOOL bError;
    while ((bError = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bError == -1)
        {
           // There was an error, handle it
        }
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    GetMessage will get the the messages in you application's queue and then TranslateMessage and DispatchMessage will translate the message(if translating is needed) and send the message to your WndProc, respectively.

    and once you close at the end of WinMain you should return the wParam of msg.
    Code:
    return static_cast<int>(msg.wParam);
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

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

Featured