I have only been working with C# for a few weeks and I have gotten stuck on a threading issue.
I've been building a custom slideshow that implements a .ppt presentation hosted in a winform handled by an independent class. The slideshow switches off with another program that displays dynamic 开发者_开发知识库data from a shop floor. This second program needs to be sent a command to enter full screen mode as I do not have access to it's source code.
Using Setforegroundwindow API I can easily switch between the two applications and control the power point with the COM interface but the transition is choppy with screens resizing. I would like to overlay a blank black winform that fades in and out while this transition occurs. So far I have been unsuccessful in doing this smoothly, I have tried running the fading form and transition on separate threads but I haven't got it quite figured out. If anyone might have any advice on how to do this it would be much appreciated.
Thank you.
One thought...
Have two ppt loaders.
The current one and the new one you are loading.
Keep the current visible and active until the new one is ready. Then once the new one is ready hide the current one. That should give you a nice transition.
Since there will be two viewers active, it will be a little more resource intensive, but switching should be smoother.
Might want to distinquish loading versus starting. That way you can load behind the scenese and once loaded, show and start the ppt.
After working through some example code I implemented a background worker that did the trick. For more info From msdn - How to: Use a Background Worker.
I'm trying to find a more reliable way to ensure the F11 command is processed by the application, but for now I've put in a Thread.Sleep before the SendKeys.
Please feel free to leave comments or questions.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
Process p;
//Process p is initialized: p = Process.Start(@"<path\application.exe>");
Form2 oForm;
private void transition()
{
BackgroundWorker bw = new BackgroundWorker();
oForm = new Form2();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
oForm.Show();
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
oForm.closethis();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
SetForegroundWindow(p.MainWindowHandle);
p.WaitForInputIdle(3000);
System.Threading.Thread.Sleep(500);
SendKeys.SendWait("{F11}");
//Sends the application into full screen mode
}
}
And the code for the form that fades in and out..
public partial class Form2 : Form
{
public Form2()
{
this.Visible = false;
this.Opacity = 0;
InitializeComponent();
this.TopMost = true;
this.Visible = true;
while (this.Opacity < 1)
{
this.Opacity += .05;
Thread.Sleep(5);
this.TopMost = true;
}
}
private void closefade()
{
while (this.Opacity > 0)
{
this.Opacity -= .05;
Thread.Sleep(5);
this.TopMost = true;
}
this.Close();
}
delegate void CloseBack();
public void closethis()
{
if (this.InvokeRequired)
{
CloseBack b = new CloseBack(closethis);
}
else
closefade();
}
}
精彩评论