NGMsoftware

NGMsoftware
로그인 회원가입
  • 매뉴얼
  • 학습
  • 매뉴얼

    학습


    C# C# .NET 매크로 프로그램 만들기. (크기 연산)

    페이지 정보

    본문

    안녕하세요. 엔지엠소프트웨어입니다. 이전 시간에 이어서 오늘은 크기 연산 액션에 대해서 만들어볼께요. 크기 연산은 엔지엠 매크로 액션중에서 사용빈도가 가장 작은 액션중에 하나입니다. 위치에 따라서 마우스 동작이 영향을 많이 받는데요. 크기의 경우에는 일반적으로 윈도우가 이전 크기를 기억하고 다시 실행할 때 그대로 열어주기 때문에 큰 문제가 되지는 않습니다. 그리고, 크기의 경우 고정된 크기만 지원하거나 특정 비율에 맞게 크기가 설정되는 프로그램도 많아서 매크로를 제작할 때 그에 맞게 만들거든요.

     

    그래도, 좌표 연산만 있으면 뭔가 부족한거 같아서 크기 연산도 만들어두는게 좋을듯 합니다. 잘 사용하지 않지만, 없으면 또 필요할 때 자동화를 만들 수 없게되는 문제가 있거든요. 참고로, 좌표를 나타내는 개체는 Point입니다. 크기를 나타내는 개체는 Size인데요. 둘다 구조체고 2개의 속성을 가집니다. 좌표는 X와 Y를 가지고, 크기는 Width와 Height입니다. 이 둘 모두 이름만 다를뿐 속성 타입은 정수로 같습니다. 그래서, 좌표 연산과 코드는 99프로 동일합니다.

     

    아래와 같은 이름으로 클래스를 하나 추가했습니다.

    public class SizeFormulaModel : BaseModel

     

    크기 연산 후 결과를 재사용하기 위해 데이타 속성을 아래와 같이 추가했습니다.

    [LocalizedCategory("Data")]
    [LocalizedDisplayName("ObjectResult")]
    [LocalizedDescription("ObjectResult")]
    [Browsable(true)]
    [DefaultValue(null)]
    public Size? ObjectResult { get; set; }
    
    [LocalizedCategory("Data")]
    [LocalizedDisplayName("Width")]
    [LocalizedDescription("Width")]
    [Browsable(true)]
    [DefaultValue(null)]
    public int? Width { get; set; }
    
    [LocalizedCategory("Data")]
    [LocalizedDisplayName("Height")]
    [LocalizedDescription("Height")]
    [Browsable(true)]
    [DefaultValue(null)]
    public int? Height { get; set; }

     

    크기 연산을 어떻게 처리할지 결정하는 옵션은 2가지입니다. 자세한 내용은 좌표 연산과 동일하니 이전 글을 참고하면 좋을듯 합니다.

    [LocalizedCategory("Action")]
    [LocalizedDisplayName("Operator")]
    [LocalizedDescription("Operator")]
    [Browsable(true)]
    [DefaultValue(typeof(Ai.Definition.Operator), "Plus")]
    public Ai.Definition.Operator Operator { get; set; } = Definition.Operator.Plus;
    
    [LocalizedCategory("Action")]
    [LocalizedDisplayName("FormulaOption")]
    [LocalizedDescription("FormulaOption")]
    [Browsable(true)]
    [DefaultValue(typeof(Ai.Definition.SizeFormulaOption), "All")]
    public Ai.Definition.SizeFormulaOption Option { get; set; } = Definition.SizeFormulaOption.All;

     

    2개의 크기를 사칙연산으로 처리하는 방법도 좌표 연산과 동일한데요. 형식이 달라서 이름만 달라집니다.

    public override string? Execute(IPlayer player)
    {
        ObjectResult = null;
        Width = null;
        Height = null;
        var id = base.Execute(player);
    
        var lv = Ai.Common.Helper.GetMatches(player, this.GetType().GetProperty(nameof(LeftValue)), LeftValue);
        var rv = Ai.Common.Helper.GetMatches(player, this.GetType().GetProperty(nameof(RightValue)), RightValue);
    
        var leftValue = System.Windows.Size.Parse(Regex.Replace(lv, @"[^0-9,]", ""));
        var rightValue = System.Windows.Size.Parse(Regex.Replace(rv, @"[^0-9,]", ""));
    
        switch (Operator)
        {
            case Definition.Operator.Plus:
                switch (Option)
                {
                    case Definition.SizeFormulaOption.All:
                        ObjectResult = new Size((int)(leftValue.Width + rightValue.Width), (int)(leftValue.Height + rightValue.Height));
                        break;
                    case Definition.SizeFormulaOption.Width:
                        ObjectResult = new Size((int)(leftValue.Width + rightValue.Width), (int)leftValue.Height);
                        break;
                    case Definition.SizeFormulaOption.Height:
                        ObjectResult = new Size((int)leftValue.Width, (int)(leftValue.Height + rightValue.Height));
                        break;
                }
                break;
            case Definition.Operator.Minus:
                switch (Option)
                {
                    case Definition.SizeFormulaOption.All:
                        ObjectResult = new Size((int)(leftValue.Width - rightValue.Width), (int)(leftValue.Height - rightValue.Height));
                        break;
                    case Definition.SizeFormulaOption.Width:
                        ObjectResult = new Size((int)(leftValue.Width - rightValue.Width), (int)leftValue.Height);
                        break;
                    case Definition.SizeFormulaOption.Height:
                        ObjectResult = new Size((int)leftValue.Width, (int)(leftValue.Height - rightValue.Height));
                        break;
                }
                break;
            case Definition.Operator.Multiply:
                switch (Option)
                {
                    case Definition.SizeFormulaOption.All:
                        ObjectResult = new Size((int)(leftValue.Width * rightValue.Width), (int)(leftValue.Height * rightValue.Height));
                        break;
                    case Definition.SizeFormulaOption.Width:
                        ObjectResult = new Size((int)(leftValue.Width * rightValue.Width), (int)leftValue.Height);
                        break;
                    case Definition.SizeFormulaOption.Height:
                        ObjectResult = new Size((int)leftValue.Width, (int)(leftValue.Height * rightValue.Height));
                        break;
                }
                break;
            case Definition.Operator.Divide:
                switch (Option)
                {
                    case Definition.SizeFormulaOption.All:
                        ObjectResult = new Size((int)(leftValue.Width / rightValue.Width), (int)(leftValue.Height / rightValue.Height));
                        break;
                    case Definition.SizeFormulaOption.Width:
                        ObjectResult = new Size((int)(leftValue.Width / rightValue.Width), (int)leftValue.Height);
                        break;
                    case Definition.SizeFormulaOption.Height:
                        ObjectResult = new Size((int)leftValue.Width, (int)(leftValue.Height / rightValue.Height));
                        break;
                }
                break;
        }
    
        if (ObjectResult.HasValue)
        {
            Width = ObjectResult.Value.Width;
            Height = ObjectResult.Value.Height;
            Result = ObjectResult.Value.ToString();
        }
    
        return id;
    }

     

    매크로를 실행하고, 아래와 같이 테스트를 해봤는데요. 잘 동작하는군요^^

    LECQAFV.jpeg

     

     

    계산 옵션을 Width로 변경하고, 실행하면 왼쪽 값과 오른쪽 값의 Width만 연산이 됩니다.

    UpKEVuA.jpeg

     

     

    개발자에게 후원하기

    MGtdv7r.png

     

    추천, 구독, 홍보 꼭~ 부탁드립니다.

    여러분의 후원이 빠른 귀농을 가능하게 해줍니다~ 답답한 도시를 벗어나 귀농하고 싶은 개발자~

    감사합니다~

    • 네이버 공유하기
    • 페이스북 공유하기
    • 트위터 공유하기
    • 카카오스토리 공유하기
    추천0 비추천0

    댓글목록

    등록된 댓글이 없습니다.