Tài liệu Đồ hoạ, đa phương tiện ( Mutilmedia) và In ấn Đồ họa phần cuối doc

10 424 1
Tài liệu Đồ hoạ, đa phương tiện ( Mutilmedia) và In ấn Đồ họa phần cuối doc

Đang tải... (xem toàn văn)

Thông tin tài liệu

using System; using System.Windows.Forms; using System.Management; using System.Collections; public class PrintQueueTest : System.Windows.Forms.Form { private System.Windows.Forms.ListBox lstJobs; private System.Windows.Forms.Button cmdRefresh; private System.Windows.Forms.TextBox txtJobInfo; private System.Windows.Forms.Button cmdPause; private System.Windows.Forms.Button cmdResume; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; // (Bỏ qua phần mã designer.) private void PrintQueueTest_Load(object sender, System.EventArgs e) { cmdRefresh_Click(null, null); } private void cmdRefresh_Click(object sender, System.EventArgs e) { // Chọn tất cả các tác vụ in đang chờ. string query = "SELECT * FROM Win32_PrintJob"; ManagementObjectSearcher jobQuery = new ManagementObjectSearcher(query); ManagementObjectCollection jobs = jobQuery.Get(); // Thêm các tác vụ trong hàng đợi vào ListBox. lstJobs.Items.Clear(); txtJobInfo.Text = ""; foreach (ManagementObject job in jobs) { lstJobs.Items.Add(job["JobID"]); } } // Phương thức này thực hiện truy vấn WMI trả về // tác vụ WMI cho item hiện đang được chọn trong ListBox. private ManagementObject GetSelectedJob() { try { // Chọn tác vụ in phù hợp. string query = "SELECT * FROM Win32_PrintJob " + "WHERE JobID='" + lstJobs.Text + "'"; ManagementObjectSearcher jobQuery = new ManagementObjectSearcher(query); ManagementObjectCollection jobs = jobQuery.Get(); IEnumerator enumerator = jobs.GetEnumerator(); enumerator.MoveNext(); return (ManagementObject)enumerator.Current; }catch (InvalidOperationException){ // Thuộc tính Current của enumerator không hợp lệ return null; } } private void lstJobs_SelectedIndexChanged(object sender, System.EventArgs e) { ManagementObject job = GetSelectedJob(); if (job == null) { txtJobInfo.Text = ""; return; } // Hiển thị thông tin về tác vụ. string jobInfo = "Document: " + job["Document"].ToString(); jobInfo += Environment.NewLine; jobInfo += "DriverName: " + job["DriverName"].ToString(); jobInfo += Environment.NewLine; jobInfo += "Status: " + job["Status"].ToString(); jobInfo += Environment.NewLine; jobInfo += "Owner: " + job["Owner"].ToString(); jobInfo += Environment.NewLine; jobInfo += "PagesPrinted: " + job["PagesPrinted"].ToString(); jobInfo += Environment.NewLine; jobInfo += "TotalPages: " + job["TotalPages"].ToString(); if (job["JobStatus"] != null) { txtJobInfo.Text += Environment.NewLine; txtJobInfo.Text += "JobStatus: " + job["JobStatus"].ToString(); } if (job["StartTime"] != null) { jobInfo += Environment.NewLine; jobInfo += "StartTime: " + job["StartTime"].ToString(); } txtJobInfo.Text = jobInfo; } private void cmdPause_Click(object sender, System.EventArgs e) { if (lstJobs.SelectedIndex == -1) return; ManagementObject job = GetSelectedJob(); if (job == null) return; // Tạm dừng tác vụ. int returnValue = Int32.Parse( job.InvokeMethod("Pause", null).ToString()); // Hiển thị thông tin về giá trị trả về. if (returnValue == 0) { MessageBox.Show("Successfully paused job."); }else { MessageBox.Show( "Unrecognized return value when pausing job."); } } private void cmdResume_Click(object sender, System.EventArgs e) { if (lstJobs.SelectedIndex == -1) return; ManagementObject job = GetSelectedJob(); if (job == null) return; if ((Int32.Parse(job["StatusMask"].ToString()) & 1) == 1) { // Phục hồi tác vụ. int returnValue = Int32.Parse( job.InvokeMethod("Resume", null).ToString()); // Hiển thị thông tin về giá trị trả về. if (returnValue == 0) { MessageBox.Show("Successfully resumed job."); }else if (returnValue == 5) { MessageBox.Show("Access denied."); }else { MessageBox.Show( "Unrecognized return value when resuming job."); } } } } Hình 8.14 Lấy thông tin từ hàng đợi in The image part with relationsh ip ID rId 5 was not fo und in the file. Nhớ rằng các quyền Windows có thể ngăn bạn tạm dừng hoặc gỡ bỏ một tác vụ in do một người dùng khác tạo ra. Thực ra, các quyền này còn có thể ngăn bạn lấy thông tin trạng thái có thể gây ra ngoại lệ. # Các phương thức WMI khác mà bạn có thể sử dụng trong một kịch bản in bao gồm AddPrinterConnection, SetDefaultPrinter, CancelAllJobs, PrintTestPage, tất cả đều làm việc với lớp Win32_Printer. Để có thêm thông tin về WMI, bạn hãy tham khảo tài liệu MSDN tại địa chỉ [http://msdn.microsoft.com/library/en-us/wmisdk/ wmi/computer_system_hardware_classes.asp]. 1.1 Sử dụng Microsoft Agent V V Bạn muốn ứng dụng của mình có sự trợ giúp của những nhân vật hoạt hình như trong Microsoft Word, Microsoft Excel # # Sử dụng điều kiểm Microsoft Agent. Microsoft Agent là một kỹ thuật dùng để thêm các nhân vật hoạt hình có tính tương tác vào ứng dụng hay trang web. Tính tương tác là chức năng chính yếu của kỹ thuật Microsoft Agent: các nhân vật Microsoft Agent có thể nói đáp lại đầu vào của người dùng thông qua việc nhận dạng tổng hợp giọng nói. Microsoft sử dụng kỹ thuật này trong các ứng dụng như Word, Excel, PowerPoint ; giúp người dùng tìm câu trả lời cho những câu hỏi hiểu cách thức hoạt động của ứng dụng. Microsoft cung cấp thông tin về kỹ thuật Microsoft Agent tại [www.microsoft.com/msagent]. Điều kiểm Microsoft Agent cho phép người dùng tương tác với các ứng dụng trang web thông qua giọng nói—dạng giao tiếp tự nhiên nhất của con người. Khi người dùng nói vào micro, điều kiểm này sử dụng một bộ máy nhận dạng giọng nói—đây là ứng dụng dịch âm thanh đầu vào từ micro thành ngôn ngữ mà máy tính hiểu được. Điều kiểm Microsoft Agent cũng sử dụng một bộ máy text-to-speech—đây là ứng dụng dịch các từ do người dùng nhập vào thành âm thanh có thể nghe được qua loa. Điều kiểm Microsoft Agent cho phép bạn truy xuất bốn nhân vật đã được định nghĩa sẵn—Genie (vị thần), Merlin (thuật sĩ), Peedy (con vẹt) Robby (người máy). M ỗi nhân vật có một tập các hành động mà bạn có thể sử dụng trong ứng dụng nhằm minh họa các quan điểm hay chức năng khác nhau. Chẳng hạn, tập các hành động của Peedy gồm các dạng bay khác nhau mà bạn có thể sử dụng để dịch chuyển Peedy trên màn hình. Bạn cũng có thể tự tạo cho mình các nhân vật hoạt hình với sự trợ giúp của Microsoft Agent Character Editor Microsoft Linguistic Sound Editing Tool (có trong đĩa CD đ ính kèm). Ví dụ dưới đây minh họa cách xây dựng một ứng dụng đơn giản với điều kiểm Microsoft Agent. Ứng dụng này gồm hai ComboBox dùng để chọn một nhân vật một hành động. Khi người dùng chọn các ComboBox này, nhân vật được chọn sẽ xuất hiện thực hiện hành động được chọn. Ứng dụng này sử dụng việc nhận dạng tổng hợp giọng nói để điều khiển các hành động của nhân vật: người dùng có thể bảo nhân vật thực hiện hành động bằng cách nhấn phím [Scroll Lock] rồi đọc tên hành động vào micro. Ví dụ này cũng cho phép người dùng chuyển sang một nhân vật m ới bằng cách gọi tên nhân vật, còn tạo thêm một lệnh tùy biến là MoveToMouse. Ngoài ra, nhân vật cũng sẽ đọc bất cứ text nào mà người dùng nhập vào TextBox. Trước khi chạy ví dụ này, bạn phải cài đặt điều kiểm Microsoft Agent, bộ máy nhận dạng giọng nói, bộ máy text-to-speech, các định nghĩa nhân vật (có trong đĩa CD đính kèm). Để có thể thêm điều kiểm Microsoft Agent vào dự án, bạn hãy nhắp phải vào hộp công cụ và ch ọn Add/Remove Items. Kế đó, vào thẻ COM Components, chọn Microsoft Agent 2.0. Như thế, Microsoft Agent sẽ được thêm vào vào hộp công cụ. Khi bạn thả điều kiểm này lên form, các Interop Assembly cần thiết sẽ được sinh ra được thêm vào dự án. Hình 8.15 Chọn Microsoft Agent Control 2.0 trong cửa sổ Customize Toolbox public class FrmAgent : System.Windows.Forms.Form { // (Bỏ qua phần mã designer.) // Đối tượng agent hiện tại. private AgentObjects.IAgentCtlCharacter mSpeaker; // Phương thức thụ lý sự kiện KeyDown của txtLocation. private void txtLocation_KeyDown(object sender, The image part with relationsh ip ID rI d6 was not fo und in the file. System.Windows.Forms.KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { // Thiết lập nơi lưu trữ nhân vật vào txtLocation. string location = txtLocation.Text; // Khởi tạo các nhân vật. try { // Nạp các nhân vật vào đối tượng agent. mainAgent.Characters.Load("Genie", location & "Genie.acs") mainAgent.Characters.Load("Merlin", location + "Merlin.acs"); mainAgent.Characters.Load("Peedy", location & "Peedy.acs") mainAgent.Characters.Load("Robby", location & "Robby.acs") // Vô hiệu txtLocation kích hoạt các điều kiểm khác. txtLocation.Enabled = false; txtSpeech.Enabled = true; cmdSpeak.Enabled = true; characterCombo.Enabled = true; actionsCombo.Enabled = true; // Thiết lập nhân vật hiện tại là Merlin hiện nhân vật này. mSpeaker = mainAgent.Characters["Merlin"]; GetAnimationNames(); // Thu lấy danh sách hành động. mSpeaker.Show(0); } catch (FileNotFoundException) { MessageBox.Show("Invalid character location", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } // Phương thức thụ lý sự kiện Click của nút Speak. private void cmdSpeak_Click(System.Object sender, System.EventArgs e) { // Nếu txtSpeech rỗng, nhân vật nhắc người dùng nhập text vào txtSpeech. if (txtSpeech.Text == "") { mSpeaker.Speak("Please type the words you want me to speak", ""); } else { mSpeaker.Speak(txtSpeech.Text, ""); } } // Phương thức thụ lý sự kiện Click của agent. private void mainAgent_ClickEvent(object sender, AxAgentObjects._AgentEvents_ClickEvent e) { mSpeaker.Play("Confused"); mSpeaker.Speak("Why are you poking me?", ""); mSpeaker.Play("RestPose"); } // Phương thức thụ lý sự kiện SelectedIndexChanged của characterCombo // (người dùng chọn nhân vật mới). private void characterCombo_SelectedIndexChanged(System.Object sender, System.EventArgs e) { ChangeCharacter(characterCombo.Text); } // Ẩn nhận vật hiện tại hiện nhân vật mới. private void ChangeCharacter(string name) { mSpeaker.Hide(0); mSpeaker = mainAgent.Characters[name]; GetAnimationNames(); // Sinh lại danh sách hành động. mSpeaker.Show(0); } // Thu lấy tên các hành động đưa vào actionsCombo. private void GetAnimationNames() { // Bảo đảm tính an toàn về tiểu trình. object synclockObject = (this); Monitor.Enter(synclockObject); try { // Lấy tên các hành động. IEnumerator enumerator = mainAgent.Characters.Character(mSpeaker.Name). AnimationNames.GetEnumerator(); string voiceString; // Xóa actionsCombo. actionsCombo.Items.Clear(); mSpeaker.Commands.RemoveAll(); // Thêm tất cả các hành động (cho phép ra lệnh bằng giọng nói). while (enumerator.MoveNext()) { voiceString = Convert.ToString(enumerator.Current); voiceString = voiceString.Replace("_", "underscore"); actionsCombo.Items.Add(enumerator.Current); mSpeaker.Commands.Add(Convert.ToString(enumerator.Current), "" voiceString, true, false); } // Thêm lệnh tùy biến. mSpeaker.Commands.Add("MoveToMouse", "MoveToMouse", "Move To Mouse", true, true); } finally { Monitor.Exit(synclockObject); } } // Phương thức thụ lý sự kiện SelectedIndexChanged của actionsCombo // (người dùng chọn hành động mới). private void actionsCombo_SelectedIndexChanged(System.Object sender, System.EventArgs e) { mSpeaker.Stop(null); mSpeaker.Play(actionsCombo.Text); mSpeaker.Play("RestPose"); } // Phương thức thụ lý sự kiện Command của agent. private void mainAgent_Command(System.Object sender, AxAgentObjects._AgentEvents_CommandEvent e) { AgentObjects.IAgentCtlUserInput command = ((AgentObjects.IAgentCtlUserInput)(e.userInput)); // Đổi nhân vật nếu người dùng đọc tên nhân vật. if (command.Voice == "Peedy" || command.Voice == "Robby" || command.Voice == "Merlin" || command.Voice == "Genie") { ChangeCharacter(command.Voice); return; } // Gửi agent đến chuột. if (command.Name == "MoveToMouse") { mSpeaker.MoveTo(Convert.ToInt16(Cursor.Position.X - 60), Convert.ToInt16(Cursor.Position.Y - 60), null); return; } // Thực hiện hành động mới. mSpeaker.Stop(null); mSpeaker.Play(command.Name); } } . if (( Int32.Parse(job["StatusMask"].ToString()) & 1) == 1) { // Phục hồi tác vụ. int returnValue = Int32.Parse( job.InvokeMethod("Resume",. thị thông tin về tác vụ. string jobInfo = "Document: " + job["Document"].ToString(); jobInfo += Environment.NewLine; jobInfo +=

Ngày đăng: 21/01/2014, 01:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan