Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my application i need to draw some shapes over video continuously.
i have used direct show for video play back & gdi for drawing shapes

now suppose for drawing shapes i have used following function


void  threadPaint( void * )
{
	WINDOWPLACEMENT lpwndpl_v1;   
	GetWindowPlacement(hw,&lpwndpl_v1); // hw is handler to picture box control (used for video playback) 

	HDC hdc;
	hdc = BeginPaint(hw, &ps);                
	Graphics graphics(hw);      
	Pen  pen3(Color(255, 0,0,255),10);
	while(threadAna) /////////
	 {
      graphics.DrawLine(&pen3,lpwndpl_v1.rcNormalPosition.left+40,lpwndpl_v1.rcNormalPosition.top+90,lpwndpl_v1.rcNormalPosition.right,lpwndpl_v1.rcNormalPosition.top+90);
     }
EndPaint(hw, &ps);  
}



i call this thread using

threadAna=1;
_beginthread( threadPaint, 0, (void*)12 );
Sleep(20);

in my application i used function SetWindowPos() & ShowWindow() at various places.

for example at WM_MOUSEMOVE

::SetWindowPos(GetDlgItem(g_hDialogWindow, IDC_MODE_TEXT), HWND_TOPMOST,  lpwndpl_home.rcNormalPosition.left+9,  lpwndpl_home.rcNormalPosition.bottom, 55,18,   SWP_NOZORDER );      

SetWindowText(GetDlgItem(g_hDialogWindow, IDC_MODE_TEXT), "HOME");
ShowWindow(GetDlgItem(g_hDialogWindow, IDC_MODE_TEXT), TRUE);


now my problem is that whenever i mouse move or press some button where i used SetWindowPos() & ShowWindow() whole window is affected(i think its updated in unusual way).

if i did not use the thread then application runs fine..
Posted
Updated 31-Jan-12 20:48pm
v2
Comments
Emilio Garavaglia 1-Feb-12 2:50am    
Where do you handle WM_PAINT ?
pranav_30 1-Feb-12 3:50am    
in same main.cpp file where is my thread present but suppose in wm_paint i did not write anything
pranav_30 1-Feb-12 4:01am    
suppose instead of thread if i call the threadPaint( void * ) function from WM_TIMER (where i set the timer event for 10 ms) then applications runs fine(no crashes & no abnormal window update) but drawing of line over video is not smooth as like using thread.

1 solution

Try to use the following principles:

Do all your rendering only in the UI thread, but the result of rendering will depends on some data, and the data should be shared between threads, interlocked using Critical Section. Better yet, use multiple readers / single-writer lock, it is available in Windows, boost, etc.:
http://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock[^],
see also this CodeProject article: Ultra-simple C++ Read/Write Lock Class for Windows[^].

In the case of multi-reader lock, the writer is a non-UI thread which modifies data.

When data is modified, trigger sending WM_PAINT Windows message by calling one of Invalidate functions:
http://msdn.microsoft.com/en-us/library/ax04k970%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd145002%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/2f3csed3%28v=vs.80%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd145003%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/532h978c%28v=vs.80%29.aspx[^].

You can invalidate rectangle or region to gain some performance by repainting only a part of the scene.

See also: http://msdn.microsoft.com/en-us/library/dd162759%28v=vs.85%29.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900