Fenstergröße



  • Abend

    Ich möchte von meinem Fenster die Koordinaten des Punktes haben, der links ganz untern ist und von dem, de rechts ganz oben ist.

    Aber nicht die vom Clientrekt. Kann mit jemadn bei der Ermittlung helfen?

    ich würde hierfür die POINT strukt verwenden wollen

    vielen Dank für schnelle Antworten



  • GetWindowRect() ?



  • Okay, ich kann mein Problem verdammt schlecht erklären...

    aber folgendes: Ich habe eine Window Klasse(in der ich die ganze WndProc aushebeln konnte!!!!:

    Folgendes Prob tritt auf: ich kann entweder kein Fullscreenmodus erzeugen, da die Korrdinaten ncihtmmehr stimmen, obwohl ich:

    win.create(L"window 1", L"window 1", WS_OVERLAPPEDWINDOW, false, 0, 0, 640, 480, 32);
    win.show();
    win.center();
    win.resize(0, 0, 800, 600);
    

    nur gemacht habe...

    oder da Fenster ändert seine Größe, wenn ich die center Funktion mehrmals hintereinader aufrufe...
    Kann mir jemand helfen bzw einen verbesserungsvorschlage geben? ich bin echt am verzweifeln...

    class Window
    {
    private:
    	::HWND__*					handle_;
    	bool						active_;
    	std::basic_string<wchar_t>	class_name_;
    	std::basic_string<wchar_t>	caption_;
    	int							bits_per_pel_;
    	bool						full_screen_mode_;
    	WindowRect					window_size_;
    
    private:
    	static _w64 long static_window_proc(::HWND__* hwnd, unsigned int msg, 
    									    unsigned int wParam, _w64 long lParam);
    
    	virtual _w64 long window_proc(unsigned int msg, unsigned int wParam, _w64 long lParam);
    
    protected:
    	// massage handling functions...
    	virtual void on_activate_app(::HWND__* hwnd, bool activate, unsigned long thread_id);
    	virtual void on_close		(::HWND__* hwnd);
    	virtual bool on_create		(::HWND__* hwnd, ::CREATESTRUCTW* create_struct);
    	virtual void on_destroy		(::HWND__* hwnd);
    
    public:
    	// constructor
    	Window(void);
    	// destructor
    	~Window(void);
    
    	// destroy the window
    	void destroy(void);
    
    	// create
    	bool create(const std::basic_string<wchar_t>&	class_name,
    				const std::basic_string<wchar_t>&	caption,
    				long								style,
    				bool								fullscreen,
    				int									x,
    				int									y,
    				int									width,
    				int									height,
    				int									bpp);
    
    	// show the window
    	void show(void);
    	// hide the window
    	void hide(void);
    	// center the window
    	void center(void);
    	// minimize the window
    	void minimize(void);
    	// maximize the window
    	void maximize(void);
    	// 
    	void restore_dimension(void);
    	// is window minimized?
    	bool is_minimized(void) const;
    	// is maximized?
    	bool is_maximized(void) const;
    	//
    	void resize(const int& x, const int& y, 
    				const int& width, const int& height);
    	// set full screen mode
    	bool change_to_full_screen(void);
    	// set windowed mode
    	void change_to_windowed_mode(void);
    	// 
    	bool is_full_screen_mode(void) const;
    	//
    	bool is_active(void) const;
    
    	std::basic_string<wchar_t> get_class_name	(void) const;
    	std::basic_string<wchar_t> get_caption		(void) const;
    
    	void set_caption(const std::basic_string<wchar_t>& caption);
    
    	void set_window_rect		(const WindowRect& rect);
    	void set_window_x			(const int& x);
    	void set_window_y			(const int& y);
    	void set_window_width		(const int& width);
    	void set_window_height		(const int& height);
    
    	WindowRect get_window_rect	(void) const;
    	int get_window_x			(void) const;
    	int get_window_y			(void) const;
    	int get_window_height		(void) const;
    	int get_window_width		(void) const;
    
    	void set_sytle(const unsigned long& style);
    	unsigned long get_style(void) const;
    
    	::HWND__* get_handle(void) const;
    
    	// void take_snapshoot(void);
    };
    
    _w64 long Window::static_window_proc(::HWND__* hwnd, unsigned int msg, 
    									 unsigned int wParam, _w64 long lParam)
    {
    // disable BOOL to bool cast warning
    #pragma warning(disable : 4311)
    #pragma warning(disable : 4312)
    
    	Window* window;
    
    	// don't ask me^^ but the follow stuff make your window independent from the wndProc!!!
    	if(msg == WM_CREATE)
    	{
    		window = static_cast<Window*>(((::LPCREATESTRUCT) lParam)->lpCreateParams);
    
    		::SetWindowLongPtrW(hwnd, GWL_USERDATA, reinterpret_cast<long>(window));
    	}
    	else
    	{
    		window = reinterpret_cast<Window*>(::GetWindowLongPtrW(hwnd, GWL_USERDATA));
    
    		if(!window)
    			return(::DefWindowProcW(hwnd, msg, wParam,lParam));
    
    	}
    
    // enable BOOL to bool cast warning
    #pragma warning(default : 4311)
    #pragma warning(default : 4312)
    
    	window->handle_ = hwnd;
    
    	return(window->window_proc(msg, wParam, lParam));
    }
    
    _w64 long Window::window_proc(unsigned int msg, unsigned int wParam, _w64 long lParam)
    {
    	if(msg == WM_SIZE || msg == WM_MOVE)
    	{
    		::RECT rect;
    		::GetClientRect(handle_, &rect);
    
    		window_size_.x_ = rect.left;
    		window_size_.y_ = rect.top;
    		window_size_.width_ = rect.right - rect.left;
    		window_size_.height_ = rect.bottom - rect.top;
    	}
    	/*
    	// window size was changed?
    	if(msg == WM_SIZE)
    	{
    		::RECT rect;
    		::GetWindowRect(handle_, &rect);
    		//::RECT rect = {0, 0, LOWORD(lParam), HIWORD(lParam)};
    		//::AdjustWindowRectEx(&rect, get_style(), false, 0);
    		cout << rect.left << " " << rect.right << " " << rect.top << " " << rect.bottom << "\n";
    		window_size_.width_  = rect.right  - rect.left;
    		window_size_.height_ = rect.bottom - rect.top;
    
    		LOG(LOG_INFO, L"size w: %d h: %d", window_size_.width_, window_size_.height_);
    	}
    	// window was moved?
    	else if(msg == WM_MOVE)
    	{
    		::RECT rect;
    		::GetWindowRect(handle_, &rect);
    
    		// --> should be correct
    		window_size_.x_ = rect.left;
    		window_size_.y_ = rect.top;
    
    		LOG(LOG_INFO, L"move x: %d y: %d", window_size_.x_, window_size_.y_);
    	}
    	*/
    
    	// just handle the windowX messages
    	switch(msg)
    	{
    #pragma warning(disable : 4800) 
    		HANDLE_MSG(handle_, WM_ACTIVATEAPP,		on_activate_app);
    		HANDLE_MSG(handle_, WM_CREATE,			on_create);
    		HANDLE_MSG(handle_, WM_CLOSE,			on_close);
    		HANDLE_MSG(handle_, WM_DESTROY,			on_destroy);
    #pragma warning(default : 4800) 
    	}
    
    	return(::DefWindowProcW(handle_, msg, wParam, lParam));
    }
    
    void Window::on_activate_app(::HWND__* hwnd, bool activate, unsigned long thread_id)
    {
    	active_ = activate;
    }
    
    void Window::on_close(::HWND__* hwnd)
    {
    	destroy();
    }
    
    bool Window::on_create(::HWND__* hwnd, ::CREATESTRUCTW* create_struct)
    {
    	return(true);
    }
    
    void Window::on_destroy(::HWND__* hwnd)
    {
    	::PostQuitMessage(0);
    }
    
    Window::Window(void) : handle_(0)
    {
    
    }
    
    Window::~Window(void)
    {	
    	if(handle_)
    		destroy();
    }
    
    bool Window::create(const std::basic_string<wchar_t>& class_name, 
    					const std::basic_string<wchar_t>& caption, 
    					long style, 
    					bool full_screen, 
    					int x, 
    					int y, 
    					int width, 
    					int height, 
    					int bpp)
    {
    	DEBUG_STACK;
    
    	class_name_				= class_name;
    	caption_				= caption;
    	bits_per_pel_			= bpp;
    
    	window_size_.x_			= x;
    	window_size_.y_			= y;
    	window_size_.width_		= width;
    	window_size_.height_	= height;
    
    	::WNDCLASSEXW window_class;
    	// zero WNDCLASSEX
    	::ZeroMemory(&window_class,		sizeof(::WNDCLASSEXW));
    
    	// initialize WNDCLASSEX
    	window_class.cbSize				= sizeof(WNDCLASSEX);
    	window_class.style				= CS_VREDRAW | CS_HREDRAW;
    	window_class.lpfnWndProc		= reinterpret_cast<::WNDPROC>(static_window_proc);
    	window_class.cbClsExtra			= 0;
    	window_class.cbWndExtra			= 0;
    	window_class.hInstance			= ::GetModuleHandleW(0);
    	window_class.hIcon				= ::LoadIcon	(0,		IDI_APPLICATION);
    	window_class.hCursor			= ::LoadCursor	(0,	IDC_ARROW);
    	window_class.hbrBackground		= static_cast<::HBRUSH__*>(::GetStockObject(0));
    	window_class.lpszMenuName		= 0;
    	window_class.lpszClassName		= class_name_.c_str();
    	window_class.hIconSm			= ::LoadIcon(0, IDI_APPLICATION);
    
    	// try to register the window class
    	if(!::RegisterClassEx(&window_class))
    	{
    		::MessageBoxW(0, L"Application requires Unicode support",
    			caption_.c_str(), MB_ICONERROR);
    
    		return(LOG(LOG_ERROR, L"Can't register window class!"));
    	}
    
    	// create a window
    	handle_ = ::CreateWindowExW(0,
    								class_name_.c_str(), 
    								caption_.c_str(), 
    								style, 
    								x, 
    								y, 
    								width, 
    								height, 
    								0, 
    								0, 
    								::GetModuleHandleW(0), 
    								this);
    
    	// window dosn't exist?
    	if(!handle_)
    		return(LOG(LOG_ERROR, L"Can't create window!"));
    
    	// full screen wanted?
    	if(full_screen)
    		change_to_full_screen();
    
    	LOG(LOG_INFO, L"window was successful created(wcn: %s)", class_name_.c_str());
    	return(true);
    }
    
    void Window::destroy(void)
    {
    	DEBUG_STACK;
    	LOG(LOG_INFO, L"destroy window(wcn: %s)", class_name_.c_str());
    
    	// change on windowed mode
    	if(full_screen_mode_)
    		::ChangeDisplaySettingsW(0 ,0);
    
    	if(handle_)
    	{
    		DestroyWindow(handle_);
    	}
    
    	handle_ = 0;
    
    	::UnregisterClassW(class_name_.c_str(), ::GetModuleHandleW(0));
    }
    
    void Window::show(void)
    {
    	::ShowWindow(handle_, SW_NORMAL);
    }
    
    void Window::hide(void)
    {
    	::ShowWindow(handle_, SW_HIDE);
    }
    
    void Window::center(void)
    {
    	if(!full_screen_mode_ || !handle_)
    		return;
    
    	// create a window rectangle
    	::RECT window_rect = {0, 0, window_size_.width_, window_size_.height_};
    
    	// get the true window size
    	::AdjustWindowRectEx(&window_rect, get_style(), false, 0);
    
    	// calculate the centered window position
    	int center_x = ::GetSystemMetrics(SM_CXFULLSCREEN)/2-(abs<long>(window_rect.left)+window_rect.right)/2;
    	int center_y = ::GetSystemMetrics(SM_CYFULLSCREEN)/2-(abs<long>(window_rect.top)+window_rect.bottom)/2;
    
    	// move window
    	::MoveWindow(handle_, center_x, center_y, window_rect.right - window_rect.left, 
    				 window_rect.bottom - window_rect.top, true);
    
    	LOG(LOG_INFO, L"center window(w: %d h: %d)", window_size_.width_, window_size_.height_);
    }
    
    void Window::minimize(void)
    {
    	::ShowWindow(handle_, SW_MINIMIZE);
    }
    
    void Window::maximize(void)
    {
    	::ShowWindow(handle_, SW_MAXIMIZE);
    }
    
    void Window::restore_dimension(void)
    {
    	::ShowWindow(handle_, SW_RESTORE);
    }
    
    bool Window::is_minimized(void) const
    {
    	return(::IsIconic(handle_) ? 1 : 0);
    }
    
    bool Window::is_maximized(void) const
    {
    	return(::IsZoomed(handle_) ? 1 : 0);
    }
    
    void Window::resize(const int& x, const int& y, const int& 
    					width, const int& height)
    {
    	::MoveWindow(handle_, x, y, width, height, true);
    }
    
    bool Window::change_to_full_screen(void)
    {
    	::DEVMODEW screenSetting;
    	::ZeroMemory(&screenSetting, sizeof(screenSetting));
    
    	// set screen settings
    	screenSetting.dmSize		= sizeof(::DEVMODEW);
    	screenSetting.dmPelsWidth	= window_size_.width_;
    	screenSetting.dmPelsHeight	= window_size_.height_;
    	screenSetting.dmBitsPerPel	= bits_per_pel_;
    	screenSetting.dmFields		= DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
    
    	// try to change into the full screen mode
    	if(::ChangeDisplaySettingsW(&screenSetting, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
    	{
    		full_screen_mode_ = true; 
    
    		LOG(LOG_INFO, L"Successful changed into the full screen mode");
    
    		return(true);
    	}
    
    	return(LOG(LOG_ERROR, L"Your video card dosn't support the full screen mode(w: %d h: %d bpp: %d)", 
    						  window_size_.width_, window_size_.height_, bits_per_pel_));
    }
    
    void Window::change_to_windowed_mode(void)
    {
    	::ChangeDisplaySettingsW(0, 0);
    
    	full_screen_mode_ = false;
    }
    
    bool Window::is_full_screen_mode(void) const
    {
    	 return(full_screen_mode_);
    }
    
    bool Window::is_active(void) const
    {
    	return(active_);
    }
    
    std::basic_string<wchar_t> Window::get_class_name(void) const
    {
    	return(class_name_);
    }
    
    std::basic_string<wchar_t> Window::get_caption(void) const
    {
    	return(caption_);
    }
    
    void Window::set_caption(const std::basic_string<wchar_t>& caption)
    {
    	caption_ = caption;
    }
    
    void Window::set_window_rect(const WindowRect& rect)
    {
    	resize(rect.x_, rect.y_, rect.width_, rect.width_);
    }
    
    void Window::set_window_x(const int& x)
    {
    	resize(x, window_size_.y_, window_size_.width_, window_size_.height_);
    }
    
    void Window::set_window_y(const int& y)
    {
    	resize(window_size_.x_, y, window_size_.width_, window_size_.height_);
    }
    
    void Window::set_window_width(const int& width)
    {
    	resize(window_size_.x_, window_size_.y_, width, window_size_.height_);
    }
    
    void Window::set_window_height(const int& height)
    {
    	resize(window_size_.x_, window_size_.y_, window_size_.width_, height);
    }
    
    WindowRect Window::get_window_rect(void) const
    {
    	return(window_size_);
    }
    
    int Window::get_window_x(void) const
    {
    	return(window_size_.x_);
    }
    
    int Window::get_window_y(void) const
    {
    	return(window_size_.y_);
    }
    
    int Window::get_window_width(void) const
    {
    	return(window_size_.width_);
    }
    
    int Window::get_window_height(void) const
    {
    	return(window_size_.height_);
    }
    
    void Window::set_sytle(const unsigned long& style)
    {
    	::SetWindowLongW(handle_, GWL_STYLE, style);
    }
    
    unsigned long Window::get_style(void) const
    {
    	return(::GetWindowLongW(handle_, GWL_STYLE));
    }
    
    ::HWND__* Window::get_handle(void) const
    {
    	return(handle_);
    }
    


  • Aussschlaggebend ist folgendes:

    _w64 long Window::window_proc(unsigned int msg, unsigned int wParam, _w64 long lParam)
    {
        if(msg == WM_SIZE || msg == WM_MOVE)
        {
            ::RECT rect;
            ::GetClientRect(handle_, &rect);
    
            window_size_.x_ = rect.left;
            window_size_.y_ = rect.top;
            window_size_.width_ = rect.right - rect.left;
            window_size_.height_ = rect.bottom - rect.top;
        }
        /*
        // window size was changed?
        if(msg == WM_SIZE)
        {
            ::RECT rect;
            ::GetWindowRect(handle_, &rect);
            //::RECT rect = {0, 0, LOWORD(lParam), HIWORD(lParam)};
            //::AdjustWindowRectEx(&rect, get_style(), false, 0);
            cout << rect.left << " " << rect.right << " " << rect.top << " " << rect.bottom << "\n";
            window_size_.width_  = rect.right  - rect.left;
            window_size_.height_ = rect.bottom - rect.top;
    
            LOG(LOG_INFO, L"size w: %d h: %d", window_size_.width_, window_size_.height_);
        }
        // window was moved?
        else if(msg == WM_MOVE)
        {
            ::RECT rect;
            ::GetWindowRect(handle_, &rect);
    
            // --> should be correct
            window_size_.x_ = rect.left;
            window_size_.y_ = rect.top;
    
            LOG(LOG_INFO, L"move x: %d y: %d", window_size_.x_, window_size_.y_);
        }
        */
    
        // just handle the windowX messages
        switch(msg)
        {
    #pragma warning(disable : 4800)
            HANDLE_MSG(handle_, WM_ACTIVATEAPP,        on_activate_app);
            HANDLE_MSG(handle_, WM_CREATE,            on_create);
            HANDLE_MSG(handle_, WM_CLOSE,            on_close);
            HANDLE_MSG(handle_, WM_DESTROY,            on_destroy);
    #pragma warning(default : 4800)
        }
    
        return(::DefWindowProcW(handle_, msg, wParam, lParam));
    }
    

    und:

    void Window::center(void)
    {
        if(!full_screen_mode_ || !handle_)
            return;
    
        // create a window rectangle
        ::RECT window_rect = {0, 0, window_size_.width_, window_size_.height_};
    
        // get the true window size
        ::AdjustWindowRectEx(&window_rect, get_style(), false, 0);
    
        // calculate the centered window position
        int center_x = ::GetSystemMetrics(SM_CXFULLSCREEN)/2-(abs<long>(window_rect.left)+window_rect.right)/2;
        int center_y = ::GetSystemMetrics(SM_CYFULLSCREEN)/2-(abs<long>(window_rect.top)+window_rect.bottom)/2;
    
        // move window
        ::MoveWindow(handle_, center_x, center_y, window_rect.right - window_rect.left,
                     window_rect.bottom - window_rect.top, true);
    
        LOG(LOG_INFO, L"center window(w: %d h: %d)", window_size_.width_, window_size_.height_);
    }
    


  • AdjustWindowRectEx() würde ich bei der Center()-Funktion gar nicht aufrufen. Die Funktion soll doch nur das Fenster zentrieren, wenn es bereits ne Größe hat oder nich?

    void Window::center(void)
    {
        if(!full_screen_mode_ || !handle_)
            return;
    
        // create a window rectangle
        ::RECT window_rect;
    
        // get current window size
        ::GetWindowRect(hWnd,&window_rect);
    
        // calculate the centered window position
        int center_x = ::GetSystemMetrics(SM_CXFULLSCREEN)/2-(window_rect.right-window_rect.left)/2;
        int center_y = ::GetSystemMetrics(SM_CYFULLSCREEN)/2-(window_rect.bottom-window_rect.top)/2;
    
        // move window
        ::MoveWindow(handle_, center_x, center_y, window_rect.right - window_rect.left,
                     window_rect.bottom - window_rect.top, true);
    
        LOG(LOG_INFO, L"center window(w: %d h: %d)", window_size_.width_, window_size_.height_);
    }
    


  • Das wirds wohl gewesen sein. THX --> und ich musste den Einkommentierten Code nutzen, damit es funzte!



  • Leider gibt es mit meienr Klasse ein neues Problem:

    Das hier funktioniert prima:

    win.create(L"window 1", L"window 1", WS_OVERLAPPEDWINDOW, false, 0, 0, 640, 480, 32);
    win.show();
    win.center();
    win.resize(0, 0, 800, 600);
    

    --> kracht und sagt mir, dass es in der create Funktion zu einem illegalen Memory Acces gekommen ist und verweist auf die xstring.h...

    Window* win = 0;
    win->create(L"wixstr", L"wixstr", WS_OVERLAPPEDWINDOW, false, 0, 0, 640, 480, 32);
    if(win)
       delete win;
    

  • Mod

    *g schrieb:

    --> kracht und sagt mir, dass es in der create Funktion zu einem illegalen Memory Acces gekommen ist und verweist auf die xstring.h...

    Window* win = 0;
    win->create(L"wixstr", L"wixstr", WS_OVERLAPPEDWINDOW, false, 0, 0, 640, 480, 32);
    if(win)
       delete win;
    

    Wie soll das fnktionieren, wenn Dein Fenster Objekt nicht allokiert wurde. Hier fehlt zumindest ein win = new Window();


Anmelden zum Antworten