| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 
 |  
void OnLButtonDown(UINT nFlags,CPoint point)
{
  CRect rect;
  if (TrackRubberBand(rect,point))
  {
    if (!rect.IsRectEmpty())
    {
       rect.right++;
       rect.bottom++;
 
       // ici rect est le rectangle sélectionné
 
    }
  }
}
 
// gestion du rectangle de sélection
bool TrackRubberBand(LPRECT pRect,CPoint refpt)
{
  bool result=false;
 
  if (GetCapture()==NULL)
  {
    CPen pen;
    if (pen.CreatePen(PS_DOT,0,RGB(0,0,0)))
    {
      UpdateWindow();                             // mise à jour de l'affichage
 
      AfxLockTempMaps();
 
      SetCapture();                               // capture de la souris
 
      CRect oldrect;                              // rectangle de l'ancienne sélection
      bool visible=false;                         // drapeau 'rectangle visible'
 
      while (GetCapture()==this)
      {
        MSG msg;
        VERIFY(GetMessage(&msg,NULL,0,0));
 
        switch (msg.message)
        {
          case WM_MOUSEMOVE:
          {
            CRect newrect;                        // rectangle de la nouvelle sélection
 
            if (!CalcRubberBand(newrect,refpt,msg.lParam))
              StartAutoScroll();                  // démarrage du défilement automatique
            else
              StopAutoScroll();                   // arret du défilement automatique
 
            if (!newrect.IsRectEmpty())
            {
              newrect.right++;
              newrect.bottom++;
 
              if (visible)
              {
                if (DrawRubberBand(&pen,oldrect,newrect))
                  oldrect=newrect;
              }
              else
              {
                visible=DrawRubberBand(&pen,newrect);
                if (visible)
                  oldrect=newrect;
              }
            }
            else
            {
              if (visible)
              {
                if (!DrawRubberBand(&pen,oldrect))
                {
                  Invalidate();
                  UpdateWindow();
                }
 
                visible=false;
              }
            }
 
            continue;
          }
          case WM_LBUTTONUP:
          {
            if (visible)
              if (!DrawRubberBand(&pen,oldrect))  // effacement du rectangle de sélection
                Invalidate();
 
            CalcRubberBand(pRect,refpt,msg.lParam);   // calcul du rectangle de sélection
 
            result=true;
 
            break;
          }
          case WM_KEYDOWN:
          {
            if (msg.wParam==VK_ESCAPE)
              break;
 
            continue;
          }
          case WM_TIMER:
          {
            if (msg.wParam==ID_AUTOSCROLL_TIMER)
            {
              if (visible)
                if (!DrawRubberBand(&pen,oldrect))
                  Invalidate();
 
              LONG oldx=m_XOrigin;
              LONG oldy=m_YOrigin;
 
              DispatchMessage(&msg);
 
              UpdateWindow();
 
              refpt.x+=m_XOrigin-oldx;
              refpt.y+=m_YOrigin-oldy;
 
              CPoint newpt;
              GetCursorPos(&newpt);
              ScreenToClient(&newpt);
 
              CalcRubberBand(oldrect,refpt,newpt);
 
              if (!oldrect.IsRectEmpty())
              {
                oldrect.right++;
                oldrect.bottom++;
 
                visible=DrawRubberBand(&pen,oldrect);
              }
              else
                visible=false;
            }
            else
              DispatchMessage(&msg);
 
            continue;
          }
          default:
          {
            if (  (msg.message<WM_MOUSEFIRST  ||  msg.message>WM_MOUSELAST) &&
                  (msg.message<WM_KEYFIRST    ||  msg.message>WM_KEYFIRST)  )
            {
              DispatchMessage(&msg);
            }
 
            continue;
          }
        }
 
        ReleaseCapture();                         // libération de la souris
        break;
      }
 
      StopAutoScroll();                         // arret du défilement automatique
      AfxUnlockTempMaps(FALSE);
    }
  }
 
  return result;
}
 
// calcul du rectangle de sélection
bool CalcRubberBand(LPRECT pRect,CPoint refpt,CPoint newpt)
{
  CRect rect;
  GetClientRect(rect);
 
  bool inside=true;
 
  if (newpt.x<0)
  {
    newpt.x=0;
    inside=false;
  }
  else
    if (newpt.x>=rect.right)
    {
      newpt.x=rect.right-1;
      inside=false;
    }
 
  if (newpt.y<0)
  {
    newpt.y=0;
    inside=false;
  }
  else
    if (newpt.y>=rect.bottom)
    {
      newpt.y=rect.bottom-1;
      inside=false;
    }
 
  if (refpt.x<newpt.x)
  {
    pRect->left=refpt.x;
    pRect->right=newpt.x;
  }
  else
  {
    pRect->left=newpt.x;
    pRect->right=refpt.x;
  }
 
  if (refpt.y<newpt.y)
  {
    pRect->top=refpt.y;
    pRect->bottom=newpt.y;
  }
  else
  {
    pRect->top=newpt.y;
    pRect->bottom=refpt.y;
  }
 
  return inside;
}
 
// dessin du rectangle de sélection
bool DrawRubberBand(CPen *pPen,LPCRECT pRect1,LPCRECT pRect2 = /*NULL*/ )
{
  CDC *pDC=GetDC();                             // 'Device Context' de la fenetre
  if (pDC==NULL)
    return false;                               // erreur
 
  CPen *pOldPen=pDC->SelectObject(pPen);        // sélection du stylo
  CGdiObject *pOldBrush=pDC->SelectStockObject(HOLLOW_BRUSH);   // sélection de la brosse
  int ROP=pDC->SetROP2(R2_NOTXORPEN);           // sélection du mode de mixage
 
  pDC->Rectangle(pRect1);
 
  if (pRect2!=NULL)
    pDC->Rectangle(pRect2);
 
  pDC->SetROP2(ROP);                            // restitution de l'ancien mode de mixage
  pDC->SelectObject(pOldBrush);                 // restitution de l'ancienne brosse
  pDC->SelectObject(pOldPen);                   // restitution de l'ancien stylo
 
  ReleaseDC(pDC);                               // libération du 'Device Context'
 
  return true;                                  // succès
} | 
Partager