33#ifdef _WIN32
44#include <Objbase.h>
55#include <Shobjidl.h>
6+ #include <Windows.h>
67#endif
78
89static PyObject * mpl_GetCurrentProcessExplicitAppUserModelID (PyObject * module )
@@ -39,6 +40,31 @@ static PyObject* mpl_SetCurrentProcessExplicitAppUserModelID(PyObject* module, P
3940#endif
4041}
4142
43+ static PyObject * mpl_GetForegroundWindow (PyObject * module )
44+ {
45+ #ifdef _WIN32
46+ return PyLong_FromVoidPtr (GetForegroundWindow ());
47+ #else
48+ Py_RETURN_NONE ;
49+ #endif
50+ }
51+
52+ static PyObject * mpl_SetForegroundWindow (PyObject * module , PyObject * arg )
53+ {
54+ #ifdef _WIN32
55+ HWND handle = PyLong_AsVoidPtr (arg );
56+ if (PyErr_Occurred ()) {
57+ return NULL ;
58+ }
59+ if (!SetForegroundWindow (handle )) {
60+ return PyErr_Format (PyExc_RuntimeError , "Error setting window" );
61+ }
62+ Py_RETURN_NONE ;
63+ #else
64+ Py_RETURN_NONE ;
65+ #endif
66+ }
67+
4268static PyMethodDef functions [] = {
4369 {"Win32_GetCurrentProcessExplicitAppUserModelID" ,
4470 (PyCFunction )mpl_GetCurrentProcessExplicitAppUserModelID , METH_NOARGS ,
@@ -50,6 +76,16 @@ static PyMethodDef functions[] = {
5076 "Win32_SetCurrentProcessExplicitAppUserModelID(appid, /)\n--\n\n"
5177 "Wrapper for Windows's SetCurrentProcessExplicitAppUserModelID. On \n"
5278 "non-Windows platforms, a no-op." },
79+ {"Win32_GetForegroundWindow" ,
80+ (PyCFunction )mpl_GetForegroundWindow , METH_NOARGS ,
81+ "Win32_GetForegroundWindow()\n--\n\n"
82+ "Wrapper for Windows' GetForegroundWindow. On non-Windows platforms, \n"
83+ "always returns None." },
84+ {"Win32_SetForegroundWindow" ,
85+ (PyCFunction )mpl_SetForegroundWindow , METH_O ,
86+ "Win32_SetForegroundWindow(hwnd)\n--\n\n"
87+ "Wrapper for Windows' SetForegroundWindow. On non-Windows platforms, \n"
88+ "a no-op." },
5389 {NULL , NULL }}; // sentinel.
5490static PyModuleDef util_module = {
5591 PyModuleDef_HEAD_INIT , "_c_internal_utils" , "" , 0 , functions , NULL , NULL , NULL , NULL };
0 commit comments