⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo5_5.c

📁 WINDOWS图形编程随书光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
314       case WM_SYSCOMMAND :
315 
316                 if ((wParam & 0xfff0) != SC_MOUSEMENU &&
317                     (wParam & 0xfff0) != SC_KEYMENU)
318                   {
319                     hDC = GetDC(hWnd);
320                     BitBlt(hMemDC, 0, 0, CX, CY,
321                            hDC, 0, 0, SRCCOPY);
322                     ReleaseDC(hWnd, hDC);
323 
324                     hMenu = GetMenu(hWnd);
325                     EnableMenuItem(hMenu, IDM_UNDO,
326                                    MF_GRAYED);
327                     CanUndo = FALSE;
328                   }
329 
330       default :
331          return(DefWindowProc(hWnd, message, wParam, lParam));
332     }
333 }
334 
335 
336 
337 HBRUSH MyCreateBrush(int nHatchStyle, COLORREF crColor)
338 {
339    HBRUSH hBrush;
340 
341    if (nHatchStyle == -1)
342      hBrush = CreateSolidBrush(crColor);
343    else
344      hBrush = CreateHatchBrush(nHatchStyle, crColor);
345 
346    return (hBrush);
347 }
348 
349 
350 
351 void DrawGraph(HDC hDC, HMENU hMenu, BOOL bSure)
352 {
353    HPEN   hPen, hPrePen;
354    HBRUSH hBrush, hPreBrush;
355 
356    if (ToolID==IDM_PENCIL || bSure)
357      {
358        hPen   = CreatePen(nPenStyle, nPenWidth,
359                         MKCOLOR(crPCurColor[nPenColor]));
360        hPrePen   = SelectObject(hDC, hPen);
361 
362        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
363            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
364          {
365            hBrush = MyCreateBrush(nHatch,
366                         MKCOLOR(crBCurColor[nBrushColor]));
367            hPreBrush  = SelectObject(hDC, hBrush);
368          }
369        else
370          {
371            hBrush = GetStockObject(NULL_BRUSH);
372            hPreBrush  = SelectObject(hDC, hBrush);
373          }
374      }
375    else
376      SelectObject(hDC, GetStockObject(NULL_BRUSH));
377 
378    switch (ToolID)
379      {
380        case IDM_PENCIL :
381               DrawPencil(hDC, hMenu);
382               break;
383 
384        case IDM_LINE :
385               DrawLine(hDC, hMenu, bSure);
386               break;
387 
388        case IDM_RECT_F :
389        case IDM_RECT   :
390               DrawRect(hDC, hMenu, bSure);
391               break;
392 
393        case IDM_ELLIP_F :
394        case IDM_ELLIP   :
395               DrawEllip(hDC, hMenu, bSure);
396               break;
397 
398        case IDM_CIRCLE_F :
399        case IDM_CIRCLE   :
400               DrawCircle(hDC, hMenu, bSure);
401               break;
402 
403        case IDM_ROUNDRECT_F :
404        case IDM_ROUNDRECT   :
405               DrawRoundRect(hDC, hMenu, bSure);
406               break;
407      }
408 
409    if (ToolID==IDM_PENCIL || bSure)
410      {
411        SelectObject(hDC, hPrePen);
412        DeleteObject(hPen);
413 
414        if (ToolID==IDM_RECT_F   || ToolID==IDM_ELLIP_F ||
415            ToolID==IDM_CIRCLE_F || ToolID==IDM_ROUNDRECT_F)
416          {
417            SelectObject(hDC, hPreBrush);
418            DeleteObject(hBrush);
419          }
420        else
421          {
422            SelectObject(hDC, hPreBrush);
423          }
424      }
425 }
426 
427 
428 
429 void DrawPencil(HDC hDC, HMENU hMenu)
430 {
431    MoveTo(hDC, PrePoint.x, PrePoint.y);
432    LineTo(hDC, CurPoint.x, CurPoint.y);
433 
434    if (! CanUndo)
435      {
436        EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
437        CanUndo = TRUE;
438      }
439 }
440 
441 
442 
443 void DrawLine(HDC hDC, HMENU hMenu, BOOL bSure)
444 {
445    int  nDrawMode;
446 
447    if (! bSure)
448      {
449        nDrawMode = SetROP2(hDC, R2_NOT);
450 
451        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
452        LineTo(hDC, PrePoint.x, PrePoint.y);
453 
454        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
455        LineTo(hDC, CurPoint.x, CurPoint.y);
456 
457        SetROP2(hDC, nDrawMode);
458      }
459    else
460      {
461        MoveTo(hDC, OrgPoint.x, OrgPoint.y);
462        LineTo(hDC, CurPoint.x, CurPoint.y);
463 
464        if (! CanUndo)
465          {
466            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
467            CanUndo = TRUE;
468          }
469      }
470 }
471 
472 
473 
474 void DrawRect(HDC hDC, HMENU hMenu, BOOL bSure)
475 {
476    int  nDrawMode;
477 
478    if (! bSure)
479      {
480        nDrawMode = SetROP2(hDC, R2_NOT);
481 
482        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
483                       PrePoint.x, PrePoint.y);
484 
485        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
486                       CurPoint.x, CurPoint.y);
487 
488        SetROP2(hDC, nDrawMode);
489      }
490    else
491      {
492        Rectangle(hDC, OrgPoint.x, OrgPoint.y,
493                       CurPoint.x, CurPoint.y);
494 
495        if (! CanUndo)
496          {
497            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
498            CanUndo = TRUE;
499          }
500      }
501 }
502 
503 
504 
505 void DrawEllip(HDC hDC, HMENU hMenu, BOOL bSure)
506 {
507    int  nDrawMode;
508 
509    if (! bSure)
510      {
511        nDrawMode = SetROP2(hDC, R2_NOT);
512 
513        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
514                     PrePoint.x, PrePoint.y);
515 
516        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
517                     CurPoint.x, CurPoint.y);
518 
519        SetROP2(hDC, nDrawMode);
520      }
521    else
522      {
523        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
524                     CurPoint.x, CurPoint.y);
525 
526        if (! CanUndo)
527          {
528            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
529            CanUndo = TRUE;
530          }
531      }
532 }
533 
534 
535 
536 void DrawRoundRect(HDC hDC, HMENU hMenu, BOOL bSure)
537 {
538    int  nDrawMode;
539 
540    if (! bSure)
541      {
542        nDrawMode = SetROP2(hDC, R2_NOT);
543 
544        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
545                       PrePoint.x, PrePoint.y,
546                       (PrePoint.x-OrgPoint.x)/4,
547                       (PrePoint.y-OrgPoint.y)/4);
548 
549        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
550                       CurPoint.x, CurPoint.y,
551                       (CurPoint.x-OrgPoint.x)/4,
552                       (CurPoint.y-OrgPoint.y)/4);
553 
554        SetROP2(hDC, nDrawMode);
555      }
556    else
557      {
558        RoundRect(hDC, OrgPoint.x, OrgPoint.y,
559                       CurPoint.x, CurPoint.y,
560                       (CurPoint.x-OrgPoint.x)/4,
561                       (CurPoint.y-OrgPoint.y)/4);
562 
563        if (! CanUndo)
564          {
565            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
566            CanUndo = TRUE;
567          }
568      }
569 }
570 
571 
572 
573 void DrawCircle(HDC hDC, HMENU hMenu, BOOL bSure)
574 {
575    int    nDrawMode;
576    int    nLogPixSx, nLogPixSy;
577    int    Width, Height;
578    int    SignX, SignY;
579 
580    nLogPixSx = GetDeviceCaps(hDC, LOGPIXELSX);
581    nLogPixSy = GetDeviceCaps(hDC, LOGPIXELSY);
582 
583    Width  = CurPoint.x - OrgPoint.x;
584    Height = CurPoint.y - OrgPoint.y;
585    SignX  = (Width  >= 0 ? 1 : -1);
586    SignY  = (Height >= 0 ? 1 : -1);
587 
588    if (fabs((float) Width/nLogPixSx) >
589        fabs((float) Height/nLogPixSy) )
590       {
591          CurPoint.x = OrgPoint.x + (float)
592            fabs(Height) * nLogPixSx / nLogPixSy * SignX;
593       }
594    else
595       {
596          CurPoint.y = OrgPoint.y + (float)
597            fabs(Width) * nLogPixSy / nLogPixSx * SignY;
598       }
599 
600 
601    if (! bSure)
602      {
603        nDrawMode = SetROP2(hDC, R2_NOT);
604 
605        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
606                     PrePoint.x, PrePoint.y);
607 
608        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
609                     CurPoint.x, CurPoint.y);
610 
611        SetROP2(hDC, nDrawMode);
612      }
613    else
614      {
615        Ellipse(hDC, OrgPoint.x, OrgPoint.y,
616                     CurPoint.x, CurPoint.y);
617 
618        if (! CanUndo)
619          {
620            EnableMenuItem(hMenu, IDM_UNDO, MF_ENABLED);
621            CanUndo = TRUE;
622          }
623      }
624 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -