Skip to content

Instantly share code, notes, and snippets.

@tophf
Last active February 23, 2016 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tophf/a32eb123807607eb62eb to your computer and use it in GitHub Desktop.
Save tophf/a32eb123807607eb62eb to your computer and use it in GitHub Desktop.
sublimeRunner starts ST3 and removes its window title bar
#include <windows.h>
#include <shlwapi.h>
#include <strsafe.h>
#pragma optimize("gsy", on)
#pragma comment(linker, "/ENTRY:start")
#pragma comment(linker, "/NODEFAULTLIB:no")
void reuseRunningInstance(HWND hST) {
int argc;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
COPYDATASTRUCT cds;
TCHAR fullpath[MAX_PATH+1]; fullpath[0] = '/';
auto heap = GetProcessHeap();
for (int i = 1; i < argc; i++) {
GetFullPathName(argv[i], MAX_PATH, &fullpath[1], NULL);
for (int j = 1; j <= MAX_PATH; j++) {
auto ch = fullpath[j];
if (!ch)
break;
if (ch == '\\') {
fullpath[j] = '/';
} else if (ch == ':') {
StringCchCopy(&fullpath[j], MAX_PATH+1-j, &fullpath[j+1]);
j--;
}
}
auto fullpathptr = fullpath[1] == '/' ? &fullpath[1] : &fullpath[0];
auto n = WideCharToMultiByte(CP_UTF8, 0, fullpathptr, -1, NULL, 0, NULL, NULL);
cds.dwData = 0x59;
cds.cbData = 3 * sizeof(DWORD) + n;
cds.lpData = HeapAlloc(heap, 0, cds.cbData);
typedef struct { DWORD a; DWORD b; DWORD len; BYTE data; } DATA;
DATA* dataptr = cds.lpData;
dataptr->a = 7;
dataptr->b = 1;
dataptr->len = n-1;
WideCharToMultiByte(CP_UTF8, 0, fullpathptr, -1, &dataptr->data, n, NULL, NULL);
SendMessage(FindWindowA("com.sublimetext.three.timer_window", NULL), WM_COPYDATA, hST, &cds);
HeapFree(heap, 0, dataptr);
}
LocalFree(argv);
}
LPCTSTR findParamsPtr() {
TCHAR* cmdline = GetCommandLine();
// find params by skipping the executable file name
TCHAR skipTo = *cmdline == '"' ? '"' : ' ';
TCHAR current;
while (current = *(++cmdline)) {
if (current == skipTo) {
++cmdline;
break;
}
}
return cmdline;
}
void removeWindowFrame() {
for (int i = 0; i < 200; i++) {
Sleep(10);
auto hST = FindWindowA("PX_WINDOW_CLASS", NULL);
if (!hST) {
MSG msg;
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
continue;
}
auto style = GetWindowLong(hST, GWL_STYLE);
SetWindowLong(hST, GWL_STYLE, style & ~(WS_CAPTION | WS_THICKFRAME));
WINDOWPLACEMENT wp;
if (GetWindowPlacement(hST, &wp)) {
wp.rcNormalPosition.bottom++; SetWindowPlacement(hST, &wp);
wp.rcNormalPosition.bottom--; SetWindowPlacement(hST, &wp);
}
ExitProcess(0);
}
}
void start() {
auto hST = FindWindowA("PX_WINDOW_CLASS", NULL);
if (hST) {
reuseRunningInstance(hST);
ExitProcess(0);
}
ShellExecuteW(0, NULL, L"sublime_text", findParamsPtr(), NULL, SW_SHOWDEFAULT);
removeWindowFrame();
ExitProcess(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment