在WinCE上播放声音、设置透明图片、系统音量 C#

记录一下用到的C#在WinCE平台上的相关技巧备查

1。C#在WinCE上实现透明图片

1
2
3
4
5
6
7
8
9
using System.Drawing.Imaging;
     
public static void DrawImageTransparent(Graphics gx, Image image, Rectangle destRect)
{
    ImageAttributes imageAttr = new ImageAttributes();
    Color transpColor = ((Bitmap)image).GetPixel(image.Width - 1, image.Height - 1);
    imageAttr.SetColorKey(transpColor, transpColor);
    gx.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}

2。WinCE上播放声音文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public enum Flags
{
    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.DLL", EntryPoint = "PlaySound", SetLastError = true)]
public extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);
     
/// <summary>
/// 同步播放声音文件
/// </summary>
/// <param name="filename"></param>
public static void PlaySYNCSound(string filename)
{
    if (!string.IsNullOrEmpty(filename))
    {
        if (File.Exists(filename))
        {
            WCE_PlaySound(filename, IntPtr.Zero, (int)(SendClass.Flags.SND_SYNC | SendClass.Flags.SND_FILENAME));
        }
    }
}
     
/// <summary>
/// 异步播放声音文件
/// </summary>
/// <param name="filename"></param>
public static void PlaySound(string filename)
{
    if (!string.IsNullOrEmpty(filename))
    {
        if (File.Exists(filename))
        {
            WCE_PlaySound(filename, IntPtr.Zero, (int)(SendClass.Flags.SND_ASYNC | SendClass.Flags.SND_FILENAME));
        }
    }
}

3。获取 / 设置WinCE系统音量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <summary>
/// 获取系统音量
/// </summary>
/// <returns></returns>
public static int GetVolume()
{
   // By the default set the volume to 0
   uint CurrVol = 0;
   // At this point, CurrVol gets assigned the volume
   SendClass.Common.waveOutGetVolume(IntPtr.Zero, ref CurrVol);
   // Calculate the volume
   ushort CalcVol = (ushort)(CurrVol & 0x0000ffff);
   // Get the volume on a scale of 1 to 20 (to fit the trackbar)
   return CalcVol / (ushort.MaxValue / 20);
}
/// <summary>
/// 设置系统音量
/// </summary>
/// <param name="value">要设置的音量值</param>
public static void SetVolume(int value)
{
   int volume = CalcVolumeValue(value);
   SendClass.Common.waveOutSetVolume(IntPtr.Zero, (uint)volume);
   RegistryKey hUSER = Registry.CurrentUser.OpenSubKey(@"ControlPanelVolume"true);
   hUSER.SetValue("Volume", volume, RegistryValueKind.DWord);
   hUSER.Flush();
   hUSER.Close();
}
private static int CalcVolumeValue(int value)
{
   // Calculate the volume that's being set
   int NewVolume = (ushort.MaxValue * value / 20);
   // Set the same volume for both the left and the right channels
   return ((NewVolume & 0x0000ffff) | (NewVolume << 16));
}

 4。设定WinCE屏幕背光变暗的等待时间,以及唤醒屏幕背光

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public enum EventFlags
{
    EVENT_PULSE = 1,
    EVENT_RESET = 2,
    EVENT_SET = 3
}
[DllImport("coredll.dll")]
public static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, String lpName);
[DllImport("coredll.dll")]
private static extern void EventModify(IntPtr h, int p);
[DllImport("coredll.dll")]
public static extern void CloseHandle(IntPtr h);
     
     
public static void CreateChangeEvent(String EventName, bool bInitialState)
{
    IntPtr hEvent = IntPtr.Zero;
    try
    {
        hEvent = CreateEvent(IntPtr.Zero, false, bInitialState, EventName);
        if (hEvent != IntPtr.Zero)
        {
            EventModify(hEvent, (int)EventFlags.EVENT_SET);
        }
    }
    catch { }
    finally
    {
        CloseHandle(hEvent);
    }
}
/// <summary>
/// 设定背光超时时间
/// </summary>
/// <param name="Seconds">背光超时时间(秒)</param>
/// <param name="IsAC">设定交流供电还是电池供电</param>
/// <returns></returns>
public static bool SetBackLightTimeOut(int Seconds, bool IsAC)
{
    Boolean returnvalue = false;
    try
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey("ControlPanel\Backlight"true))
        {
            if (key != null)
            {
                if (IsAC)
                {
                    key.SetValue("ACTimeout", Seconds, RegistryValueKind.DWord);
                    key.SetValue("UseExt", 1, RegistryValueKind.DWord);
                }
                else
                {
                    key.SetValue("BatteryTimeout", Seconds, RegistryValueKind.DWord);
                    key.SetValue("UseBattery", 1, RegistryValueKind.DWord);
                }
                // 创建事件通知
                CreateChangeEvent("BackLightChangeEvent"true);
            }
            else
                returnvalue = false;
        }
    }
    catch
    {
        returnvalue=false;
    }          
    return returnvalue;
}
     
/// <summary>
/// 如果屏幕背光是变暗的状态,则亮起屏幕背光
/// </summary>
public static void SetBackLightOn()
{
    CreateChangeEvent("BackLightChangeEvent"false);
}
原文地址:https://www.cnblogs.com/mooncher/p/6595061.html