как перемещать масштабированные линии в GDI + - PullRequest
1 голос
/ 14 декабря 2011

У меня есть коробка с картинками.В графическом окне я рисую линии, используя:

gfx.DrawLine(nPen, line.xBegin, line.yBegin, line.xEnd, line.yEnd);

line - это объект, содержащий начальные и конечные значения линий.

Я рисую 10-кратные линии в этом графическом окне,Мой слушатель mouse_Wheel содержит код:

gfx.TranslateTransform((panelpreview.Width) / 2, (panelpreview.Height) / 2);
gfx.ScaleTransform(imageScale, imageScale);
gfx.TranslateTransform(-(panelpreview.Width) / 2, -(panelpreview.Height) / 2);

В настоящее время я пытаюсь переместить строки на:

    private void panelPreview_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            checkLinesIntersection(e.Location); //checks if e.Location intersects with the line
            panelPreview.MouseUp += new MouseEventHandler(panelPreview_MouseUpLine);
            panelPreview.MouseMove += new MouseEventHandler(panelPreview_MouseMoveLine);
        }
    }

    void panelPreview_MouseMoveLine(object sender, MouseEventArgs e)
    {
        if (_Leading.movable)
            newFont.Leading = e.Y - (_base.yBegin + newFont._descenderHeight);

        if (_xHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.xHeight = _base.yBegin - e.Y;

        if (_capHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.capHeight = _base.yBegin - e.Y;

        if (_ascenderHeight.movable)
            if (_base.yBegin - e.Y >= 0)
                newFont.ascenderHeight = _base.yBegin - e.Y;

        if (_descenderHeight.movable)
            if (e.Y - _base.yBegin >= 0)
                newFont.descenderHeight = e.Y - _base.yBegin;

        UpdatePreviewWindow();
    }

    void panelPreview_MouseUpLine(object sender, MouseEventArgs e)
    {
        panelPreview.MouseMove -= new MouseEventHandler(panelPreview_MouseMoveLine);
        panelPreview.MouseUp -= new MouseEventHandler(panelPreview_MouseUpLine);
    }

Проблема заключается в том, что после увеличения, хотя визуально я нажимаю на строку, еене реагирует так, как должно.

    public void checkLinesIntersection(Point mouseLocation)
    {
        lineIntersects(_Leading, mouseLocation);
        lineIntersects(_xHeight, mouseLocation);
    }

    private void lineIntersects(nLine line, Point mouseLocation)
    {
        if (mouseLocation.X >= line.xBegin && mouseLocation.X <= line.xEnd)
            if (mouseLocation.Y >= line.yBegin || mouseLocation.Y + 2 >= line.yBegin)
                if (mouseLocation.Y <= line.yEnd || mouseLocation.Y - 2 <= line.yEnd)
                    switch (line.sName)
                    {
                        case "xHeight":
                            newFont.selectedLine = nFont.Lines._xHeight;
                            break;
                        default:
                            break;
                    }
    }

1 Ответ

0 голосов
/ 30 декабря 2011

Так! Поработав некоторое время над этим инструментом, я изменил подход и использовал XNA для обработки графики (рисование, масштабирование, перемещение объектов рисования). Что мне очень помогло, так это использование Camera в XNA и Matrix для преобразования. ТЕМ НЕ МЕНИЕ! Я не собираюсь публиковать свое решение для XNA для решения этой проблемы, НО с помощью XNA я нашел подход для решения этой проблемы в WinForms.

float Zoom = 1f;
Microsoft.Xna.Framework.Rectangle mouseRect;
Matrix inverse;

void TAB_PICTURE_BOX_MouseWheel(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Delta > 0)
            {
                Zoom+= .1f;
            }
            if (e.Delta < 0)
            {
                Zoom -= .1f;
            }

            gfx.ResetTransform();
            gfx.TranslateTransform((Width + img.Width) / 2, (Height - img.Height) / 2);
            gfx.ScaleTransform(Zoom, Zoom);
            gfx.TranslateTransform(-(Width + img.Width) / 2, -(Height - img.Height) / 2);      
                getInversePosition(e);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

И последний штрих getInversePosition(e); Метод:

 private void getInversePosition(MouseEventArgs e)
    {
        Matrix matrix = Matrix.CreateTranslation(new Vector3(-(Width + img.Width) / 2, -(Height - img.Height) / 2, 0)) *
                  Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
                  Matrix.CreateTranslation(new Vector3((Width + img.Width) / 2, (Height - img.Height) / 2, 0));

        inverse = Matrix.Invert(matrix);
        Vector2 mousePosition = Vector2.Transform(new Vector2(e.X, e.Y), inverse);
        mouseRect = new Microsoft.Xna.Framework.Rectangle((int)mousePosition.X, (int)mousePosition.Y, 1, 1);
    }

Я уверен, что есть способ справиться с этим без участия XNA, НО в моем сценарии XNA сделал свою работу.

...