issues/14

1

Update LunaHost.cpp

Update LunaHost.cpp

Update LunaHost.cpp
This commit is contained in:
恍兮惚兮 2024-04-19 20:12:03 +08:00
parent a3137685ec
commit 3352075e4c
7 changed files with 92 additions and 56 deletions

View File

@ -95,4 +95,5 @@
#define LIST_HOOK L"Hook"
#define LIST_TEXT L"Text"
#define PROC_CONN L"process connected %d"
#define PROC_DISCONN L"process disconnected %d"
#define PROC_DISCONN L"process disconnected %d"
#define COPYSELECTION L"auto send selected text in textbox to clipboard"

View File

@ -95,4 +95,5 @@
#define LIST_HOOK L"Hook"
#define LIST_TEXT L"文本"
#define PROC_CONN L"进程已连接 %d"
#define PROC_DISCONN L"进程已断开连接 %d"
#define PROC_DISCONN L"进程已断开连接 %d"
#define COPYSELECTION L"自动将文本框中选取的文本复制到剪贴板"

View File

@ -17,21 +17,24 @@ auto gmf=[&](DWORD processId)->std::optional<std::wstring>{
if (GetModuleFileNameExW(process, 0, buffer.data(), MAX_PATH)) return buffer.data();
return {};
};
void LunaHost::toclipboard(std::wstring& sentence){
bool sendclipboarddata_i(const std::wstring&text,HWND hwnd){
if (!OpenClipboard((HWND)hwnd)) return false;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (text.size() + 1) * sizeof(wchar_t));
memcpy(GlobalLock(hMem), text.c_str(), (text.size() + 1) * sizeof(wchar_t));
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
GlobalUnlock(hMem);
CloseClipboard();
return true;
}
bool sendclipboarddata(const std::wstring&text,HWND hwnd){
for (int loop = 0; loop < 10; loop++) {
if (OpenClipboard(winId)) {
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t));
memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
GlobalUnlock(hMem);
CloseClipboard();
break;
}
auto succ=sendclipboarddata_i(text,hwnd);
if(succ)return true;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
return false;
}
void LunaHost::on_close(){
savesettings();
delete configs;
@ -45,6 +48,7 @@ void LunaHost::on_close(){
void LunaHost::savesettings()
{
configs->set("ToClipboard",check_toclipboard);
configs->set("ToClipboardSelection",check_toclipboard_selection);
configs->set("AutoAttach",autoattach);
configs->set("AutoAttach_SavedOnly",autoattach_savedonly);
configs->set("flushDelay",TextThread::flushDelay);
@ -56,6 +60,7 @@ void LunaHost::savesettings()
configs->set("savedhookcontext",savedhookcontext);
}
void LunaHost::loadsettings(){
check_toclipboard_selection=configs->get("ToClipboardSelection",false);
check_toclipboard=configs->get("ToClipboard",false);
autoattach=configs->get("AutoAttach",false);
autoattach_savedonly=configs->get("AutoAttach_SavedOnly",true);
@ -155,42 +160,10 @@ bool queryversion(WORD *_1, WORD *_2, WORD *_3, WORD *_4)
}
LunaHost::LunaHost(){
std::wstring title=WndLunaHostGui;
WORD _1,_2,_3,_4;
WCHAR vs[32];
if(queryversion(&_1,&_2,&_3,&_4)){
wsprintf(vs,L" | %s v%d.%d.%d",VersionCurrent,_1,_2,_3);
title+=vs;
std::thread([&](){
if (HttpRequest httpRequest{
L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
L"api.github.com",
L"GET",
L"/repos/HIllya51/LunaHook/releases/latest"
}){
try{
auto resp=nlohmann::json::parse(WideStringToString(httpRequest.response));
std::string ver=resp["tag_name"];
settext(text()+L" | "+VersionLatest+L" "+ StringToWideString(ver));
}
catch(std::exception&e){}
}
}).detach();
}
settext(title.c_str());
configs=new confighelper;
loadsettings();
std::thread([&]{
while(1){
doautoattach();
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}).detach();
setfont(25);
btnshowsettionwindow=new button(this, BtnShowSettingWindow);
g_selectprocessbutton =new button(this,BtnSelectProcess) ;
@ -251,7 +224,7 @@ LunaHost::LunaHost(){
auto tt=Host::GetThread(handle);
if(!tt)return {};
Menu menu;
menu.add(MenuCopyHookCode,[&,tt](){toclipboard(std::wstring(tt->hp.hookcode));});
menu.add(MenuCopyHookCode,[&,tt](){sendclipboarddata(tt->hp.hookcode,winId);});
menu.add_sep();
menu.add(MenuRemoveHook,[&,tt](){Host::RemoveHook(tt->tp.processId,tt->tp.addr);});
menu.add(MenuDetachProcess,[&,tt](){
@ -309,6 +282,58 @@ LunaHost::LunaHost(){
mainlayout->setfixedheigth(1,30);
setlayout(mainlayout);
setcentral(1200,800);
std::wstring title=WndLunaHostGui;
settext(title);
std::thread([&](){
//https://github.com/HIllya51/LunaHook/issues/14
std::wstring sel;
while(1)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
if(check_toclipboard_selection)
{
auto _sel=g_showtexts->getsel();
if(_sel!=sel){
sel=_sel;
sendclipboarddata(sel,winId);
}
}
}
}).detach();
std::thread([&]{
while(1){
doautoattach();
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}).detach();
WORD _1,_2,_3,_4;
WCHAR vs[32];
if(queryversion(&_1,&_2,&_3,&_4)){
wsprintf(vs,L" | %s v%d.%d.%d",VersionCurrent,_1,_2,_3);
title+=vs;
settext(title);
std::thread([&](){
if (HttpRequest httpRequest{
L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
L"api.github.com",
L"GET",
L"/repos/HIllya51/LunaHook/releases/latest"
}){
try{
auto resp=nlohmann::json::parse(WideStringToString(httpRequest.response));
std::string ver=resp["tag_name"];
settext(text()+L" | "+VersionLatest+L" "+ StringToWideString(ver));
}
catch(std::exception&e){}
}
}).detach();
}
}
void LunaHost::on_text_recv_checkissaved(TextThread& thread)
{
@ -440,6 +465,12 @@ Settingwindow::Settingwindow(LunaHost* host):mainwindow(host){
};
g_check_clipboard->setcheck(host->check_toclipboard);
copyselect=new checkbox(this,COPYSELECTION);
copyselect->onclick=[=](){
host->check_toclipboard_selection=copyselect->ischecked();
};
copyselect->setcheck(host->check_toclipboard_selection);
autoattach =new checkbox(this,LblAutoAttach);
autoattach->onclick=[=](){
host->autoattach=autoattach->ischecked();
@ -488,6 +519,7 @@ Settingwindow::Settingwindow(LunaHost* host):mainwindow(host){
mainlayout->addcontrol(autoattach,6,0,1,2);
mainlayout->addcontrol(autoattach_so,7,0,1,2);
mainlayout->addcontrol(readonlycheck,8,0,1,2);
mainlayout->addcontrol(copyselect,9,0,1,2);
setlayout(mainlayout);
setcentral(600,500);

View File

@ -20,6 +20,7 @@ class Settingwindow:public mainwindow{
checkbox* readonlycheck;
checkbox* autoattach;
checkbox* autoattach_so;
checkbox* copyselect;
spinbox* spinmaxbuffsize;
spinbox* spinmaxhistsize;
gridlayout* mainlayout;
@ -91,7 +92,6 @@ class LunaHost:public mainwindow{
button* btnsearchhooks;
button* btnshowsettionwindow;
//button* btnsavehook;
void toclipboard(std::wstring& sentence);
processlistwindow *_processlistwindow=0;
Settingwindow *settingwindow=0;
Pluginmanager* plugins;
@ -109,6 +109,7 @@ public:
confighelper* configs;
int64_t currentselect=0;
bool check_toclipboard;
bool check_toclipboard_selection;
bool autoattach;
bool autoattach_savedonly;
std::set<std::string>autoattachexes;

View File

@ -20,18 +20,12 @@ extern "C" __declspec(dllexport) wchar_t* OnNewSentence(wchar_t* sentence, const
}
return sentence;
}
bool sendclipboarddata(const std::wstring&text,HWND hwnd);
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
if (sentenceInfo["current select"] && sentenceInfo["process id"] != 0 &&sentenceInfo["toclipboard"])
{
if (!OpenClipboard((HWND)sentenceInfo["HostHWND"])) return false;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (sentence.size() + 2) * sizeof(wchar_t));
memcpy(GlobalLock(hMem), sentence.c_str(), (sentence.size() + 2) * sizeof(wchar_t));
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, hMem);
GlobalUnlock(hMem);
CloseClipboard();
sendclipboarddata(sentence,(HWND)sentenceInfo["HostHWND"]);
}
return false;
}

View File

@ -90,6 +90,12 @@ multilineedit::multilineedit(mainwindow* parent):texteditbase(parent){
0,0,0,0, parent->winId, NULL, NULL, NULL);
SendMessage(winId, EM_SETLIMITTEXT, 0, 0);
}
std::wstring multilineedit::getsel(){
DWORD start, end;
SendMessage(winId, EM_GETSEL, reinterpret_cast<WPARAM>(&start), reinterpret_cast<LPARAM>(&end));
int length = end - start;
return text().substr(start,length);
}
lineedit::lineedit(mainwindow* parent):texteditbase(parent){
winId=CreateWindowEx(0, L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER| ES_AUTOHSCROLL ,
0,0,0,0, parent->winId, NULL, NULL, NULL);

View File

@ -54,6 +54,7 @@ public:
class multilineedit:public texteditbase{
public:
multilineedit(mainwindow*);
std::wstring getsel();
};
class lineedit:public texteditbase{
public: