如何使活动经常进行? [英] How to make the event to work often?

查看:82
本文介绍了如何使活动经常进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有按钮单击事件的MFC应用程序,该事件捕获窗口的屏幕截图并将其保存在文件中.我想制作一个用于循环的线程,在该线程中继续进行屏幕捕获.请帮助我...我正在使用以下代码,该代码在10秒的间隔内拍摄10张屏幕截图.安装后如何使它作为服务连续运行..使用此代码帮助我.....


I have created a MFC application with button click event which captures the screen shots of the window and save it in a file. I want to make a thread for looping in which the capturing of screen continues to go on. Please help me... Am using the following code which takes 10 screen shots in the interval of 10 seconds. How to make it as a service to run continuously after installing.. help me.. with this code...


void CscreensaveDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here   
	HDC hScrDC = ::GetDC(NULL);
	HDC hMemDC = NULL;

	BYTE *lpBitmapBits = NULL; 

	int nWidth = GetSystemMetrics(SM_CXSCREEN);
	int nHeight = GetSystemMetrics(SM_CYSCREEN); 

	hMemDC = ::CreateCompatibleDC(hScrDC); 

	BITMAPINFO bi; 
	ZeroMemory(&bi, sizeof(BITMAPINFO));
	bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bi.bmiHeader.biWidth = nWidth;
	bi.bmiHeader.biHeight = nHeight;
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biBitCount = 24;

	HBITMAP bitmap = ::CreateDIBSection(hMemDC, &bi, DIB_RGB_COLORS, (LPVOID*)&lpBitmapBits, NULL, 0);
	HGDIOBJ oldbmp = ::SelectObject(hMemDC, bitmap); 

	::BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);

	BITMAPFILEHEADER bh;
	ZeroMemory(&bh, sizeof(BITMAPFILEHEADER));
	bh.bfType = 0x4d42; //bitmap 
	bh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bh.bfSize = bh.bfOffBits + ((nWidth*nHeight)*3);

	CFile file;	
	char buffer[1000];
	DWORD threadId;
  int value = 10;
  //hThread = CreateThread( NULL, 0, runThread, &value, 0, &threadId);
	for(int c=0;c<10;c++)
	{
		 sprintf_s(buffer,"D:\image%u.jpg",c);
		 CString sName(buffer);  
         LPCTSTR lpszName = sName;  
		if(file.Open(lpszName, CFile::modeCreate | CFile::modeWrite))
		{ 
			file.Write(&bh, sizeof(BITMAPFILEHEADER));
			file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
			file.Write(lpBitmapBits, 3 * nWidth * nHeight);
			file.Close();
			Sleep(10000);
		}
		
	}
	::SelectObject(hMemDC, oldbmp);
	::DeleteObject(bitmap);
	::DeleteObject(hMemDC);
	::ReleaseDC(NULL, hScrDC); 

	
    
	
}

推荐答案

在CP上查看此文章: ^ ].它将向您展示如何使用MFC进行线程化.可以在这里找到更多信息: http://www.dotnetheaven.com/Uploadfile/mahesh/MultithreadingUsingMFC05212005025727AM/MultithreadingUsingMFC .aspx [ ^ ] .

从最初按下按钮开始在后台启动线程后,按钮中的逻辑(应为切换按钮)应确保在将按钮切换回其原始设置时再次停止线程.

最好的问候,

-MRB
Look at this article here on CP: Threads with MFC[^]. It''ll show you how to do threading with MFC. Some more can be found here: http://www.dotnetheaven.com/Uploadfile/mahesh/MultithreadingUsingMFC05212005025727AM/MultithreadingUsingMFC.aspx[^].

Once the thread is started in the background from pressing the button initially, the logic in your button (should be a toggle button) should assure that the thread is stopped again when the button is toggled back into it''s original setting.

Best Regards,

-MRB


您还可以使用计时器解决问题,该计时器每n毫秒触发一次,并调用您提供的方法.看看 CWnd :: SetTimer()的示例代码a> [ ^ ]
You could also solve the problem using a timer, which fires every n milliseconds, and calls a method you supply. Have a look at the sample code for CWnd::SetTimer()[^]


我真的不喜欢manfred建议的CodeProject中的第一个链接,实际上我认为这篇文章有点不好,但是第二个链接要好得多. br/>
总之,总结:
1.创建一个线程,即工作线程或UI线程.命名约定严格地是命名的,UI线程不必仅用于用户界面,它们实际上非常有用,因为它们具有消息泵,如果有的话,您可以直接向线程发送消息(使用PostThreadMessage()).不需要同步.
2.任务线程,在工作线程中,该线程通常只有一个任务,并在完成时结束,您的主程序可以等待它,也可以继续.一个UI线程可以有多个任务.
3.杀死你的线程,很好的照顾你.未能以干净的方式关闭线程将导致内存泄漏.
I don''t really like the first link within CodeProject that manfred suggested, actually I think that article is kind of bad, but the second one is much better.

Anyway, in summary:
1. Create a thread, either worker thread or UI thread. Naming convention is strictly naming, UI threads don''t have to be for just user interfaces, they''re actually very useful since they have a message pump allowing you to message to the thread directly (using PostThreadMessage()) if there''s no requirement for synchronicity.
2. Task your thread, in a worker thread, the thread will generally only have one task and will end when its finished, your main program can either wait for it or just continue. A UI thread can have multiple tasks.
3. Kill your thread, nicely mind you. Failing to bring a thread down in a clean manner will result in leaked memory.


这篇关于如何使活动经常进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆