HDrawView.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // HDrawView.cpp : implementation of the CHDrawView class
  2. // Download by http://www.codefans.net
  3. #include "stdafx.h"
  4. #include "HDraw.h"
  5. #include "HDrawDoc.h"
  6. #include "HDrawView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CHDrawView
  14. BEGIN_MESSAGE_MAP(CHDrawView, CView)
  15. //{{AFX_MSG_MAP(CHDrawView)
  16. ON_WM_LBUTTONDOWN()
  17. ON_WM_LBUTTONUP()
  18. ON_WM_MOUSEMOVE()
  19. ON_COMMAND(ID_BMP_SAVE, OnBmpSave)
  20. ON_WM_RBUTTONDOWN()
  21. ON_COMMAND(ID_PIC_COLOR_BLUE, OnPicColorBlue)
  22. ON_COMMAND(ID_PIC_COLOR_GREEN, OnPicColorGreen)
  23. ON_COMMAND(ID_PIC_COLOR_OTHER, OnPicColorOther)
  24. ON_COMMAND(ID_PIC_COLOR_RED, OnPicColorRed)
  25. ON_COMMAND(ID_PIC_DASH, OnPicDash)
  26. ON_COMMAND(ID_PIC_SOLID, OnPicSolid)
  27. ON_COMMAND(ID_PIC_W1, OnPicW1)
  28. ON_COMMAND(ID_PIC_W2, OnPicW2)
  29. ON_COMMAND(ID_PIC_W3, OnPicW3)
  30. ON_COMMAND(ID_PIC_DELETE, OnPicDelete)
  31. ON_WM_SETCURSOR()
  32. //}}AFX_MSG_MAP
  33. // Standard printing commands
  34. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  35. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  36. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CHDrawView construction/destruction
  40. CHDrawView::CHDrawView()
  41. {
  42. // TODO: add construction code here
  43. m_pen.CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
  44. }
  45. CHDrawView::~CHDrawView()
  46. {
  47. m_pDocument = pOldDoc;
  48. }
  49. BOOL CHDrawView::PreCreateWindow(CREATESTRUCT& cs)
  50. {
  51. // TODO: Modify the Window class or styles here by modifying
  52. // the CREATESTRUCT cs
  53. return CView::PreCreateWindow(cs);
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CHDrawView drawing
  57. void CHDrawView::OnDraw(CDC* pDC)
  58. {
  59. CHDrawDoc* pDoc = GetDocument();
  60. ASSERT_VALID(pDoc);
  61. // TODO: add draw code for native data here
  62. OnPrepareDC(pDC);
  63. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  64. pDoc->m_strokeList.GetAt(i)->DrawStroke(pDC);
  65. }
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CHDrawView diagnostics
  69. #ifdef _DEBUG
  70. void CHDrawView::AssertValid() const
  71. {
  72. CView::AssertValid();
  73. }
  74. void CHDrawView::Dump(CDumpContext& dc) const
  75. {
  76. CView::Dump(dc);
  77. }
  78. CHDrawDoc* CHDrawView::GetDocument() // non-debug version is inline
  79. {
  80. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHDrawDoc)));
  81. return (CHDrawDoc*)m_pDocument;
  82. }
  83. #endif //_DEBUG
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CHDrawView message handlers
  86. /************************************************************************
  87. 很复杂的条件判断
  88. 条件1:鼠标是否点中图形:是/否
  89. 条件2:是否按下Ctrl:是/否
  90. 条件3:选中多少个:一/多/零
  91. 非Ctrl+Track:
  92. 点中+Ctrl: 选中或取消选中
  93. 点中+非Ctrl: 选中点中,取消所有非点中
  94. ************************************************************************/
  95. void CHDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  96. {
  97. CDC* pDC = GetDC();
  98. OnPrepareDC(pDC);
  99. pDC->DPtoLP(&point);
  100. CHDrawDoc* pDoc = GetDocument();
  101. BOOL refresh = FALSE, bTrack = false, ctrlPressed = nFlags & MK_CONTROL;
  102. int i;
  103. //没有Ctrl, Track
  104. if(!ctrlPressed){
  105. for(i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  106. if(pDoc->m_strokeList.GetAt(i)->m_bSelected){
  107. int hit = pDoc->m_strokeList.GetAt(i)->m_tracker.HitTest(point);
  108. if(hit >= 0 &&
  109. pDoc->m_strokeList.GetAt(i)->m_tracker.Track(this,point)){
  110. //step1. cavas reset
  111. pDoc->m_strokeList.GetAt(i)->ReSize(
  112. pDoc->m_strokeList.GetAt(i)->m_tracker.m_rect);
  113. Invalidate();
  114. GetDocument()->BackUp();//备份
  115. bTrack = true;
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. for(i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  122. if(pDoc->m_strokeList.GetAt(i)->IsPointIn(point)){
  123. //情况1. 点中,Ctrl按下,选中或取消选中
  124. if(ctrlPressed){
  125. //select/deselect
  126. pDoc->m_strokeList.GetAt(i)->m_bSelected =
  127. !pDoc->m_strokeList.GetAt(i)->m_bSelected;
  128. }
  129. //情况2. 点中,没有Ctrl,选中
  130. else{
  131. //select
  132. pDoc->m_strokeList.GetAt(i)->m_bSelected = true;
  133. }
  134. refresh = true;
  135. }
  136. //情况3. 没有点中,没有Ctrl,取消选中
  137. else if(!ctrlPressed)
  138. pDoc->m_strokeList.GetAt(i)->m_bSelected = false;
  139. }
  140. if(refresh){
  141. Invalidate();
  142. }
  143. else{
  144. //画图
  145. if(!bTrack && !ctrlPressed){
  146. //Step1. 设置捕获
  147. SetCapture();
  148. //Step2. 加入新图形
  149. m_stroke = pDoc->NewStroke();
  150. //Step3. 设置起点
  151. m_stroke->SetCurrentPoint(point);
  152. //Step4. 设置文件已经修改状态
  153. pDoc->SetModifiedFlag();
  154. }
  155. }
  156. ReleaseDC(pDC);
  157. CView::OnLButtonDown(nFlags, point);
  158. }
  159. void CHDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  160. {
  161. // TODO: Add your message handler code here and/or call default
  162. if(GetCapture() == this){
  163. CHDrawDoc *pDoc = GetDocument();
  164. CDC *pDC = GetDC();
  165. OnPrepareDC(pDC);
  166. pDC->DPtoLP(&point);
  167. //Step0.1 点下松开,没有画
  168. if(m_stroke->m_points.GetSize() == 1){
  169. delete m_stroke;
  170. }
  171. //Step0.2 选择框
  172. else if(PIC_select == m_stroke->m_picType){
  173. bool refresh = false;
  174. CRect rect(m_stroke->m_points[0], m_stroke->m_points[1]);
  175. rect.NormalizeRect();//防止从右下往左上选
  176. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  177. if(rect.PtInRect(pDoc->m_strokeList.GetAt(i)->m_points.GetAt(0)) &&
  178. rect.PtInRect(pDoc->m_strokeList.GetAt(i)->m_points.GetAt(1))){
  179. pDoc->m_strokeList.GetAt(i)->m_bSelected = true;
  180. refresh = true;
  181. }
  182. }
  183. if(refresh)
  184. Invalidate();
  185. delete m_stroke;
  186. }
  187. //Step0.3 画
  188. else{
  189. //Step1. 加入新点
  190. m_stroke->SetCurrentPoint(point);
  191. pDoc->m_strokeList.Add(m_stroke);
  192. pDoc->BackUp();//备份
  193. }
  194. //Step2. 释放捕获
  195. ReleaseCapture();
  196. m_stroke = NULL;
  197. ReleaseDC(pDC);
  198. //Step3. 全部重画
  199. Invalidate();
  200. }
  201. CView::OnLButtonUp(nFlags, point);
  202. }
  203. void CHDrawView::OnMouseMove(UINT nFlags, CPoint point)
  204. {
  205. CString curPos;
  206. curPos.Format("(%d,%d)", point.x, point.y);
  207. GetParentFrame()->SetMessageText(curPos);
  208. CDC *pDC = GetDC();
  209. OnPrepareDC(pDC);
  210. pDC->DPtoLP(&point);
  211. //state1: 处于捕获状态,画图
  212. if(GetCapture() == this){
  213. m_stroke->ReDrawStroke(pDC, point);
  214. }
  215. //state2: 非捕获状态,高亮
  216. else{
  217. bool refresh = false;
  218. for(int i = 0; i < GetDocument()->m_strokeList.GetSize(); i ++)
  219. {
  220. if(GetDocument()->m_strokeList.GetAt(i)->IsPointIn(point)){
  221. //修改鼠标
  222. //SetCursor(LoadCursor(NULL, IDC_ARROW));
  223. //if state changed from NOT selected to selected
  224. if(!GetDocument()->m_strokeList.GetAt(i)->IsHightLight())
  225. refresh = true;
  226. GetDocument()->m_strokeList.GetAt(i)->SetHighLight(TRUE);
  227. }
  228. else{
  229. //if state changed from selected to NOT selected
  230. if(GetDocument()->m_strokeList.GetAt(i)->IsHightLight())
  231. refresh = true;
  232. GetDocument()->m_strokeList.GetAt(i)->SetHighLight(FALSE);
  233. }
  234. }
  235. if(refresh)
  236. Invalidate();
  237. }
  238. ReleaseDC(pDC);
  239. CView::OnMouseMove(nFlags, point);
  240. }
  241. void CHDrawView::SetDocument(CHDrawDoc *pDoc)
  242. {
  243. pOldDoc = m_pDocument;
  244. m_pDocument = pDoc;
  245. }
  246. void CHDrawView::OnBmpSave()
  247. {
  248. // TODO: Add your command handler code here
  249. CFileDialog dlg(FALSE, "bmp","hjz.bmp");
  250. if(dlg.DoModal() != IDOK){
  251. return ;
  252. }
  253. CString filePath = dlg.GetPathName();
  254. // TODO: Add your command handler code here
  255. CClientDC client(this);//用于本控件的,楼主可以不用此句
  256. CDC cdc;
  257. CBitmap bitmap;
  258. RECT rect;CRect r;
  259. GetClientRect(&rect);
  260. int cx = rect.right - rect.left;
  261. int cy = rect.bottom - rect.top;
  262. bitmap.CreateCompatibleBitmap(&client, cx, cy);
  263. cdc.CreateCompatibleDC(NULL);
  264. CBitmap * oldbitmap = (CBitmap* ) cdc.SelectObject(&bitmap);
  265. cdc.FillRect(&rect, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
  266. for(int i = 0; i < GetDocument()->m_strokeList.GetSize(); i ++){
  267. GetDocument()->m_strokeList.GetAt(i)->DrawStroke(&cdc);
  268. }
  269. cdc.SelectObject(oldbitmap);
  270. ::OpenClipboard(this->m_hWnd);
  271. ::EmptyClipboard();
  272. ::SetClipboardData(CF_BITMAP, bitmap);
  273. ::CloseClipboard();
  274. HBITMAP hBitmap = (HBITMAP)bitmap;
  275. HDC hDC;
  276. int iBits;
  277. WORD wBitCount;
  278. DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0;
  279. BITMAP Bitmap;
  280. BITMAPFILEHEADER bmfHdr;
  281. BITMAPINFOHEADER bi;
  282. LPBITMAPINFOHEADER lpbi;
  283. HANDLE fh, hDib, hPal,hOldPal=NULL;
  284. hDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  285. iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
  286. DeleteDC(hDC);
  287. if (iBits <= 1) wBitCount = 1;
  288. else if (iBits <= 4) wBitCount = 4;
  289. else if (iBits <= 8) wBitCount = 8;
  290. else wBitCount = 24;
  291. GetObject(hBitmap, sizeof(Bitmap), (LPSTR)&Bitmap);
  292. bi.biSize = sizeof(BITMAPINFOHEADER);
  293. bi.biWidth = Bitmap.bmWidth;
  294. bi.biHeight = Bitmap.bmHeight;
  295. bi.biPlanes = 1;
  296. bi.biBitCount = wBitCount;
  297. bi.biCompression = BI_RGB;
  298. bi.biSizeImage = 0;
  299. bi.biXPelsPerMeter = 0;
  300. bi.biYPelsPerMeter = 0;
  301. bi.biClrImportant = 0;
  302. bi.biClrUsed = 0;
  303. dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
  304. hDib = GlobalAlloc(GHND,dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
  305. lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
  306. *lpbi = bi;
  307. hPal = GetStockObject(DEFAULT_PALETTE);
  308. if (hPal)
  309. {
  310. hDC = ::GetDC(NULL);
  311. hOldPal = ::SelectPalette(hDC, (HPALETTE)hPal, FALSE);
  312. RealizePalette(hDC);
  313. }
  314. GetDIBits(hDC, hBitmap, 0, (UINT) Bitmap.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER)
  315. +dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS);
  316. if (hOldPal)
  317. {
  318. ::SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
  319. RealizePalette(hDC);
  320. ::ReleaseDC(NULL, hDC);
  321. }
  322. fh = CreateFile(filePath, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
  323. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  324. if (fh == INVALID_HANDLE_VALUE)
  325. return ;
  326. bmfHdr.bfType = 0x4D42; // "BM"
  327. dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
  328. bmfHdr.bfSize = dwDIBSize;
  329. bmfHdr.bfReserved1 = 0;
  330. bmfHdr.bfReserved2 = 0;
  331. bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
  332. WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
  333. WriteFile(fh, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
  334. GlobalUnlock(hDib);
  335. GlobalFree(hDib);
  336. CloseHandle(fh);
  337. }
  338. void CHDrawView::OnRButtonDown(UINT nFlags, CPoint point)
  339. {
  340. //context menu
  341. CMenu rmenu;
  342. rmenu.LoadMenu(IDR_MENU_SET);
  343. ClientToScreen(&point);
  344. rmenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
  345. rmenu.Detach();
  346. CView::OnRButtonDown(nFlags, point);
  347. }
  348. void CHDrawView::OnPicColorBlue()
  349. {
  350. CHDrawDoc *pDoc = GetDocument();
  351. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++)
  352. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  353. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  354. pDoc->m_strokeList.GetAt(i)->m_penColor = RGB(0,0,255);
  355. GetDocument()->BackUp();//备份
  356. Invalidate();
  357. }
  358. void CHDrawView::OnPicColorRed()
  359. {
  360. // TODO: Add your command handler code here
  361. CHDrawDoc *pDoc = GetDocument();
  362. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++)
  363. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  364. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  365. pDoc->m_strokeList.GetAt(i)->m_penColor = RGB(255,0,0);
  366. GetDocument()->BackUp();//备份
  367. Invalidate();
  368. }
  369. void CHDrawView::OnPicColorGreen()
  370. {
  371. // TODO: Add your command handler code here
  372. CHDrawDoc *pDoc = GetDocument();
  373. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++)
  374. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  375. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  376. pDoc->m_strokeList.GetAt(i)->m_penColor = RGB(0,255,0);
  377. GetDocument()->BackUp();//备份
  378. Invalidate();
  379. }
  380. void CHDrawView::OnPicColorOther()
  381. {
  382. // TODO: Add your command handler code here
  383. CColorDialog dlg;
  384. if(dlg.DoModal() == IDOK){
  385. CHDrawDoc *pDoc = GetDocument();
  386. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  387. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  388. pDoc->m_strokeList.GetAt(i)->m_bHighLight){
  389. pDoc->m_strokeList.GetAt(i)->m_penColor = dlg.GetColor();
  390. }
  391. }
  392. GetDocument()->BackUp();//备份
  393. Invalidate();
  394. }
  395. }
  396. void CHDrawView::OnPicSolid()
  397. {
  398. // TODO: Add your command handler code here
  399. CHDrawDoc *pDoc = GetDocument();
  400. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++)
  401. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  402. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  403. pDoc->m_strokeList.GetAt(i)->m_penStyle = PS_SOLID;
  404. Invalidate();
  405. }
  406. void CHDrawView::OnPicDash()
  407. {
  408. // TODO: Add your command handler code here
  409. CHDrawDoc *pDoc = GetDocument();
  410. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++)
  411. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  412. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  413. pDoc->m_strokeList.GetAt(i)->m_penStyle = PS_DASH;
  414. GetDocument()->BackUp();//备份
  415. Invalidate();
  416. }
  417. void CHDrawView::OnPicW1()
  418. {
  419. // TODO: Add your command handler code here
  420. CHDrawDoc *pDoc = GetDocument();
  421. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  422. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  423. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  424. pDoc->m_strokeList.GetAt(i)->m_penWidth = 1;
  425. }
  426. GetDocument()->BackUp();//备份
  427. Invalidate();
  428. }
  429. void CHDrawView::OnPicW2()
  430. {
  431. // TODO: Add your command handler code here
  432. CHDrawDoc *pDoc = GetDocument();
  433. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  434. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  435. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  436. pDoc->m_strokeList.GetAt(i)->m_penWidth = 2;
  437. }
  438. GetDocument()->BackUp();//备份
  439. Invalidate();
  440. }
  441. void CHDrawView::OnPicW3()
  442. {
  443. // TODO: Add your command handler code here
  444. CHDrawDoc *pDoc = GetDocument();
  445. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  446. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  447. pDoc->m_strokeList.GetAt(i)->m_bHighLight)
  448. pDoc->m_strokeList.GetAt(i)->m_penWidth = 3;
  449. }
  450. GetDocument()->BackUp();//备份
  451. Invalidate();
  452. }
  453. void CHDrawView::OnPicDelete()
  454. {
  455. // TODO: Add your command handler code here
  456. CHDrawDoc *pDoc = GetDocument();
  457. //remove all strokes
  458. bool refresh = false;
  459. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  460. if(pDoc->m_strokeList.GetAt(i)->m_bSelected ||
  461. pDoc->m_strokeList.GetAt(i)->m_bHighLight){
  462. delete pDoc->m_strokeList.GetAt(i);
  463. pDoc->m_strokeList.RemoveAt(i);
  464. i--;
  465. refresh = true;
  466. }
  467. }
  468. //refresh if needed
  469. if(refresh){
  470. GetDocument()->BackUp();//备份
  471. Invalidate();
  472. }
  473. }
  474. BOOL CHDrawView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  475. {
  476. if(pWnd == this){
  477. CPoint point;
  478. //Step1. get cursor position
  479. GetCursorPos(&point);
  480. //Step2. convert point from screen to client
  481. ScreenToClient(&point);
  482. CDC* pDC = GetDC();//获得DC
  483. OnPrepareDC(pDC);//设置DC的滚动属性,与ScollView的滚动有关
  484. pDC->DPtoLP(&point);//转换当前点为逻辑位置坐标
  485. CHDrawDoc *pDoc = GetDocument();
  486. //Track判断
  487. for(int i = 0; i < pDoc->m_strokeList.GetSize(); i ++){
  488. if(pDoc->m_strokeList.GetAt(i)->m_bSelected &&
  489. pDoc->m_strokeList.GetAt(i)->m_tracker.HitTest(point) >= 0){
  490. pDoc->m_strokeList.GetAt(i)->m_tracker.SetCursor(pWnd, nHitTest);
  491. ReleaseDC(pDC);
  492. return TRUE;
  493. }
  494. }
  495. ReleaseDC(pDC);
  496. }
  497. return CView::OnSetCursor(pWnd, nHitTest, message);
  498. }
  499. BOOL CHDrawView::PreTranslateMessage(MSG* pMsg)
  500. {
  501. // TODO: Add your specialized code here and/or call the base class
  502. CHDrawDoc *pDoc = GetDocument();
  503. BOOL deleted = FALSE;
  504. int i, x, y;
  505. if (pMsg->message == WM_KEYDOWN)
  506. {
  507. switch (pMsg->wParam){
  508. case VK_DELETE:
  509. for(i = 0; i <pDoc->m_strokeList.GetSize(); i ++){
  510. if(pDoc->m_strokeList.GetAt(i)->m_bSelected){
  511. pDoc->m_strokeList.RemoveAt(i--);
  512. deleted = TRUE;
  513. }
  514. }
  515. if(deleted)
  516. Invalidate();
  517. break;
  518. case 'A':
  519. case 'a':
  520. if(::GetKeyState(VK_CONTROL) < 0){
  521. for(int i = 0; i <pDoc->m_strokeList.GetSize(); i ++){
  522. pDoc->m_strokeList.GetAt(i)->m_bSelected = TRUE;
  523. }
  524. Invalidate();
  525. }
  526. break;
  527. case VK_UP:
  528. case VK_DOWN:
  529. case VK_LEFT:
  530. case VK_RIGHT:
  531. x = (pMsg->wParam==VK_RIGHT) - (pMsg->wParam==VK_LEFT);
  532. y = (pMsg->wParam==VK_DOWN) - (pMsg->wParam==VK_UP);
  533. if(::GetKeyState(VK_SHIFT) < 0){
  534. x *= 8;
  535. y *= 8;
  536. }
  537. for(int i = 0; i <pDoc->m_strokeList.GetSize(); i ++){
  538. if(pDoc->m_strokeList.GetAt(i)->m_bSelected){
  539. pDoc->m_strokeList.GetAt(i)->Move(x,y);
  540. }
  541. }
  542. Invalidate();
  543. break;
  544. }
  545. }
  546. return CView::PreTranslateMessage(pMsg);
  547. }