C#调用把BarTender模板 联系客服

发布时间 : 星期一 文章C#调用把BarTender模板更新完毕开始阅读

The Engine class represents a BarTender process and provides the backbone for programming with the BarTender Print SDK. All programs written with the BarTender Print SDK will rely on the Engine class to provide BarTender print functionality.

The BarTender Print Engine

The BarTender process (bartend.exe) provides standard BarTender

functionality, such as opening label formats, changing label settings, and printing. The BarTender background process is controlled using an instance of the Engine class.

The Role of the Engine Class

The Engine class contains many methods, properties, and events to assist in printing and controlling the BarTender Print Engine.

Features of the Engine Class

The Engine class allows users to:

? ? ? ?

Start, stop, and restart a BarTender background process. Open, access, and save label formats. Use Engine-level events to monitor printing. Manage the BarTender Application window.

How To: Start and Stop an Engine

An engine must be created and started in order to launch a BarTender process and commence printing. The Engine class provides the Engine.Stop method to explicitly shut down the BarTender Print Engine. If the engine is not stopped, a bartend.exe process may be left running in the background. After calling the Engine.Stop method, it is best practice to call the Engine.Dispose method. The Dispose method ensures all non-memory resources are properly released for the class; this includes shutting down the BarTender process if Engine.Stop was not successfully called. The following is the minimal code necessary to create, start, stop, and dispose an Engine object:

In C#:

// Calling constructor with 'true' automatically starts engine. using (EnginebtEngine = new Engine(true)) {

// Do something with the engine. // Stop the BarTender process. btEngine.Stop(); } In VB:

' Calling constructor with 'true' automatically starts engine. UsingbtEngineAs NewEngine(True) ' Do something with the engine. ' Stop the BarTender process. btEngine.Stop() End Using

In the above example, an engine is created and started implicitly by passing 'true' as an argument to the constructor. The engine is then stopped by calling the Engine.Stop method. This terminates the background bartend.exe process. Finally, Engine.Dispose is called automatically when execution leaves the 'using' statement, releasing all Engine resources.

It is also possible to start the engine explicitly after it has been created using the default Engine constructor and the Engine.Start method. By default, Engine.Stop will close all open formats without saving, but an overloaded version is provided that allows manual control. The following example shows alternative code for starting and stopping an engine and saving changes: In C#:

using (EnginebtEngine = newEngine())

{

// Application specific code // Explicitly start the engine btEngine.Start();

// Application-specific code

// Assuming the application wants to save changes, //it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges); } In VB:

UsingbtEngineAs NewEngine(True) ' Application specific code ' Explicitly start the engine btEngine.Start()

' Application-specific code

' Assuming the application wants to save changes, ' it can be easily done at Stop time. btEngine.Stop(SaveOptions.SaveChanges) End Using

In the above example, a new Engine is created, but not started until later. Some application activity is assumed to execute, then the Stop method is called. In this case, changes to labels done while using the engine are saved back to file. The SaveOptions enumeration specifies the operation concerning open label formats to perform during exit of BarTender. In the above examples and many other examples in this document, the Engine.Dispose method is called implicitly by a 'using' statement. While it is not always appropriate to utilize 'using', it is a convenient way to ensure Dispose is called even if the block is exited during an exception.

How To: Use Engine as a Field in a Class

The above examples, and most examples in this document, present use of an Engine instance in a single method. This approach is not practical for most real applications. Starting and stopping Engine objects, and by extension BarTender processes, should be done as rarely as possible for optimal performance. Ideally, Engine instances should be started once and only stopped at the end of the application to minimize the overhead of managing processes. The most straightforward object-oriented approach is make an Engine object a field in a class and allow the encapsulating class to determine the Engine object's lifetime.

The following is the minimal suggested code for making an Engine object a field in a class: In C#:

public classEngineWrapper : IDisposable {

// Engine Field

privateEnginem_engine = null;

// This property will create and start the engine the first time it is // called. Most methods in this class (and methods in child classes) // should utilize this property instead of the m_engine field. protectedEngineBtEngine { get {

// If the engine has not been created yet, create and start it. if (m_engine == null) {

m_engine = newEngine(true);