Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Focus problem
Re: Focus problem [message #57818 is a reply to message #57817] Sun, 19 December 2021 14:44 Go to previous messageGo to previous message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
2): put
Point p;

as global is unnecessary and highly likely conceptually wrong: what if you want multiple but independent windows who need to maintain their own current positions?.

The following revised code takes care of that, also demonstrate a simple use of One.

U++ is created by powerful programmers who actually use it. There are a lot of convenience utilities like One,DUMP,LOG, etc (I don't know many either, but read U++ source code, read old posts, etc and build up this knowledge; it will significantly increase your productivity: almost in all situation you need a specific tool or facility, someone before you has encounter it and figure out a smart solution).

BTW, welcome to U++ community!
#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct MyPanel : ImageCtrl {
	Point p;

	virtual void Paint(Draw& w)override
	{
		w.DrawRect(GetSize(), White());
		w.DrawText(p.x, p.y, "#", Arial(30), Red);
		Refresh();
	}

	virtual bool Key(dword key, int count) override
	{
		switch (key)
		{
			case K_W:
				p.y-=1;
				break;
			case K_S:
				p.y+=1;
				break;
			case K_A:
				p.x-=1;
				break;
			case K_D:
				p.x+=1;
				break;
			default:
				;
		}
		Refresh();
        return true;
	}
	
	void LeftDown(Point p, dword keyflags)	override
	{
		SetFocus();
	}
	
};

struct MainWindow : TopWindow {


		EditString inputtext;
		MyPanel panel;

//		void Close() override
//		{
//			delete this;
//		}

	// Costruttore dove inserisci le inizializzazioni
		MainWindow()
		{
	
			Title("Test Focus").Zoomable().Sizeable();
			Add(inputtext.TopPosZ(0, 16).HSizePos());
			Add(panel.VSizePos(26, 0).HSizePos(0, 0));
	
			inputtext <<= "test";
	
			SetRect(0, 0, 300, 300);
			panel.p.x = 150;
			panel.p.y = 150;
		}
};

GUI_APP_MAIN
{
        // dynamically allocated MainWindow
	One<MainWindow> m;
	m.Create<MainWindow>().OpenMain();

        // allocated from Stack.
        MainWindow().OpenMain();
       
        // because now Point p is per instance (not global)
        // you can control each independent of the other.
//(new MainWindow)->OpenMain();
	Ctrl::EventLoop();
}

[Updated on: Sun, 19 December 2021 15:30]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to output variable values with time stamp in windows application
Next Topic: Clang linker error: no such file or directory
Goto Forum:
  


Current Time: Sun Aug 24 02:07:20 CEST 2025

Total time taken to generate the page: 0.03759 seconds