rainmeter-studio/Plugins/PluginWindowMessage/WindowMessagePlugin.cpp

149 lines
3.6 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2005 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-02-10 18:37:48 +00:00
*/
#include <windows.h>
2012-01-08 17:35:29 +00:00
#include <stdio.h>
#include "../../Library/RawString.h"
2011-02-03 18:09:24 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
2012-01-08 17:35:29 +00:00
struct MeasureData
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
CRawString windowName;
CRawString windowClass;
CRawString value;
2009-02-10 18:37:48 +00:00
WPARAM wParam;
LPARAM lParam;
DWORD uMsg;
2012-01-08 17:35:29 +00:00
MeasureData() : wParam(), lParam(), uMsg() {}
2009-02-10 18:37:48 +00:00
};
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Initialize(void** data)
{
MeasureData* measure = new MeasureData;
*data = measure;
}
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
measure->windowName = RmReadString(rm, L"WindowName", L"");
measure->windowClass = RmReadString(rm, L"WindowClass", L"");
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
DWORD uMsg, wParam, lParam;
LPCWSTR message = RmReadString(rm, L"WindowMessage", L"");
if (3 == swscanf(message, L"%u %u %u", &uMsg, &wParam, &lParam))
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
measure->uMsg = uMsg;
measure->wParam = wParam;
measure->lParam = lParam;
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2011-03-29 19:21:57 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
HWND hwnd = FindWindow(
measure->windowClass.empty() ? NULL : measure->windowClass.c_str(),
measure->windowName.empty() ? NULL : measure->windowName.c_str());
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
if (hwnd)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
if (measure->uMsg == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
// Get window text
WCHAR buffer[256];
GetWindowText(hwnd, buffer, 256);
measure->value = buffer;
2009-02-10 18:37:48 +00:00
}
else
{
2012-01-08 17:35:29 +00:00
return (double)SendMessage(hwnd, measure->uMsg, measure->wParam, measure->lParam);
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
else if (measure->uMsg == 0)
{
measure->value.clear();
}
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
return 0.0;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT LPCWSTR GetString(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
if (measure->uMsg == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
return measure->value.c_str();
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
return NULL;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
delete measure;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void ExecuteBang(void* data, LPCWSTR args)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
const WCHAR* pos = wcschr(args, L' ');
if (pos)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
size_t len = pos - args;
if (_wcsnicmp(args, L"SendMessage", len) == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
++pos;
2009-02-10 18:37:48 +00:00
// Parse parameters
DWORD uMsg, wParam, lParam;
2012-01-08 17:35:29 +00:00
if (3 == swscanf(pos, L"%u %u %u", &uMsg, &wParam, &lParam))
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
HWND hwnd = FindWindow(
measure->windowClass.empty() ? NULL : measure->windowClass.c_str(),
measure->windowName.empty() ? NULL : measure->windowName.c_str());
if (hwnd)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
PostMessage(hwnd, uMsg, wParam, lParam);
2009-02-10 18:37:48 +00:00
}
else
{
2012-01-08 17:35:29 +00:00
RmLog(LOG_ERROR, L"WindowMessagePlugin.dll: Unable to find window");
2009-02-10 18:37:48 +00:00
}
}
else
{
2012-01-08 17:35:29 +00:00
RmLog(LOG_WARNING, L"WindowMessagePlugin.dll: Incorrect number of arguments for bang");
2009-02-10 18:37:48 +00:00
}
return;
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
RmLog(LOG_WARNING, L"WindowMessagePlugin.dll: Unknown bang");
2009-02-10 18:37:48 +00:00
}