Работа программы в фоновом режиме - C# (214673)
Формулировка задачи:
Есть программа с progressbar, при нажатии на кнопку она преобразуется в мини окно.
Но если работать в другом окне (мини-окно при этом работает в режиме поверх всех окон), то программа перестает высчитывать данные и менять значения progressbar.
Как такое исправить?
Решение задачи: «Работа программы в фоновом режиме»
textual
Листинг программы
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using VBTablet; using Aero = CodeKing.Native; using System.Threading; using Microsoft.WindowsAPICodePack; namespace DigitizerInterface { public partial class frm_mini : Form { public Tablet Digitizer; int tmpl; string tmps; int Xold, Yold, RectWidth; int iFormX, iFormY, iMouseX, iMouseY; public frm_mini() { InitializeComponent(); backgroundWorker.RunWorkerAsync(); } private void MainFrm_Load(object sender, EventArgs e) { try { // On Error GoTo errorload Digitizer = new Tablet(); //Actually create the tablet object //that uses a tablet attribute. Remember not everyone has _your_ tablet. Digitizer.UnavailableIsError = false; //Tablet.hWnd = frmMain.DefInstance.Handle prgPressure.Maximum = (int)Digitizer.Device.NormalPressure.get_Max(true); /*Digitizer.ContextClosed += new VBTablet.Tablet.ContextClosedEventHandler(ContextClosed); Digitizer.ContextOpened += new VBTablet.Tablet.ContextOpenedEventHandler(ContextOpened); Digitizer.ContextRepositioned += new Tablet.ContextRepositionedEventHandler(ContextRepositioned); Digitizer.ContextUpdated += new Tablet.ContextUpdatedEventHandler(ContextUpdated); Digitizer.CursorChange += new Tablet.CursorChangeEventHandler(CursorChange); Digitizer.InfoChange += new Tablet.InfoChangeEventHandler(InfoChange); Digitizer.ProximityChange += new Tablet.ProximityChangeEventHandler(ProximityChange);*/ Digitizer.PacketArrival += new Tablet.PacketArrivalEventHandler(PacketArrival); } catch (Exception ex) { MessageBox.Show(ex.Message + ". Please Connect the Digitizer Device First !!"); } } private void MainFrm_Activated(object Sender, EventArgs e) { bool temp,res; /*if (!Digitizer.Context.hCtx.Equals(IntPtr.Zero)) { temp = true; res = Digitizer.Context.Reposition(ref temp); }*/ } private void MainFrm_Deactivated(object Sender, EventArgs e) { bool temp, res; /*if (!Digitizer.Context.hCtx.Equals(IntPtr.Zero)) { temp = false; res = Digitizer.Context.Reposition(ref temp); }*/ } private void cmdConnect_Click(object sender, EventArgs e) { IntPtr Hwnd; bool IsDigitizingContext=false; string ContextID = "FirstContext"; Hwnd = this.Handle; Digitizer.hWnd = Hwnd; Digitizer.AddContext(ContextID, ref IsDigitizingContext); Digitizer.SelectContext(ref ContextID); Digitizer.Connected = true; Digitizer.Context.QueueSize = 32;//Set queue size to a reasonable value } private void MainFrm_Closed(object Sender, EventArgs e) { } private void OutContext() //Cursor has moved out of the context { lblPressure.Enabled = false; prgPressure.Enabled = false; } private void InContext() //Cursor has moved into context { lblPressure.Enabled = true; prgPressure.Enabled = true; //If digitising Then timClear.Enabled = False } // Event Handlers for the Digitizer Object public void PacketArrival(ref IntPtr ContextHandle, ref int Cursor_Renamed, ref int X , ref int Y , ref int Z, ref int Buttons , ref int Pressure , ref int TangentPressure, ref int Azimuth, ref int Altitude, ref int Twist, ref int Pitch , ref int Roll , ref int Yaw,ref int PacketSerial, ref int PacketTim) { tmpl = System.Math.Abs(Pressure); if( tmpl != prgPressure.Value) prgPressure.Value = tmpl; Thread.CurrentThread.Start(); if( Convert.ToInt32(lblPressure.Text)!= Pressure ) lblPressure.Text = Pressure.ToString(); } private void frm_mini_MouseDown(object sender, MouseEventArgs e) { iFormX = this.Location.X; iFormY = this.Location.Y; iMouseX = MousePosition.X; iMouseY = MousePosition.Y; } private void frm_mini_MouseMove(object sender, MouseEventArgs e) { int iMouseX2 = MousePosition.X; int iMouseY2 = MousePosition.Y; if (e.Button == MouseButtons.Left) this.Location = new Point(iFormX + (iMouseX2 - iMouseX), iFormY + (iMouseY2 - iMouseY)); } private void frm_mini_MouseDoubleClick(object sender, MouseEventArgs e) { if (e.Clicks == 2) { Application.Exit(); } } private void frm_mini_MouseHover(object sender, EventArgs e) { if (this.Focused == true) { this.Opacity = 0.9; } } private void item_exitMini_Click(object sender, EventArgs e) { frm_main main = new frm_main(); Close(); main.Show(); } private void item_about_Click(object sender, EventArgs e) { } private void item_exit_Click(object sender, EventArgs e) { Application.Exit(); } private void frm_mini_MouseLeave(object sender, EventArgs e) { } } }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д