Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@user1788175 - just use a RECT struct. When the drawing starts, you set the left &amp; top members to the x,y pos of the mouse. When the mouse is released, set the right,bottom members. Swap left,right if necessary, to ensure that left <p>Here's some code ripped from a class I wrote to deal with with selection rectangles. You can ignore the normalize code - it just converts the pixel coordinates to ones in the range [0..1], so I can draw the selection on a scaled-down version of the image, yet still select the same region if the image is shown at a different scale. My images were 4944x6992, so I had to display a scaled-down version of them.</p> <pre><code>LRESULT CALLBACK cSelBoxImg::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { RECT mappedRect, tmpRect, myRect; HBRUSH myBrush; BITMAP bm; PAINTSTRUCT ps; HDC mDC; HBITMAP tmpBmp; switch (uMsg) { case WM_LBUTTONDOWN: if (bMouseSelectionEnabled) { bRubberBanding = true; mouseClickDownPos.x = LOWORD(lParam); mouseClickDownPos.y = HIWORD(lParam); curMousePos = mouseClickDownPos; selectionRect.left = min(curMousePos.x, mouseClickDownPos.x); selectionRect.right = max(curMousePos.x, mouseClickDownPos.x); selectionRect.top = min(curMousePos.y, mouseClickDownPos.y); selectionRect.bottom = max(curMousePos.y, mouseClickDownPos.y); normalizeSelRect(); InvalidateRect(mHwnd, NULL, isBkgTransparent); PostMessage(GetParent(hWnd), WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd); } return 1; case WM_LBUTTONUP: if (bMouseSelectionEnabled) if (bRubberBanding) { bRubberBanding = false; mouseClickUpPos.x = LOWORD(lParam); mouseClickUpPos.y = HIWORD(lParam); selectionRect.left = min(mouseClickUpPos.x, mouseClickDownPos.x); selectionRect.right = max(mouseClickUpPos.x, mouseClickDownPos.x); selectionRect.top = min(mouseClickUpPos.y, mouseClickDownPos.y); selectionRect.bottom = max(mouseClickUpPos.y, mouseClickDownPos.y); normalizeSelRect(); InvalidateRect(mHwnd, NULL, isBkgTransparent); PostMessage(GetParent(hWnd), WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd); } return 1; case WM_MOUSEMOVE: if (bMouseSelectionEnabled) if (bRubberBanding) { curMousePos.x = LOWORD(lParam); curMousePos.y = HIWORD(lParam); selectionRect.left = min(curMousePos.x, mouseClickDownPos.x); selectionRect.right = max(curMousePos.x, mouseClickDownPos.x); selectionRect.top = min(curMousePos.y, mouseClickDownPos.y); selectionRect.bottom = max(curMousePos.y, mouseClickDownPos.y); // UpdateWindow(hWnd); //RedrawWindow(hWnd, NULL, NULL, RDW_UPDATENOW); normalizeSelRect(); InvalidateRect(hWnd, NULL, false); PostMessage(GetParent(hWnd), WM_COMMAND, GetDlgCtrlID(hWnd), (LPARAM)hWnd); // printf("Message posted\n"); } return 0; case WM_PAINT: mDC = BeginPaint(hWnd, &amp;ps); GetClientRect(hWnd, &amp;tmpRect); // GetObject(mBmp, sizeof(bm), &amp;bm); mappedRect.left = mLeft * tmpRect.right; mappedRect.right = mRight * tmpRect.right; mappedRect.top = mTop * tmpRect.bottom; mappedRect.bottom = mBottom * tmpRect.bottom; displayImage(); if (mBmp) drawRect(mDC, mappedRect, RGB(0,0,255)); DeleteObject(tmpBmp); EndPaint(hWnd, &amp;ps); return 0; case WM_ERASEBKGND: if (isBkgTransparent) { GetClientRect(hWnd, &amp;myRect); myBrush = (HBRUSH) GetWindowLong(hWnd, GCL_HBRBACKGROUND); FillRect((HDC)wParam, &amp;myRect, myBrush); printf("background cleared\n"); } return true; case WM_SETCURSOR: SetCursor(mCursor); return true; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload