I'm currently trying to integrate some animation drawing code of mine into a third party application, under the form of an external plugin.
This animation code in realtime 3d, based on OpenGL, and is supposed to render as fast as it can, usually at 60 frames per second.
In my base application, where I'm the king of the world, I control the application message pump, so that drawing occurs whenever possible. Like that :
for (;;)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
do
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
}
draw();
}
Now that I'm no more king in the world, I have to play nice with the application messages, so that it keeps being responsive. To my knowledge, as I'm a plugin, I can't hijack the whole application message pump ; so I tried various things, doing my drawing in WM_PAINT message handler :
Use WM_TIMER, which doesn't work :I don't know in advance which time step I need (often not fixed) and the timing in not accurate.
Call InvalidateRect as soon as I'm done drawing, doesn't work : completely prevents the rest of the application of being responsive and doing its own refreshing.
Create a 'worker' thread, whose only job is to post a user message to the plugin window. This message is posted as soon as the drawing is finished (signaled by an event). The user message handler, in turn, calls InvalidateRect (see there).
So far, my last attempt is the better, and sometimes work fine.
DWORD WINAPI PaintCommandThreadProc(LPVOID lpParameter)
{
Plugin* plugin = static_cast<Plugin*>(lpParameter);
HANDLE updateEvent = plugin->updateEvent();
while (updateEvent == plugin->updateEvent())
{
::WaitForSingleObject(updateEvent, 100);
::Sleep(0);
if (updateEvent == plugin->updateEvent())
{
::PostMessage(plugin->hwnd(), WM_USER+0x10, 0, 0);
}
}
return 0;
}
...
LRESULT CALLBACK PluginWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
bool processDefault = true;
LRESULT result = 0;
Plugin* plugin = reinterpret_cast<Plugin*>( GetWindowLong(hWnd, GWL_USERDATA) );
switch (msg) {
...
case WM_GL_MESSAGE:
{
::InvalidateRect( hWnd, NULL, FALSE );
processDefault = false;
result = TRUE;
}
break;
case WM_PAINT:
{
draw(hWnd);
::SetEvent( plugin->updateEvent() );
processDefault = false;
result = TRUE;
}
break;
...
}
if (processDefault && plugin && plugin->m_wndOldProc)
result = ::CallWindowProc(plugin->m_wndOldProc, hWnd, msg, wParam, lParam);
return result;
}
On some occasions, the host application still seems to miss messages. The main characteristics of the problem are that I have to press the 'Alt' key for modal dialogs to show up ; and I have to move the mouse to give some processing time to the host application !...
Is there any 'industry standard' solution for this kind of as-often-as-you-can animation repaint problem ?
Each thread has its own message queue, and messages sent to a window arrive in the queue of the thread that created the window. If you create your plugin window yourself, you can create it in a separate thread, and that way you will have complete control over its message pump.
An alternative solution (which imho is better), is to only have OpenGL rendering in a separate thread. All OpenGL calls must occur in the thread that created the OpenGL context. However, you can create a window in one thread (your application main thread), but create the OpenGL context in another thread. That way the original application message pumps stays intact, and in your rendering thread you can loop forever doing rendering (with calls to SwapBuffers to vsync).
The main problem with that second solution is that communication between the plugin WindowProc and the rendering loop must take into account threading (ie. use locks when accessing shared memory). However since the message pump is separate from rendering, it can be simultaneous, and your message handling is as responsive as it can get.
Related
I am trying to allow a window to be dragged by the client area, and still detect WM_LBUTTONUP messages for processing. I have found two commonly suggested ways to drag the windows (listed below), both relying on using non client related functions, but they are unable to detect WM_LBUTTONUP messages upon release of the left click button.
My attempt at receiving WM_LBUTTONUP messages while being able to drag the screen by the client area is to SetTimer every millisecond on WM_LBUTTONDOWN , and use SetWindowPos to move the window with the cursor, then KillTimer on WM_LBUTTONUP. It works and detects WM_LBUTTONUP messages successfully. However, I do not like this solution as the window seems less smooth as it is being dragged around, as compared to the solutions that I found online.
I wish to have the smoothness from the nonclient based solutions. Is there a way to use them to drag the window, AND also receive WM_LBUTTONUP messages at the same time? Thank you for reading!
Example first Non Client Method found online:
case WM_LBUTTONDOWN:
mouseState = 1;
break;
case WM_LBUTTONUP:
mouseState = 0;
break;
case WM_MOUSEMOVE:
{
if (mouseState)
{
POINT CursPt;
GetCursorPos(&CursPt);
PostMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(CursPt.x, CursPt.y));
//mouseState = 0;
}
}break;
Example second Non Client Method found online:
case WM_NCHITTEST:
{
LRESULT hit = DefWindowProc(hWnd, message, wParam, lParam);
if (hit == HTCLIENT) hit = HTCAPTION;
return hit;
}
The first "example" is nonsensical, I would not use it.
The second "example" might work, but then you have to realize that if you use this, you will never receive WM_LBUTTONDOWN or WM_LBUTTONUP, you will only receive WM_NCLBUTTONDOWN and WM_NCLBUTTONUP. So, you will have to make do with WM_NCLBUTTONUP, and also pray that nothing else in your window relies on proper client-area mouse events.
Another way of doing it (which, as it turns out, does not produce good results, see discussion in the comments blow,) is to do your own window moving:
start moving on WM_LBUTTONDOWN
stop moving on WM_LBUTTONUP
use SetWindowPos() on each WM_MOUSEMOVE while the mouse button is down.
This question already has answers here:
Disable X-Button Icon on the top of the right in Messagebox Using C++ Win32 API?
(4 answers)
Closed 2 years ago.
I had this problem for quite a while now.
In my message box, I have a cancel button. I don't want the close button to interfere because my cancel button does something, which shouldn't happen if I press the close button.
I used a loop-and-find trick, but when I open a folder that also has the same title as the message box, the code disables the close button of Explorer, which is very annoying.
This was what I used to disable the close button that also interferes with other windows that have the same title:
DWORD WINAPI CreateMessageBox(void *argr){
*(int*)argr = MessageBoxA( NULL,
"Time is past active hours\nPlease save your work\nSystem is shutting down in 1 minute.\n",
"Warning",
MB_OKCANCEL|MB_ICONSTOP|MB_SYSTEMMODAL );
return 0;
}
thread = CreateThread(NULL, 0, CreateMessageBox, &opt, 0, NULL);
while (!(msg = FindWindow(NULL, "Warning")));
style = GetWindowLongPtr(msg, GWL_STYLE);
SetWindowLongPtr(msg, GWL_STYLE, style & ~WS_SYSMENU);
WaitForSingleObject(thread, INFINITE);
But now, I read somewhere that there is this SC_CLOSE thing that also disables the close button exclusively to that window. How do I use it? I've gone through some posts about it, but none of them uses MessageBox() and that just overwhelms me.
Please, can somebody help me?
Your call to FindWindow() is not specifying a class name, thus it will find any top-level window with a matching title. In your case, an Explorer window. Provide the actual class name for the MessageBox window, which is "#32770", eg:
msg = FindWindow("#32770", "Warning")
However, there is a much more reliable approach that doesn't invoke searching window titles - get rid of the calls to CreateThread() and FindWindow() completely, and instead make the thread that calls MessageBox() use a local WH_CBT hook via SetWindowsHookEx() to catch the HWND that MessageBox() creates, then you can manipulate that window as desired, eg:
LRESULT CALLBACK MyCBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
((CBT_CREATEWND*)lParam)->lpcs->style & ~WS_SYSMENU;
}
/* alternatively:
if (nCode == HCBT_ACTIVATE)
{
HWND hwnd = (HWND)wParam;
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
SetWindowLongPtr(hwnd, GWL_STYLE, style & ~WS_SYSMENU);
}
*/
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
...
HHOOK hHook = SetWindowsHookEx(WH_CBT, &MyCBTProc, NULL, GetCurrentThreadId());
opt = MessageBoxA(NULL,
"Time is past active hours\nPlease save your work\nSystem is shutting down in 1 minute.\n",
"Warning",
MB_OKCANCEL|MB_ICONSTOP|MB_SYSTEMMODAL);
if (hHook)
UnhookWindowsHookEx(hHook);
Alternatively, simply use TaskDialog() or TaskDialogIndirect() instead of MessageBox(). A Task Dialog does not have a close button in the title bar, unless you call TaskDialogIndirect() with the TDF_ALLOW_DIALOG_CANCELLATION flag enabled:
Indicates that the dialog should be able to be closed using Alt-F4, Escape, and the title bar's close button even if no cancel button is specified in either the dwCommonButtons or pButtons members.
TaskDialog(NULL, NULL,
L"Warning",
L"Time is past active hours",
L"Please save your work\nSystem is shutting down in 1 minute.",
TDCBF_OK_BUTTON | TDCBF_CANCEL_BUTTON,
TD_ERROR_ICON,
&opt);
I try to get some messages from other windows. I need to find window by name, I used FindWindowW(). How can I get active window and get message from its?
When I add hwnd in GetMessage(), it doesn't work.
//WndProc
case WM_KEYDOWN:
OutputDebugStringW(L"Key down");
break;
//main.c
//WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
hwnd = FindWindowW(NULL, L"Sublime Text");
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
Only the thread that creates a window can directly receive and dispatch messages for that window. GetMessage() retrieves messages from the calling thread's message queue, so it can only be used with windows that are owned by the calling thread.
Since you are trying to catch messages for a window that is not yours, you will have to use SetWindowsHookEx() or SetWinEventHook() to install a hook callback into that window's owning thread, and then that callback can intercept the desired messages/events for that window. You can use GetWindowThreadProcessId() to get the IDs of the Process and Thread that own the window.
If you use SetWindowsHookEx() and are trying to hook a window in another process, your callback must reside in a DLL so it can be injected into that process. You do not need to do that with SetWinEventHook().
I write simple windows c program to display the bitmap at the place where the left mouse button is pressed. At the first time when I click my left mouse button, the bitmap appears. But on the second time on words the bitmap is not getting displayed on the place where I made a left mouse click.
Here is my code.........
LRESULT CALLBACK myHandler(HWND hw, UINT m, UINT mextra, long co_ord)
{
HDC hdc, hmemdc;
PAINTSTRUCT ps;
HBITMAP hbmp;
RECT r;
HGDIOBJ holdbmp;
int x, y;
switch(m)
{
case WM_LBUTTONDOWN:
hdc = BeginPaint(hw,&ps);
hmemdc = CreateCompatibleDC(hdc);
hbmp = LoadBitmap(h, MAKEINTRESOURCE(IDB_BITMAP1));
holdbmp = SelectObject(hmemdc, hbmp);
x = LOWORD(co_ord);
y = HIWORD(co_ord);
BitBlt(hdc, x, y, 190, 220, hmemdc, 0, 0, SRCCOPY);
EndPaint(hw,&ps);
SelectObject(hmemdc, holdbmp);
DeleteObject(hbmp);
DeleteDC(hmemdc);
DeleteDC(hdc);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hw,m,mextra,co_ord);
}
return 0L;
}
The code is wrong about seven different ways from Sunday. Even after the changes you've made in response to WhozCraig's comments, it is still wrong.
For starters, the only place you are allowed to call BeginPaint and EndPaint is in response to a WM_PAINT message. You are trying to call these functions in response to a WM_LBUTTONDOWN message. That cannot work. What you'll want to do is trigger a WM_PAINT message from within your WM_LBUTTONDOWN message handler, which you can do by calling the InvalidateRect() function, passing your window handle and NULL for the rectangle to invalidate (to invalidate your entire window). Then, inside of the WM_PAINT message handler, you can call BeginPaint/EndPaint and do your drawing. If you want the drawing to be different depending on whether the left mouse button is down, you can either set a flag inside of the WM_LBUTTONDOWN message handler and test the value of that flag inside of your WM_PAINT message handler, or you can use something like GetKeyState to determine whether the mouse button is down (VK_LBUTTON).
You are also leaking GDI objects because you are not correctly releasing/destroying them. A bitmap that has been loaded with LoadBitmap needs to be destroyed by calling DeleteObject. (However, loading a bitmap repeatedly inside of a WM_PAINT message handler will lead to poor performance. Instead, prefer to load the bitmap a single time in response to the WM_CREATE message, cache its handle in a global or class-level variable, use it when necessary, and destroy the bitmap via that handle in response to the WM_DESTROY message.)
The LOWORD and HIWORD macros should never be used for extracting cursor coordinates. These are liable to return the wrong result on multiple monitor systems. Instead, you should use GET_X_LPARAM and GET_Y_LPARAM. This is specifically mentioned in the MSDN documentation for the WM_LBUTTONDOWN message. Always read the documentation for things you are unfamiliar with!
Finally, the signature for your window procedure is also completely wrong. I have no idea where you got that signature, but not only do you have non-standard parameter names that obscures the actual meaning of those arguments, but you have the wrong types. A window procedure looks like this:
LRESULT CALLBACK myHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
...
}
It is extremely difficult to learn Windows API programming by just hacking around, especially if you aren't disciplined about reading the MSDN documentation. If you really want to learn it, consider purchasing a book, like Charles Petzold's classic Programming Windows, 5th edition (yes, you need the 5th edition, not a newer edition).
I have a program that sets a global keyboard hook and handles key presses. This is my WinMain:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow)
{
MSG msg;
logFile = fopen("C:\\keylog.txt", "w");
hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyEvent, GetModuleHandle(NULL), 0);
GetMessage(&msg, NULL, 0, 0);
UnhookWindowsHookEx(hKeyHook);
fclose(logFile);
return 0;
}
Since it doesn't create any windows it won't ever receive a window message, so GetMessage would stall the program, and it is very light on CPU cycles. However, when it detects a certain key is pressed I want it to exit the program. I can't broadcast a message because it won't receive it. If I do something like:
while(State == true)
Sleep(500);
The program itself and the global hook would stall and this is undesirable. Making it Sleep(0) makes it consume an unmanageable amount of CPU cycles.
So, what is the best alternative for my problem? The only thing I can think of is going through the trouble to register my own window class and create a window to receive the message, but perhaps there is a better way?
Why not create synchronization object (maybe Event) and replace GetMessage with WaitForSingleObject?
You can easily make your keyboard hook routine trigger the object and your app will do the pre-termination stuff.
You can't broadcast a message using HWND_BROADCAST.
You don't need a window to receive messages. However, if you had some way to publish your thread id, then PostThreadMessage() could be used to send a message (probably a WM_QUIT) to your app.