Mauskoordinaten im Fenster ausgeben



  • D.h., es verändert sich gar nichts 😕



  • vertikal geht meine Anzeige von 0 - 990 und beginnt nochmal von 0 auf 990 und dann nochmal.
    Das erste mal entsprich etwa 20 Pixel,
    dass zweite mal vieleicht 80 pixel,
    und das letzte mal gehts von 80 pixel runter auf den rand..

    horizontal das selbe...

    EDIT:
    Das ist ja interessant, ich hab den Button in ein Funktion gepackt und jetzt läuft es.

    void button(HWND hWnd, HWND hButton, int x, int y)
    {
    	hButton = CreateWindow("Button", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    						80, 150, x, y, hWnd, (HMENU) 1, hInstGlobal, NULL);
    


  • Ok, ich werd verarscht jetzt gehts wieder nicht!! 😡
    Komisch, Ich möchte gern ein prog. das jeh nach mauspos. im Fenster einen Button in der Grösse ändert.
    Das hab ich jetzt:

    #include<windows.h>
    #include<string.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    void groesse(int xPos, int y);
    
    void button(HWND hWnd, HWND hButton, int x, int y);
    
    HINSTANCE hInstGlobal;
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	const char szT[] = "Steuerelemente";
    
    	hInstGlobal = hInstance;
    
    	WNDCLASS wc;
    
    	HWND hWnd;
    
    	MSG msg;
    
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = "WinProg";
    	wc.lpszMenuName = 0;
    	wc.style = 0;
    
    	RegisterClass(&wc);
    
    	hWnd = CreateWindow("WinProg", szT, WS_OVERLAPPEDWINDOW, 0, 0, 330, 400, NULL, NULL, hInstance, NULL);
    
    	ShowWindow(hWnd, nCmdShow);
    
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&msg, NULL, 0, 0))
    	{
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    		static int xPos = 0; 
    		static int yPos = 0;
    
    		static int x = 160;
    		static int y = 40;
    
    		char xPosc[5] = {0};
    		char yPosc[5] = {0};
    
    		static HWND hButton;
    
    	switch (message)
    	{
    
    	case WM_CREATE:
    
    		return 0;
    
    	case WM_MOUSEMOVE:
    		xPos = LOWORD(lParam);  // horizontal position of cursor 
    		yPos = HIWORD(lParam);  // vertical position of cursor 
    
    		groesse(xPos, y);
    
    		InvalidateRect(hWnd, NULL, NULL);
    		UpdateWindow(hWnd);
    		return 0;
    
    	case WM_PAINT:
    
    		button(hWnd, hButton, x, y);
    
    		HDC hdc;
    		PAINTSTRUCT ps;
    
    		wsprintf(xPosc, "%i", xPos); 
    		wsprintf(yPosc, "%i", yPos);
    
    		hdc = BeginPaint(hWnd, &ps);
    
    		TextOut(hdc, 10, 20, "xPos:", 5);
    		TextOut(hdc, 10, 40, "yPos:", 5);
    		TextOut(hdc, 50, 20, xPosc, strlen(xPosc));
    		TextOut(hdc, 50, 40, yPosc, strlen(yPosc));
    
    		EndPaint(hWnd, &ps);
    
    		return 0;
    
    	case WM_DESTROY:
    
    		PostQuitMessage(0);
    
    		return 0;
    
    	default:
    
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    }
    
    void button(HWND hWnd, HWND hButton, int x, int y)
    {
    	hButton = CreateWindow("Button", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    						80, 150, x, y, hWnd, (HMENU) 1, hInstGlobal, NULL);
    
    }
    
    void groesse(int xPos, int y)
    {
    
    	if(xPos > 50)
    		y = 100;
    	else
    		y = 200;
    
    }
    

    aber irgendwie klappt nix



  • 😮 Du erstellst ja jedesmal bei WM_PAINT einen neuen Button 😮
    und deine groesse Funktion ist auch nicht sehr sinnvoll! - schonmal was von Pointern * und Referenzen & gehört?
    Wenn nicht würde ich dir dringend raten, die erstmal noch ein wenig mit C/C++ auseinanderzusetzen 😉

    Zum Verschieben: MoveWindow oder SetWindowPos



  • Ach schei.. diese blöden Variablen kopien!!
    Denke ich werds mit ner Ref lösen.

    Aber wie würdest du den den Button aktualisieren?



  • Was meinst du mit aktualisieren? Neu zeichnen lassen? -> InvalidateRect. Verschieben hab ich ja schon geschrieben.



  • irgendiwe klappt's nicht.
    hab jetzt damit begonnen sinlos Updates zu machen aber es will nicht:

    #include<windows.h>
    #include<string.h>
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    void groesse(int *yPos, int *y);
    
    void button(HWND hWnd, HWND hButton, int x, int y);
    
    void move(int *laenge, int *breite, HWND hWnd);
    
    HINSTANCE hInstGlobal;
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	const char szT[] = "Steuerelemente";
    
    	hInstGlobal = hInstance;
    
    	WNDCLASS wc;
    
    	HWND hWnd;
    
    	MSG msg;
    
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = "WinProg";
    	wc.lpszMenuName = 0;
    	wc.style = 0;
    
    	RegisterClass(&wc);
    
    	hWnd = CreateWindow("WinProg", szT, WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInstance, NULL);
    
    	ShowWindow(hWnd, nCmdShow);
    
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&msg, NULL, 0, 0))
    	{
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    		static int xPos = 0; 
    		static int yPos = 0;
    
    		static int x;
    		static int y;
    
    		char xPosc[5] = {0};
    		char yPosc[5] = {0};
    
    		static int yy = 150;
    		static int xx = 80;
    
    		HWND hButton;
    
    	switch (message)
    	{
    
    	case WM_CREATE:
    		x = 140;
    		y = 20;
    		button(hWnd, hButton, x, y);
    		return 0;
    
    	case WM_MOUSEMOVE:
    		xPos = LOWORD(lParam);  // horizontal position of cursor 
    		yPos = HIWORD(lParam);  // vertical position of cursor 
    
    		groesse(&yPos, &y);
    
    		InvalidateRect(hWnd, NULL, NULL);
    		InvalidateRect(hButton, NULL, NULL);
    
    		return 0;
    
    	case WM_PAINT:
    
    		HDC hdc;
    		PAINTSTRUCT ps;
    
    		wsprintf(xPosc, "%i", xPos); 
    		wsprintf(yPosc, "%i", yPos);
    
    		hdc = BeginPaint(hWnd, &ps);
    
    		TextOut(hdc, 10, 20, "xPos:", 5);
    		TextOut(hdc, 10, 40, "yPos:", 5);
    		TextOut(hdc, 50, 20, xPosc, strlen(xPosc));
    		TextOut(hdc, 50, 40, yPosc, strlen(yPosc));
    
    		EndPaint(hWnd, &ps);
    
    		move(&xx, &yy, hButton);
    		ShowWindow(hButton, NULL);
    		UpdateWindow(hButton);
    
    		return 0;
    
    	case WM_DESTROY:
    
    		PostQuitMessage(0);
    
    		return 0;
    
    	default:
    
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    }
    
    void move(int *laenge, int *breite, HWND hButton)
    {
    	MoveWindow(hButton, 80, 150, *laenge, *breite, FALSE);
    }
    
    void groesse(int *yPos, int *y)
    {
    
    	if(*yPos > 300)
    	{*y = 150;}
    	else
    	{*y = 20;}
    
    }
    
    void button(HWND hWnd, HWND hButton, int x, int y)
    {
    	hButton = CreateWindow("Button", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    						80, 150, x, y, hWnd, (HMENU) 1, hInstGlobal, NULL);
    
    }
    

    yy und xx hab ich um die Movefunk zu Testen aber sie möchte anscheinend nicht recht 😞



  • Du hast auch ein paar Fehler im Code! (z.B. bei hButton oder bei InvalidateRect)
    Was soll denn das Programm im Endeffekt machen?



  • Wenn's dann fertig ist soll sich die Button grösse zur Mausposition ändern

    y-Mausposition == 100 --> Button 20 Pixel hoch
    y-Mausposition == 200 --> Button 40 Pixel hoch

    kannst du mir mit den Fehlern helfen?



  • Ist auch alles noch sehr unsauber bzw. unschön, aber meinst du irgendwie so:

    #include<windows.h> 
    #include<string.h> 
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 
    
    void groesse(int yPos, int &y); 
    
    HWND button(HWND hWnd, int x, int y); 
    
    void move(int laenge, int breite, HWND hWnd); 
    
    HINSTANCE hInstGlobal; 
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
    { 
    		const char szT[] = "Steuerelemente"; 
    
        hInstGlobal = hInstance; 
    
        WNDCLASS wc; 
    
        HWND hWnd; 
    
        MSG msg; 
    
        wc.cbClsExtra = 0; 
        wc.cbWndExtra = 0; 
        wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
        wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
        wc.hInstance = hInstance; 
        wc.lpfnWndProc = WndProc; 
        wc.lpszClassName = "WinProg"; 
        wc.lpszMenuName = 0; 
        wc.style = 0; 
    
        RegisterClass(&wc); 
    
        hWnd = CreateWindow("WinProg", szT, WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hInstance, NULL); 
    
        ShowWindow(hWnd, nCmdShow); 
    
        UpdateWindow(hWnd); 
    
        while(GetMessage(&msg, NULL, 0, 0)) 
        { 
            DispatchMessage(&msg); 
        } 
    
        return msg.wParam; 
    } 
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
    { 
            static int xPos = 0;  
            static int yPos = 0; 
    
            static int x; 
            static int y; 
    
            char xPosc[5] = {0}; 
            char yPosc[5] = {0}; 
    
            static int yy = 150; 
            static int xx = 80; 
    
            static HWND hButton; 
    
        switch (message) 
        { 
    
        case WM_CREATE: 
            x = 140; 
            y = 20; 
            hButton = button(hWnd, x, y); 
            ShowWindow(hButton, TRUE); 
            return 0; 
    
        case WM_MOUSEMOVE: 
            xPos = LOWORD(lParam);  // horizontal position of cursor  
            yPos = HIWORD(lParam);  // vertical position of cursor  
    
            move(xPos/10, yPos/10, hButton); 
           //groesse(yPos, y); 
    
            InvalidateRect(hWnd,NULL, TRUE); 
            //InvalidateRect(hButton, NULL, NULL); 
    
            return 0; 
    
        case WM_PAINT: 
    
            HDC hdc; 
            PAINTSTRUCT ps; 
    
    		wsprintf(xPosc, "%i", xPos);  
            wsprintf(yPosc, "%i", yPos); 
    
            hdc = BeginPaint(hWnd, &ps); 
    
            TextOut(hdc, 10, 20, "xPos:", 5); 
            TextOut(hdc, 10, 40, "yPos:", 5); 
            TextOut(hdc, 50, 20, xPosc, strlen(xPosc)); 
            TextOut(hdc, 50, 40, yPosc, strlen(yPosc)); 
    
            EndPaint(hWnd, &ps); 
    
            return 0; 
    
        case WM_DESTROY: 
    
            PostQuitMessage(0); 
    
            return 0; 
    
        default: 
    
            return DefWindowProc(hWnd, message, wParam, lParam); 
        } 
    } 
    
    void move(int laenge, int breite, HWND hButton) 
    { 
        MoveWindow(hButton, 80, 150, laenge, breite, FALSE); 
    } 
    
    void groesse(int yPos, int &y) 
    { 
    
        if(yPos > 300) 
        {y = 150;} 
        else 
        {y = 20;} 
    
    } 
    
    HWND button(HWND hWnd, int x, int y) 
    { 
        return CreateWindow("Button", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,  
                            80, 150, x, y, hWnd, (HMENU) 1, hInstGlobal, NULL); 
    
    }
    


  • Im Prinzip schon, danke
    Gute lösung mit der Button Funktion (Return)

    Bei Mausbewegung erklärst du ja das Hauptfenster für ungültig(hWnd).
    Wieso nicht den Button?



  • Wozu denn? Es geht doch eigentlich hauptäschlich darum, dass die Maus-Position neu ins Fenster geschrieben werden muss 😉



  • was soll die move() funktion???



  • es geht um die variable grösse des buttons, die koordinaten hab ich nur um die LO- HiWORD Makros zu testen



  • Dann kannst du das InvalidateRect ganz weglassen und bei MoveWindow für den letzten Parameter TRUE einsetzen 😉



  • Is ja klasse, mein "fast" eigenes Winapiprog ist (schonwieder) "fast" fertig.

    Hab mal was über buffer gelesen, mal gucken ob ich das finde


Anmelden zum Antworten