Don't feel more cool without writing code

xiaoxiao2021-03-06  54

The problem is like this, you have to draw a background area in Chart. This is easy, and the code is written in the window in the window in OnBeforedrawaxes.

The next problem is that in different steps, multiple background areas may be needed, then copy the code in the window is obviously inappropriate. I am going to put the drawing work in a specialized class. In the class, the CHART that needs to be drawn is incremented and taken to the event, and the ONBEFOREDRAWAXES is triggered each time the CHART redraws will call the AREA's drawing method. So whenever you need to add this background for a CHART: area: = tarea.create (chart);

Ok, it is currently more simple. I want to draw multiple areas, I hope to be like this: area1: = tarea.create (chart); is: = tarea.create (chart); problem, Delphi event The mechanism is to process the function pointer, that is, Area handed the address of its own function to Chart, let it call at the right time, but there are two functions, but only one pointer. A simple image just written just the second area.

I think it has encountered such a problem, which is solved, and saves the original event handler before the object takes over the event, and execute the original event after each trigger. The code is probably like this: procedure tanda.create (ACHART: TCHART); begin ... foldeventfunc: = achart.onbeforeDrawaxes; {保 原 处理}} achart.onbeforeDrawaxes: = DRAWAREA; {Takeout event} END;

Procedure tarea.drawarea (sender: TOBJECT); Begin ... {Foldeventfunc) THEN FOLDEVENTFUNC (SENDER); END; This object is triggered like a chain as a chain.

Unfortunately, I also remembered a question that originally used this method. If you accidentally take it twice, then you will form a ring, this event is handled. On the other hand, in the object to deal with an object of a particular event, it is impossible to improve the structure.

In this way, I start thinking about better solutions. You can write an EventManager to manage all events of Chart. There is an event list for each event, EventManager, and each of the objects that need to listen to the event, not directly change the Chart's event pointer, but join through EventManager AddXXEventListener to join . When each event is triggered, EventManager traverses the event handler of each listener in the call list. Then there should be like this in the Tarea constructor: EventManager.addbefordRawaxeslintener (Drawarea);

Considering that I don't want to create an EventManager yourself in the creation of the area, there should be a global factory class, which is responsible for maintaining an EventManager for the chart to draw. The constructor is changed to this: emfactory.getChartem (ACHART) .addbefordrawaxeslintener (Drawarea); Ok, now I have already had a satisfactory plan, I have begun to consider writing an example of test cases for testing, generating an event source And two listeners, indicating how this structure works. However, when I was using an example name for the test, I suddenly sounded the saying: "You won't need it." I really need such a complicated thing? Although I said that I believe it is universal, the structure is also clear enough, but it seems too complicated, I won't use such a complicated thing, isn't it?

Look back, I need to draw multiple areas. (I hope that I haven't forgotten what to do. I just need to adjust the use of the original imagination: areapainter: = tareapainter.create (chart); areapainter.newarea; ... {Set zone attribute} areapainter.newarea; ... This is not solved? And I don't need to implement the newarea method before I really want to draw the second area. Then this object is almost complicated than the initially envisioned area, haha, I really put myself. Now you can use the complex thing, start writing a simple implementation, the only regret is that interface drawing is not suitable for use unit testing.

转载请注明原文地址:https://www.9cbs.com/read-118641.html

New Post(0)