diff --git a/desmume/src/windows/CWindow.cpp b/desmume/src/windows/CWindow.cpp index 000160fd6..636908eda 100644 --- a/desmume/src/windows/CWindow.cpp +++ b/desmume/src/windows/CWindow.cpp @@ -22,6 +22,67 @@ #include "CWindow.h" #include "debug.h" +//----------------------------------------------------------------------------- +// Window class handling +//----------------------------------------------------------------------------- + +vector ReggedWndClasses; + +bool RegWndClass(string name, WNDPROC wndProc, int extraSize) +{ + return RegWndClass(name, wndProc, 0, NULL, extraSize); +} + +bool RegWndClass(string name, WNDPROC wndProc, HICON icon, int extraSize) +{ + return RegWndClass(name, wndProc, 0, icon, extraSize); +} + +bool RegWndClass(string name, WNDPROC wndProc, UINT style, HICON icon, int extraSize) +{ + // If the class is already regged, don't re-reg it + if (find(ReggedWndClasses.begin(), ReggedWndClasses.end(), name) != ReggedWndClasses.end()) + return true; + + WNDCLASSEX wc; + + wc.cbSize = sizeof(wc); + wc.lpszClassName = name.c_str(); + wc.hInstance = hAppInst; + wc.lpfnWndProc = wndProc; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hIcon = icon; + wc.lpszMenuName = 0; + wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); + wc.style = style; + wc.cbClsExtra = 0; + wc.cbWndExtra = extraSize; + wc.hIconSm = 0; + + if (RegisterClassEx(&wc) != 0) + { + // If registration succeeded, add the class name into the list + ReggedWndClasses.push_back(name); + return true; + } + else + return false; +} + +void UnregWndClass(string name) +{ + vector::iterator it = find(ReggedWndClasses.begin(), ReggedWndClasses.end(), name); + + // If the class wasn't regged, we can't unreg it :P + if (it == ReggedWndClasses.end()) + return; + + // Otherwise unreg the class and remove its name from the list + UnregisterClass(name.c_str(), hAppInst); + ReggedWndClasses.erase(it); +} + + WINCLASS::WINCLASS(LPSTR rclass, HINSTANCE hInst) { memset(regclass, 0, sizeof(regclass)); diff --git a/desmume/src/windows/CWindow.h b/desmume/src/windows/CWindow.h index a7ff3e9ed..3e9879f99 100644 --- a/desmume/src/windows/CWindow.h +++ b/desmume/src/windows/CWindow.h @@ -24,6 +24,23 @@ #include "../common.h" +#include +#include +#include + +using namespace std; + +//----------------------------------------------------------------------------- +// Window class handling +//----------------------------------------------------------------------------- + +bool RegWndClass(string name, WNDPROC wndProc, int extraSize = 0); +bool RegWndClass(string name, WNDPROC wndProc, HICON icon, int extraSize = 0); +bool RegWndClass(string name, WNDPROC wndProc, UINT style, HICON icon, int extraSize = 0); + +void UnregWndClass(string name); + + class WINCLASS { private: diff --git a/desmume/src/windows/main.cpp b/desmume/src/windows/main.cpp index 64ad814fa..f68d95daf 100644 --- a/desmume/src/windows/main.cpp +++ b/desmume/src/windows/main.cpp @@ -1473,23 +1473,6 @@ void ChangeLanguage(int id) MenuInit(); } -int RegClass(WNDPROC Proc, LPCTSTR szName) -{ - WNDCLASS wc; - - wc.style = CS_DBLCLKS; - wc.cbClsExtra = wc.cbWndExtra=0; - wc.lpfnWndProc=Proc; - wc.hInstance=hAppInst; - wc.hIcon=LoadIcon(hAppInst,"ICONDESMUME"); - wc.hCursor=LoadCursor(NULL,IDC_ARROW); - wc.hbrBackground=(HBRUSH)(COLOR_BACKGROUND); - wc.lpszMenuName=(LPCTSTR)NULL; - wc.lpszClassName=szName; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; - return RegisterClass(&wc); -} static void ExitRunLoop() { @@ -1605,7 +1588,7 @@ int _main() }*/ ColorCtrl_Register(); - if (!RegClass(WindowProcedure, "DeSmuME")) + if (!RegWndClass("DeSmuME", WindowProcedure, CS_DBLCLKS, LoadIcon(hAppInst, "ICONDESMUME"))) { MessageBox(NULL, "Error registering windows class", "DeSmuME", MB_OK); exit(-1); @@ -1981,6 +1964,8 @@ int _main() if (lpBackSurface != NULL) IDirectDraw7_Release(lpBackSurface); if (lpDDraw != NULL) IDirectDraw7_Release(lpDDraw); + UnregWndClass("DeSmuME"); + return 0; }