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

Thread: Help with code

  1. #1
    Join Date
    Jul 2008
    Posts
    22

    Help with code

    ok so I have this code

    #include <windows.h>
    #include <winuser.h>
    #include <iomanip>
    #include <iostream>


    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_LBUTTONDOWN:
    SetWindowText(hwndStatic, "You clicked the left mouse button!");
    break;
    case WM_RBUTTONDOWN:
    SetWindowText(hwndStatic, "You clicked the right mouse button!");
    break;
    }
    return 0;
    }

    And I get these errors:

    In function 'LRESULT WndProc(HWND_*,UINT,WPARAM,LPARAM)':
    'hwndStatic' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

  2. #2
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Help with code

    What its saying is that 'hwndStatic' has not been defined.

    Indeed, it does not exist in your code.

    If you mean to write the text into the window that was passed into the procedure, use 'hwnd, in place of 'hwndStatic'.

    This is obviously a cut and paste code sample...

    You should also call 'DefWindowProc' for unhandled messages.
    Last edited by bitshifter420; August 19th, 2008 at 03:11 PM.

  3. #3
    Join Date
    Jul 2008
    Posts
    22

    Re: Help with code

    What do you mean call 'DefWindowProc'?

    I fixed it this way but now i get these errors:

    #include <windows.h>
    #include <winuser.h>
    #include <iomanip>
    #include <iostream>

    //LRESULT DefWindowProc(HWND hwnd,UINT Msg,WPARAM wParam,LPARAM lParam);

    LRESULT CALLBACK WndProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)

    {
    switch(Msg)
    {
    case WM_LBUTTONDOWN:
    SetWindowText(hwnd, "You clicked the left mouse button!");
    break;
    case WM_RBUTTONDOWN:
    SetWindowText(hwnd, "You clicked the right mouse button!");
    break;
    }
    return 0;
    }

    [Linker error] undefined reference to 'WinMain@16'
    Id returned 1 exit status
    Last edited by Pitufo; August 20th, 2008 at 10:09 AM.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with code

    Quote Originally Posted by Pitufo
    What do you mean call 'DefWindowProc'?
    You always call DefWindowProc() for any unhandled Windows messages, otherwise your program will exhibit erratic behaviour.

    This should have been clearly documented in whatever reference you're using to create the code you created.
    I fixed it this way but now i get these errors:
    [Linker error] undefined reference to 'WinMain@16'
    Id returned 1 exit status
    This is a linker error not a compiler error. Your project settings for whatever IDE you're using must not be correct.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2008
    Posts
    22

    Re: Help with code

    Thanks I fixed it...The code that i have now changes the title when I click a mouse button...how can I make it send info to notepad...for example, if i click on the right button it will send a 1 to notepad or if i click on left button it will send a 2?

    #include <windows.h>
    #include <winuser.h>
    #include <iomanip>
    #include <iostream>


    const char g_szClassName[] = "myWindowClass";

    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_LBUTTONDOWN:
    SetWindowText(hwnd, "You clicked the left mouse button!");
    break;
    case WM_RBUTTONDOWN:
    SetWindowText(hwnd, "You clicked the right mouse button!");
    break;
    case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
    MessageBox(NULL, "Window Registration Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "The title of my window",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
    NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
    MessageBox(NULL, "Window Creation Failed!", "Error!",
    MB_ICONEXCLAMATION | MB_OK);
    return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    return Msg.wParam;
    }

  6. #6
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Help with code

    API Functions:
    CreateFile
    WriteFile
    CloseHandle

    psuedo Example:
    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       static HANDLE hFile = INVALID_HANDLE_VALUE;
    
       switch(msg)
       {
          case WM_CREATE:
          hFile = CreateFile(...);
          return 0;
    
          case WM_DESTROY:
          CloseHandle(hFile);
          PostQuitMessage(0);
          return 0;
    
          case WM_LBUTTONDOWN:
          WriteFile(...);
          return 0;
    
          case WM_RBUTTONDOWN:
          WriteFile(...);
          return 0;
    
          default:
          return DefWindowProc(hwnd, msg, wParam, lParam);
       }
    }
    You should also implement some error testing to verify the file handle is valid.
    Notice the file is opened when the window is created and closed when the window is destroyed.
    It would be wasteful to open the file write some data and close the file every time you clicked the mouse.

  7. #7
    Join Date
    Jul 2008
    Posts
    22

    Re: Help with code

    What exactly do i put in the (...) because I tried different things and i just kept on getting errors?

  8. #8
    Join Date
    Jun 2007
    Location
    MA-USA
    Posts
    247

    Re: Help with code

    You must never guess what parameters a function expects.
    If you are unsure of what to use, look it up at msdn or google.

    CreateFile
    http://msdn.microsoft.com/en-us/library/aa363858.aspx

    WriteFile
    http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx

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