Exemple #1
0
        private void SyncButtonData(bool AppendFlag = false)
        {
            if (ThisFormData.ListData == null)
            {
                ThisFormData.ListData = new List <ButtonData>();
            }
            List <ButtonData> ButtonDataList = ThisFormData.ListData;
            int stock_button = 0;

            if (AppendFlag)
            {
                stock_button = btnPanel.Children.Count;
            }
            else
            {
                btnPanel.Children.Clear();
                ButtonList.Clear();
                ContentBrushes.Clear();
            }
            if (ButtonDataList != null)
            {
                keysdic.Clear();
                keyslist.Clear();
                for (int i = 0; i < ButtonDataList.Count; i++)
                {
                    ButtonData btnData = ButtonDataList[i];
                    if (btnData.Width == 0 || btnData.Width == double.NaN)
                    {
                        btnData.Width = 1;
                    }
                    if (btnData.Height == 0 || btnData.Height == double.NaN)
                    {
                        btnData.Height = 20;
                    }
                    foreach (var keydata in btnData.KeyDataList)
                    {
                        var code = keydata.Code;
                        if (!keysdic.ContainsKey(code))
                        {
                            keyslist.Add(code);
                            keysdic.Add(code, ThisFormData.Monitor);
                        }
                    }
                    BrushObject brush = new BrushObject();
                    brush.Background       = whiteBrush;
                    brush.Foreground       = blackBrush;
                    brush.Border           = ClearBrush;
                    brush.HoverBackground  = grayBrush;
                    brush.HoverForeground  = null;
                    brush.HoverBorder      = darkgrayBrush;
                    brush.ToggleBackground = lightgrayBrush;
                    brush.ToggleForeground = null;
                    brush.ToggleBorder     = null;
                    var    style   = new Style();
                    object content = "";
                    if (btnData.Width > 0)
                    {
                        style.Setters.Add(new Setter(WidthProperty, btnData.Width * ThisFormData.Scale));
                    }
                    style.Setters.Add(new Setter(HeightProperty, btnData.Height * ThisFormData.Scale));
                    var margin = new Thickness(btnData.Margin.Left * ThisFormData.Scale, btnData.Margin.Top * ThisFormData.Scale, btnData.Margin.Right * ThisFormData.Scale, btnData.Margin.Bottom * ThisFormData.Scale);
                    style.Setters.Add(new Setter(MarginProperty, margin));
                    if (btnData.Size <= 0)
                    {
                        btnData.Size = 12;
                    }
                    style.Setters.Add(new Setter(FontSizeProperty, btnData.Size * ThisFormData.Scale));
                    ImageBrush img = null;
                    if (btnData.PicPath == "" || btnData.PicPath == null || !File.Exists(btnData.PicPath))
                    {
                        content = btnData.Value;
                        if (btnData.Background != "" && btnData.Background != null)
                        {
                            brush.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.Background));
                        }
                        if (btnData.HoverBackground != "" && btnData.HoverBackground != null)
                        {
                            brush.HoverBackground  = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.HoverBackground));
                            brush.ToggleBackground = brush.HoverBackground;
                        }
                        if (btnData.ToggleBackground != "" && btnData.ToggleBackground != null)
                        {
                            brush.ToggleBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.ToggleBackground));
                        }
                        brush.Border       = brush.Background;
                        brush.ToggleBorder = brush.ToggleBackground;
                    }
                    else
                    {
                        try
                        {
                            img              = new ImageBrush();
                            img.ImageSource  = new BitmapImage(new Uri(btnData.PicPath, UriKind.RelativeOrAbsolute));
                            img.Stretch      = Stretch.Uniform;
                            brush.Background = img;
                            if (btnData.HoverPicPath != "" && File.Exists(btnData.HoverPicPath))
                            {
                                img             = new ImageBrush();
                                img.ImageSource = new BitmapImage(new Uri(btnData.HoverPicPath, UriKind.RelativeOrAbsolute));
                                img.Stretch     = Stretch.Uniform;
                            }
                            brush.HoverBackground = img;
                            if (btnData.TogglePicPath != "" && File.Exists(btnData.TogglePicPath))
                            {
                                img             = new ImageBrush();
                                img.ImageSource = new BitmapImage(new Uri(btnData.TogglePicPath, UriKind.RelativeOrAbsolute));
                                img.Stretch     = Stretch.Uniform;
                            }
                            brush.ToggleBackground = img;
                        }
                        catch { }
                    }
                    if (btnData.Foreground != "" && btnData.Foreground != null)
                    {
                        brush.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.Foreground));
                    }
                    if (btnData.Border != "" && btnData.Border != null)
                    {
                        brush.Border = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.Border));
                    }
                    Button btn = new Button();
                    btn.Template = FindResource("buttonTemplate") as ControlTemplate;
                    btn.Click   += (o, e2) => { Button_Click(o, e2); return; };
                    var febtn = btn;
                    febtn.SetValue(ContentProperty, content);
                    febtn.SetValue(FocusableProperty, false);
                    Style festyle = style;
                    if (btnData.HoverForeground != "" && btnData.HoverForeground != null)
                    {
                        brush.HoverForeground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.HoverForeground));
                    }
                    if (btnData.HoverBorder != "" && btnData.HoverBorder != null)
                    {
                        brush.HoverBorder = new SolidColorBrush((Color)ColorConverter.ConvertFromString(btnData.HoverBorder));
                    }
                    if (brush.Background != null)
                    {
                        festyle.Setters.Add(new Setter(BackgroundProperty, brush.Background));
                    }
                    if (brush.Foreground != null)
                    {
                        festyle.Setters.Add(new Setter(ForegroundProperty, brush.Foreground));
                    }
                    if (brush.Border != null)
                    {
                        festyle.Setters.Add(new Setter(BorderBrushProperty, brush.Border));
                    }

                    btn.Name      = ButtonName + (i).ToString();
                    btn.Focusable = false;
                    btn.Style     = style;
                    ContentBrushes.Add(btn, brush);
                    btnPanel.Children.Add(btn);
                    ButtonList.Add(btn);
                }
                SyncResize();
            }
        }
Exemple #2
0
 public MainWindow()
 {
     InitializeComponent();
     btnPanel             = FindName("panel") as WrapPanel;
     cInput               = new CI(1, Dispatcher);
     ButtonList           = new List <ContentControl>();
     ThisFormData         = new FormData(this);
     FormEditingMode      = false;
     MouseLeftButtonDown += (o, e) => {
         try { DragMove(); } catch { }
     };
     //割り当て全解除
     MouseRightButtonDown += (o, e) => {
         foreach (var kvPair in keysdic)
         {
             cInput.KeySend(kvPair.Key, false, true);
         }
     };
     //割合変形
     btnPanel.SizeChanged += (o, e) =>
     {
         SyncResize();
     };
     //FromPanelSetButtonData();
     //WriteFormJSON();
     ReadFormJSON();
     SyncFormViewData();
     SyncButtonData();
     MoniterTimer          = new DispatcherTimer(DispatcherPriority.Normal, Dispatcher);
     MoniterTimer.Interval = TimeSpan.FromMilliseconds(MoniterTimerInterval);
     MoniterTimer.Tick    += (o, e) =>
     {
         if (ButtonList == null)
         {
             return;
         }
         for (int i = 0; i < keyslist.Count; i++)
         {
             keysdic[keyslist[i]] = CI.GetAsyncKeyState((Keys)keyslist[i]) != 0;
         }
         for (int i = 0; i < ThisFormData.ListData.Count; i++)
         {
             ContentControl btn   = ButtonList[i];
             BrushObject    brush = ContentBrushes[btn];
             if (btn.IsMouseOver)
             {
                 if (brush.HoverBackground != null)
                 {
                     btn.Background = brush.HoverBackground;
                 }
                 if (brush.HoverForeground != null)
                 {
                     btn.Foreground = brush.HoverForeground;
                 }
                 if (brush.HoverBorder != null)
                 {
                     btn.BorderBrush = brush.HoverBorder;
                 }
                 continue;
             }
             var  data     = ThisFormData.ListData[i];
             bool keycheck = false;
             if (ThisFormData.Monitor)
             {
                 foreach (var keydata in data.KeyDataList)
                 {
                     keycheck = keysdic[keydata.Code];
                     if (!keycheck)
                     {
                         break;
                     }
                 }
             }
             Brush cb_back, cb_front, cb_border;
             if (keycheck)
             {
                 if (brush.ToggleBackground == null)
                 {
                     cb_back = brush.HoverBackground;
                 }
                 else
                 {
                     cb_back = brush.ToggleBackground;
                 }
                 if (brush.ToggleBackground == null)
                 {
                     cb_front = brush.HoverForeground;
                 }
                 else
                 {
                     cb_front = brush.ToggleForeground;
                 }
                 if (brush.ToggleBorder == null)
                 {
                     cb_border = brush.HoverBorder;
                 }
                 else
                 {
                     cb_border = brush.ToggleBorder;
                 }
             }
             else
             {
                 cb_back   = brush.Background;
                 cb_front  = brush.Foreground;
                 cb_border = brush.Border;
             }
             if (cb_back != null)
             {
                 ButtonList[i].Background = cb_back;
             }
             if (cb_front != null)
             {
                 ButtonList[i].Foreground = cb_front;
             }
             if (cb_border != null)
             {
                 ButtonList[i].BorderBrush = cb_border;
             }
         }
     };
     MoniterTimer.Start();
 }