Dimensionen von Konsolenfenster ermitteln



  • Der Titel sagt's eigentlich schon: Wie kann ich herausfinden wie gross mein Konsolenfenster in Pixeln ist?



  • Was das Problem auch lösen würde: Wie bekommt man heraus, wie hoch und breit ein Zeichen in der Konsole ist-Daraus könnte ich die Dimensionen des Fensters dann ja berechnen.
    Übrigend: Diese Werte kann man, wenn das Fenster geöffnet ist, über Eigenschaften herausfinden, weshalb man sie wohl auch per Code kriegen müsste...

    Der Grund ist übrigens der, dass ich gerade versuche in der Konsole ein paar Pixel zu zeichnen:

    main.cpp:

    #include <iostream>
    #include "cmg.h"
    
    int main(void)
    {
    	cmg::graphics gra("Testprogram - cmg");
    	//Diagonale durch Fenster zeichnen
    	for(short cnt=0;cnt<=100;cnt++)
    		gra.set_pixel(cmg::cord(cnt,cnt),cmg::rgb_color(255,255,255));
    	std::cin.get();
    }
    

    cmg.h:

    //* cmg.h *//
    
    #include <windows.h>
    #include <string>
    
    #ifndef ___CMG___
    #define ___CMG___
    
    namespace cmg{
    	typedef struct cord_struct{
    		long x;
    		long y;
    
    		cord_struct(void){
    			this->x=0;
    			this->y=0;
    		}
    
    		cord_struct(long _x,long _y){
    			this->x=_x;
    			this->y=_y;
    		}
    	}cord;
    
    	typedef struct rgb_color_struct{
    		unsigned char red;
    		unsigned char green;
    		unsigned char blue;
    
    		rgb_color_struct(void){
    			this->red=0;
    			this->green=0;
    			this->blue=0;
    		}
    
    		rgb_color_struct(unsigned char _red,unsigned char _green,unsigned char _blue){
    			this->red=_red;
    			this->green=_green;
    			this->blue=_blue;
    		}
    	}rgb_color;
    
    	class graphics{
    	protected:
    		HWND console_handle;
    		std::string console_title;
    	public:
    		graphics(std::string _console_title){
    			this->console_title=_console_title;
    			SetConsoleTitleA(this->console_title.c_str());
    			this->console_handle=FindWindowA(NULL,this->console_title.c_str());
    		}
    		void set_pixel(cord _cord,rgb_color _color){
    			if(((_cord.x>=0)&&(_cord.x<=100))&&((_cord.y>=0)&&(_cord.y<=100))){
    				CONSOLE_SCREEN_BUFFER_INFO info_buffer;
    				GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&info_buffer);
    				cord real_cord(
    						_cord.x*((info_buffer.srWindow.Right+1)*8-1)/100,	//8  -> Zeichenbreite in Pixel
    						_cord.y*((info_buffer.srWindow.Bottom+1)*12-1)/100	//12 -> Zeichenhoehe in Pixel
    					);														//Der rechte untere Ecken soll
    				HDC device_context=GetDC(this->console_handle);				//die Koordinaten <x=100|y=100> haben
    				SetPixel(
    						device_context,
    						real_cord.x,
    						real_cord.y,
    						RGB(_color.red,_color.green,_color.blue)
    					);
    				ReleaseDC(this->console_handle,device_context);
    				DeleteDC(device_context);
    			}
    		}
    	};
    }
    
    #endif
    


  • Ich kenne mich mit der Konsole zwar nicht so gut aus, aber geht das nicht auch einfach über GetClientRect?



  • _matze schrieb:

    Ich kenne mich mit der Konsole zwar nicht so gut aus, aber geht das nicht auch einfach über GetClientRect?

    Danke dir für den Tip!
    Eine Sackgasse war es übrigens auch, für die Koordinaten UChar-Werte zu verwenden. - Da passt nun einmal nichts höheres als 255 rein...

    So sieht's momentan aus (und funktioniert auch 😃 ):

    //* main.cpp *//
    #include <iostream>
    #include "cmg.h"
    
    int main(void)
    {
    	cmg::graphics gra("Testprogram - cmg");
    	cmg::rgb_color pixel_color(255,255,255);
    	cmg::coord pixel_coords;
    	gra.get_windowdimensions(&pixel_coords.x,&pixel_coords.y);
    	gra.set_pixel(pixel_coords,pixel_color);
    	std::cin.get();
    }
    
    //* cmg.h *//
    
    #include <windows.h>
    #include <string>
    
    #ifndef ___CMG___
    #define ___CMG___
    
    namespace cmg{
    	//Data-Structures
    	typedef struct coord_struct{
    		unsigned long x;
    		unsigned long y;
    		coord_struct(void){
    			this->x=0;
    			this->y=0;
    		}
    		coord_struct(unsigned long _x,unsigned long _y){
    			this->x=_x;
    			this->y=_y;
    		}
    	}coord;
    
    	typedef struct rgb_color_struct{
    		unsigned char red;
    		unsigned char green;
    		unsigned char blue;
    		rgb_color_struct(void){
    			this->red=0;
    			this->green=0;
    			this->blue=0;
    		}
    		rgb_color_struct(unsigned char _red,unsigned char _green,unsigned char _blue){
    			this->red=_red;
    			this->green=_green;
    			this->blue=_blue;
    		}
    	}rgb_color;
    
    	//Class-Prototype
    	class graphics{
    	protected:
    		HWND console_handle;
    		std::string console_title;
    	public:
    		graphics(std::string _console_title);
    		void get_windowdimensions(unsigned long *_width,unsigned long *_height);
    		void set_pixel(coord _coords,rgb_color _color);
    	};
    
    	//Methodes
    	graphics::graphics(std::string _console_title){
    			this->console_title=_console_title;
    			SetConsoleTitleA(this->console_title.c_str());
    			this->console_handle=FindWindowA(NULL,this->console_title.c_str());
    	}
    
    	void graphics::get_windowdimensions(unsigned long *_width,unsigned long *_height){
    			RECT window_dimensions;
    			GetClientRect(this->console_handle,&window_dimensions);
    			*_height=window_dimensions.bottom-1;
    			*_width=window_dimensions.right-1;
    	}
    
    	void graphics::set_pixel(coord _coords,rgb_color _color){
    				HDC device_context=GetDC(this->console_handle);
    				SetPixel(
    						device_context,
    						_coords.x,
    						_coords.y,
    						RGB(_color.red,_color.green,_color.blue)
    					);
    				ReleaseDC(this->console_handle,device_context);
    				DeleteDC(device_context);
    	}
    }
    
    #endif
    


  • _matze schrieb:

    Ich kenne mich mit der Konsole zwar nicht so gut aus, aber geht das nicht auch einfach über GetClientRect?

    ja, kann sein.
    ich tipp das auch gerade, ich machs mit GetWindowRect



  • deine pixelfunktion ist ineffektiv, jedes mal getdc releasedc -> nix guuut.
    programm-beginn: getdc
    programm-ende: releasedc 😉



  • Es ist grundsätzlich eine völlig verkehre Idee in der Konsole mit Grafik zu operieren, wo es doch extra dafür Fenster gibt...
    Das Ganze ist mehr so eine Laune und ergo ist es mir auch völlig egal wenn's uneffektiver Spaghetti-Code ist was ich schreibe.



  • ja schrieb:

    _matze schrieb:

    Ich kenne mich mit der Konsole zwar nicht so gut aus, aber geht das nicht auch einfach über GetClientRect?

    ja, kann sein.
    ich tipp das auch gerade, ich machs mit GetWindowRect

    Da bekommst du, denke ich, die Koordinaten des gesammten Fensters und nicht nur die des Bereichs ohne die Titelleiste und was sonst noch zu einem Fenser gehört.

    Mit GetClientRect() geht's sehr einfach.



  • romo schrieb:

    Mit GetClientRect() geht's sehr einfach.

    OK IST IN ORDNUNG WEITER MACHEN


Anmelden zum Antworten