Knowledge Base Nr: 00265 eieruhr.cpp - http://www.swe-kaiser.de

Downloads:

.NET: c#: soundausgabe auf pocket-pc; power save mode unterdrücken (eieruhr)

  
...
using System.Runtime.InteropServices;

namespace Eieruhr
{
public class Form1 : System.Windows.Forms.Form
{
...
protected long m_nTimerOut = 0;
protected bool m_bTimerRunning = false;
...
private void DisplayTimer()
{
TimeSpan ts = new TimeSpan(m_nTimerOut*1000*TimeSpan.TicksPerMillisecond);

string strTimeOut = ts.Hours + ":"
+ ts.Minutes/10 + ts.Minutes%10 + ":"
+ ts.Seconds/10 + ts.Seconds%10;

label1.Text = strTimeOut;
}

private void buttonStartStop_Click(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.Lavender;
m_bTimerRunning = !m_bTimerRunning;
m_bTimerAlarm = false;
if (m_bTimerRunning)
{
buttonStartStop.Text = "Stop";

//timer synchronisieren
timer1.Enabled = false;
timer1.Enabled = true;
}
else
{
buttonStartStop.Text = "Start";

TimeSpan ts = new TimeSpan(m_nTimerOut*1000*TimeSpan.TicksPerMillisecond);

m_nH1 = ts.Hours;
m_nM1 = ts.Minutes/10;
m_nM2 = ts.Minutes%10;
m_nS1 = ts.Seconds/10;
m_nS2 = ts.Seconds%10;

UpdateTimer();
}
}

private void buttonH1P_Click(object sender, System.EventArgs e)
{
m_nTimerOut++;
DisplayTimer();
}

private void buttonM1P_Click(object sender, System.EventArgs e)
{
m_nTimerOut--;
if (m_nTimerOut < 0)
m_nTimerOut = 0;
DisplayTimer();
}

private void buttonReset_Click(object sender, System.EventArgs e)
{
m_nTimerOut = 0;
m_bTimerRunning = false;
DisplayTimer();
}

const string FILE_NAME = "\\Windows\\Alarm1.wav";

private void timer1_Tick(object sender, System.EventArgs e)
{
if (m_bTimerRunning)
{
m_nTimerOut--;

Externals.SetVolume(1.0); //100%
Externals.SystemIdleTimerReset();

if (m_nTimerOut < 0)
{
this.BringToFront();
this.Show();
this.Focus();
BackColor = m_bBlink ? System.Drawing.Color.Red
: System.Drawing.Color.Lavender;
m_bBlink = !m_bBlink;

Externals.Play(FILE_NAME);
m_nTimerOut = 0;
m_bTimerAlarm = true;
}

DisplayTimer();
}
}

}

internal class Helpers
{
[Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, /* play synchronously (default) */
SND_ASYNC = 0x0001, /* play asynchronously */
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}

[DllImport("coredll")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );
[DllImport("coredll")]
public static extern void SystemIdleTimerReset();
[DllImport("coredll")]
public static extern int GetTickCount();
[DllImport("coredll")]
public static extern int waveOutSetVolume(IntPtr hwo, int nVol);
}

public class Externals
{
public static void Play( string strFileName )
{
Helpers.PlaySound( strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME | Helpers.PlaySoundFlags.SND_ASYNC );
}
public static void SystemIdleTimerReset()
{
Helpers.SystemIdleTimerReset();
}
public static int GetTickCount()
{
return Helpers.GetTickCount();
}
public static void SetVolume(double fPercent)
{
int dwNewVolume = (int)((float)0xffff * fPercent) & 0xffff;
int dwVolume = (int)((dwNewVolume<<16)&0xffff0000) | dwNewVolume;
Helpers.waveOutSetVolume(IntPtr.Zero, dwVolume);
}
}