Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /WinCS/APIGraphicsWindow.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations) (download) (as text)
Wed Feb 10 18:21:00 2010 UTC (14 years, 2 months ago) by sho1get
File MIME type: text/x-c++src
File size: 10454 byte(s)


1 sho1get 11 #include "stdafx.h"
2     #include "WinCS.h"
3     #include "APIGraphicsWindow.h"
4     #include "APIReportDlg.h"
5    
6     IMPLEMENT_DYNAMIC(CAPIGraphicsWindow, CWnd)
7    
8     BEGIN_MESSAGE_MAP(CAPIGraphicsWindow, CWnd)
9     ON_WM_CREATE()
10     ON_WM_CLOSE()
11     ON_WM_DESTROY()
12     ON_MESSAGE(WM_SHOW_LOG, &CAPIGraphicsWindow::OnShowLog)
13     ON_MESSAGE(WM_OUTPUT_REPORT, &CAPIGraphicsWindow::OnOutputReport)
14     END_MESSAGE_MAP()
15    
16     //////////////////////////////////////////////////////////////////////////
17    
18     CAPIGraphicsWindow::CAPIGraphicsWindow() :
19     m_nWidth(0),
20     m_nHeight(0),
21     m_dwNodes(0),
22     m_dwProcessors(0),
23     m_fDrawing(FALSE),
24     m_fView(FALSE),
25     m_fSystem(FALSE),
26     m_fSaveLog(FALSE)
27     {
28     ZeroMemory(&m_CalcSize, sizeof(m_CalcSize));
29     m_Timer.hTimer = NULL;
30     m_hIcon = AfxGetApp()->LoadIcon(IDI_PLUGIN);
31     }
32    
33     CAPIGraphicsWindow::~CAPIGraphicsWindow()
34     {
35     }
36    
37     BOOL CAPIGraphicsWindow::ForceClose()
38     {
39     CSingleLock sl(&m_DrawLock, TRUE);
40    
41     if (IsWindow(m_hWnd) && m_fDrawing)
42     {
43     m_fDrawing = FALSE;
44     m_fView = FALSE;
45     StopTimer();
46     SendMessage(WM_CLOSE);
47     return TRUE;
48     }
49    
50     return FALSE;
51     }
52    
53     void CAPIGraphicsWindow::FinishDrawing()
54     {
55     CString cs, csTime;
56    
57     m_fDrawing = FALSE;
58    
59     if (IsWindow(m_hWnd))
60     {
61     m_Stopwatch.GetHMS(csTime);
62     cs.Format(_T("%s (%s) - (Completed)"), m_csWndName, csTime);
63     SetWindowText(cs);
64     }
65     }
66    
67     // static
68     void WINAPI CAPIGraphicsWindow::doRuntimeTimer(LPVOID pvContext, BOOLEAN fTimeout)
69     {
70     ((CAPIGraphicsWindow *)pvContext)->RuntimeTimer();
71     }
72    
73     void CAPIGraphicsWindow::RuntimeTimer()
74     {
75     CString cs, csTime;
76    
77     if (IsWindow(m_hWnd) && m_fDrawing)
78     {
79     m_Stopwatch.GetHMS(csTime);
80    
81     if (m_fSystem)
82     {
83     cs.Format(_T("%s (%s) - (%d / %d)"), m_csWndName, csTime, m_nRecvCount, m_nTotalCount);
84     }
85     else
86     {
87     cs.Format(_T("%s (%s) - (Processing)"), m_csWndName, csTime);
88     }
89    
90     SetWindowText(cs);
91     }
92     }
93    
94     void CAPIGraphicsWindow::GetTimer(CString &cs)
95     {
96     m_Stopwatch.GetHMS(cs);
97     }
98    
99     BOOL CAPIGraphicsWindow::StartTimer()
100     {
101     StopTimer();
102    
103     CSingleLock sl(&m_Timer.cs, TRUE);
104     m_Stopwatch.Start();
105    
106     if (!CreateTimerQueueTimer(&m_Timer.hTimer, NULL, doRuntimeTimer, this, 1000, 1000, 0))
107     {
108     m_Timer.hTimer = NULL;
109     return FALSE;
110     }
111    
112     return TRUE;
113     }
114    
115     void CAPIGraphicsWindow::StopTimer()
116     {
117     CSingleLock sl(&m_Timer.cs, TRUE);
118    
119     if (m_Timer.hTimer != NULL)
120     {
121     DeleteTimerQueueTimer(NULL, m_Timer.hTimer, NULL);
122     m_Timer.hTimer = NULL;
123     }
124     }
125    
126     BOOL CAPIGraphicsWindow::Create(const CString &csName, int cx, int cy, BOOL bFront)
127     {
128     CString strClass;
129     DWORD dwStyle, dwExStyle;
130     CRect rect;
131    
132     try
133     {
134     strClass = AfxRegisterWndClass(0, 0, 0, 0);
135     }
136     catch (CResourceException *e)
137     {
138     TRACE0("***** ERROR: AfxRegisterWndClass *****\n");
139     e->Delete();
140     return FALSE;
141     }
142    
143     if (!strClass.IsEmpty())
144     {
145     dwExStyle = bFront ? WS_EX_PALETTEWINDOW : WS_EX_OVERLAPPEDWINDOW;
146     dwStyle = (WS_POPUP | WS_CAPTION | WS_VISIBLE | WS_SYSMENU |WS_BORDER);
147    
148     m_csWndName = csName;
149     m_nWidth = cx;
150     m_nHeight = cy;
151     rect.SetRect(0, 0, m_nWidth, m_nHeight);
152    
153     if (CreateEx(dwExStyle, strClass, m_csWndName, dwStyle, rect, NULL, NULL))
154     {
155     ResetWindow();
156     return TRUE;
157     }
158     else
159     {
160     TRACE1("***** ERROR: CreateEx(%d) *****\n", GetLastError());
161     return FALSE;
162     }
163     }
164    
165     return FALSE;
166     }
167    
168     int CAPIGraphicsWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
169     {
170     if (CWnd::OnCreate(lpCreateStruct) == -1)
171     {
172     return -1;
173     }
174    
175     SetIcon(m_hIcon, TRUE);
176     SetIcon(m_hIcon, FALSE);
177    
178     m_fDrawing = TRUE;
179     m_fView = TRUE;
180    
181     return 0;
182     }
183    
184     void CAPIGraphicsWindow::OnClose()
185     {
186     if (m_fDrawing)
187     {
188     return;
189     }
190    
191     CWnd::OnClose();
192     }
193    
194     void CAPIGraphicsWindow::OnDestroy()
195     {
196     CWnd::OnDestroy();
197    
198     m_nWidth = 0;
199     m_nHeight = 0;
200     m_fDrawing = FALSE;
201     m_fView = FALSE;
202     }
203    
204     BOOL CAPIGraphicsWindow::SetPixel(int x, int y, COLORREF color)
205     {
206     if (IsWindow(m_hWnd) && m_fView)
207     {
208     CSingleLock sl(&m_DrawLock, TRUE);
209     CClientDC dc(this);
210    
211     if (m_fDrawing)
212     {
213     ASSERT(IsWindow(m_hWnd));
214     dc.SetPixel(x, y, color);
215     return TRUE;
216     }
217     }
218    
219     return FALSE;
220     }
221    
222     BOOL CAPIGraphicsWindow::SetXLine(int y, int cx, LPCOLORREF lpColor)
223     {
224     if (IsWindow(m_hWnd) && m_fView)
225     {
226     CSingleLock sl(&m_DrawLock, TRUE);
227     CClientDC dc(this);
228     CBitmap Bmp, *pBmp;
229     CDC MemDC, *pDC;
230    
231     if (m_fDrawing)
232     {
233     pDC = GetDC();
234     MemDC.CreateCompatibleDC(pDC);
235     Bmp.CreateCompatibleBitmap(pDC, cx, 1);
236     pBmp = MemDC.SelectObject(&Bmp);
237    
238     for (int i = 0; i < cx; i++)
239     {
240     MemDC.SetPixel(i, 0, lpColor[i]);
241     }
242    
243     dc.BitBlt(0, y, cx, 1, &MemDC, 0, 0, SRCCOPY);
244     MemDC.SelectObject(pBmp);
245     MemDC.DeleteDC();
246     Bmp.DeleteObject();
247     ReleaseDC(pDC);
248    
249     return TRUE;
250     }
251     }
252    
253     return FALSE;
254     }
255    
256     BOOL CAPIGraphicsWindow::SetYLine(int x, int cy, LPCOLORREF lpColor)
257     {
258     if (IsWindow(m_hWnd) && m_fView)
259     {
260     CSingleLock sl(&m_DrawLock, TRUE);
261     CClientDC dc(this);
262     CBitmap Bmp, *pBmp;
263     CDC MemDC, *pDC;
264    
265     if (m_fDrawing)
266     {
267     ASSERT(IsWindow(m_hWnd));
268    
269     pDC = GetDC();
270     MemDC.CreateCompatibleDC(pDC);
271     Bmp.CreateCompatibleBitmap(pDC, 1, cy);
272     pBmp = MemDC.SelectObject(&Bmp);
273    
274     for (int i = 0; i < cy; i++)
275     {
276     MemDC.SetPixel(0, i, lpColor[i]);
277     }
278    
279     dc.BitBlt(x, 0, 1, cy, &MemDC, 0, 0, SRCCOPY);
280     MemDC.SelectObject(pBmp);
281     MemDC.DeleteDC();
282     Bmp.DeleteObject();
283     ReleaseDC(pDC);
284    
285     return TRUE;
286     }
287     }
288    
289     return FALSE;
290     }
291    
292     void CAPIGraphicsWindow::SetWindowSize(int cx, int cy, int x, int y)
293     {
294     CSingleLock sl(&m_DrawLock, TRUE);
295    
296     if (IsWindow(m_hWnd))
297     {
298     MoveWindow(x, y, cx, cy);
299     }
300     }
301    
302     void CAPIGraphicsWindow::SetCenterWindow()
303     {
304     CSingleLock sl(&m_DrawLock, TRUE);
305    
306     if (IsWindow(m_hWnd))
307     {
308     CenterWindow(GetDesktopWindow());
309     }
310     }
311    
312     void CAPIGraphicsWindow::ResetWindow()
313     {
314     if (IsWindow(m_hWnd) && m_fView)
315     {
316     CSingleLock sl(&m_DrawLock, TRUE);
317     CClientDC dc(this);
318     CRect rect;
319    
320     if (m_fDrawing)
321     {
322     GetClientRect(&rect);
323     dc.FillSolidRect(&rect, RESET_WINDOW_COLOR);
324     }
325     }
326     }
327    
328     void CAPIGraphicsWindow::OutputReportList()
329     {
330     CAPIReportDlg dlg(this);
331     UINT nSize = m_ReportList.GetSize();
332    
333     if (IsWindow(m_hWnd) && nSize > 0)
334     {
335     if (m_fSaveLog)
336     {
337     SaveReport();
338     }
339    
340     dlg.SetReportList(m_ReportList);
341     dlg.SetLog(m_fSaveLog);
342     dlg.DoModal();
343     }
344     }
345    
346     void CAPIGraphicsWindow::InitRenderingCount(UINT nTotal)
347     {
348     CSingleLock sl(&m_RgCountLock, TRUE);
349     m_nRecvCount = 0;
350     m_nTotalCount = nTotal;
351     }
352    
353     void CAPIGraphicsWindow::AddRenderingCount()
354     {
355     m_RgCountLock.Lock();
356     m_nRecvCount++;
357     m_RgCountLock.Unlock();
358     RuntimeTimer(); // Redraw
359     }
360    
361     BOOL CAPIGraphicsWindow::SaveReport()
362     {
363     if (IsWindow(m_hWnd) && m_fView)
364     {
365     CSingleLock sl(&m_DrawLock, TRUE);
366     SYSTEMTIME st;
367     CClientDC dc(this);
368     CBitmap bmp;
369     CString cs, csReport, csText, csTime, csImgPath, csTxtPath, csDirPath;
370     HANDLE hFile;
371     double dbAvg, dbLate, dbFast;
372     CImage img;
373     DWORD dwFileSize, dwWriteSize;
374     TCHAR szLogPath[_MAX_PATH * 2];
375     CRect cr;
376     UINT nSize;
377     CDC cdc;
378    
379     GetLocalTime(&st);
380     csDirPath.Format(_T("./Log/Plugin/%s_%04d%02d%02d%02d%02d%02d"),
381     m_csWndName,
382     st.wYear, st.wMonth, st.wDay,
383     st.wHour, st.wMinute, st.wSecond);
384     csTxtPath.Format(_T("%s/index.html"), csDirPath);
385     csImgPath.Format(_T("%s/img.png"), csDirPath);
386     _tfullpath(szLogPath, csTxtPath, MC_ARYSIZE(szLogPath) - sizeof(TCHAR));
387     m_LogPath.Format(_T("%s"), szLogPath);
388    
389     if (!CreateDirectory(csDirPath, NULL))
390     {
391     return FALSE;
392     }
393    
394     // Image
395     GetClientRect(&cr);
396     bmp.CreateCompatibleBitmap(&dc, cr.Width(), cr.Height());
397     cdc.CreateCompatibleDC(&dc);
398     cdc.SelectObject(bmp);
399     cdc.BitBlt(0, 0, cr.Width(), cr.Height(), &dc, 0, 0, SRCCOPY);
400     img.Attach(static_cast<HBITMAP>(bmp));
401     img.Save(csImgPath);
402    
403     // HTML
404     nSize = m_ReportList.GetSize();
405     dbAvg = 0.0;
406     dbLate = 0.0;
407     dbFast = static_cast<double>(INT_MAX);
408    
409     for (UINT i = 0; i < nSize; i++)
410     {
411     cs.Format(LOG_TEMPLATE_HTML_PLUGIN_DATA,
412     m_ReportList[i].wNodeID,
413     m_ReportList[i].wSubID,
414     DwToIPAddress(m_ReportList[i].dwAddress),
415     m_ReportList[i].dbRuntime);
416     csReport += cs;
417    
418     dbAvg += m_ReportList[i].dbRuntime;
419     dbLate = max(dbLate, m_ReportList[i].dbRuntime);
420     dbFast = min(dbFast, m_ReportList[i].dbRuntime);
421     }
422    
423     dbAvg = dbAvg / nSize;
424     csTime.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"),
425     st.wYear, st.wMonth, st.wDay,
426     st.wHour, st.wMinute, st.wSecond);
427     csText.Format(LOG_TEMPLATE_HTML_PLUGIN_CONTAINER,
428     m_csWndName, // Title
429     m_csWndName, // Plugin name
430     m_fSystem ? _T("System") : _T("Single"), // Mode
431     csTime, // DateTime
432     m_dwNodes, // Nodes
433     m_dwProcessors, // Processors
434     m_CalcSize.nWidth, // Width
435     m_CalcSize.nHeight, // Height
436     m_CalcSize.nRange, // Range
437     dbAvg, // Average
438     dbFast, // Fastest
439     dbLate, // Latest
440     csReport); // Report data
441    
442     hFile = CreateFile(csTxtPath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
443     FILE_ATTRIBUTE_NORMAL, NULL);
444    
445     if (hFile != INVALID_HANDLE_VALUE)
446     {
447     dwFileSize = csText.GetLength() * sizeof(TCHAR);
448     WriteFile(hFile, csText, dwFileSize, &dwWriteSize, NULL);
449     CloseHandle(hFile);
450     return TRUE;
451     }
452     }
453    
454     return FALSE;
455     }
456    
457     LRESULT CAPIGraphicsWindow::OnShowLog(WPARAM wParam, LPARAM lParam)
458     {
459     SHELLEXECUTEINFO sei;
460     CString cs;
461    
462     ZeroMemory(&sei, sizeof(sei));
463     sei.cbSize = sizeof(SHELLEXECUTEINFO);
464     sei.hwnd = m_hWnd;
465     sei.nShow = SW_SHOWNORMAL;
466     sei.fMask = SEE_MASK_NOCLOSEPROCESS;
467     sei.lpFile = MC_CStoSTR(m_LogPath);
468    
469     if(!ShellExecuteEx(&sei) || reinterpret_cast<int>(sei.hInstApp) <= 32)
470     {
471     TRACE1("***** ERROR: OnShowLog(%d) *****\n", GetLastError());
472     cs.LoadString(IDS_WCS_ERROR_SHOW_LOG);
473     AfxMessageBox(cs);
474     return FALSE;
475     }
476    
477     WaitForSingleObject(sei.hProcess, INFINITE);
478     CloseHandle(sei.hProcess);
479    
480     return TRUE;
481     }
482    
483     LRESULT CAPIGraphicsWindow::OnOutputReport(WPARAM wParam, LPARAM lParam)
484     {
485     return OutputReportList(), TRUE;
486     }
487    
488     //////////////////////////////////////////////////////////////////////////
489    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26