5 Replies - 220 Views - Last Post: 12 July 2020 - 07:44 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 869
  • Joined: 15-December 14

Open form1 at the same place each time you run a program

Posted 10 July 2020 - 01:09 PM

So I am taking the tutorial from modi123_1 converting it from vb basic to C# and I have a dumb question. When I run a program I want the form to open at the same place each time I run the program. I used my googlefu and tried poking around visual studio without any success. Would someone please explain to me how to do it?
Is This A Good Question/Topic? 0
  • +

Replies To: Open form1 at the same place each time you run a program

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15804
  • View blog
  • Posts: 63,308
  • Joined: 12-June 08

Re: Open form1 at the same place each time you run a program

Posted 10 July 2020 - 01:14 PM

Moving from 'introduce yourself' to C#.

Winform?

Which tutorial?

Winform ave a 'startposition' to use or manually set 'location' property on the form load.
Was This Post Helpful? 0
  • +
  • -

#3 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 64
  • View blog
  • Posts: 391
  • Joined: 07-May 15

Re: Open form1 at the same place each time you run a program

Posted 10 July 2020 - 04:31 PM

I made in Lua a function that launch application always at center of the display, considering taskbar, later on in another functions its capable of launching window at last size and position it has when it was closed, as well as maximized.
--Set Window On Center
function CenterWindow()
	--Get Handle
	hWnd = Application.GetWndHandle();
	--Get Current Window Size
	CurrentWindowSize = window.GetSize(hWnd);
	--Get Display Informations
	local DisplayInfo = System.GetDisplayInfo();
	--Get Taskbar Settings
	GetTaskbar();
	--Taskbar Valid & Hidden (2 - No, 3 - Yes)
	if TaskbarSettings ~= nil and TaskbarSettings[9] == 2 then
		--Taskbar Size & Multiplier (0 - 255, Each Multiplier Are 256)
		local TaskbarSizeTopBottom = TaskbarSettings[21] + TaskbarSettings[22] * 256;
		local TaskbarSizeLeftRight = TaskbarSettings[17] + TaskbarSettings[18] * 256;
		--Taskbar Location (0 - Left, 1 - Top, 2 - Right, 3 - Bottom)
		if TaskbarSettings[13] == 0 then
			local SetX = (DisplayInfo.Width + TaskbarSizeLeftRight - CurrentWindowSize.Width) / 2;
			local SetY = (DisplayInfo.Height - CurrentWindowSize.Height) / 2;
			window.SetPos(hWnd, SetX, SetY);
		elseif TaskbarSettings[13] == 1 then
			local SetX = (DisplayInfo.Width - CurrentWindowSize.Width) / 2;
			local SetY = (DisplayInfo.Height + TaskbarSizeTopBottom - CurrentWindowSize.Height) / 2;
			window.SetPos(hWnd, SetX, SetY);
		elseif TaskbarSettings[13] == 2 then
			local SetX = (DisplayInfo.Width - TaskbarSizeLeftRight - CurrentWindowSize.Width) / 2;
			local SetY = (DisplayInfo.Height - CurrentWindowSize.Height) / 2;
			window.SetPos(hWnd, SetX, SetY);
		elseif TaskbarSettings[13] == 3 then
			local SetX = (DisplayInfo.Width - CurrentWindowSize.Width) / 2;
			local SetY = (DisplayInfo.Height - TaskbarSizeTopBottom - CurrentWindowSize.Height) / 2;
			window.SetPos(hWnd, SetX, SetY);
		else
			Crash();
		end
	else
		--No Valid Taskbar Settings Or Hidden
		local SetX = (DisplayInfo.Width - CurrentWindowSize.Width) / 2;
		local SetY = (DisplayInfo.Height - CurrentWindowSize.Height) / 2;
		window.SetPos(hWnd, SetX, SetY);
	end
	Error();
end

Basically it takes current screen resolution (cut in half to get center of the display X and Y), current application resolution (cut in half for offset X and Y), then render/show it. I use this only on launching. You could notice it in "gift" application from me, i think it contains a functions for not moving window outside the screen or resize bigger then the screen. If its launched without control, then OS decide where he wanna put a new window where it makes sense (like when you launch multiple terminals, each one are launched below previous one with consistent offset).
IDK if you have options, this is just a workaround example without options, where even resizing or moving window with cursor has to be made manually (probably because it is cross platform).

This post has been edited by Radius Nightly: 10 July 2020 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 869
  • Joined: 15-December 14

Re: Open form1 at the same place each time you run a program

Posted 12 July 2020 - 04:37 PM

modi123_1
Sorry for taking so long, I had a busy week. This is the tutorial
https://www.dreaminc...-basics-part-1/
OOP with Video Game Basics Part 1


Radius Nightly
You used visual studio and C# for that snippit?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7462
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: Open form1 at the same place each time you run a program

Posted 12 July 2020 - 06:34 PM

Use User Settings to persist the location. Then when your program is starting up, read the setting and set your Form's Location and ClientSize.

(As an aside, it documentation I linked to, you'll find that the Form.Location can be bound to a user setting so that you don't manually saved or loaded, but the Form.ClientSize cannot. As noted in the documentation you'll need to do this yourself. Anyway which way, you'll need to the loading and setting yourself because if you want your code to be production quality, you'll need to account for the user changing screen resolutions, desktop dimensions, or both. Blindly just reading the setting and slapping it on the form could potentially put the form is some off screen location, or be suddenly intersecting with the Windows Taskbar. I personally, like to move my taskbar between the left side of screen and the bottom of the screen. Nothing ticks me off more than launching an app and having it under the taskbar.)
Was This Post Helpful? 0
  • +
  • -

#6 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 64
  • View blog
  • Posts: 391
  • Joined: 07-May 15

Re: Open form1 at the same place each time you run a program

Posted 12 July 2020 - 07:44 PM

View Postalbert003, on 13 July 2020 - 12:37 AM, said:

You used visual studio and C# for that snippit?

Its the whole wxLua function (its based on C, C++, VB... while this languages would mostly (i guess C) work in Lua without adding anything, Lua will not work in them). But i like it, because its easy, simple, with small code that got some logic i can get results without lots of lines, without taking care of memory leaks and similar garbage, means i can invest my time more into the project functionality and other things that i wanna make, making everything pretty fast. Downside, everything has to be made manually, as there are no special options where i can simply put checkbox on movable window, hit F5, and move it.

Without taskbar it may be easier to understand then my written text.

	hWnd = Application.GetWndHandle();
	CurrentWindowSize = window.GetSize(hWnd);
	local DisplayInfo = System.GetDisplayInfo();

	local SetX = (DisplayInfo.Width - CurrentWindowSize.Width) / 2;
	local SetY = (DisplayInfo.Height - CurrentWindowSize.Height) / 2;

	window.SetPos(hWnd, SetX, SetY);


It doesn not consider multiple displays, but since any OS would have already its configured main display by the user, application starts where system decide, and its mostly on active monitor or the main monitor, since lots of applications doesnt consider multiple monitors anyway, except if software are made to use multiple monitors. Where i work, we got some software that works only on 2 monitors, having only 1 or more then 2 monitors, and software would not like to work at all, its poor quality software for big things, but i kind of understand that multiple monitors challenge. And i dont have a plan making softwares that works on multiple displays anytime soon to deal with taskbars.
I got many stuff, but this is what i use just to center window/application on the monitor.

View PostSkydiver, on 13 July 2020 - 02:34 AM, said:

Nothing ticks me off more than launching an app and having it under the taskbar.

Or over it, fighting for their 1st and 2nd position like in sport.

This post has been edited by Radius Nightly: 12 July 2020 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1