ARGH
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Performance / src / MainWnd.cpp
1 //////////////////////////////////////////////\r
2 // MainWnd.cpp\r
3 //  Definitions for the CMainWindow class\r
4 \r
5 #include "stdafx.h"\r
6 #include "PerfApp.h"\r
7 #include "MainWnd.h"\r
8 #include "resource.h"\r
9 \r
10 \r
11 CMainWindow::CMainWindow() : m_nTestMessages(0), m_nTestWindows(0)\r
12 {\r
13 }\r
14 \r
15 CMainWindow::~CMainWindow()\r
16 {\r
17 }\r
18 \r
19 HWND CMainWindow::Create(CWnd* pParent /*= 0*/)\r
20 {\r
21         CString str = _T("Main Window");\r
22 \r
23         // Create the main window\r
24         return CreateEx(WS_EX_TOPMOST, NULL, str, WS_OVERLAPPEDWINDOW | WS_VISIBLE,\r
25                 20 , 50, 400, 300, pParent, NULL);\r
26 }\r
27 \r
28 void CMainWindow::CreateTestWindows(int nWindows)\r
29 {\r
30         m_nTestWindows = nWindows;\r
31 \r
32         for (int i = 0 ; i < nWindows ; i++)\r
33         {\r
34                 // Create the test windows\r
35                 TestWindowPtr pTestWindow = new CTestWindow();\r
36                 m_pCTestWindows.push_back(pTestWindow);\r
37                 m_pCTestWindows[i]->CreateWin(i);\r
38         }\r
39 }\r
40 \r
41 void CMainWindow::OnCreate()\r
42 {\r
43         CRect r = GetClientRect();\r
44         DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL;\r
45 \r
46         // Create an Edit window over the client area of the main window\r
47         m_Edit.CreateEx(0L, _T("Edit"), _T(""), dwStyle, r.left, r.top, r.right - r.left, r.bottom - r.top,\r
48                                                 this, NULL, NULL);\r
49 \r
50         // Set a default font\r
51         m_Font.CreateFont(16, 0, 0, 0, FW_DONTCARE, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,\r
52                         CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_MODERN, _T("Courier New"));\r
53         m_Edit.SendMessage(WM_SETFONT, (WPARAM)m_Font.GetHandle(), 0);\r
54 }\r
55 \r
56 void CMainWindow::OnInitialUpdate()\r
57 {\r
58         CMyDialog MyDialog(IDD_DIALOG1, this);\r
59         MyDialog.DoModal();\r
60 }\r
61 \r
62 void CMainWindow::OnSize()\r
63 {\r
64         CRect r = GetClientRect();\r
65 \r
66         // Resize the edit window when the main window is resized\r
67         m_Edit.MoveWindow(0, 0, r.right - r.left, r.bottom - r.top, TRUE);\r
68 }\r
69 \r
70 void CMainWindow::OnAllWindowsCreated()\r
71 {\r
72         CString str;\r
73         str.Format(_T("%d  Windows Created"), m_nTestWindows);\r
74         SendText(str);\r
75         SendText(_T("Ready to run performance test"));\r
76 \r
77         int nResult = MessageBox(_T("Start the Performance Test?"), _T("Ready"), MB_OKCANCEL | MB_ICONEXCLAMATION);\r
78         if (nResult != IDOK) return;\r
79 \r
80         PerformanceTest();\r
81 \r
82         // Loop the performance test\r
83         nResult = IDYES;\r
84         while(nResult == IDYES)\r
85         {\r
86                 nResult = MessageBox(_T("Run Test Again?"), _T("Ready"), MB_YESNO | MB_ICONEXCLAMATION);\r
87                 if (nResult != IDYES) break;\r
88 \r
89                 PerformanceTest();\r
90         }\r
91         SendText(_T("Testing complete"));\r
92 }\r
93 \r
94 void CMainWindow::PerformanceTest()\r
95 {\r
96         LRESULT lr = 0;\r
97         int nMessages = 0;\r
98 \r
99         SendText(_T(""));\r
100         CString str;\r
101         str.Format(str, _T("Sending %d  Messages"), m_nTestMessages);\r
102         SendText(str);\r
103 \r
104         // Choose a Window handle(HWND) to send the messages to\r
105         HWND hWnd = m_pCTestWindows[(m_nTestWindows-1)/2]->GetHwnd();\r
106 \r
107         // Store the starting time\r
108         DWORD tStart = ::GetTickCount();\r
109 \r
110         // Send the messages\r
111         while(nMessages++ < m_nTestMessages)\r
112         lr = ::SendMessage(hWnd, WM_TESTMESSAGE, 0, 0);\r
113 \r
114         // Calculate the time the messages took to send\r
115         DWORD tEnd = ::GetTickCount();\r
116         DWORD mSeconds = tEnd - tStart;\r
117 \r
118         // Display the results\r
119         str.Format(_T("%d milliseconds to process %d messages"), mSeconds, m_nTestMessages);\r
120         SendText(str);\r
121 \r
122         str.Format(_T("%d total messages sent\n"), lr);\r
123         TRACE(str);\r
124 \r
125         MessageBox(str, _T("Info"), MB_OK);\r
126 }\r
127 \r
128 void CMainWindow::SendText(LPCTSTR str)\r
129 {\r
130         // Send text to the Edit window\r
131         m_Edit.SendMessage(EM_REPLACESEL,  (WPARAM)FALSE, (LPARAM)str);\r
132         m_Edit.SendMessage(EM_REPLACESEL,  (WPARAM)FALSE, (LPARAM)_T("\r\n"));\r
133         m_Edit.SendMessage(EM_SCROLLCARET, (WPARAM)0,     (LPARAM)0);\r
134 \r
135         TRACE(str);\r
136         TRACE(_T("\n"));\r
137 }\r
138 \r
139 LRESULT CMainWindow::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
140 {\r
141         static int nWindowsCreated = 0;\r
142 \r
143         switch (uMsg)\r
144         {\r
145         case WM_DESTROY:\r
146                 // End the program.\r
147                 ::PostQuitMessage(0);\r
148                 break;\r
149 \r
150         case WM_SIZE:\r
151                 OnSize();\r
152                 break;\r
153 \r
154         case WM_WINDOWCREATED:\r
155                 // Message recieved when a test window is created\r
156                 if (++nWindowsCreated == m_nTestWindows)\r
157                         OnAllWindowsCreated();\r
158                 break;\r
159         }\r
160 \r
161         return WndProcDefault(uMsg, wParam, lParam);\r
162 }\r
163 \r

UCC git Repository :: git.ucc.asn.au