LIFTTDlg.cpp
上传用户:jmteguang
上传日期:2022-05-24
资源大小:61k
文件大小:11k
源码类别:

进程与线程

开发平台:

Visual C++

  1. // LIFTTDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "LIFTT.h"
  5. #include "LIFTTDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  10. class CAboutDlg : public CDialog
  11. {
  12. public:
  13. CAboutDlg();
  14. // 对话框数据
  15. enum { IDD = IDD_ABOUTBOX };
  16. protected:
  17. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
  18. // 实现
  19. protected:
  20. DECLARE_MESSAGE_MAP()
  21. };
  22. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  23. {
  24. }
  25. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. }
  29. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  30. END_MESSAGE_MAP()
  31. // CLIFTTDlg 对话框
  32. CLIFTTDlg::CLIFTTDlg(CWnd* pParent /*=NULL*/)
  33. : CDialog(CLIFTTDlg::IDD, pParent)
  34. {
  35. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  36. }
  37. void CLIFTTDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. }
  41. BEGIN_MESSAGE_MAP(CLIFTTDlg, CDialog)
  42. ON_WM_SYSCOMMAND()
  43. ON_WM_PAINT()
  44. ON_WM_QUERYDRAGICON()
  45. //}}AFX_MSG_MAP
  46. ON_WM_CLOSE()
  47. ON_MESSAGE(WM_BUTTONCLICK,&CLIFTTDlg::OnGetButton)
  48. END_MESSAGE_MAP()
  49. // CLIFTTDlg 消息处理程序
  50. BOOL CLIFTTDlg::OnInitDialog()
  51. {
  52. CDialog::OnInitDialog();
  53. // 将“关于...”菜单项添加到系统菜单中。
  54. // IDM_ABOUTBOX 必须在系统命令范围内。
  55. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  56. ASSERT(IDM_ABOUTBOX < 0xF000);
  57. CMenu* pSysMenu = GetSystemMenu(FALSE);
  58. if (pSysMenu != NULL)
  59. {
  60. CString strAboutMenu;
  61. strAboutMenu.LoadString(IDS_ABOUTBOX);
  62. if (!strAboutMenu.IsEmpty())
  63. {
  64. pSysMenu->AppendMenu(MF_SEPARATOR);
  65. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  66. }
  67. }
  68. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  69. //  执行此操作
  70. SetIcon(m_hIcon, TRUE); // 设置大图标
  71. SetIcon(m_hIcon, FALSE); // 设置小图标
  72. // TODO: 在此添加额外的初始化代码
  73. for (int i = 0;i < 20;i ++)
  74. {
  75. theApp.liftButton[i].SetAllNum(LIFTUP);
  76. theApp.liftButton[i].SetButtonNum(i+1);
  77. theApp.liftButton[i].Create(L"↑",WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(30,505-i*25,80,530-i*25),this,0);
  78. }
  79. for (int i = 0;i < 20;i ++)
  80. {
  81. theApp.liftButton[i+20].SetAllNum(LIFTDOWN);
  82. theApp.liftButton[i+20].SetButtonNum(i+1);
  83. theApp.liftButton[i+20].Create(L"↓",WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(80,505-i*25,130,530-i*25),this,0);
  84. }
  85. theApp.liftButton[19].EnableWindow(FALSE);
  86. theApp.liftButton[20].EnableWindow(FALSE);
  87. CString cstr;
  88. for (int i = 0;i < 20;i ++)
  89. {
  90. cstr.Format(L"%d",i+1);
  91. theApp.liftButton[i+40].SetAllNum(LIFTIN);
  92. theApp.liftButton[i+40].SetButtonNum(i+1);
  93. theApp.liftButton[i+40].Create(cstr,WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(130,505-i*25,180,530-i*25),this,0);
  94. theApp.liftButton[i+40].EnableWindow(FALSE);
  95. }
  96. for (int i = 0;i < 5;i ++)
  97. {
  98. cstr.Format(L"%d",i+1);
  99. liftThread[i].liftSet.SetAllNum(LIFTCTR);
  100. liftThread[i].liftSet.SetButtonNum(i+1);
  101. liftThread[i].liftSet.Create(cstr,WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(180,430-i*100,230,530-i*100),this,0);
  102. }
  103. for (int i = 0;i < 5;i ++)
  104. {
  105. liftThread[i].liftMove.SetAllNum(LIFTWARN);
  106. liftThread[i].liftMove.SetButtonNum(i+1);
  107. liftThread[i].liftMove.Create(L"关",WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(280+i*100,505,380+i*100,530),this,i+120);
  108. }
  109. theApp.liftButton[70].SetAllNum(LIFTOPEN);
  110. theApp.liftButton[70].SetButtonNum(0);
  111. theApp.liftButton[71].SetAllNum(LIFTCLOSE);
  112. theApp.liftButton[71].SetButtonNum(0);
  113. theApp.liftButton[70].Create(L"O",WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(230,30,280,280),this,0);
  114. theApp.liftButton[70].EnableWindow(FALSE);
  115. theApp.liftButton[71].Create(L"C",WS_VISIBLE|BS_VCENTER|BS_CENTER|WS_CHILD,CRect(230,280,280,530),this,0);
  116. theApp.liftButton[71].EnableWindow(FALSE);
  117. for (int i = 0;i < 5;i ++)
  118. {
  119. liftThread[i].StartLiftThread();
  120. }
  121. return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
  122. }
  123. void CLIFTTDlg::OnSysCommand(UINT nID, LPARAM lParam)
  124. {
  125. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  126. {
  127. CAboutDlg dlgAbout;
  128. dlgAbout.DoModal();
  129. }
  130. else
  131. {
  132. CDialog::OnSysCommand(nID, lParam);
  133. }
  134. }
  135. // 如果向对话框添加最小化按钮,则需要下面的代码
  136. //  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  137. //  这将由框架自动完成。
  138. void CLIFTTDlg::OnPaint()
  139. {
  140. if (IsIconic())
  141. {
  142. CPaintDC dc(this); // 用于绘制的设备上下文
  143. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  144. // 使图标在工作矩形中居中
  145. int cxIcon = GetSystemMetrics(SM_CXICON);
  146. int cyIcon = GetSystemMetrics(SM_CYICON);
  147. CRect rect;
  148. GetClientRect(&rect);
  149. int x = (rect.Width() - cxIcon + 1) / 2;
  150. int y = (rect.Height() - cyIcon + 1) / 2;
  151. // 绘制图标
  152. dc.DrawIcon(x, y, m_hIcon);
  153. }
  154. else
  155. {
  156. CDialog::OnPaint();
  157. }
  158. }
  159. //当用户拖动最小化窗口时系统调用此函数取得光标显示。
  160. //
  161. HCURSOR CLIFTTDlg::OnQueryDragIcon()
  162. {
  163. return static_cast<HCURSOR>(m_hIcon);
  164. }
  165. void CLIFTTDlg::OnCancel()
  166. {
  167. }
  168. void CLIFTTDlg::OnOK()
  169. {
  170. }
  171. void CLIFTTDlg::OnClose()
  172. {
  173. CDialog::OnCancel();
  174. }
  175. LRESULT CLIFTTDlg::OnGetButton(WPARAM liftInfo,LPARAM lParam)
  176. {
  177. lift * liftlx = (lift *)liftInfo;
  178. switch(liftlx->allNumber)
  179. {
  180. case LIFTWARN:
  181. liftThread[liftlx->buttonNumber-1].SetLiftTo(*liftlx);
  182. break;
  183. case LIFTOPEN:
  184. case LIFTCLOSE:
  185. liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].SetLiftTo(*liftlx);
  186. break;
  187. case LIFTCTR:
  188. for (int i = 0;i < 20;i ++)
  189. {
  190. theApp.liftButton[i+40].SetAllNum(liftlx->buttonNumber);
  191. theApp.liftButton[i+40].EnableWindow(TRUE);
  192. }
  193. theApp.liftButton[70].SetButtonNum(liftlx->buttonNumber);
  194. theApp.liftButton[70].EnableWindow(TRUE);
  195. theApp.liftButton[71].SetButtonNum(liftlx->buttonNumber);
  196. theApp.liftButton[71].EnableWindow(TRUE);
  197. break;
  198. default:
  199. if (liftlx->allNumber != LIFTUP && liftlx->allNumber != LIFTDOWN)
  200. {
  201. if (liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftState() == LIFTUP)
  202. {
  203. if (liftlx->buttonNumber > liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftFloor())
  204. {
  205. liftlx->allNumber = LIFTUP;
  206. }
  207. else
  208. {
  209. liftlx->allNumber = LIFTDOWN;
  210. }
  211. }
  212. if (liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftState() == LIFTDOWN)
  213. {
  214. if (liftlx->buttonNumber < liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftFloor())
  215. {
  216. liftlx->allNumber = LIFTDOWN;
  217. }
  218. else
  219. {
  220. liftlx->allNumber = LIFTUP;
  221. }
  222. }
  223. if (liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftState() == LIFTCLOSE)
  224. {
  225. if (liftlx->buttonNumber > liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].GetLiftFloor())
  226. {
  227. liftlx->allNumber = LIFTUP;
  228. }
  229. else
  230. {
  231. liftlx->allNumber = LIFTDOWN;
  232. }
  233. }
  234. liftThread[theApp.liftButton[70].GetLiftInfo().buttonNumber-1].SetLiftTo(*liftlx);
  235. }
  236. else
  237. {
  238. for (int i = 0;i < 5;i ++)
  239. {
  240. if (!liftThread[i].liftWarn)
  241. {
  242. if (liftThread[i].GetLiftState() == LIFTUP)
  243. {
  244. if (liftThread[i].GetLiftFloor() > liftlx->buttonNumber && liftlx->allNumber == LIFTUP && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin)
  245. {
  246. liftThread[i].setTo = liftThread[i].liftTurn.liftMax * 2 - liftThread[i].liftTurn.liftMin * 2 + liftlx->buttonNumber - liftThread[i].GetLiftFloor() + (int)liftThread[i].liftTurn.upDown.size() * 3;
  247. }
  248. if ((liftThread[i].GetLiftFloor() < liftlx->buttonNumber && liftlx->allNumber == LIFTUP && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin) || (liftlx->buttonNumber < liftThread[i].liftTurn.liftMin && liftlx->allNumber == LIFTUP && liftThread[i].GetLiftFloor() < liftlx->buttonNumber) || (liftlx->buttonNumber > liftThread[i].liftTurn.liftMax && liftThread[i].GetLiftFloor() < liftlx->buttonNumber))
  249. {
  250. liftThread[i].setTo = liftlx->buttonNumber - liftThread[i].GetLiftFloor() + (int)liftThread[i].liftTurn.upDown.size() * 3;
  251. }
  252. if ((liftlx->allNumber == LIFTDOWN && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin) || (liftlx->buttonNumber < liftThread[i].liftTurn.liftMin && liftThread[i].GetLiftFloor() > liftlx->buttonNumber) || (liftlx->buttonNumber < liftThread[i].liftTurn.liftMin && liftlx->allNumber == LIFTDOWN && liftThread[i].GetLiftFloor() < liftlx->buttonNumber))
  253. {
  254. liftThread[i].setTo = liftThread[i].liftTurn.liftMax * 2 - liftlx->buttonNumber - liftThread[i].GetLiftFloor() + (int)liftThread[i].liftTurn.upDown.size() * 3;
  255. }
  256. }
  257. if (liftThread[i].GetLiftState() == LIFTDOWN)
  258. {
  259. if ((liftlx->allNumber == LIFTUP && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin) || (liftlx->buttonNumber > liftThread[i].liftTurn.liftMax && liftThread[i].GetLiftFloor() < liftlx->buttonNumber) || (liftlx->buttonNumber > liftThread[i].liftTurn.liftMax && liftThread[i].GetLiftFloor() > liftlx->buttonNumber && liftlx->allNumber == LIFTUP))
  260. {
  261. liftThread[i].setTo = liftlx->buttonNumber + liftThread[i].GetLiftFloor() - liftThread[i].liftTurn.liftMin * 2 + (int)liftThread[i].liftTurn.upDown.size() * 3;
  262. }
  263. if ((liftThread[i].GetLiftFloor() > liftlx->buttonNumber && liftlx->allNumber == LIFTDOWN && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin) || (liftlx->buttonNumber < liftThread[i].liftTurn.liftMin && liftThread[i].GetLiftFloor() > liftlx->buttonNumber) || (liftlx->buttonNumber > liftThread[i].liftTurn.liftMax && liftThread[i].GetLiftFloor() > liftlx->buttonNumber && liftlx->allNumber == LIFTDOWN))
  264. {
  265. liftThread[i].setTo = liftThread[i].GetLiftFloor() - liftlx->buttonNumber + (int)liftThread[i].liftTurn.upDown.size() * 3;
  266. }
  267. if (liftThread[i].GetLiftFloor() < liftlx->buttonNumber && liftlx->allNumber == LIFTDOWN && liftlx->buttonNumber < liftThread[i].liftTurn.liftMax && liftlx->buttonNumber > liftThread[i].liftTurn.liftMin)
  268. {
  269. liftThread[i].setTo = liftThread[i].liftTurn.liftMax * 2 - liftThread[i].liftTurn.liftMin * 2 - liftlx->buttonNumber + liftThread[i].GetLiftFloor() + (int)liftThread[i].liftTurn.upDown.size() * 3;
  270. }
  271. }
  272. if (liftThread[i].GetLiftState() == LIFTCLOSE)
  273. {
  274. if (liftThread[i].GetLiftFloor() > liftlx->buttonNumber)
  275. {
  276. liftThread[i].setTo = liftThread[i].GetLiftFloor() - liftlx->buttonNumber;
  277. }
  278. else
  279. {
  280. liftThread[i].setTo = liftlx->buttonNumber - liftThread[i].GetLiftFloor();
  281. }
  282. }
  283. }
  284. }
  285. int tempSetTo = 0;
  286. int tempToSet = liftThread[0].setTo;
  287. for (int i = 0;i < 5;i ++)
  288. {
  289. if (liftThread[i].liftWarn)
  290. {
  291. continue;
  292. }
  293. if (liftThread[i].setTo < tempToSet)
  294. {
  295. tempSetTo = i;
  296. tempToSet = liftThread[i].setTo;
  297. }
  298. }
  299. liftThread[tempSetTo].SetLiftTo(*liftlx);
  300. }
  301. break;
  302. }
  303. return 0;
  304. }