Hi All, Im new to threading and forms and really bad at API. Jonsca pointed me to Backgroundworker because my original problem was to constantly check if a window is open. If it is open, terminate my program. I failed at that so I tried Hotkeys.

The problem is:

My background worker doesnt constantly run in the background?? Well it doesnt seem to.
What the form below does is:

Makes a form, Initializes backgroundworker. In the background worker, it is SUPPOSED to constantly check if a Hotkey is pressed. The hotkey is supposed to work systemwide.. so even if the form is not in focus, it should still detect it.

I've tried it with WndProc but with no success and I'm not a pro at API or Threading.

Can someone shed some insight as to what Im doing wrong?

P.S. I removed quite a lot of code to make it faster for someone helping to read through.

#pragma once
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "user32.lib")
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <cstdlib>
#include "Query.h"

#using<system.dll>

namespace SmartChat {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Security::Permissions;
	using namespace System::Runtime::InteropServices;
	using namespace System::Threading;

	public ref class Form1 : public System::Windows::Forms::Form
	{
	private:
		System::ComponentModel::BackgroundWorker^ backgroundWorker1;

	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			InitializeBackgoundWorker();
		}

	private:
   // Set up the BackgroundWorker object by 
   // attaching event handlers. 
   void InitializeBackgoundWorker()
   {
      backgroundWorker1->DoWork += gcnew DoWorkEventHandler( this, &Form1::backgroundWorker1_DoWork );
   }
	private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
	{ 
		RegisterHotKey(NULL,1,0x4000,0x7A);  //0x7A is 'F11'
		MSG msg = {0};
		while(msg.message != WM_HOTKEY)
		{
			MSG msg = {0};
			if(msg.message == WM_HOTKEY)  
			{
				MessageBox::Show("Hi");  
			}
		}
		MessageBox::Show("Works");
	}


	public:
		//Query ^ F = gcnew Query();
		System::String^ StringF;

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

		void InitializeComponent(void)
		{
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->SuspendLayout();
			// 
			// backgroundWorker1
			// 
			this->backgroundWorker1 = gcnew System::ComponentModel::BackgroundWorker;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->AutoValidate = System::Windows::Forms::AutoValidate::EnablePreventFocusChange;
			this->ClientSize = System::Drawing::Size(30, 35);
			this->Controls->Add(this->button1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedToolWindow;
			this->MaximizeBox = false;
			this->MinimizeBox = false;
			this->Name = L"Form1";
			this->ShowIcon = false;
			this->ShowInTaskbar = false;
			this->Text = L"SChat";
			this->WindowState = System::Windows::Forms::FormWindowState::Minimized;
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);

		}
#pragma endregion
	public: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
					Form1::Visible = false;
					Query ^ F = gcnew Query();
                    //F->Show();
					F->ShowDialog();
					StringF = F->textBox1->Text;
					System::String^ AddStr = "Simba - " + StringF;
					IntPtr ptr2 = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(AddStr);

				 HWND Smart = FindWindowW(0, L"Public SMARTv6.6 - SMART Minimizing Autoing Resource Thing - By BenLand100");
				 HWND Simba = FindWindow(0, (LPCSTR)ptr2.ToPointer());
				 if(Simba == NULL)
				 {
					 MessageBox::Show("Cannot Find Window");
				 }
				 System::Runtime::InteropServices::Marshal::FreeHGlobal(ptr2);

				 //MessageBox::Show(button1->Parent->ToString());

				 IntPtr ptr = button1->Handle;
				 HWND hButton = (HWND)ptr.ToPointer();

				 HWND sChat = FindWindowW(0, L"SChat");
				 ShowWindow(sChat, SW_HIDE);
				 SetParent(hButton, Smart);
				 button1->Location = System::Drawing::Point(528, 469);
				 RegisterHotKey(NULL,1,0x4000,0x7A);  //0x7A is 'F11'
				 backgroundWorker1->RunWorkerAsync();
			 }
 protected: virtual void Form1::WndProc(System::Windows::Forms::Message %m) override  
      { 
			MSG msg = {0};
                 if(msg.message == WM_HOTKEY)  
                 {
					 MessageBox::Show("Hi");  
                 }
			Form::WndProc(m);
       } 
	};
}

Edited 5 Years Ago by triumphost: n/a

See this thread on Stack. It's using C#, but you should be able to translate the syntax readily.

Like I had said in the other thread, it takes a little wizardry to update the GUI from another thread (which is basically what the backgroundworker is).

I have never actually implemented this (I've used backgroundworker for different tasks), and I definitely don't know how to handle it with raw API calls.

Pay close attention to the answer with the green check (and the associated comments), as I think that gives more or less what you need.

OMG! Genius! above, I solved it! Got it to constantly check if a window is open, and I got the thread to check for GLOBAL HOTKEYS :)

Works perfectly! U sir need an award!

private: System::Void backgroundWorker1_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
	{ 
		if (RegisterHotKey(NULL,1,MOD_ALT,0x53))  //0x53 is 's'
		{
			MessageBox::Show("Registered HotKey - ALT + S");
		}
		MSG msg = {0};
		while (GetMessage(&msg, NULL, 0, 0) == 1)
		{
			if (msg.message == WM_HOTKEY)
			{
				MessageBox::Show(":)");
			}
			HWND Smart = FindWindowW(0, L"FireFox");
			if (Smart = NULL)
			{
				MessageBox::Show("FireFox Not Running -- Terminating Program");
				Form1::Close();
			}
		}
	}

Also had to do this in Form1_Load: backgroundWorker1->RunWorkerAsync();

Edited 5 Years Ago by triumphost: n/a

U sir need an award!

I accept all large financial donations, which I will, of course, pass on to charity ;)

This article has been dead for over six months. Start a new discussion instead.