Hi bit-pal ,
sono di nuovo qui, non perché sia pigro, ma perché quando non se ne viene a capo ....
Ambiente : CodeBlocks + minGW + Win32 + C++
Ho copiato il programmino sulla gestione della posizione del mouse che Rubik (generosamente) mi ha inviato.
Tutto OK finché resta file unico. Quando cerco di suddividerlo nei tre files canonici : .h .cpp main succede :
error : ld returned exit status
da ricerche fatte, sembra legato alla funzione main (). Di seguito i tre files fatti : .h .cpp main
#ifndef MY_H_INCLUDED
#define MY_H_INCLUDED
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
const char *Name = "Coordinate mouse";
const char *Prog = "Coordinate mouse";
const DWORD Win = WS_OVERLAPPEDWINDOW;
const WORD Wwin = 800;
const WORD Hwin = 600;
BOOL Register_Class ( HINSTANCE hIst );
BOOL Create_Window ( HINSTANCE hIst, int show );
void Show_PAINT ( HWND hWnd, char Text[20] );
void Error ( const char *msg, HWND hWnd );
#endif // MY_H_INCLUDED
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <My.h>
LRESULT CALLBACK WndProc ( HWND hWnd, UINT msg, WPARAM wPar, LPARAM lPar ) {
switch ( msg ) {
case WM_LBUTTONDOWN: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "L_BUTTON\0" ); break;
case WM_RBUTTONDOWN: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "R_BUTTON\0" ); break;
case WM_MBUTTONDOWN: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "M_BUTTON\0" ); break;
case WM_MOUSEWHEEL: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "WEEL\0" ); break;
case WM_KEYDOWN: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "KEYBOARD\0" ); break;
case WM_MOUSEMOVE: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT( hWnd, "MOVE\0" );
Sleep(50); break;
case WM_SIZE: InvalidateRect ( hWnd, NULL, TRUE );
Show_PAINT ( hWnd, "SIZE\0" );
Sleep(1); break;
case WM_CLOSE: PostQuitMessage ( 0 ); break;
default: return DefWindowProc ( hWnd, msg, wPar, lPar ); }
return 0; }
BOOL Register_Class ( HINSTANCE hIst ) {
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hIst;
wc.hCursor = LoadCursor ( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject ( GRAY_BRUSH );
wc.lpszClassName = Name;
return RegisterClass ( &wc ) != 0; }
BOOL Create_Window ( HINSTANCE hIst, int mostra ) {
RECT r = { 0,0,Wwin,Hwin };
AdjustWindowRect ( &r, Win, FALSE );
OffsetRect ( &r,-r.left+10,-r.top+10 );
HWND hWnd = CreateWindow (Name,Prog,Win,r.left,r.top,r.right-r.left,r.bottom-r.top,0,0,hIst,NULL );
if ( hWnd != NULL ) ShowWindow ( hWnd, mostra );
return hWnd != NULL; }
void Show_PAINT ( HWND hWnd, char Text[10] ) {
POINT pCoor;
GetCursorPos (&pCoor);
int x=pCoor.x;
int y=pCoor.y;
ScreenToClient (hWnd, &pCoor);
PAINTSTRUCT ps;
HDC hdc = BeginPaint ( hWnd, &ps );
RECT r;
char buff[128];
GetClientRect ( hWnd, &r );
wsprintf ( buff,"Finestra %d x %d\n\nPosWin %d x %d\n\nPosDesk %d x %d\n\n%s",r.right, r.bottom, pCoor.x, pCoor.y, x, y, Text );
InflateRect ( &r, -7, -7 );
DrawText ( hdc, buff, -1, &r, DT_EXPANDTABS );
EndPaint ( hWnd, &ps ); }
void Error ( const char *msg, HWND hWnd ) {MessageBox ( hWnd, msg, Prog, MB_ICONERROR ); }
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <My.h>
int WINAPI WinMain ( HINSTANCE hIst, HINSTANCE hPrevInst, LPSTR cmd, int show ) {
MSG msg = { 0 };
if ( !Register_Class( hIst ) ) { Error ( "Classe non registrata. ", NULL ); return 0; }
if ( !Create_Window( hIst, show ) ) { Error ( "Finestra non creata. ", NULL ); return 0; }
BOOL msgOK ;
while (( msgOK = GetMessage ( &msg, NULL,0,0 ) != 0 )) {
if ( msgOK == -1 ) { } // codice per gestire l’errore
else {
TranslateMessage(&msg);
DispatchMessage( &msg ); } }
return 0; }
Dove sbaglio ? Grazie