0

I'm trying to create a C# program, but I don't want the window to be active when I open it. I'd like it to open in the background, and the window to show up on top of my other programs, except I want my active window to stay the same. It's because I'm using full screen programs, and I don't want my little popup to take my out of the full screen mode.

Program Use (might help in understanding what I need): I'm creating a set of macros that turn a spare mouse into a media controller. The scroll wheel controls volume, left button controls play/pause, etc. I use Spotify for music, and I want to be able to change the volume of Spotify independently from my computer's global volume. I already have this figured out using code here. I want a popup to display telling me that when I use the scroll wheel, I'm changing the volume of Spotify opposed to global volume. I want to be able to activate the macro, display the popup, change the volume as I wish, and then deactivate the macro without exiting my full screen applications. Hopefully this helps, thank you!

Program Use Edit: Here's just an explanation video, should be easier than trying to explain. To clarify, I want the program to not change activated window when it starts and to always be top most, without me having to activate it first. Thank you!!! https://streamable.com/2pewz

I'm using a program called QuickMacros to open the popup and I've tried a few different settings in there but haven't had any luck. I don't have any experience with C#, so I haven't tried anything inside C#.

My code is unrelated to the issue, but here it is just in case. All this does is give me the ability to move the popup.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpotifyPopup
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

    }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left += e.X - lastPoint.X;
                this.Top += e.Y - lastPoint.Y;
            }
        }
        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Left += e.X - lastPoint2.X;
                this.Top += e.Y - lastPoint2.Y;
            }
        }
        Point lastPoint2;
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint2 = new Point(e.X, e.Y);
        }
    }

        } 

Thank you for your help!

KPopOG
  • 85
  • 9

2 Answers2

0

Your question is a little bit unclear but if I am right what you want is to start your application in minimized state, to do that simply use code below in your form constructor

this.WindowState = FormWindowState.Minimized;

And when your event is fired and you want your app to be on top just use

this.TopMost = true;
this.WindowState = FormWindowState.Normal;

and for proper positioning of your form you can use this answer

Edit

Ok, now your needs are more clear, this a demo of what i think you want, in this example the form starts minimized and comes to top on mouse wheel, and then goes to background when idle, u can add more events to code and adapt it for your needs,
I used global hooks for this demo thanks to this link, so dont forget to add the proper nuget package based on the provided link.
here is the code:

using Gma.System.MouseKeyHook;
using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
    private Timer timer;
    private IKeyboardMouseEvents m_GlobalHook;

    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        Subscribe();

        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += Timer_Tick;

        // Set up how the form should be displayed.
        ClientSize = new System.Drawing.Size(292, 266);
        Text = "Notify Icon Example";

        WindowState = FormWindowState.Minimized;

        Rectangle workingArea = Screen.GetWorkingArea(this);
        Location = new Point(workingArea.Right - Size.Width - 100,
                                  workingArea.Bottom - Size.Height - 100);
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        WindowState = FormWindowState.Minimized;
        TopMost = false;
    }

    public void Subscribe()
    {
        // Note: for the application hook, use the Hook.AppEvents() instead
        m_GlobalHook = Hook.GlobalEvents();

        m_GlobalHook.MouseWheel += M_GlobalHook_MouseWheel;
    }

    private void M_GlobalHook_MouseWheel(object sender, MouseEventArgs e)
    {
        WindowState = FormWindowState.Normal;
        TopMost = true;
        timer.Stop();
        timer.Start();
    }

    public void Unsubscribe()
    {
        m_GlobalHook.MouseDownExt -= M_GlobalHook_MouseWheel;

        //It is recommened to dispose it
        m_GlobalHook.Dispose();
    }

}
hessam hedieh
  • 780
  • 5
  • 18
  • Thanks for your response! I had some luck with the minimized window state, but the top most part didn't seem to work. The window starts up minimized, but doesn't display anything until it's clicked on. – KPopOG Jan 03 '19 at 06:48
  • @KPopOG, I edited my answer have a look if it helps. – hessam hedieh Jan 03 '19 at 06:49
  • A bit... let me add in some context in my original post on the project's goals, and that might be a little more helpful. I didn't want to add in too much unnecessary reading but might help you. – KPopOG Jan 03 '19 at 18:19
  • Alright, I'm done. Adding in some more information, hopefully it's useful. Thank you! – KPopOG Jan 03 '19 at 18:29
  • I've been messing around with what you suggested some more, but no luck. Any other ideas? – KPopOG Jan 04 '19 at 04:54
  • @KPopOG,have a look and let me know it helps. – hessam hedieh Jan 04 '19 at 10:27
  • Ah! That's so close. I feel awful for having you do all this work... I'll go in a little more detail... editing the original post. – KPopOG Jan 04 '19 at 21:00
  • @KPopOG, dont feel that way, I enjoy helping and I learn through it, I saw video, still im not sure i got it, so u just want it to always be on top from the moment it starts?, ok so start window in `Normal` state instead of `Minimized` and let `TopMost = true` do the job, if this is not what you want, plz run the demo code I sent and tell exactly what behavior should be changed, if you think there is lot to explain, maybe we can contact via `skype` find the question and answer and edit ours in here for others reference. – hessam hedieh Jan 05 '19 at 05:53
-2

Have a look here: Bring a window to the front in WPF

This thread discusses the general mechanism of presenting, activating and showing windows with WPF.

tordam
  • 1
  • 1