/// <summary> /// Diese Funktion dient dazu, die Grid-Handles proportional zu skalieren, so dass /// Benutzer das Bild zwar in der Größe ändern kann, jedoch das Seitenverhältnis immer /// beibehalten bleibt. /// </summary> /// <remarks> /// Stichwörter: bild, image, resize, size, größe, groesse, scale, "width", "height". /// </remarks> void IHTMLEditHost.SnapRect(IHTMLElement pIElement, ref tagRECT prcNew, _ELEMENT_CORNER eHandle) { var img = pIElement as IHTMLImgElement; if (img != null) { var key = string.Format(@"{0}-{1}", pIElement.id, img.src); if (!_initialImgSizes.ContainsKey(key)) { _initialImgSizes.Add(key, new Size(img.width, img.height)); } var initialSize = _initialImgSizes[key]; switch (eHandle) { case _ELEMENT_CORNER.ELEMENT_CORNER_RIGHT: case _ELEMENT_CORNER.ELEMENT_CORNER_LEFT: { var fac = initialSize.Height / (float)initialSize.Width; var newWidth = prcNew.right - prcNew.left; var newHeight = fac * newWidth; // Niemals > 100%. newWidth = Math.Min(newWidth, initialSize.Width); newHeight = Math.Min(newHeight, initialSize.Height); prcNew.right = prcNew.left + newWidth; prcNew.bottom = (int)(prcNew.top + newHeight); img.width = newWidth; img.height = (int)newHeight; } break; case _ELEMENT_CORNER.ELEMENT_CORNER_TOP: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOM: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMLEFT: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMRIGHT: case _ELEMENT_CORNER.ELEMENT_CORNER_TOPLEFT: case _ELEMENT_CORNER.ELEMENT_CORNER_TOPRIGHT: { var fac = initialSize.Width / (float)initialSize.Height; var newHeight = prcNew.bottom - prcNew.top; var newWidth = fac * newHeight; // Niemals > 100%. newWidth = Math.Min(newWidth, initialSize.Width); newHeight = Math.Min(newHeight, initialSize.Height); prcNew.right = (int)(prcNew.left + newWidth); prcNew.bottom = prcNew.top + newHeight; img.width = (int)newWidth; img.height = newHeight; } break; // TODO } //prcNew.right = prcNew.left + img.width; //prcNew.bottom = prcNew.top + img.height; } }
/// <summary> /// Diese Funktion dient dazu, die Grid-Handles proportional zu skalieren, so dass /// Benutzer das Bild zwar in der Größe ändern kann, jedoch das Seitenverhältnis immer /// beibehalten bleibt. /// </summary> /// <remarks> /// Stichwörter: bild, image, resize, size, größe, groesse, scale, "width", "height". /// </remarks> void IHTMLEditHost.SnapRect(IHTMLElement pIElement, ref tagRECT prcNew, _ELEMENT_CORNER eHandle) { var img = pIElement as IHTMLImgElement; if (img != null) { var key = String.Format(@"{0}-{1}", pIElement.id, img.src); if (!_initialImgSizes.ContainsKey(key)) { _initialImgSizes.Add(key, new Size(img.width, img.height)); } var initialSize = _initialImgSizes[key]; switch (eHandle) { case _ELEMENT_CORNER.ELEMENT_CORNER_RIGHT: case _ELEMENT_CORNER.ELEMENT_CORNER_LEFT: { var fac = initialSize.Height / (float)initialSize.Width; var newWidth = prcNew.right - prcNew.left; var newHeight = fac * newWidth; // Niemals > 100%. newWidth = Math.Min(newWidth, initialSize.Width); newHeight = Math.Min(newHeight, initialSize.Height); prcNew.right = prcNew.left + newWidth; prcNew.bottom = (int)(prcNew.top + newHeight); img.width = newWidth; img.height = (int)newHeight; } break; case _ELEMENT_CORNER.ELEMENT_CORNER_TOP: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOM: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMLEFT: case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMRIGHT: case _ELEMENT_CORNER.ELEMENT_CORNER_TOPLEFT: case _ELEMENT_CORNER.ELEMENT_CORNER_TOPRIGHT: { var fac = initialSize.Width / (float)initialSize.Height; var newHeight = prcNew.bottom - prcNew.top; var newWidth = fac * newHeight; // Niemals > 100%. newWidth = Math.Min(newWidth, initialSize.Width); newHeight = Math.Min(newHeight, initialSize.Height); prcNew.right = (int)(prcNew.left + newWidth); prcNew.bottom = prcNew.top + newHeight; img.width = (int)newWidth; img.height = newHeight; } break; // TODO } } }
private bool IsTrueCorner(_ELEMENT_CORNER elementCorner) { return elementCorner == _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMLEFT || elementCorner == _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMRIGHT || elementCorner == _ELEMENT_CORNER.ELEMENT_CORNER_TOPLEFT || elementCorner == _ELEMENT_CORNER.ELEMENT_CORNER_TOPRIGHT; }
int IHTMLEditHostRaw.SnapRect(IHTMLElement pIElement, ref RECT prcNEW, _ELEMENT_CORNER elementCorner) { try { // forward SnapRect out via events if (SnapRectEvent != null) SnapRectEvent(pIElement, ref prcNEW, elementCorner); // anytime the editor calls SnapRect it implies an edit -- force the editor dirty IsDirty = true; return HRESULT.S_OK; } catch (Exception ex) { Trace.Fail("Unexpected exception during SnapRect event: " + ex.ToString()); return HRESULT.E_FAILED; } }
private void htmlEditor_SnapRectEvent(IHTMLElement pIElement, ref RECT prcNEW, _ELEMENT_CORNER elementCorner) { IHTMLImgElement imgElement = pIElement as IHTMLImgElement; if (imgElement != null) { // see if we are scaling a new image if (snapRectImageElement == null || snapRectImageElement.sourceIndex != snapRectImageElement.sourceIndex) { // save the image element so that mouseUp handler can regenerate the resized image. snapRectImageElement = pIElement; //save the initial image size so that we can scale the image based on the original size. //Note: this is important to minimize rounding errors that may occurs while the image is snapping //the old logic scaled based on the imgElement height and width attributes, so each snap iteration would //case the image to distort more and more. snapRectInitialImageSize = new Size(imgElement.width, imgElement.height); } // only preserve constraints if we are sizing a true corner if (IsTrueCorner(elementCorner)) { snapRectPreserveConstraints = true; // get the new width and height int width = prcNEW.right - prcNEW.left; int height = prcNEW.bottom - prcNEW.top; // scale the picture (using its current proportions!) based on the size of the snap rectangle. Size constrainedSize = ImageUtils.GetScaledImageSize(width, height, snapRectInitialImageSize); width = constrainedSize.Width; height = constrainedSize.Height; // adjust the location and size of the image switch (elementCorner) { case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMLEFT: prcNEW.left = prcNEW.right - width; prcNEW.bottom = prcNEW.top + height; break; case _ELEMENT_CORNER.ELEMENT_CORNER_BOTTOMRIGHT: prcNEW.right = prcNEW.left + width; prcNEW.bottom = prcNEW.top + height; break; case _ELEMENT_CORNER.ELEMENT_CORNER_TOPLEFT: prcNEW.left = prcNEW.right - width; prcNEW.top = prcNEW.bottom - height; break; case _ELEMENT_CORNER.ELEMENT_CORNER_TOPRIGHT: prcNEW.right = prcNEW.left + width; prcNEW.top = prcNEW.bottom - height; break; default: Trace.Fail("Unexpected element corner: " + elementCorner.ToString()); break; } } //Hack: unset the width and height attributes so that the MSHTMLEditor doesn't set the height/width //using the style attribute when it handles the snapRect. Once its set using style, it can never be unset... pIElement.removeAttribute("width", 0); pIElement.removeAttribute("height", 0); } //suppress resizing of the the extended entry splitter if (pIElement.id == PostBodyEditingElementBehavior.EXTENDED_ENTRY_ID) { IHTMLElement2 e2 = (IHTMLElement2)pIElement; IHTMLRect rect = e2.getBoundingClientRect(); prcNEW.top = rect.top; prcNEW.bottom = rect.bottom; prcNEW.left = rect.left; prcNEW.right = rect.right; } }