void CMainWindow::InsertSelectionFromHBITMAP(HBITMAP bitmap, HWND window) { int width = GetDIBWidth(bitmap); int height = GetDIBHeight(bitmap); int curWidth = imageModel.GetWidth(); int curHeight = imageModel.GetHeight(); if (width > curWidth || height > curHeight) { BOOL shouldEnlarge = TRUE; if (askBeforeEnlarging) { TCHAR programname[20]; TCHAR shouldEnlargePromptText[100]; LoadString(hProgInstance, IDS_PROGRAMNAME, programname, SIZEOF(programname)); LoadString(hProgInstance, IDS_ENLARGEPROMPTTEXT, shouldEnlargePromptText, SIZEOF(shouldEnlargePromptText)); switch (MessageBox(shouldEnlargePromptText, programname, MB_YESNOCANCEL | MB_ICONQUESTION)) { case IDYES: break; case IDNO: shouldEnlarge = FALSE; break; case IDCANCEL: return; } } if (shouldEnlarge) { if (width > curWidth) curWidth = width; if (height > curHeight) curHeight = height; imageModel.Crop(curWidth, curHeight, 0, 0); } } HWND hToolbar = FindWindowEx(toolBoxContainer.m_hWnd, NULL, TOOLBARCLASSNAME, NULL); SendMessage(hToolbar, TB_CHECKBUTTON, ID_RECTSEL, MAKELPARAM(TRUE, 0)); toolBoxContainer.SendMessage(WM_COMMAND, ID_RECTSEL); imageModel.CopyPrevious(); selectionModel.InsertFromHBITMAP(bitmap); placeSelWin(); selectionWindow.ShowWindow(SW_SHOW); ForceRefreshSelectionContents(); }
void endPaintingL(HDC hdc, LONG x, LONG y, COLORREF fg, COLORREF bg) { switch (activeTool) { case TOOL_FREESEL: { POINT *ptStackCopy; int i; rectSel_src[0] = rectSel_src[1] = 0x7fffffff; rectSel_src[2] = rectSel_src[3] = 0; for (i = 0; i <= ptSP; i++) { if (ptStack[i].x < rectSel_src[0]) rectSel_src[0] = ptStack[i].x; if (ptStack[i].y < rectSel_src[1]) rectSel_src[1] = ptStack[i].y; if (ptStack[i].x > rectSel_src[2]) rectSel_src[2] = ptStack[i].x; if (ptStack[i].y > rectSel_src[3]) rectSel_src[3] = ptStack[i].y; } rectSel_src[2] += 1 - rectSel_src[0]; rectSel_src[3] += 1 - rectSel_src[1]; rectSel_dest[0] = rectSel_src[0]; rectSel_dest[1] = rectSel_src[1]; rectSel_dest[2] = rectSel_src[2]; rectSel_dest[3] = rectSel_src[3]; if (ptSP != 0) { DeleteObject(hSelMask); hSelMask = CreateBitmap(rectSel_src[2], rectSel_src[3], 1, 1, NULL); DeleteObject(SelectObject(hSelDC, hSelMask)); ptStackCopy = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(POINT) * (ptSP + 1)); for (i = 0; i <= ptSP; i++) { ptStackCopy[i].x = ptStack[i].x - rectSel_src[0]; ptStackCopy[i].y = ptStack[i].y - rectSel_src[1]; } Poly(hSelDC, ptStackCopy, ptSP + 1, 0x00ffffff, 0x00ffffff, 1, 2, TRUE); HeapFree(GetProcessHeap(), 0, ptStackCopy); SelectObject(hSelDC, hSelBm = CreateDIBWithProperties(rectSel_src[2], rectSel_src[3])); resetToU1(); MaskBlt(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], hDrawingDC, rectSel_src[0], rectSel_src[1], hSelMask, 0, 0, MAKEROP4(SRCCOPY, WHITENESS)); Poly(hdc, ptStack, ptSP + 1, bg, bg, 1, 2, TRUE); newReversible(); MaskBlt(hDrawingDC, rectSel_src[0], rectSel_src[1], rectSel_src[2], rectSel_src[3], hSelDC, 0, 0, hSelMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND)); placeSelWin(); ShowWindow(hSelection, SW_SHOW); /* force refresh of selection contents */ SendMessage(hSelection, WM_LBUTTONDOWN, 0, 0); SendMessage(hSelection, WM_MOUSEMOVE, 0, 0); SendMessage(hSelection, WM_LBUTTONUP, 0, 0); } HeapFree(GetProcessHeap(), 0, ptStack); ptStack = NULL; break; } case TOOL_RECTSEL: resetToU1(); if ((rectSel_src[2] != 0) && (rectSel_src[3] != 0)) { DeleteObject(hSelMask); hSelMask = CreateBitmap(rectSel_src[2], rectSel_src[3], 1, 1, NULL); DeleteObject(SelectObject(hSelDC, hSelMask)); Rect(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], 0x00ffffff, 0x00ffffff, 1, 2); SelectObject(hSelDC, hSelBm = CreateDIBWithProperties(rectSel_src[2], rectSel_src[3])); resetToU1(); BitBlt(hSelDC, 0, 0, rectSel_src[2], rectSel_src[3], hDrawingDC, rectSel_src[0], rectSel_src[1], SRCCOPY); Rect(hdc, rectSel_src[0], rectSel_src[1], rectSel_src[0] + rectSel_src[2], rectSel_src[1] + rectSel_src[3], bgColor, bgColor, 0, TRUE); newReversible(); BitBlt(hDrawingDC, rectSel_src[0], rectSel_src[1], rectSel_src[2], rectSel_src[3], hSelDC, 0, 0, SRCCOPY); placeSelWin(); ShowWindow(hSelection, SW_SHOW); /* force refresh of selection contents */ SendMessage(hSelection, WM_LBUTTONDOWN, 0, 0); SendMessage(hSelection, WM_MOUSEMOVE, 0, 0); SendMessage(hSelection, WM_LBUTTONUP, 0, 0); } break; case TOOL_RUBBER: Erase(hdc, last.x, last.y, x, y, bg, rubberRadius); break; case TOOL_PEN: Line(hdc, last.x, last.y, x, y, fg, 1); SetPixel(hdc, x, y, fg); break; case TOOL_LINE: resetToU1(); if (GetAsyncKeyState(VK_SHIFT) < 0) roundTo8Directions(start.x, start.y, &x, &y); Line(hdc, start.x, start.y, x, y, fg, lineWidth); break; case TOOL_BEZIER: pointSP++; if (pointSP == 4) pointSP = 0; break; case TOOL_RECT: resetToU1(); if (GetAsyncKeyState(VK_SHIFT) < 0) regularize(start.x, start.y, &x, &y); Rect(hdc, start.x, start.y, x, y, fg, bg, lineWidth, shapeStyle); break; case TOOL_SHAPE: resetToU1(); pointStack[pointSP].x = x; pointStack[pointSP].y = y; if ((pointSP > 0) && (GetAsyncKeyState(VK_SHIFT) < 0)) roundTo8Directions(pointStack[pointSP - 1].x, pointStack[pointSP - 1].y, &pointStack[pointSP].x, &pointStack[pointSP].y); pointSP++; if (pointSP >= 2) { if ((pointStack[0].x - x) * (pointStack[0].x - x) + (pointStack[0].y - y) * (pointStack[0].y - y) <= lineWidth * lineWidth + 1) { Poly(hdc, pointStack, pointSP, fg, bg, lineWidth, shapeStyle, TRUE); pointSP = 0; } else { Poly(hdc, pointStack, pointSP, fg, bg, lineWidth, shapeStyle, FALSE); } } if (pointSP == 255) pointSP--; break; case TOOL_ELLIPSE: resetToU1(); if (GetAsyncKeyState(VK_SHIFT) < 0) regularize(start.x, start.y, &x, &y); Ellp(hdc, start.x, start.y, x, y, fg, bg, lineWidth, shapeStyle); break; case TOOL_RRECT: resetToU1(); if (GetAsyncKeyState(VK_SHIFT) < 0) regularize(start.x, start.y, &x, &y); RRect(hdc, start.x, start.y, x, y, fg, bg, lineWidth, shapeStyle); break; } }
LRESULT CALLBACK SelectionWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_PAINT: { if (!moving) { HDC hDC = GetDC(hwnd); DefWindowProc(hwnd, message, wParam, lParam); SelectionFrame(hDC, 1, 1, rectSel_dest[2] * zoom / 1000 + 5, rectSel_dest[3] * zoom / 1000 + 5); ReleaseDC(hwnd, hDC); } break; } case WM_LBUTTONDOWN: xPos = LOWORD(lParam); yPos = HIWORD(lParam); SetCapture(hwnd); if (action != 0) SetCursor(LoadCursor(NULL, cursors[action])); moving = TRUE; break; case WM_MOUSEMOVE: if (moving) { TCHAR sizeStr[100]; int xDelta; int yDelta; resetToU1(); xFrac += (short)LOWORD(lParam) - xPos; yFrac += (short)HIWORD(lParam) - yPos; if (zoom < 1000) { xDelta = xFrac * 1000 / zoom; xFrac = 0; yDelta = yFrac * 1000 / zoom; yFrac = 0; } else { xDelta = xFrac * 1000 / zoom; xFrac -= (xFrac * 1000 / zoom) * zoom / 1000; yDelta = yFrac * 1000 / zoom; yFrac -= (yFrac * 1000 / zoom) * zoom / 1000; } switch (action) { case 0: rectSel_dest[0] += xDelta; rectSel_dest[1] += yDelta; break; case 1: rectSel_dest[0] += xDelta; rectSel_dest[1] += yDelta; rectSel_dest[2] -= xDelta; rectSel_dest[3] -= yDelta; break; case 2: rectSel_dest[1] += yDelta; rectSel_dest[3] -= yDelta; break; case 3: rectSel_dest[2] += xDelta; rectSel_dest[1] += yDelta; break; case 4: rectSel_dest[0] += xDelta; rectSel_dest[2] -= xDelta; break; case 5: rectSel_dest[2] += xDelta; break; case 6: rectSel_dest[0] += xDelta; rectSel_dest[2] -= xDelta; rectSel_dest[3] += yDelta; break; case 7: rectSel_dest[3] += yDelta; break; case 8: rectSel_dest[2] += xDelta; rectSel_dest[3] += yDelta; break; } _stprintf(sizeStr, _T("%d x %d"), rectSel_dest[2], rectSel_dest[3]); SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) sizeStr); if (action != 0) StretchBlt(hDrawingDC, rectSel_dest[0], rectSel_dest[1], rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, GetDIBWidth(hSelBm), GetDIBHeight(hSelBm), SRCCOPY); else if (transpBg == 0) MaskBlt(hDrawingDC, rectSel_dest[0], rectSel_dest[1], rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, hSelMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND)); else { HBITMAP tempMask; HBRUSH oldBrush; HDC tempDC; tempMask = CreateBitmap(rectSel_dest[2], rectSel_dest[3], 1, 1, NULL); oldBrush = SelectObject(hSelDC, CreateSolidBrush(bgColor)); tempDC = CreateCompatibleDC(hSelDC); SelectObject(tempDC, tempMask); MaskBlt(tempDC, 0, 0, rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, hSelMask, 0, 0, MAKEROP4(NOTSRCCOPY, BLACKNESS)); DeleteDC(tempDC); DeleteObject(SelectObject(hSelDC, oldBrush)); MaskBlt(hDrawingDC, rectSel_dest[0], rectSel_dest[1], rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, tempMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND)); DeleteObject(tempMask); } SendMessage(hImageArea, WM_PAINT, 0, 0); xPos = LOWORD(lParam); yPos = HIWORD(lParam); //SendMessage(hwnd, WM_PAINT, 0, 0); } else { int w = rectSel_dest[2] * zoom / 1000 + 6; int h = rectSel_dest[3] * zoom / 1000 + 6; xPos = LOWORD(lParam); yPos = HIWORD(lParam); SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM) NULL); action = identifyCorner(xPos, yPos, w, h); if (action != 0) SetCursor(LoadCursor(NULL, cursors[action])); } break; case WM_LBUTTONUP: if (moving) { moving = FALSE; ReleaseCapture(); if (action != 0) { HDC hTempDC; HBITMAP hTempBm; hTempDC = CreateCompatibleDC(hSelDC); hTempBm = CreateDIBWithProperties(rectSel_dest[2], rectSel_dest[3]); SelectObject(hTempDC, hTempBm); SelectObject(hSelDC, hSelBm); StretchBlt(hTempDC, 0, 0, rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, GetDIBWidth(hSelBm), GetDIBHeight(hSelBm), SRCCOPY); DeleteObject(hSelBm); hSelBm = hTempBm; hTempBm = CreateBitmap(rectSel_dest[2], rectSel_dest[3], 1, 1, NULL); SelectObject(hTempDC, hTempBm); SelectObject(hSelDC, hSelMask); StretchBlt(hTempDC, 0, 0, rectSel_dest[2], rectSel_dest[3], hSelDC, 0, 0, GetDIBWidth(hSelMask), GetDIBHeight(hSelMask), SRCCOPY); DeleteObject(hSelMask); hSelMask = hTempBm; SelectObject(hSelDC, hSelBm); DeleteDC(hTempDC); } placeSelWin(); ShowWindow(hSelection, SW_HIDE); ShowWindow(hSelection, SW_SHOW); } break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; }