Click to See Complete Forum and Search --> : Help with code


Pitufo
August 19th, 2008, 12:47 PM
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.)

bitshifter420
August 19th, 2008, 03:09 PM
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.

Pitufo
August 20th, 2008, 09:56 AM
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

Paul McKenzie
August 20th, 2008, 10:26 AM
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 statusThis is a linker error not a compiler error. Your project settings for whatever IDE you're using must not be correct.

Regards,

Paul McKenzie

Pitufo
August 20th, 2008, 10:31 AM
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;
}

bitshifter420
August 20th, 2008, 12:03 PM
API Functions:
CreateFile
WriteFile
CloseHandle

psuedo Example:
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.

Pitufo
August 26th, 2008, 01:37 PM
What exactly do i put in the (...) because I tried different things and i just kept on getting errors?

bitshifter420
August 26th, 2008, 09:34 PM
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/library/aa365747(VS.85).aspx