How hard is it to publish a. NET Windows form to change the class name of a form? "In the blink of an eye big six months later, but in the first few days there are friends of this article to comment on, basically has been very little attention to it, after all, it is only a study of doubts, in the development of no practical use. But because of the comments of squares , combined with the recent work of their own, inspired a now, but really found a solution to the law, have to sigh, "the problem will always have a solution, but their ability is not enough or a moment did not think about it." OK, the prelude is finished, get to the chase.
Recent related work
Recently, re-picked up the familiar interface UI development, because of the need to understand some of the HOOK API knowledge. HOOK API C + + already has better open source resources,Mhook and minhook. The HOOK API is the key to resolving the Windows form changing the class name of the form.
Inspiration and ideas
Remember the last article that mentioned why you can't change the name of a Windows Form form class? is the Microsoft code only to recognize the system registration ClassName, as long as we set in the CreateParams attribute ClassName is not the system registration ClassName, will be error. So, set your favorite ClassName, can only follow the window creation process, implement a window yourself. And the process of implementing a window is simple:
- Use API function registerclass Registration window;
- Create a window using the API function CreateWindowEx;
- Use API function ShowWindow display window;
- At last exit, use the API function DestroyWindow to destroy the window.
The process is very simple, and the Winform window cannot escape the process. So, the HOOK API is not an advantage? As long as we HOOK registerclass and CreateWindowEx, in the Winform Registration window, we use the class name to change it to the class name we need; the same is true when creating a window. Of course, in the actual processing process,unregisterclass,getclassinfo also need hooks to deal with.
Final implementation
Not much to say, very simple, everything to speak in code.
1#include"ClassNameManager.h"2#include <Windows.h>3#include <tchar.h>4#include <assert.h>5#include".. /minhook/include/minhook.h"6 7 #ifdef _m_x648 #pragmaComment (lib, "... /lib/minhook/minhook.x64.lib ")9 #elseTen #pragmaComment (lib, "... /lib/minhook/minhook.x86.lib ") One #endif A - namespaceStarts2000 { - namespaceWindow { the namespaceForms { - - #defineForm_class_name L "Windowsforms10.window.8.app" - #defineForm_custom_class_name L "Starts2000.window" + -typedef ATOM (WINAPI * trueregisterclassw) (_in_ CONST WNDCLASSW *); +typedef BOOL (WINAPI *TRUEUNREGISTERCLASSW) (_in_ lpcwstr, _in_opt_ hinstance); Atypedef BOOL (WINAPI *truegetclassinfow) ( at _in_opt_ hinstance, - _in_ LPCWSTR, - _out_ LPWNDCLASSW); -typedef HWND (WINAPI *TRUECREATEWINDOWEXW) ( - _in_ DWORD, - _in_opt_ LPCWSTR, in _in_opt_ LPCWSTR, - _in_ DWORD, to_in_int, +_in_int, -_in_int, the_in_int, * _in_opt_ HWND, $ _in_opt_ HMENU,Panax Notoginseng _in_opt_ hinstance, - _in_opt_ lpvoid); the +TRUEREGISTERCLASSW _REGISTERCLASSW =NULL; ATRUEUNREGISTERCLASSW _UNREGISTERCLASSW =NULL; theTruegetclassinfow _getclassinfow =NULL; +TRUECREATEWINDOWEXW _CREATEWINDOWEXW =NULL; - $ATOM WINAPI registerclasswd (_in_ CONST WNDCLASSW *Lpwndclass) { $ if(_tcsstr (lpwndclass->lpszClassName, Form_class_name)) { - WNDCLASSW wndclass; -memcpy (&wndclass, Lpwndclass,sizeof(WNDCLASSW)); theWndclass.lpszclassname =Form_custom_class_name; -Auto ret = _REGISTERCLASSW (&wndclass);Wuyi returnret; the } - Wu return_REGISTERCLASSW (lpwndclass); - } About $ BOOL WINAPI unregisterclasswd (_in_ lpcwstr lpclassname, _in_opt_ hinstance hinstance) { - if(_tcsstr (Lpclassname, Form_class_name)) { - return_UNREGISTERCLASSW (Form_custom_class_name, hinstance); - } A + return_UNREGISTERCLASSW (Lpclassname, hinstance); the } - $ BOOL WINAPI getclassinfowd (_in_opt_ hinstance hinstance, the _in_ lpcwstr Lpclassname, the _out_ LPWNDCLASSW Lpwndclass) { the if(_tcsstr (Lpclassname, Form_class_name)) { the return_getclassinfow (hinstance, Form_custom_class_name, lpwndclass); - } in the return_getclassinfow (hinstance, Lpclassname, lpwndclass); the } About the HWND WINAPI createwindowexwd ( the _in_ DWORD dwExStyle, the _in_opt_ lpcwstr Lpclassname, + _in_opt_ lpcwstr Lpwindowname, - _in_ DWORD dwstyle, the_in_intX,Bayi_in_intY, the_in_intnwidth, the_in_intnheight, - _in_opt_ HWND hwndparent, - _in_opt_ HMENU HMENU, the _in_opt_ hinstance hinstance, the _in_opt_ lpvoid Lpparam) { the if(_tcsstr (Lpclassname, Form_class_name)) { theAuto hwnd =_createwindowexw (dwExStyle, Form_custom_class_name, Lpwindowname, Dwstyle, - X, Y, nwidth, nheight, hWndParent, HMenu, hinstance, Lpparam); the assert (HWND); the returnhwnd; the }94 the return_createwindowexw (dwExStyle, Lpclassname, Lpwindowname, Dwstyle, the X, Y, nwidth, nheight, hWndParent, HMenu, hinstance, Lpparam); the }98 About Classnamemanager::classnamemanager () { -Auto ret =mh_initialize ();101ASSERT (ret = =MH_STATUS::MH_OK);102 103ret = MH_CREATEHOOKAPI (L"User32.dll", 104 "REGISTERCLASSW", &REGISTERCLASSWD, reinterpret_cast<lpvoid*> (&_registerclassw)); theASSERT (ret = =MH_STATUS::MH_OK);106 107ret = MH_CREATEHOOKAPI (L"User32.dll",108 "UNREGISTERCLASSW", &UNREGISTERCLASSWD, reinterpret_cast<lpvoid*> (&_unregisterclassw));109ASSERT (ret = =MH_STATUS::MH_OK); the 111ret = MH_CREATEHOOKAPI (L"User32.dll", the "getclassinfow", &getclassinfowd, reinterpret_cast<lpvoid*> (&_getclassinfow));113ASSERT (ret = =MH_STATUS::MH_OK); the theret = MH_CREATEHOOKAPI (L"User32.dll", the "CREATEWINDOWEXW", &CREATEWINDOWEXWD, reinterpret_cast<lpvoid*> (&_CREATEWINDOWEXW));117ASSERT (ret = =MH_STATUS::MH_OK);118 119RET =Mh_enablehook (mh_all_hooks); -ASSERT (ret = =MH_STATUS::MH_OK);121 }122 123classnamemanager::~Classnamemanager () {124 } the 126classnamemanager::!Classnamemanager () {127Auto ret =mh_uninitialize (); -ASSERT (ret = =MH_STATUS::MH_OK);129 } the }131 } the}
Final effect
The last of the last
Source code is to be on, download the project source .
How difficult is it to change the form class name of a. NET Windows form? Sequel