Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. // Okno.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "lab3.h"
  6. #include "Okno.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #endif
  11.  
  12. //TODO: Deklaracje zmiennych globalnych
  13. int volatile przerwij;
  14. CString wynik;
  15. char volatile JestWynik;
  16. int CzasyPracy[5];
  17. int volatile licznik;
  18. CCriticalSection sk;
  19. CEvent czekam;
  20.  
  21.  
  22. #define WM_WIADOMOSC (WM_USER+100)
  23.  
  24. // CAboutDlg dialog used for App About
  25.  
  26. class CAboutDlg : public CDialog
  27. {
  28. public:
  29. CAboutDlg();
  30.  
  31. // Dialog Data
  32. enum { IDD = IDD_ABOUTBOX };
  33.  
  34. protected:
  35. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  36.  
  37. // Implementation
  38. protected:
  39. DECLARE_MESSAGE_MAP()
  40. };
  41.  
  42. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  43. {
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CDialog::DoDataExchange(pDX);
  49. }
  50.  
  51. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  52. END_MESSAGE_MAP()
  53.  
  54.  
  55. // COkno dialog
  56.  
  57.  
  58.  
  59. COkno::COkno(CWnd* pParent /*=NULL*/)
  60. : CDialog(COkno::IDD, pParent)
  61. {
  62. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  63. }
  64.  
  65. void COkno::DoDataExchange(CDataExchange* pDX)
  66. {
  67. CDialog::DoDataExchange(pDX);
  68. DDX_Control(pDX, IDC_EKRAN, m_ekran);
  69. DDX_Control(pDX, IDC_W1, m_w1);
  70. DDX_Control(pDX, IDC_W2, m_w2);
  71. DDX_Control(pDX, IDC_W3, m_w3);
  72. DDX_Control(pDX, IDC_W4, m_w4);
  73. DDX_Control(pDX, IDC_W5, m_w5);
  74. DDX_Control(pDX, IDC_URUCHOM, m_uruchom);
  75. DDX_Control(pDX, IDC_PRZERWIJ, m_przerwij);
  76. DDX_Control(pDX, IDC_KONIEC, m_koniec);
  77. }
  78.  
  79. BEGIN_MESSAGE_MAP(COkno, CDialog)
  80. ON_WM_SYSCOMMAND()
  81. ON_WM_PAINT()
  82. ON_WM_QUERYDRAGICON()
  83. //}}AFX_MSG_MAP
  84. ON_MESSAGE(WM_WIADOMOSC, OnWiadomosc)
  85. ON_BN_CLICKED(IDC_URUCHOM, OnBnClickedUruchom)
  86. ON_WM_TIMER()
  87. ON_BN_CLICKED(IDC_PRZERWIJ, OnBnClickedPrzerwij)
  88. ON_BN_CLICKED(IDC_KONIEC, OnBnClickedKoniec)
  89. ON_WM_DESTROY()
  90. END_MESSAGE_MAP()
  91.  
  92.  
  93. // COkno message handlers
  94.  
  95. BOOL COkno::OnInitDialog()
  96. {
  97. CDialog::OnInitDialog();
  98.  
  99. // Add "About..." menu item to system menu.
  100.  
  101. // IDM_ABOUTBOX must be in the system command range.
  102. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  103. ASSERT(IDM_ABOUTBOX < 0xF000);
  104.  
  105. CMenu* pSysMenu = GetSystemMenu(FALSE);
  106. if (pSysMenu != NULL)
  107. {
  108. CString strAboutMenu;
  109. strAboutMenu.LoadString(IDS_ABOUTBOX);
  110. if (!strAboutMenu.IsEmpty())
  111. {
  112. pSysMenu->AppendMenu(MF_SEPARATOR);
  113. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  114. }
  115. }
  116.  
  117. // Set the icon for this dialog. The framework does this automatically
  118. // when the application's main window is not a dialog
  119. SetIcon(m_hIcon, TRUE); // Set big icon
  120. SetIcon(m_hIcon, FALSE); // Set small icon
  121.  
  122. // Add extra initialization here
  123. srand((unsigned )time(0));
  124. SetTimer(1,4000,0);
  125.  
  126.  
  127. return TRUE; // return TRUE unless you set the focus to a control
  128. }
  129.  
  130. void COkno::OnSysCommand(UINT nID, LPARAM lParam)
  131. {
  132. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  133. {
  134. CAboutDlg dlgAbout;
  135. dlgAbout.DoModal();
  136. }
  137. else
  138. {
  139. CDialog::OnSysCommand(nID, lParam);
  140. }
  141. }
  142.  
  143. // If you add a minimize button to your dialog, you will need the code below
  144. // to draw the icon. For MFC applications using the document/view model,
  145. // this is automatically done for you by the framework.
  146.  
  147. void COkno::OnPaint()
  148. {
  149. if (IsIconic())
  150. {
  151. CPaintDC dc(this); // device context for painting
  152.  
  153. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  154.  
  155. // Center icon in client rectangle
  156. int cxIcon = GetSystemMetrics(SM_CXICON);
  157. int cyIcon = GetSystemMetrics(SM_CYICON);
  158. CRect rect;
  159. GetClientRect(&rect);
  160. int x = (rect.Width() - cxIcon + 1) / 2;
  161. int y = (rect.Height() - cyIcon + 1) / 2;
  162.  
  163. // Draw the icon
  164. dc.DrawIcon(x, y, m_hIcon);
  165. }
  166. else
  167. {
  168. CDialog::OnPaint();
  169. }
  170. }
  171.  
  172. // The system calls this function to obtain the cursor to display while the user drags
  173. // the minimized window.
  174. HCURSOR COkno::OnQueryDragIcon()
  175. {
  176. return static_cast<HCURSOR>(m_hIcon);
  177. }
  178.  
  179. UINT COkno::WatekRoboczy(void * pParametr)
  180. {
  181.  
  182. /*TODO: Implementacja watku roboczego*/
  183.  
  184. int i= (int)pParametr;
  185. licznik++;
  186. do{
  187. if(przerwij==1)
  188. {
  189. licznik--;
  190. return 0;
  191. }
  192. AfxGetMainWnd() ->PostMessage(WM_WIADOMOSC,i,1);
  193. Sleep(500);
  194. CzasyPracy[i]=CzasyPracy[i]-1;
  195. if(CzasyPracy[i]==0)
  196. break;
  197. }while(1);
  198. if(przerwij==1)
  199. {
  200. licznik--;
  201. return 0;
  202. }
  203. AfxGetMainWnd() ->PostMessage(WM_WIADOMOSC,i,2);
  204. sk.Lock();
  205. if(przerwij==1)
  206. {
  207. licznik--;
  208. return 0;
  209. }
  210. wynik = "Watek zakonczyl sie\r\n";
  211. AfxGetMainWnd() ->PostMessage(WM_WIADOMOSC,i,3);
  212. czekam.Lock();
  213. sk.Unlock();
  214. if(przerwij==1)
  215. {
  216. licznik--;
  217. return 0;
  218. }
  219. AfxGetMainWnd() ->PostMessage(WM_WIADOMOSC,i,4);
  220. licznik--;
  221. return 0;
  222. }
  223.  
  224. void COkno::OnBnClickedUruchom()
  225. {
  226. m_uruchom.EnableWindow(FALSE);
  227. m_przerwij.EnableWindow();
  228.  
  229. //TODO: Obsluga klikniecia przycisku "Uruchom"
  230. przerwij=0;
  231. for(int i=0;i<5;i++)
  232. {
  233. CzasyPracy[i]=10+(30*rand())/RAND_MAX;
  234. AfxBeginThread(WatekRoboczy,(void*)i);
  235. }
  236.  
  237. }
  238.  
  239. afx_msg LRESULT COkno::OnWiadomosc(WPARAM n, LPARAM m)
  240. {
  241. static char* w[4] =
  242. {"Oblicza...", "Czeka na ekran...", "Wyswietla wynik...", "Zakonczyl prace"};
  243.  
  244. CString tmp;
  245. tmp = w[m-1];
  246.  
  247. //TODO: Dodatkowa obsluga wyswietlania pozostalego czasu obliczen
  248.  
  249.  
  250. switch (n) {
  251. case 0: m_w1.SetWindowText(tmp);
  252. break;
  253. case 1: m_w2.SetWindowText(tmp);
  254. break;
  255. case 2: m_w3.SetWindowText(tmp);
  256. break;
  257. case 3: m_w4.SetWindowText(tmp);
  258. break;
  259. case 4: m_w5.SetWindowText(tmp);
  260. break;
  261. }
  262.  
  263. return 0;
  264. }
  265.  
  266. void COkno::OnTimer(UINT nIDEvent)
  267. {
  268. CString s;
  269. char buffer[1024];
  270.  
  271. CDialog::OnTimer(nIDEvent);
  272.  
  273. /*TODO: Jesli nie ma wyniku - koniec*/
  274. if(wynik=="")
  275. return;
  276.  
  277. m_ekran.GetWindowText(buffer,1024);
  278. s = buffer;
  279. /*TODO: dodanie zmiennej z wynikiem do s */;
  280. s = s + wynik;
  281.  
  282. m_ekran.SetWindowText(s);
  283. wynik="";
  284. /*TODO: Ustawienie stanu odpowiedniego obiektu synchronizacji*/
  285. czekam.SetEvent();
  286.  
  287. }
  288.  
  289. void COkno::OnBnClickedPrzerwij()
  290. {
  291. /*TODO: Ustawienie zmiennej informujacej o wcisnieciu "Przerwij"*/
  292.  
  293. przerwij=1;
  294. m_uruchom.EnableWindow();
  295. m_przerwij.EnableWindow(FALSE);
  296. OnTimer(1);
  297.  
  298. /*TODO: Zaczekac na koniec wszystkich watkow*/
  299. while(licznik)
  300. Sleep(500);
  301. }
  302.  
  303. void COkno::OnBnClickedKoniec()
  304. {
  305. EndDialog(1);
  306. }
  307.  
  308. void COkno::OnDestroy()
  309. {
  310. KillTimer(1);
  311. OnBnClickedPrzerwij();
  312. CDialog::OnDestroy();
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement