我的 Classic WordPad 啓動器
[編輯] [转简体] (简体译文)
|
作者:huidong
| 分類:【編程】C/C++
[
9 瀏覽
0 評論
2 贊
3 踩
]
概要
Windows 經典版 WordPad 程序啓動器,可以設置輸入框爲護眼色,開場有「爲人民服務」字樣。
正文
#include <windows.h> #include <tchar.h> #include <stdio.h> #include <richedit.h> struct ProcessWindow { DWORD dwProcessId; HWND hwndWindow; }; // 查找进程主窗口的回调函数 BOOL CALLBACK EnumWindowCallBack(HWND hWnd, LPARAM lParam) { ProcessWindow* pProcessWindow = (ProcessWindow*)lParam; DWORD dwProcessId; GetWindowThreadProcessId(hWnd, &dwProcessId); // 判断是否是指定进程的主窗口 if (pProcessWindow->dwProcessId == dwProcessId && IsWindowVisible(hWnd) && GetParent(hWnd) == NULL) { pProcessWindow->hwndWindow = hWnd; return FALSE; } return TRUE; } HWND CreateMyProcess() { // 进程启动信息 STARTUPINFO si; memset(&si, 0, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_SHOW; // 进程信息 PROCESS_INFORMATION pi; // 创建进程 // 第二个参数是要启动的应用程序路径名,比如:C:/Test.exe TCHAR sz[] = _T(".\\wordpad_oldversion.exe"); if (CreateProcess(NULL, sz, NULL, NULL, false, 0, NULL, NULL, &si, &pi)) { ProcessWindow procwin; procwin.dwProcessId = pi.dwProcessId; procwin.hwndWindow = NULL; // 等待新进程初始化完毕 WaitForInputIdle(pi.hProcess, 5000); // 查找主窗口 EnumWindows(EnumWindowCallBack, (LPARAM)&procwin); return procwin.hwndWindow; } return NULL; } HWND GetFocusedChild(HWND wnd) { DWORD SelfThreadId = GetCurrentThreadId();//获取本身的线程ID DWORD TargetThreadId = GetWindowThreadProcessId(wnd, NULL);//根据窗口句柄获取线程ID AttachThreadInput(TargetThreadId, SelfThreadId, true);//附加线程 HWND child_wnd = GetFocus();//获取具有输入焦点的窗口句柄 AttachThreadInput(TargetThreadId, SelfThreadId, false);//取消附加的线程 return child_wnd; } int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow) { ShowWindow(GetConsoleWindow(), SW_HIDE); HWND main_wnd = CreateMyProcess(); HWND child_wnd = GetFocusedChild(main_wnd); COLORREF eye_color = 0x00cfe8cc; SendMessage(child_wnd, EM_SETBKGNDCOLOR, 0, eye_color); Sleep(200); HDC hDC = GetDC(child_wnd); SetBkMode(hDC, TRANSPARENT); SetTextColor(hDC, 0x000000cc); HFONT hFont = CreateFont ( 72, 0, // 高度40, 宽取0表示由系统选择最佳值 0, 0, // 文本倾斜,与字体倾斜都为0 FW_BOLD/*FW_NORMAL*/, 0, 0, 0, // 斜体,下划线,无中划线 GB2312_CHARSET, // 字符集 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, // 一系列的默认值 DEFAULT_PITCH | FF_DONTCARE, _T("楷体") // 字体名称 ); SelectObject(hDC, hFont); LPCTSTR lpsz = _T("為 人 民 服 務"); RECT rct = { 0 }; GetWindowRect(child_wnd, &rct); rct.right -= rct.left, rct.bottom -= rct.top; rct.left = 0, rct.top = 0; DrawText(hDC, lpsz, lstrlen(lpsz), &rct, DT_SINGLELINE | DT_CENTER | DT_VCENTER); DeleteObject(hFont); ReleaseDC(child_wnd, hDC); Sleep(3000); InvalidateRect(child_wnd, nullptr, TRUE); UpdateWindow(child_wnd); return 0; }