Last active
March 3, 2024 16:41
-
-
Save JamesHoi/3d884caaeaa3626ae0ee3d8367c8ae75 to your computer and use it in GitHub Desktop.
A example for starting a new program minimized using RBTray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <string> | |
#include <tlhelp32.h> | |
#include <shellapi.h> | |
using namespace std; | |
#define WM_ADDTRAY 0x0401 | |
#define NAME L"RBTrayHook" | |
struct handle_data { | |
unsigned long process_id; | |
HWND window_handle; | |
}; | |
BOOL is_main_window(HWND handle) | |
{ | |
return (GetWindow(handle, GW_OWNER) == (HWND)0) & IsWindowVisible(handle); | |
} | |
BOOL CALLBACK enum_windows_callback(HWND handle, LPARAM lParam) | |
{ | |
handle_data& data = *(handle_data*)lParam; | |
unsigned long process_id = 0; | |
GetWindowThreadProcessId(handle, &process_id); | |
if (data.process_id != process_id || !is_main_window(handle)) | |
return TRUE; | |
data.window_handle = handle; | |
return FALSE; | |
} | |
HWND find_main_window(unsigned long process_id) | |
{ | |
handle_data data; | |
data.process_id = process_id; | |
data.window_handle = 0; | |
while (true) | |
{ | |
EnumWindows(enum_windows_callback, (LPARAM)&data); | |
if (data.window_handle != 0) | |
{ | |
return data.window_handle; | |
} | |
Sleep(50); | |
} | |
} | |
int getProcID(const string& p_name) | |
{ | |
while (true) { | |
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | |
PROCESSENTRY32 structprocsnapshot = { 0 }; | |
structprocsnapshot.dwSize = sizeof(PROCESSENTRY32); | |
if (snapshot == INVALID_HANDLE_VALUE)return 0; | |
if (Process32First(snapshot, &structprocsnapshot) == FALSE)return 0; | |
while (Process32Next(snapshot, &structprocsnapshot)) | |
{ | |
int iSize; | |
char* pszMultiByte; | |
iSize = WideCharToMultiByte(CP_ACP, 0, structprocsnapshot.szExeFile, -1, NULL, 0, NULL, NULL); //iSize =wcslen(pwsUnicode)+1=6 | |
pszMultiByte = (char*)malloc(iSize * sizeof(char)); | |
WideCharToMultiByte(CP_ACP, 0, structprocsnapshot.szExeFile, -1, pszMultiByte, iSize, NULL, NULL); | |
if (!strcmp(pszMultiByte, p_name.c_str())) | |
{ | |
CloseHandle(snapshot); | |
return structprocsnapshot.th32ProcessID; | |
} | |
} | |
Sleep(50); | |
} | |
} | |
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE /*hPrevInstance*/, _In_ LPSTR /*szCmdLine*/, _In_ int /*iCmdShow*/) | |
{ | |
int tmp = getProcID("RBTray.exe"); // wait start | |
char cmd[] = "D:/Program Files/Zotero/zotero.exe"; | |
HINSTANCE hs = ShellExecuteA(NULL, "open", cmd, "", NULL, SW_MINIMIZE); | |
int pid = getProcID("zotero.exe"); | |
HWND _hwnd = find_main_window(pid); | |
PostMessage(FindWindow(NAME, NAME), WM_ADDTRAY, 0, (LPARAM)_hwnd); | |
return WM_QUIT; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment