home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / mswindo / programm / misc / 1761 < prev    next >
Encoding:
Text File  |  1992-09-10  |  7.6 KB  |  301 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!nuscc!solomon.technet.sg!scstech
  3. From: scstech@solomon.technet.sg (Arthur Lim)
  4. Subject: Help with MFC !!!!
  5. Message-ID: <1992Sep10.103440.18423@nuscc.nus.sg>
  6. Sender: usenet@nuscc.nus.sg
  7. Organization: Singapore Computer Systems Ltd
  8. Date: Thu, 10 Sep 1992 10:34:40 GMT
  9. Lines: 290
  10.  
  11. Hi,
  12.  
  13. I encountered a couple of problem converting a Windows C program to use
  14. MFC. Here's the senerio :
  15.  
  16. In a FrameWindow (of CFrameWnd class) I have a couple of windows derived
  17. from CWnd. These child windows are borderless. The task I wanted to to
  18. was to be able to resize and move these child windows, using rubberband
  19. lines, before finally moving/resizing the actual window.
  20.  
  21. The problems I encountered were :
  22.  
  23. 1) The rubberbanding lines won't appear. (Well, somestimes they do and
  24.    sometimes they don't). It still performs the wanted function though.
  25.  
  26. 2) After a move, the windows won't repaint even though I made a specific
  27.    call to InvalidateRect(NULL, TRUE) at the point after moving the window.
  28.    If the window is resized, then it's OK.
  29.  
  30. I have appended the segment of my codes that does the above. I hope some
  31. kind souls could shine some light for me.
  32.  
  33. Thanks in advance.
  34.  
  35. +======================================================================+
  36. | Arthur Lim                     | #include <std.disclaimer>           |
  37. | Advanced Software R&D Centre   |                                     |
  38. | Singapore Computer Systems Ltd | scstech@solomon.technet.sg          |
  39. |                                |                                     |
  40. | (Voice) 065-240-3850           | Please prefix "For Arthur :" in the |
  41. | (Fax)   065-441-2811           | subject line when mailing. Thanks.  |
  42. +======================================================================+
  43.  
  44. ======================= C U T   H E R E ===============================
  45. **********************
  46. ******PANEL.H*********
  47. **********************
  48.  
  49. class MyWindow;
  50.  
  51. class MyPanel : public CWnd
  52.     CString *    csPanelName;
  53.     BOOL        bStartMove;
  54.     CRect        rect;
  55.     CRect        rectParent;
  56.     CRect        rectNew;
  57.     CPoint        ptOrigin;
  58.     CDC *        hdc;
  59.     CPen        hpen;
  60.     char        MoveMode;
  61.     CPen *        old_pen;
  62.     CBrush *    old_brush;
  63.     int        old_ROP;
  64.  
  65.     char        DetermineMoveMode(CPoint &point);
  66.     
  67. public:
  68.             MyPanel(MyWindow *, char *, BOOL dynamic=FALSE);
  69.     afx_msg void    OnPaint();
  70.     afx_msg void    OnLButtonDown(UINT nFlags, CPoint point);
  71.     afx_msg void    OnLButtonUp(UINT nFlags, CPoint point);
  72.     afx_msg void    OnMouseMove(UINT nFlags, CPoint point);
  73.  
  74.     DECLARE_MESSAGE_MAP()
  75. };
  76.  
  77. #endif
  78. ---------------------------------------------------------------------------
  79. ***********************
  80. *******PANEL.CPP*******
  81. ***********************
  82.  
  83. #include "panel.h"
  84.  
  85. #define MV_LIMIT    5
  86. #define MV_LEFT     1
  87. #define MV_RIGHT    2
  88. #define MV_TOP        4
  89. #define MV_BOTTOM    8
  90. #define MV_WINDOW    15
  91.  
  92. BEGIN_MESSAGE_MAP (MyPanel, CWnd)
  93.     ON_WM_LBUTTONDOWN()
  94.     ON_WM_LBUTTONUP()
  95.     ON_WM_MOUSEMOVE()
  96.     ON_WM_PAINT()
  97. END_MESSAGE_MAP()
  98.  
  99. MyPanel::MyPanel(MyWindow *parent, char *pnl_name, BOOL dynamic) 
  100. {
  101.     CRect        rectDefault;
  102.  
  103.     csPanelName = new CString(pnl_name);
  104.     bDynamic = dynamic;
  105.     bStartMove = FALSE;
  106.     rectDefault.SetRect(10, 10, 110, 110);
  107.     Create(VTPANEL, "", WS_CHILD, rectDefault, (CWnd *)parent, 0);
  108.     ShowWindow(SW_SHOW);
  109.     UpdateWindow();
  110. }
  111.  
  112. char MyPanel::DetermineMoveMode(CPoint &point)
  113. {
  114.     CRect        rect;
  115.     char        mode = 0;
  116.  
  117.     GetClientRect(&rect);
  118.     if(point.x < MV_LIMIT)
  119.     {
  120.         mode = MV_LEFT;
  121.         if(point.y < 2*MV_LIMIT)
  122.             mode |= MV_TOP;
  123.         else
  124.         if(point.y > rect.Height() - 2*MV_LIMIT)
  125.             mode |= MV_BOTTOM;
  126.     }
  127.     else
  128.     if(point.x > rect.Width() - MV_LIMIT)
  129.     {
  130.         mode = MV_RIGHT;
  131.         if(point.y < 2*MV_LIMIT)
  132.             mode |= MV_TOP;
  133.         else
  134.         if(point.y > rect.Height() - 2*MV_LIMIT)
  135.             mode |= MV_BOTTOM;
  136.     }
  137.     else
  138.     if(point.y < MV_LIMIT)
  139.     {
  140.         mode = MV_TOP;
  141.         if(point.x < 2*MV_LIMIT)
  142.             mode |= MV_LEFT;
  143.         else
  144.         if(point.x > rect.Width() - 2*MV_LIMIT)
  145.             mode |= MV_RIGHT;
  146.     }
  147.     else
  148.     if(point.y > rect.Height() - MV_LIMIT)
  149.     {
  150.         mode = MV_BOTTOM;
  151.         if(point.x < 2*MV_LIMIT)
  152.             mode |= MV_LEFT;
  153.         else
  154.         if(point.x > rect.Width() - 2*MV_LIMIT)
  155.             mode |= MV_RIGHT;
  156.     }
  157.     if(!mode)
  158.         mode = MV_WINDOW;
  159.  
  160.     return mode;
  161. }
  162.  
  163. afx_msg void MyPanel::OnPaint()
  164. {
  165.     CRect        rect;
  166.     PAINTSTRUCT    ps;
  167.     CDC *        dc;
  168.     CPen        whitepen,
  169.             blackpen;
  170.  
  171.     dc = BeginPaint(&ps);
  172.     GetClientRect(&rect);
  173.     rect.bottom -= 2;
  174.     rect.right  -= 2;
  175.     whitepen.CreatePen(PS_SOLID | PS_INSIDEFRAME, 2, RGB(255, 255, 255));
  176.     blackpen.CreatePen(PS_SOLID | PS_INSIDEFRAME, 2, RGB(0, 0, 0));
  177.     dc->SelectObject(&blackpen);
  178.     dc->MoveTo(rect.left, rect.bottom);
  179.     dc->LineTo(rect.left, rect.top);
  180.     dc->LineTo(rect.right, rect.top);
  181.     blackpen.DeleteObject();
  182.     dc->SelectObject(&whitepen);
  183.     dc->LineTo(rect.right, rect.bottom);
  184.     dc->LineTo(rect.left, rect.bottom);
  185.     whitepen.DeleteObject();
  186.     EndPaint(&ps);
  187. }
  188.  
  189.  
  190. afx_msg void MyPanel::OnLButtonDown(UINT nFlags, CPoint point)
  191. {
  192.     if(nFlags & MK_SHIFT)
  193.     {
  194.         ClientToScreen(&point);
  195.         GetParent()->ScreenToClient(&point);
  196.         ptOrigin.x = point.x;
  197.         ptOrigin.y = point.y;
  198.         GetWindowRect(&rect);
  199.         GetParent()->ScreenToClient(&rect);
  200.         GetParent()->GetClientRect(&rectParent);
  201.         bStartMove = TRUE;
  202.         rectNew = rect;
  203.         SetCapture();
  204.         hdc = GetParent()->GetDC();
  205.         hpen.CreatePen(PS_SOLID, 1, RGB(0,0,0));
  206.         old_pen   = hdc->SelectObject(&hpen);
  207.         old_brush = (CBrush *)hdc->SelectStockObject(NULL_BRUSH);
  208.         old_ROP   = hdc->SetROP2(R2_NOT);
  209.         GetParent()->ClientToScreen(&point);
  210.         ScreenToClient(&point);
  211.         MoveMode = DetermineMoveMode(point);
  212.         DrawRubberBand(hdc, &rectNew);
  213.     }
  214. }
  215.  
  216. afx_msg void MyPanel::OnLButtonUp(UINT nFlags, CPoint point)
  217. {
  218.     if(bStartMove)
  219.     {
  220.         DrawRubberBand(hdc, (CRect *)NULL);
  221.         MoveWindow(&rectNew, TRUE);
  222.         ReleaseCapture();
  223.         hpen.DeleteObject();
  224.         hdc->SelectObject(old_pen);
  225.         hdc->SelectObject(old_brush);
  226.         hdc->SetROP2(old_ROP);
  227.         GetParent()->ReleaseDC(hdc);
  228.         bStartMove = FALSE;
  229.         InvalidateRect((CRect *)NULL, TRUE);
  230.     }
  231.                         
  232. }
  233.  
  234. afx_msg void MyPanel::OnMouseMove(UINT nFlags, CPoint point)
  235. {
  236.     if(bStartMove && (nFlags & (MK_SHIFT|MK_LBUTTON)))
  237.     {
  238.         ClientToScreen(&point);
  239.         GetParent()->ScreenToClient(&point);
  240.         int     x_offset = point.x - ptOrigin.x;
  241.         int    y_offset = point.y - ptOrigin.y;
  242.         hdc->SetROP2(R2_NOT);
  243.         if(rectNew.left+x_offset < 0)
  244.                 x_offset = -rectNew.left;
  245.         else
  246.         if(rectNew.right+x_offset > rectParent.right)
  247.                 x_offset = rectParent.right - rectNew.right;
  248.         if(rectNew.top+y_offset < 0)
  249.                 y_offset = -rectNew.top;
  250.         else
  251.         if(rectNew.bottom+y_offset > rectParent.bottom)
  252.                 y_offset = rectParent.bottom - rectNew.bottom;
  253.  
  254.         if((MoveMode & MV_LEFT) && (MoveMode & MV_RIGHT))
  255.         {
  256.                 rectNew.left += x_offset;
  257.                 rectNew.right += x_offset;
  258.         }
  259.         else
  260.         if(MoveMode & MV_LEFT)
  261.         {
  262.             if((rectNew.left + x_offset) > (rectNew.right - MV_LIMIT))
  263.                 x_offset = rectNew.right - MV_LIMIT - rectNew.left;
  264.             rectNew.left += x_offset;
  265.         }
  266.         else
  267.         if(MoveMode & MV_RIGHT)
  268.         {
  269.             if((rectNew.right + x_offset) < (rectNew.left + MV_LIMIT))
  270.                 x_offset = rectNew.left + MV_LIMIT - rectNew.right;
  271.             rectNew.right += x_offset;
  272.         }
  273.         if((MoveMode & MV_TOP) && (MoveMode & MV_BOTTOM))
  274.         {
  275.             rectNew.top += y_offset;
  276.             rectNew.bottom += y_offset;
  277.         }
  278.         else
  279.         if(MoveMode & MV_TOP)
  280.         {
  281.             if((rectNew.top + y_offset) > (rectNew.bottom - MV_LIMIT))
  282.                 y_offset = rectNew.bottom - MV_LIMIT - rectNew.top;
  283.             rectNew.top += y_offset;
  284.         }
  285.         else
  286.         if(MoveMode & MV_BOTTOM)
  287.         {
  288.             if((rectNew.bottom + y_offset) < (rectNew.top + MV_LIMIT))
  289.                 y_offset = rectNew.top + MV_LIMIT - rectNew.bottom;
  290.             rectNew.bottom += y_offset;
  291.         }
  292.  
  293.         //DrawRubberBand(hdc, &rectNew);
  294.         MoveWindow(&rectNew, TRUE);
  295.         ptOrigin.Offset(x_offset, y_offset);
  296.     }
  297. }
  298. ======================= E N D   O F   S E G M E N T ===================
  299.  
  300.