简体   繁体   English

C ++ Win32:使用SendMessage从另一个线程调用WM_PAINT,绘图无效

[英]C++ Win32: Calling WM_PAINT from another thread using SendMessage, drawing has no effect

I am currently developing a program (C++ Win32, Visual Studio 2012) that captures audio signals from a microphone, computes a Fourier Transformation (you know, to get the frequencies in the audio) in another thread, and finally plots the results on a screen. 我目前正在开发一个程序(C ++ Win32,Visual Studio 2012),该程序捕获来自麦克风的音频信号,在另一个线程中计算傅立叶变换(您知道,以获取音频中的频率),最后将结果绘制在屏幕上。

Everything works fine, except for the plotting, so this is a GUI related topic. 除了绘图以外,其他所有东西都可以正常工作,所以这是一个与GUI相关的主题。

In WndProc, I get a message when a buffer is full and ready to be processed, and I call 在WndProc中,当缓冲区已满并准备好处理时,我会收到一条消息,然后我调用

boost::thread t(&ComputationThread,data);

to start the computation thread. 启动计算线程。

At the end of ComputationThread I send a message to the GUI thread to plot the data, using a predefined fixed value and the data pointer as arguments: ComputationThread的末尾,我使用预定义的固定值和数据指针作为参数,向GUI线程发送一条消息以绘制数据:

SendMessage(hWnd, WM_PAINT, PARAM_FFT_COMPUTED, (LPARAM)data);

This works fine as well, I get the message and I can also write the computed data to a file, however, drawing on the form (eg using SetPixel ) has no effect, while it has an effect when wParam is not PARAM_FFT_COMPUTED , for example when the form is resized. 这也可以正常工作,我得到了消息,也可以将计算的数据写入文件,但是,在表单上绘制(例如,使用SetPixel )无效,而当wParam 不是 PARAM_FFT_COMPUTED ,则有效调整表格大小时。

InvalidateRect has no effect either. InvalidateRectInvalidateRect

What am I doing wrong? 我究竟做错了什么?

Thanks in advance! 提前致谢!

You are not meant to send the WM_PAINT . 您无意发送WM_PAINT It's a specially synthesised message. 这是一条特别合成的消息。 You never post it or send it. 您永远不会发布或发送它。 Instead you call InvalidateRect or one of the related functions to get the system to generate WM_PAINT . 而是调用InvalidateRect或相关函数之一来使系统生成WM_PAINT

And you are not meant to draw on a window outside of a WM_PAINT message handler. 并且您无意在WM_PAINT消息处理程序之外的窗口上绘制。 Or from a thread other than the GUI thread. 或来自GUI线程以外的其他线程。 Perhaps you aren't doing any of those things, but they are common mistakes so it doesn't hurt to mention it. 也许您没有做任何这些事情,但是它们是常见的错误,因此提起来也没有什么害处。

If InvalidateRect does not work then something fundamental is wrong with your code. 如果InvalidateRect无法正常工作,则您的代码存在一些根本问题。 You need to fix that rather than try to make painting work by some other mechanism. 您需要解决此问题,而不是尝试通过其他某种机制使绘画工作。

So, the solution is to use InvalidateRect and fix whatever is broken in your WM_PAINT handler. 因此,解决方案是使用InvalidateRect并修复WM_PAINT处理程序中损坏的所有内容。 Without seeing your WM_PAINT handler I would not like to speculate as to what is wrong. 在没有看到您的WM_PAINT处理程序的情况下,我不想推测出什么错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM