如何动态查看Box [英] How can I view dynamically Box

查看:80
本文介绍了如何动态查看Box的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片



按钮是动态创建的。框表示具体的情况。我想通过选择Filter框从列表中说明。但是当我再次创建一个屏幕闪烁框时。每个案例都被取消,必须填补空白。当我改变位置时,我会变慢。

image

The buttons are created dynamically.Boxes indicate a specific situation. I want to state from the list by selecting the Filter box.But when I create a screen flashing box again. Each case is canceled, the gap must be filled. re going slow when I change position.

推荐答案



我在这里提供的解决方案是解释解决方案所需的最小解决方案你的问题。完整的解决方案可以从 DynamicUpdate 下载。


The solution that I provide here is the minimal needed to explain the solution to your problem. The complete solution can be downloaded from DynamicUpdate.



正如我前面提到的,消除闪烁的一种方法是使用双缓冲。因为我做了很多图形编程,所以我开发了一个名为GraphicsBuffer的类。它基本上可以满足你所需要的一切。如果使用GraphicsBuffer类,则不会遇到跨线程错误。 GraphicsBuffer中您感兴趣的入口点是:


As I mentioned earlier, one way to eliminate flicker is to use double buffering. Because I do a lot of graphics programming, I've developed a class called GraphicsBuffer. It basically does pretty much everything you need. If you use the GraphicsBuffer class, you will not be faced with cross thread errors. The entry points in GraphicsBuffer that will be of interest to you are:



  • GraphicsBuffer - 类构造函数

  • CreateGraphicsBuffer - 完成GraphicsBuffer对象的创建

  • DeleteGraphicsBuffer - 删除当前的GraphicsBuffer实例

  • 图形 - 返回当前实例的Graphics对象

  • RenderGraphicsBuffer - 将当前GraphicsBuffer实例呈现给指定的Graphics对象



如果您对完整的GraphicsBuffer类感兴趣,请对此解决方案发表评论。


If you are interested in the complete GraphicsBuffer class, make a comment to this solution.



以下是解决方案:


Here is the solution:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Timers;
using System.Windows.Forms;

namespace DynamicUpdate
    {

    // requires that Form1 be defined in the Designer

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        // ************************************************* constants

        static Color    BACKGROUND_COLOR = SystemColors.Control;

        const int       BOXES_PER_ROW = 10;
        const int       ROWS_IN_DISPLAY = 10;

        const int       BOX_WIDTH = 50;
        const int       BOX_HEIGHT = BOX_WIDTH;

        const int       INTER_BOX_SPACING = 5;

        const int       LABEL_OFFSET = 1;

        const int       BITMAP_OFFSET = BOX_WIDTH / 5;

        const int       BITMAP_WIDTH = ( BOXES_PER_ROW *
                                         ( INTER_BOX_SPACING  +
                                           BOX_WIDTH ) ) +
                                       INTER_BOX_SPACING ;
        const int       BITMAP_HEIGHT = BITMAP_WIDTH;

        const int       FORM1_WIDTH = BITMAP_WIDTH +
                                      ( 2 * BITMAP_OFFSET );

        const int       FORM1_HEIGHT = BITMAP_HEIGHT +
                                      ( BOX_HEIGHT );

        const double    TIMER_INTERVAL = 10000.0; // ms

        // ************************************************* variables

        GraphicsBuffer      display = null;
                                        // following two variables 
                                        // simulate a change; do not 
                                        // include them in your final
                                        // version
        Random              random = new Random ( );
        int                 random_box = 0;
                                        // without qualification the 
                                        // declaration would conflict 
                                        // with System.Windows.Forms
        System.Timers.Timer timer = null; 

        // ********************************************* OnFormClosing

        // cleanup resources

        protected override void OnFormClosing ( FormClosingEventArgs e )
            {

            base.OnFormClosing ( e );

            if ( display != null )
                {
                display = display.DeleteGraphicsBuffer ( );
                }

            if ( timer != null )
                {
                if ( timer.Enabled )
                    {
                    timer.Stop ( );
                    }
                timer.Dispose ( );
                timer = null;
                }
            }

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );

            this.Width = FORM1_WIDTH;
            this.Height = FORM1_HEIGHT;
                                        // order important here!!
            this.SetStyle ( ( ControlStyles.DoubleBuffer |
                              ControlStyles.UserPaint |
                              ControlStyles.AllPaintingInWmPaint ),
                            true );
            this.UpdateStyles ( );

            timer = new System.Timers.Timer ( TIMER_INTERVAL );
            timer.Elapsed += new ElapsedEventHandler ( tick );
            timer.Start ( );

            this.Invalidate ( );
            }

        // ****************************************************** tick

        private void tick ( object    sender, 
                            EventArgs e )
            {

            this.Invalidate ( );
            }

        // ************************************ create_display_graphic

        void create_display_graphic ( )
            {

            if ( display != null )
                {
                display = display.DeleteGraphicsBuffer ( );
                }
            display = new GraphicsBuffer ( );
            display.CreateGraphicsBuffer ( BITMAP_WIDTH,
                                           BITMAP_HEIGHT );
            display.Graphic.SmoothingMode = SmoothingMode.HighQuality;

            random_box = random.Next ( 0, 100 );
            }

        // ************************************** draw_display_graphic

        void draw_display_graphic ( Graphics graphics )
            {
            int     box_count = 0;
            int     x = BITMAP_OFFSET;
            int     y = BITMAP_OFFSET;

            for ( int i = 0; ( i < ROWS_IN_DISPLAY ); i++ )
                {
                for ( int j = 0; ( j < BOXES_PER_ROW ); j++ )
                    {
                    Brush   brush = new SolidBrush ( Color.Cyan );
                    Pen     pen = new Pen ( Color.Cyan );

                    Rectangle rectangle;

                    if ( box_count == random_box )
                        {
                        brush = new SolidBrush ( Color.Pink );
                        pen = new Pen ( Color.Pink );
                        }

                    rectangle = new Rectangle ( 
                                    new Point ( x, y ),
                                    new Size ( BOX_WIDTH,
                                               BOX_HEIGHT ) );
                    graphics.DrawRectangle ( pen, rectangle );
                    graphics.FillRectangle ( brush, rectangle );
                    graphics.DrawString ( box_count.ToString ( ),
                                          SystemFonts.IconTitleFont,
                                          Brushes.Black,
                                          x + LABEL_OFFSET,
                                          y + LABEL_OFFSET );

                    pen.Dispose ( );
                    brush.Dispose ( );

                    box_count++;

                    x += ( BOX_WIDTH + INTER_BOX_SPACING );
                    }

                x = BITMAP_OFFSET;
                y += ( BOX_HEIGHT + INTER_BOX_SPACING );
                }
            }

        // *************************************************** OnPaint

        protected override void OnPaint ( PaintEventArgs e )
            {

            base.OnPaint ( e );

            e.Graphics.Clear ( BACKGROUND_COLOR );

            create_display_graphic ( );
            draw_display_graphic ( display.Graphic );

            display.RenderGraphicsBuffer ( e.Graphics );
            }

        } // class Form1

    } // namespace DynamicUpdate



虽然我不同意你更新UI的方法(我认为你应该使用事件处理程序),但代码使用了一个计时器。下面介绍各种方法的基本功能。


Although I disagree with your method of updating your UI (I think you should use an event handler), the code uses a timer. The following describes the basic function of the various methods.



  • 宣布之后常量和变量,出现OnFormClosing的方法重写。这是处理在应用程序中创建的对象所必需的,如果不进行处理,将导致内存泄漏。

  • 在Form1构造函数中,我们设置表单的大小,表单的样式,声明计时器,并调用Invalidate(导致表单第一次绘制)。

  • 每次计时器滴答时,都会调用Invalidate并且触发OnPaint事件处理程序。

  • 大量处理发生在OnPaint事件处理程序中:


  • After the declaration of the constants and variables, appears a method override of OnFormClosing. This is required to dispose of objects created in the application that, if not disposed of, would cause a memory leak.
  • In the Form1 constructor we set the size of the form, the form's styles, declare the timer, and invoke Invalidate (causing the form to paint the first time).
  • Each time that the timer ticks, Invalidate is invoked and the OnPaint event handler is triggered.
  • The bulk of processing occurs in the OnPaint event handler that:


  1. 清除表格

  2. 导致重新创建GraphicsBuffer

  3. 导致绘制GraphicsBuffer

  4. 将GraphicsBuffer渲染到屏幕上





请注意,该程序通过随机导致其中一个框以粉红色绘制来模拟外部事件。这可能是您需要额外帮助的地方。


Note that the program simulates an external event by randomly causing one of the boxes to paint in Pink. This is probably where you may need additional help.



希望有所帮助。


Hope that helps.


@gggustafson感谢您的回答,



但我有这个表格的问题,我创建动态控制(面板)。



问题是当人们点击面板时,面板的颜色会改变,我更新数据库表,当面板颜色改变时,我放下(移除)面板框。当我放下盒子时,我重新创建所有的Panel控件,因此屏幕正在刷新。如果我在asp.net中开发这个项目,我使用Ajax Update面板,而不是屏幕刷新。现在我总是屏幕刷新。我该如何减少这个问题?



你知道grapich对象点击事件吗?



感谢您的回答。
@gggustafson thanks for answer,

But I have problem with this Form, I create Dynamic Control(Panel).

Problem is When people click panel, color of panel is change and I update database table, and when panel color is change, I drop(remove) panel box. when I drop box, I re create all Panel controls , so screen is refresing. If I develop this project in asp.net, I use Ajax Update panel, and is not be screen refresing. Now I have always screen refresing. How can I reduce this problem?

And Do you know grapich object click events?

Thanks for answers.


这篇关于如何动态查看Box的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆