Wince开发经验分享

1.阻止进入节电状态,我们的程序运行的时候,如果不希望屏幕黑掉或者变暗。可以用这个方法。
    添加引用:using System.Runtime.InteropServices;
    添加一个定时器,并使用下面代码来发送虚拟键:public const int VK_NONAME = 0xFC;
        public const int KEYEVENTF_SILENT = 0x0004;
        public const int INPUT_KEYBOARD = 1;

        [DllImport("coredll.dll", EntryPoint = "SendInput")]
        public static extern uint SendInput(
            uint nInputs,
            ref INPUT pInputs,
            int cbSize
        );

        public struct KEYBDINPUT
        ...{
            public ushort wVk;
            public ushort wScan;
            public ulong dwFlags;
            public ulong time;
            public ulong dwExtraInfo;
        }

        public struct INPUT
        ...{
            public ulong type;
            public KEYBDINPUT ki;
        }

        #endregion
        private void timer1_Tick(object sender, EventArgs e)
        ...{
            INPUT ip = new INPUT();
            KEYBDINPUT ki = new KEYBDINPUT();
            ki.wVk = VK_NONAME;
            ki.dwFlags = KEYEVENTF_SILENT;
            ip.type = INPUT_KEYBOARD;
            ip.ki = ki;
            SendInput(1, ref ip, 28);
        }

2。串口通讯(2005)
     使用vs2005开发智能设备上的程序,大多需要用到串口。我们可以用System.IO.Port.SerialPort这个组件。但有一点要注意的,就是读取数据的时候。比如你要缓存串口的数据,一定不要用属性BytesToRead。而要先缓存这个属性,再操作串口。因为你的连续的两步操作可能这个属性返回的结果是不同的。正确的读取、缓存串口数据的代码是这样的:
private void serial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        ...{
            try
            ...{
                //this.Invoke(new myDelegate(UpdateRxText), serial.ReadExisting());            
                int ReadBytes = serial.BytesToRead;

                if (ReadBytes <= 0)
                ...{
                    return;
                }
                byte[] bzBuff = new byte[ReadBytes];
                serial.Read(bzBuff, 0, ReadBytes);
                this.Invoke(new myDelegate(UpdateRxText), bzBuff);
                //this.BeginInvoke(new myDelegate(UpdateRxText), bzBuff);
            }
            catch (System.IO.IOException ex)
            ...{
                MessageBox.Show(ex.Message);
            }
        }
3.我们的程序,如果不使用到wince特有的组建,比如InputPanel,那么你的程序是可以同时运行在wince/ppc/mobile和pc上的。但一旦添加了这些pc不支持的组件,如何让程序还可以运行呢?可以在需要的地方,加上这个检测代码:
if (System.Environment.OSVersion.Platform == PlatformID.WinCE)
...{
            //...
}
4.退出程序的时候,最好执行一次关闭串口的操作如果你用到了串口。并显示的调用一次关闭程序。
serial.Close();
Application.Exit();
posted @ 2008-11-28 15:34  googlegis  阅读(157)  评论(0编辑  收藏  举报

坐标合肥,非典型GIS开发人员 GitHub