Each version of the .NET Compact Framework is released with a different Win32 File Version number (this is a separate version number from the Assembly Version, which should be the same across all releases of the Version 1 .NET Compact Framework, including Service Packs).
In order to see what version is installed, use File Explorer to navigate to the \Windows directory on the device, and click the file called CGACUTIL. You will get a message box showing you the Win32 File Version of the .NET Compact Framework installed on the device.
One approach would be to create a file share on your development PC, and then connect to that share via File Explorer in the emulator. You may then copy and paste the files from the share to the emulator's local file system. Another approach would be to add the file(s) to a smart device project and set their Build Action(s) property to "Content". See the Visual Studio .NET online documentation for more information on "File Properties":
You must ask the OEM to include it in the device's image. If you are the OEM and you are using Platform Builder 4.2, then including the OS Dependencies for the .NET item automatically causes imgdecmp.dll to be part of the emulator image - if that is not working then refer to cesysgen.bat. Another method is to set the environment variable "__SYSGEN_IMGDECMP=1" to explicitly force the DLL into the image.
This is by design. You must either change the names of the DLLs, or if the DLLs are strong named, place them in the GAC and use Assembly.Load with a full strong name.
This allows ActiveSync to connect to your Emulator session from Visual Studio .NET 2003. Create an ActiveSync session to the 4.2 emulator, this will allow Visual Studio 2003 to consider it a real device (Choose PPC device as the deployment target).
While adding designer support in Visual Studio .NET 2003 for Smart Device custom controls, you may run into the following issues:
Unable to associate an Icon to the Control for showing it in the toolbox at design time
The component, when added to the toolbox, becomes greyed out
Causes
Using a design project separate from the control project. Visual Studio .NET automatically prepends the project default namespace to the bitmap name. The "default namespace" defaults to the project name. This may be a problem because the design project has a slightly different name than the runtime project.
Not setting the correct ToolBoxItemFilterAttribute values
Resolutions
Given the following example: Runtime VS.NET Project: MyProject Class Name: MyProject.MyClass Design VS.NET Project Name: MyProject.Design BitMap name in VS.NET Design Project: Foo.bmp Bitmap name in design assembly: MyProject.Design.MyClass.bmp -- This creates a problem because the bitmap needs the name: MyProject.MyClass.bmp
In the above example, setting the design project's default namespace to "MyProject" rather then "MyProject.Design" should fix the problem.
The easiest way to check the name of the bitmap within the assembly is to run ILDASM and open the Manifest. The embedded resources are listed at the end of the manifest.
If you create a custom component derived from the Component class, your code must include the following statements so that your component appears in the Toolbox:
//C#
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(...);
}
'VB
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawLine(...)
End Sub 'OnPaint
在程序的其他部分,利用控件的一个方法,可以用来建立任意控件的图形对象:
//C#
using System.Drawing;
Graphics g = this.CreateGraphics();
'VB
Imports System.Drawing
Dim g As Graphics = Me.CreateGraphics()
直接画到bitmap位图文件中:
//C#
using System.Drawing;
Bitmap bm = new Bitmap(10,10);
Graphics g = Graphics.FromImage(bm);
'VB
Imports System.Drawing
Dim bm As New Bitmap(10, 10)
Dim g As Graphics = Graphics.FromImage(bm)
//C#
using System.Drawing;
protected override void OnPaint(PaintEventArgs e)
{
string s = "Hello World"
Pen pen = new Pen(Color.Fuchsia);
Font font = new Font("Arial", 18, FontStyle.Regular);
Brush brush = new SolidBrush(Color.Black);
SizeF sSize = e.Graphics.MeasureString(s, font);
Rectangle r = new Rectangle(9, 199,(int)sSize.Width + 1, (int)sSize.Height + 1);
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawString(s, font, brush, 10.0f, 200.0f);
base.OnPaint (e);
}
'VB
Imports System.Drawing
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim s As String = "Hello World"
Dim pen As New Pen(Color.Fuchsia)
Dim font As New Font("Arial", 18, FontStyle.Regular)
Dim brush = New SolidBrush(Color.Black)
Dim sSize As SizeF = e.Graphics.MeasureString(s, font)
Dim r As New Rectangle(9, 199, Fix(sSize.Width) + 1, Fix(sSize.Height) + 1)
e.Graphics.DrawRectangle(pen, r)
e.Graphics.DrawString(s, font, brush, 10F, 200F)
MyBase.OnPaint(e)
End Sub 'OnPaint
While there is no inherent support for zooming or stretching a single image, these effects can be achieved quite easily by creating a new Bitmap object with an associated Graphics object and copying the desired portion of the original Bitmap into it. The following sample creates two bitmaps of the same size, where the second contains a zoomed center section of the first, provided the project has an embedded resource named MyImage.bmp. This same technique could be used to stretch images by modifying the source and destination rectangles such that they do not maintain their original aspect ratio.
//C#
using System.Drawing;
using System.Reflection;
Bitmap m_bmpOriginal;
Bitmap m_bmpZoom;
private void Form1_Load(object sender, System.EventArgs e)
{
Assembly asm = Assembly.GetExecutingAssembly();
m_bmpOriginal = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name
+ ".MyImage.bmp"));
// Take the center quarter of m_bmpOriginal
// and create stetch it into m_bmpZoom of the same size
m_bmpZoom = new Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height);
Graphics gZoom = Graphics.FromImage(m_bmpZoom);
Rectangle srcRect = new Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4,
m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2);
Rectangle dstRect = new Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height);
gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(m_bmpOriginal, 0, 0);
e.Graphics.DrawImage(m_bmpZoom, 125, 0);
base.OnPaint (e);
}
'VB
Imports System.Drawing
Imports System.Reflection
Private m_bmpOriginal As Bitmap
Private m_bmpZoom As Bitmap
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
m_bmpOriginal = New Bitmap(asm.GetManifestResourceStream((asm.GetName().Name _
+ ".MyImage.bmp")))
' Take the center quarter of m_bmpOriginal
' and create stetch it into m_bmpZoom of the same size
m_bmpZoom = New Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height)
Dim gZoom As Graphics = Graphics.FromImage(m_bmpZoom)
Dim srcRect As New Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4, _
m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2)
Dim dstRect As New Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height)
gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub 'Form1_Load
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawImage(m_bmpOriginal, 0, 0)
e.Graphics.DrawImage(m_bmpZoom, 125, 0)
MyBase.OnPaint(e)
End Sub 'OnPaint
Download and install to your desktop development PC a "Developer" version of the service pack (the download title will read something like: "Microsoft® .NET Compact Framework 1.0 SPx Developer Redistributable") from: http://msdn.microsoft.com/mobility/downloads/updates/default.aspx
The next step is to copy the appropriate .NET Compact Framework cab file (as per next paragraph) to the emulator. From within the emulator point File Explorer to a share on your PC and then copy and paste the cab to somewhere on the emulator's file system. Now launch the cab file from File Explorer and answer "Yes" if asked to overwrite anything.
To install SQL Server CE with an application, simply install the proper SQL Server CE CAB files as part of the application's installation. There are two sets of cabs associated with SQL Server CE.
The developer CAB includes Query Analyzer, and error strings. This CAB should not be included with application deployment. It comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
The SQL Server CE CAB, which includes the engine, client agent, and managed extensions for the client agent is required by applications utilizing System.Data.SqlServerCe components. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
Applications that access SQL Server, ie applications utilizing System.Data.SqlClient components should deploy the 'sql' CAB. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
sql.ppc3.<processor>.cab sql.wce4.<processor>.cab
All of these CABs are included in the Visual Studio .NET 2003 Professional Edtion install. The default location is:
\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\...
This article expands upon the "Dancing Rectangles" sample by implementing loading and displaying of bitmaps. It also implements some more advanced features such as animated bitmaps, source and destination key transparency, and alpha blending, i.e., translucency. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp
This article expands upon the "Dancing Zombies" sample by implementing drawing of points, lines, and custom 1 bit fonts converted from 8 bit bitmaps. It also implements an input system that overrides the functionality of the hardware buttons and tracks button states. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI3.aspTop of Page
在windows平台(Win32或.NET)下正确关闭应用程序的方法是关闭主窗体(如:Form.Close)。所有主窗体结束时仍存在的窗体需要手工关闭。Any window that's still present after the main message pump ends needs to be manually closed. 好的方法就是在应用程序调用Form.Close或Form.Dispose退出之前,关闭所有窗体。需要记住.NET框架的OnClosing()就是Win32平台下WM_CLOSE的托管版本,而不是WM_DESTROY。
Create a shortcut to your application somewhere under \windows\start menu\programs. When your application is launched from this shortcut an icon for your applications will appear the MRU list.
In order to display a non-full screen Form, ensure that the Form's FormBorderStyle property is set to FormBorderStyle.None. To center the form add the following code to the Form's FormLoad event handler: Set FormBorderStyle to FormBorderStyle.None then:
Once a Form is closed, it is disposed and therefore may be garbage collected by the system so it is not safe to attempt to show a closed Form. An alternative solution is to use Form.Hide and Form.Show to hide and display Forms respectively.
Multi-instancing is not supported by the .NET Compact Framework. The following code sample provides a solution that allows applications to be instanced rather than maximized when an application is launched but a running instance already exists.
Note: The following code is not supported and is not guaranteed to work on all versions of the OS, including future versions.
// C#
using System.Runtime.InteropServices;
using System.Reflection;
private void Form1_Load(object sender, System.EventArgs e)
{
this.Text = string.Format("Form {0}", new Random().Next());
}
[DllImport("CoreDll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("CoreDll")]
public static extern int SetWindowText(IntPtr hWnd, string lpString);
protected override void OnResize(EventArgs e)
{
Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
IntPtr hWnd = FindWindow("#NETCF_AGL_PARK_",
asm.GetModules()[0].FullyQualifiedName);
if (hWnd != IntPtr.Zero)
SetWindowText(hWnd, "#42");
base.OnResize (e);
}
'VB
Imports System.Runtime.InteropServices
Imports System.Reflection
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = String.Format("Form {0}", New Random().Next())
End Sub 'Form1_Load
<DllImport("CoreDll")> _
Public Shared Function FindWindow(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("CoreDll")> _
Public Shared Function SetWindowText(ByVal hWnd As IntPtr, _
ByVal lpString As String) As Integer
End Function
Protected Overrides Sub OnResize(ByVal e As EventArgs)
Dim asm As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
Dim hWnd As IntPtr = FindWindow("#NETCF_AGL_PARK_", _
asm.GetModules()(0).FullyQualifiedName)
If hWnd.ToInt32() <> IntPtr.Zero.ToInt32() Then
SetWindowText(hWnd, "#42")
End If
MyBase.OnResize(e)
End Sub 'OnResize
这是一个已经知道的BUG,把右键菜单设置为分割线,将抛出NotSupportedException错误。这个问题是由于WinCE系统有个限制,不允许在已经加入右键菜单的菜单项设置为分隔符,并且菜单的父类是一个控件。在Visual Studio 2003种,设计器分割移动应用代码的方式和PC上的应用程序类似。这是导致此问题的原因。解决的方法是,把右键菜单单独放在InitilizeComponent方法外的地方。
Adding subnodes to all nodes is accomplished by iterating through all of the nodes in the TreeView and adding a new node to each.
//C#
foreach (TreeNode node in treeView1.Nodes)
{
node.Nodes.Add(new TreeNode("SubNode"));
}
'VB
Dim node As TreeNode
For Each node In treeView1.Nodes
node.Nodes.Add(New TreeNode("SubNode"))
Next node
The number of rows and columns in a DataGrid can be determined from the data source itself. For example:
//C#
DataSet ds = new DataSet();
int numRows = ds.Tables[0].Rows.Count;
int numCols = ds.Tables[0].Columns.Count;
'VB
Dim ds As New DataSet()
Dim numRows As Integer = ds.Tables(0).Rows.Count
Dim numCols As Integer = ds.Tables(0).Columns.Count
If the DataGrid is bound to the DataView you can also use DataView.Count.
The tab order of the controls in the .NET Compact Framework correspond directly to the order of the Controls in the Form.Controls collection. Therefore, GetNextControl can be implemented by determining the index of the specified Control and determing its neighbors in the collection.
//C#
public Control GetNextControl(Control ctl, bool forward)
{
int curIndex = this.Controls.IndexOf(ctl);
if (forward)
{
if (curIndex < this.Controls.Count)
curIndex++;
else
curIndex = 0;
}
else
{
if (curIndex > 0)
curIndex--;
else
curIndex = this.Controls.Count - 1;
}
return this.Controls[curIndex];
}
'VB
Public Function GetNextControl(ByVal ctl As Control, _
ByVal forward As Boolean) As Control
Dim curIndex As Integer = Me.Controls.IndexOf(ctl)
If forward Then
If curIndex < Me.Controls.Count Then
curIndex += 1
Else
curIndex = 0
End If
Else
If curIndex > 0 Then
curIndex -= 1
Else
curIndex = Me.Controls.Count - 1
End If
End If
Return Me.Controls(curIndex)
End Function 'GetNextControl
Similar to the NumericUpDown control, the maximum achievable value is the first empty row above the thumb. More specifically, from the editor properties, this equates to:
Icons support transparency, however, there is a known bug in Visual Studio .NET 2003 designer that creates incorrect code and makes icons non-transparent. A work around is to add an icon file to the ImageList outside of InitializeComponent and add the icon files to the project as content or embedded resources. The following code demonstrates this:
//C#
using System.Drawing;
using System.IO;
using System.Reflection;
// Loaded as content example
private void Form1_Load(object sender, System.EventArgs e)
{
this.imageList1.Images.Add(new Icon(File.Open("fullFileName.ico",
FileMode.Open)));
this.toolBar1.Buttons[0].ImageIndex = 0;
}
// Loaded as a resource example
private void Form1_Load(object sender, System.EventArgs e)
{
this.imageList1.Images.Add(new
Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(
".filename.ico")));
this.toolBar1.Buttons[0].ImageIndex = 0;
}
'VB
Imports System.Drawing
Imports System.IO
Imports System.Reflection
' Loaded as content example
Private Sub Form1_Load1(ByVal sender As Object, ByVal e As System.EventArgs)
Me.imageList1.Images.Add(New Icon(File.Open("fullFileName.ico", _
FileMode.Open)))
Me.toolBar1.Buttons(0).ImageIndex = 0
End Sub 'Form1_Load1
' Loaded as a resource example
Private Sub Form1_Load2(ByVal sender As Object, ByVal e As System.EventArgs)
Me.imageList1.Images.Add(New _
Icon([Assembly].GetExecutingAssembly().GetManifestResourceStream( _
".filename.ico")))
Me.toolBar1.Buttons(0).ImageIndex = 0
End Sub 'Form1_Load2
//C#
using System.Runtime.InteropServices;
public class MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("CoreDll.dll")]
public static extern void GlobalMemoryStatus
(
MEMORYSTATUS lpBuffer
);
[DllImport("CoreDll.dll")]
public static extern int GetSystemMemoryDivision
(
ref uint lpdwStorePages,
ref uint lpdwRamPages,
ref uint lpdwPageSize
);
public void Test()
{
uint storePages = 0;
uint ramPages = 0;
uint pageSize = 0;
int res = GetSystemMemoryDivision(ref storePages, ref ramPages, ref pageSize);
MEMORYSTATUS memStatus = new MEMORYSTATUS();
GlobalMemoryStatus(memStatus);
}
'VB
Imports System.Runtime.InteropServices
Public Structure MEMORYSTATUS
Public dwLength As UInt32
Public dwMemoryLoad As UInt32
Public dwTotalPhys As UInt32
Public dwAvailPhys As UInt32
Public dwTotalPageFile As UInt32
Public dwAvailPageFile As UInt32
Public dwTotalVirtual As UInt32
Public dwAvailVirtual As UInt32
End Structure 'MEMORYSTATUS
<DllImport("coredll.dll")> _
Private Shared Sub GlobalMemoryStatus(ByRef ms As MEMORYSTATUS)
End Sub
<DllImport("CoreDll.dll")> _
Public Shared Function GetSystemMemoryDivision( _
ByRef lpdwStorePages As UInt32, _
ByRef lpdwRamPages As UInt32, _
ByRef lpdwPageSize As UInt32) As Integer
End Function
Public Shared Sub Test()
Dim storePages As UInt32
Dim ramPages As UInt32
Dim pageSize As UInt32
Dim res As Integer = GetSystemMemoryDivision(storePages, ramPages, pageSize)
Dim memStatus As New MEMORYSTATUS
GlobalMemoryStatus(memStatus)
End Sub 'Test
//C#
using System.Runtime.InteropServices;
[DllImport("CoreDll")]
public static extern IntPtr FindWindow(string className,string WindowsName);
[DllImport("CoreDll")]
public static extern bool ShowWindow(IntPtr hwnd,int nCmdShow);
const int SW_MINIMIZE = 6;
protected override void OnGotFocus(EventArgs e)
{
IntPtr hwnd = FindWindow(null, this.Text);
ShowWindow(hwnd, SW_MINIMIZE);
base.OnGotFocus(e);
}
'VB
Imports System.Runtime.InteropServices
<DllImport("CoreDll")> _
Public Shared Function FindWindow(ByVal className As String, ByVal WindowsName As String) As IntPtr
End Function
<DllImport("CoreDll")> _
Public Shared Function ShowWindow(ByVal hwnd As IntPtr,ByVal nCmdShow As Integer) As Boolean
End Function
Private Const SW_MINIMIZE As Integer = 6
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
Dim hwnd As IntPtr = FindWindow(Nothing, Me.Text)
ShowWindow(hwnd, SW_MINIMIZE)
MyBase.OnGotFocus(e)
End Sub 'OnGotFocus
//C#
[DllImport("coredll.dll"]
public static extern IntPtr GetCapture();
[DllImport("coredll.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
this.Text = "FindMe";
IntPtr hwnd1 = FindWindow(null, "FindMe");
this.Capture = true;
IntPtr hwnd2 = GetCapture();
this.Capture = false;
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function GetCapture() As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Me.Text = "FindMe"
Dim deskWin As IntPtr = FindWindow(Nothing, "FindMe")
Me.Capture = True
Dim hwnd As IntPtr = GetCapture()
Me.Capture = False
//C#
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceFrequency(ref Int64 lpFrequency);
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceCounter(ref Int64 lpPerformanceCount);
private void TestTimer()
{
System.Int64 freq = 0;
if (QueryPerformanceFrequency(ref freq) != 0)
{
System.Int64 count1 = 0;
System.Int64 count2 = 0;
if (QueryPerformanceCounter(ref count1) != 0)
{
System.Threading.Thread.Sleep(1200);
QueryPerformanceCounter(ref count2);
System.Int64 time_ms = (count2 - count1) * 1000 / freq;
}
}
}
'VB
<DllImport("CoreDll.dll")> _
Public Shared Function QueryPerformanceFrequency(ByRef lpFrequency As Int64) As Integer
End Function
<DllImport("coredll.dll")> _
Public Shared Function QueryPerformanceCounter(ByRef lpPerformanceCount As Int64) As Integer
End Function
Private Sub TestTimer()
Dim freq As System.Int64 = 0
If QueryPerformanceFrequency(freq) <> 0 Then
Dim count1 As System.Int64 = 0
Dim count2 As System.Int64 = 0
If QueryPerformanceCounter(count1) <> 0 Then
System.Threading.Thread.Sleep(1200)
QueryPerformanceCounter(count2)
Dim time_ms As System.Int64 = (count2 - count1) * 1000 / freq
End If
End If
End Sub 'TestTimer
using System.Runtime.InteropServices;
[DllImport("coredll.dll", SetLastError=true)]
int myFoo(...);
Foo(...)
{
int rc = myFoo(...);
if (rc == false)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Foo failed");
}
}
//C#
[DllImport("coredll.dll", SetLastError=true)]
public static extern long RegOpenKey(
IntPtr hkey,
string lpSubKey,
ref IntPtr hkeyResult
);
public long OpenMySubKey()
{
IntPtr hkey = IntPtr.Zero;
return RegOpenKey(HKEY_CLASSES_ROOT, "MySubKey", ref hkey);
}
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function RegOpenKey(ByVal hkey As IntPtr, ByVal lpSubKey As String, ByRef hkeyResult As IntPtr) As Long
End Function
Public Function OpenMySubKey() As Long
Dim hkey As IntPtr = IntPtr.Zero
Return RegOpenKey(HKEY_CLASSES_ROOT, "MySubKey", hkey)
End Function 'OpenMySubKey
//C#
unsafe
{
byte[] test = new byte[5];
fixed (byte* p = &test[0])
{
*p = 0xff;
}
}
也可以使用GCHandle指向对象。
//C#
using System.Runtime.InteropServices;
byte[] test = new byte[5];
GCHandle hObject = GCHandle.Alloc(test, GCHandleType.Pinned);
IntPtr pObject = hObject.AddrOfPinnedObject();
if(hObject.IsAllocated)
hObject.Free();
'VB
Imports System.Runtime.InteropServices
Dim test(4) As Byte
Dim hObject As GCHandle = GCHandle.Alloc(test, GCHandleType.Pinned)
Dim pObject As IntPtr = hObject.AddrOfPinnedObject()
If hObject.IsAllocated Then
hObject.Free()
End If
最后,可以使用LocalAlloc和Marshalling函数复制内存块得到数据块。
//C#
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalReAlloc(IntPtr hMem, uint uBytes, uint fuFlags);
public const uint LMEM_FIXED = 0;
public const uint LMEM_MOVEABLE = 2;
public const uint LMEM_ZEROINIT = 0x0040;
byte[] test = new byte[5];
IntPtr p = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (uint)test.Length);
if (p == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
else
{
Marshal.Copy(test, 0, p, test.Length);
}
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalAlloc(ByVal uFlags As UInt32, ByVal uBytes As UInt32) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalReAlloc(ByVal hMem As IntPtr, ByVal uBytes As UInt32, ByVal fuFlags As UInt32) As IntPtr
End Function
Public Const LMEM_FIXED As Integer = 0
Public Const LMEM_MOVEABLE As Integer = 2
Public Const LMEM_ZEROINIT As Integer = &H40
Dim test(4) As Byte
Dim p As IntPtr = LocalAlloc(Convert.ToUInt32(LMEM_FIXED Or LMEM_ZEROINIT), Convert.ToUInt32(test.Length))
If p.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
Else
Marshal.Copy(test, 0, p, test.Length)
End If
There are several issues to consider when determining the case of a MissingMethodException. When this exception occurs you should verify the following:
If targeting Pocket PC 2003 use Microsoft eMbedded Visual C++ 4.0.
If targeting Pocket PC 2000 or 2002 use Microsoft eMbedded Visual Tools 3.0
Verify that the parameters to the function match those of the original
A long in native code is typically 32-bits, whereas in the .NET Compact Framework it is 64-bits
Make sure that the function name is spelled correctly
Verify that the DLL is located correctly - Windows or executing folder of target device
Verify with DUMPBIN that the names of the functions were not mangled on exporting of the DLL (extern "C" fixes this). More information on this topic can be found in section "6.1. How do I call a function that is in a native DLL?" of this FAQ.
Verify that the DLL being imported is not dependant upon other DLLs. A missing dependant DLL will result in the MissingMethodException.
//C#
using System.Runtime.InteropServices;
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
[DllImport("coredll.dll")]
public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("coredll.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
// Set the clock ahead one hour
SYSTEMTIME st = new SYSTEMTIME();
GetSystemTime(ref st);
st.wHour = (ushort)(st.wHour + 1 % 24);
SetSystemTime(ref st);
'VB
Imports System.Runtime.InteropServices
Public Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure
<DllImport("coredll.dll")> _
Public Shared Sub GetSystemTime(ByRef lpSystemTime As SYSTEMTIME)
End Sub
<DllImport("coredll.dll")> _
Public Shared Function SetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As UInt32
End Function
'Set the clock ahead one hour
Dim st As New SYSTEMTIME
GetSystemTime(st)
st.wHour = Convert.ToUInt16(((Convert.ToInt32(st.wHour) + 1)) Mod 24)
SetSystemTime(st)
The device can be soft reset through P/Invoking of the KernelIoControl function, as demonstrated in the code below. For more information on how to use the function and extend the functionality of this sample, refer to Visual Studio .NET Help.
Note: On Smartphone devices, this will only work if you are signed with a privileged certificate.
//C#
using System.Runtime.InteropServices;
public const uint FILE_DEVICE_HAL = 0x00000101;
public const uint METHOD_BUFFERED = 0;
public const uint FILE_ANY_ACCESS = 0;
public uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}
[DllImport("Coredll.dll")]
public extern static uint KernelIoControl
(
uint dwIoControlCode,
IntPtr lpInBuf,
uint nInBufSize,
IntPtr lpOutBuf,
uint nOutBufSize,
ref uint lpBytesReturned
);
uint ResetPocketPC()
{
uint bytesReturned = 0;
uint IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15,
METHOD_BUFFERED, FILE_ANY_ACCESS);
return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0,
IntPtr.Zero, 0, ref bytesReturned);
}
private void Form1_Load(object sender, System.EventArgs e)
{
DialogResult r = MessageBox.Show
(
"Are you sure you want to reset?",
"Test",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2
);
if (r == DialogResult.Yes)
{
ResetPocketPC();
}
}
'VB
Public Const FILE_DEVICE_HAL As Integer = &H101
Public Const METHOD_BUFFERED As Integer = 0
Public Const FILE_ANY_ACCESS As Integer = 0
Public Function CTL_CODE( _
ByVal DeviceType As Integer, _
ByVal Func As Integer, _
ByVal Method As Integer, _
ByVal Access As Integer) As Integer
Return (DeviceType << 16) Or (Access << 14) Or (Func << 2) Or Method
End Function 'CTL_CODE
<DllImport("Coredll.dll")> _
Public Shared Function KernelIoControl _
( _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuf As IntPtr, _
ByVal nInBufSize As Integer, _
ByVal lpOutBuf As IntPtr, _
ByVal nOutBufSize As Integer, _
ByRef lpBytesReturned As Integer _
) As Integer
End Function
Function ResetPocketPC() As Integer
Dim bytesReturned As Integer = 0
Dim IOCTL_HAL_REBOOT As Integer = CTL_CODE(FILE_DEVICE_HAL, _
15, METHOD_BUFFERED, FILE_ANY_ACCESS)
Return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, _
IntPtr.Zero, 0, bytesReturned)
End Function 'ResetPocketPC
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim r As DialogResult = MessageBox.Show( _
"Are you sure you want to reset?", _
"Test", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2)
If r = DialogResult.Yes Then
ResetPocketPC()
End If
End Sub 'Form1_Load
This is not supported with the current version of the .NET Compact Framework. You can, however, P/Invoke Pocket PC's notificaiton system to do this. Refer to the following for more information:
This sample demonstrates how to P/Invoke numerous useful native functions that are not directly available through the .NET Compact Framework. A test Form is provided that enumerates all available test procedures and allows the user to select and run them: http://msdn.microsoft.com/library/en-us/dnnetcomp/html/PInvokeLib.asp
//C#
using System.Reflection;
using System.IO;
// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;
// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);
// This adds a file name to the path
String splashImageName = Path.Combine(fullAppPath, "myfile.txt");
'VB
Imports System.IO
Imports System.Reflection
' This is the full directory and exe name
Dim fullAppName As String = [Assembly].GetExecutingAssembly().GetName().CodeBase
' This strips off the exe name
Dim fullAppPath As String = Path.GetDirectoryName(fullAppName)
' This adds a file name to the path
Dim splashImageName As String = Path.Combine(fullAppPath, "myfile.txt")
//C#
using System.Reflection;
Assembly asm = Assembly.GetExecutingAssembly();
Bitmap bmpSprite = new Bitmap(asm.GetManifestResourceStream("AssemblyName.FileName"));
'VB
Imports System.Reflection
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
Dim bmpSprite As New Bitmap(asm.GetManifestResourceStream("AssemblyName.FileName"))
//C#
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
'VB
Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
e.Cancel = True
End Sub 'OnClosing
//C#
public class MyClass
{
protected byte[] m_data = null;
// uint uiDummy
// short sDummy
// This is a bit unsafe so you should throw an
// exception or assert if the byte array length is
// not 6. A safer but less memory efficient approach
// would be to set m_data = new byte[6] and then copy
//bytes to m_data.
public MyClass(byte[] bytes) {m_data = bytes;}
// Get/Set the uint
public uint uiDummy
{
get {return BitConverter.ToUInt32(m_data, 0);}
set
{
Buffer.BlockCopy(BitConverter.GetBytes(value),0,m_data,0,BitConverter.GetBytes(value).Length);
}
}
// Get/Set the short
public short sDummy
{
get {return BitConverter.ToInt16(m_data, 4);}
set
{
Buffer.BlockCopy(BitConverter.GetBytes(value),0,m_data,4,BitConverter.GetBytes(value).Length);
}
}
}
byte[] fromFile = {1,1,1,1,2,2};
MyClass myClass = new MyClass(fromFile);
uint test1 = myClass.uiDummy; // 0x1010101
short test2 = myClass.sDummy; // 0x202
myClass.sDummy = 0x0505; // Test setting the short
uint test4 = myClass.uiDummy; // 0x1010101
short test5 = myClass.sDummy; // 0x505
'VB
Public Class ByteClass
Protected m_data As Byte() = Nothing
' uint uiDummy
' short sDummy
' This is a bit unsafe so you should throw an exception
' or assert if the byte array length is not 6. A safer
' but less memory efficient approach would be to set
' m_data = new byte[6] and then copy bytes to m_data.
Public Sub New(ByVal bytes() As Byte)
m_data = bytes
End Sub 'New
' Get/Set the uint
Public Property uiDummy() As UInt32
Get
Return BitConverter.ToUInt32(m_data, 0)
End Get
Set(ByVal Value As System.UInt32)
Buffer.BlockCopy(BitConverter.GetBytes(Value),0,m_data,0,BitConverter.GetBytes(Value).Length)
End Set
End Property
' Get/Set the short
Public Property sDummy() As Short
Get
Return BitConverter.ToInt16(m_data, 4)
End Get
Set(ByVal Value As Short)
Buffer.BlockCopy(BitConverter.GetBytes(Value),0,m_data,4,BitConverter.GetBytes(Value).Length)
End Set
End Property
End Class 'ByteClass
Dim fromFile As Byte() = {1, 1, 1, 1, 2, 2}
Dim testClass As New ByteClass(fromFile)
Dim test1 As System.UInt32 = testClass.uiDummy ' 0x1010101
Dim test2 As Short = testClass.sDummy ' 0x202
testClass.sDummy = &H505 ' Test short
Dim test4 As System.UInt32 = testClass.uiDummy ' 0x1010101
Dim test5 As Short = testClass.sDummy ' 0x505
//C#
public void HandleMe(object o, EventArgs e) {...}
form.Invoke(new EventHandler(form.HandleMe));
'VB
Public Sub HandleMe(o As Object, e As EventArgs)
End Sub 'HandleMe
form.Invoke(New EventHandler(AddressOf form.HandleMe))
Although the following will compile, it will not work properly:
//C#
public delegate void MyHandler();
public void HandleMeBadly() {...}
form.Invoke(new MyHandler(form.HandleMeBadly));
'VB
Delegate Sub MyHandler()
Public Sub HandleMeBadly()
End Sub 'HandleMeBadly
form.Invoke(New MyHandler(form.HandleMeBadly))
Visual Studio .NET 2003帮助中的C# Programmer's Reference提供了在.net精简框架下使用不安全代码调用GetFileVersionInfo函数。 这个例子带来的问题是,这个函数是由OEM厂商决定的,并且不保证能返回正确结果。
使用Reflection可以获得程序集(Assembly)的版本:
//C#
using System.Reflection;
String ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
'VB
Imports System.Reflection;
Dim ver As String = [Assembly].GetExecutingAssembly().GetName().Version.ToString()
//C#
static void Main(string[] args)
{
// Do your thing here
}
'VB
Shared Sub Main(ByVal args() As String)
'Do your thing here
End Sub
在Visual Studio 2003中测试的时候,可以通过继承开发环境设置命令行的参数。主菜单,选择 Project->Project Name Properties. 在Property Pages对话框中,选择Configuration Properties->Debugging。在 Command Line Arguments 中输入字符串:text box.
//C#
System.IO.FileInfo fi = new System.IO.FileInfo("filename");
// remove readonly attribute
fi.Attributes -= System.IO.FileAttributes.ReadOnly;
System.IO.File.Delete("filename");
'VB
Dim fi As New System.IO.FileInfo("filename")
'remove readonly attribute
fi.Attributes -= IO.FileAttributes.ReadOnly
System.IO.File.Delete("filename")
The existence of a member can be determined at runtime using Reflection. The code below demonstrates how to use Reflection to access the "Width" property and the "GetPixel" method of a Bitmap object. In the case of the "Width" property, the code enumerates all "public" properties with a "get" component and then searches for one named "Width". The "GetPixel" sample demonstrates how one might use Reflection to call a known function where the parameter order is unknown. This sample is set up as if the author knows there is a method named "GetPixel" which takes a pixel location of x,y but does not know the order in which they appear in the parameter list. The sample enumerates the methods and searches for one named "GetPixel" and then enumerates the parameter list to determine whether the first or second parameter is named "X". Keep in mind that, due to the differences in hardware pixel formats, the value returned by GetPixel may be different from that set by SetPixel in this sample.
Reflection provides many powerful tools for determing functionality at runtime so for information, refer to the documentation regarding System.Type and the namespace System.Reflection.
//C#
using System.Reflection;
using System.Drawing;
Bitmap bm = new Bitmap(200, 100);
int width = 0;
// Explicitly set one pixel for testing
int x = 199;
int y = 20;
Color pixColor = Color.Black;
bm.SetPixel(x,y,Color.Magenta);
// Get the "Width" property
PropertyInfo[] propInfo =
bm.GetType().GetProperties(BindingFlags.GetProperty |
BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < propInfo.Length; i++)
{
if (propInfo[i].Name == "Width")
{
width = (int)propInfo[i].GetValue(bm, null);
break;
}
}
// Call the GetPixel method
MethodInfo[] methInfo = bm.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < methInfo.Length; i++)
{
if (methInfo[i].Name == "GetPixel")
{
ParameterInfo[] paramInfo = methInfo[i].GetParameters();
if (paramInfo.Length == 2)
{
object[] xy = new object[2];
if (paramInfo[0].Name == "x")
{
xy[0] = x;
xy[1] = y;
}
else
{
xy[1] = x;
xy[0] = y;
}
pixColor = (Color)methInfo[i].Invoke(bm, xy);
break;
}
'VB
Imports System.Reflection
Imports System.Drawing
Dim bm As New Bitmap(200, 100)
Dim width As Integer = 0
' Explicitly set one pixel for testing
Dim x As Integer = 199
Dim y As Integer = 20
Dim pixColor As Color = Color.Black
bm.SetPixel(x, y, Color.Magenta)
' Get the "Width" property
Dim propInfo As PropertyInfo() = _
bm.GetType().GetProperties((BindingFlags.GetProperty Or _
BindingFlags.Public Or BindingFlags.Instance))
Dim i As Integer
For i = 0 To propInfo.Length - 1
If propInfo(i).Name = "Width" Then
width = Fix(propInfo(i).GetValue(bm, Nothing))
Exit For
End If
Next i
' Call the SetPixel method
Dim methInfo As MethodInfo() = bm.GetType().GetMethods((BindingFlags.Public _
Or BindingFlags.Instance))
For i = 0 To methInfo.Length - 1
If methInfo(i).Name = "GetPixel" Then
Dim paramInfo As ParameterInfo() = methInfo(i).GetParameters()
If paramInfo.Length = 2 Then
Dim xy(1) As Object
If paramInfo(0).Name = "x" Then
xy(0) = x
xy(1) = y
Else
xy(1) = x
xy(0) = y
End If
pixColor = CType(methInfo(i).Invoke(bm, xy), Color)
Exit For
End If
End If
Next i
There is no Abort method to the Thread class in the .NET Compact Framework so a thread must be aborted by returning from the executing procedure. Typically, an application will notify threads of a closing event by setting a global variable. The main thread will then wait for worker threads to finish processing before closing the application. The following HOWTO article demonstrates how to accomplish this.
Windows Media Player is only available on the Pocket PC 2003 emulator. The Windows Media Player install package is for installation on a hardware device connected through ActiveSync and will not install to the emulator.
The Active Programs list on the Pocket PC enumerates all open Forms. To stop a Form from being displayed in the list, simply set the Form's caption to be an empty string. The following example shows how to keep only the application name in the list while a Form is displayed from within another Form:
//C#
string AppName = "MyApp";
Form1 form1 = new Form1();
this.Text = "";
form1.Text = AppName;
form1.ShowDialog();
this.Text = AppName;
'VB
Dim AppName As String = "MyApp"
Dim form1 As New Form1()
Me.Text = ""
form1.Text = AppName
form1.ShowDialog()
Me.Text = AppName
You can use Reflection to look up a control instance by its name. Here is some sample code:
//C#
private void Form1_Load(object sender, System.EventArgs e)
{
ComboBox c = (ComboBox)this.ControlFromName("combobox1");
c.Items.Add("1");
this.GetControls();
}
private Control ControlFromName(string name)
{
object o = this.GetType().GetField(name,
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase).GetValue(this);
return((Control)o);
}
private void GetControls()
{
System.Reflection.FieldInfo[] fis = this.GetType().GetFields
(
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase
);
foreach(System.Reflection.FieldInfo fi in fis)
{
if (fi.GetValue(this) is Control)
MessageBox.Show(fi.Name);
}
}
'VB
Private Function ControlFromName(ByVal name As String) As Control
Dim o As ObjectDim o As Object
o = Me.GetType().GetField(name, Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase).GetValue(Me)
Return (CType(o, Control))
End Function
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As ComboBox
c = CType(ControlFromName("_combobox1"), ComboBox)
c.Items.Add("1")
Me.GetControls()e.GetControls()
End Sub
Private Sub GetControls()
Dim fis As System.Reflection.FieldInfo()
fis = Me.GetType().GetFields(Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase)
For Each fi As Reflection.FieldInfo In fis
If TypeOf (fi.GetValue(Me)) Is Control Then
MessageBox.Show(fi.Name)
End Ifnd If
Next
End Sub
Create a modeless dialog that behaves like a model dialog.
//C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
public interface IModelessDialogCallback
{
void DialogResultCallback(DialogResult result);
}
public class Test : System.Windows.Forms.Form, IModelessDialogCallback
{
ModelessDialog dlg;
Button bShow;
int counter = 0;
protected override void OnLoad(EventArgs e)
{
this.Text = "Modal(less) Dialog Example";
this.bShow = new Button();
this.bShow.Parent = this;
this.bShow.Bounds = new Rectangle (10, 10, 150, 30);
this.bShow.Text = "Show Dialog";
this.bShow.Click += new EventHandler(this._Click);
this.dlg = new ModelessDialog(this);
}
private void _Click(object o, EventArgs e)
{
this.Enabled = false;
this.bShow.Text = "waiting for dlg";
dlg.Show();
}
public void DialogResultCallback(DialogResult result)
{
MessageBox.Show("dialog returned: " + (result == DialogResult.OK ? "OK" : "Cancel"));
this.Enabled = true;
this.bShow.Text = "Show Dialog:" + ++counter;
}
public static void Main()
{
Application.Run(new Test());
}
}
public class ModelessDialog : Form
{
IModelessDialogCallback parent;
Button bOK, bCancel;
public ModelessDialog(IModelessDialogCallback parent)
{
this.parent = parent;
this.Text = "Modeless Dialog";
this.bOK = new Button();
this.bOK.Parent = this;
this.bOK.Bounds = new Rectangle (10, 10, 150, 30);
this.bOK.Text = "OK";
this.bOK.Click += new EventHandler(this._Click);
this.bCancel = new Button();
this.bCancel.Parent = this;
this.bCancel.Bounds = new Rectangle (10, 50, 150, 30);
this.bCancel.Text = "Cancel";
this.bCancel.Click += new EventHandler(this._Click);
}
private void _Click(object o, EventArgs e)
{
this.Hide();
this.parent.DialogResultCallback(o == this.bOK ? DialogResult.OK : DialogResult.Cancel);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
this.parent.DialogResultCallback(DialogResult.Cancel);
}
}
'VB
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Collections
Imports System.ComponentModel
Public Interface IModelessDialogCallback
Sub DialogResultCallback(result As DialogResult)
End Interface IModelessDialogCallback'
Public Class Test
Inherits System.Windows.Forms.Form
Implements IModelessDialogCallback
Private dlg As ModelessDialog
Private bShow As Button
Private counter As Integer = 0
Protected Overrides Sub OnLoad(e As EventArgs)
Me.Text = "Modal(less) Dialog Example"
Me.bShow = New Button()
Me.bShow.Parent = Me
Me.bShow.Bounds = New Rectangle(10, 10, 150, 30)
Me.bShow.Text = "Show Dialog"
AddHandler Me.bShow.Click, AddressOf Me._Click
Me.dlg = New ModelessDialog(Me)
End Sub 'OnLoad
Private Sub _Click(o As Object, e As EventArgs)
Me.Enabled = False
Me.bShow.Text = "waiting for dlg"
dlg.Show()
End Sub '_Click
Public Sub DialogResultCallback(ByVal result As DialogResult) Implements _
IModelessDialogCallback.DialogResultCallback
MessageBox.Show(("dialog returned: " + IIf(result = DialogResult.OK, "OK", "Cancel")))
Me.Enabled = True
counter += 1
Me.bShow.Text = String.Format("Show Dialog: {0}", counter)
End Sub 'DialogResultCallback
Public Shared Sub Main()
Application.Run(New Test)
End Sub 'Main
End Class 'Test
Public Class ModelessDialog
Inherits Form
Private myParent As IModelessDialogCallback
Private bOK, bCancel As Button
Public Sub New(parent As IModelessDialogCallback)
Me.myParent = parent
Me.Text = "Modeless Dialog"
Me.bOK = New Button()
Me.bOK.Parent = Me
Me.bOK.Bounds = New Rectangle(10, 10, 150, 30)
Me.bOK.Text = "OK"
AddHandler Me.bOK.Click, AddressOf Me._Click
Me.bCancel = New Button()
Me.bCancel.Parent = Me
Me.bCancel.Bounds = New Rectangle(10, 50, 150, 30)
Me.bCancel.Text = "Cancel"
AddHandler Me.bCancel.Click, AddressOf Me._Click
End Sub 'New
Private Sub _Click(o As Object, e As EventArgs)
Me.Hide()
Me.myParent.DialogResultCallback(IIf(o Is Me.bOK, DialogResult.OK, DialogResult.Cancel))
End Sub '_Click
Protected Overrides Sub OnClosing(e As CancelEventArgs)
e.Cancel = True
Me.Hide()
Me.myParent.DialogResultCallback(DialogResult.Cancel)
End Sub 'OnClosing
End Class 'ModelessDialog
There are two primary methods for rounding numbers:
Convert.ToInt32
Cast or Fix (C# or VB)
Convert.ToInt32 automatically handles rounding, where remainders of .5 and greater cause the number to be rounded up. Casting or using Fix requires adding .5 to the number to ensure that it will round properly, as these methods simply remove the remainder.
Profiling on the emulator and a Compaq iPAQ H3600 series device yielded the following results for 1 million operations of each method, where num is a float set to 3.6F:
Emulator
iPAQ
Operation
Debug (ms)
Release (ms)
Debug (ms)
Release (ms)
C#: Convert.ToInt32(num)
1321
1109
6264
6283
C#: (int)(num + .5F)
170
49
1479
59
VB: Convert.ToInt32(num)
1218
1232
6531
6517
VB: Fix(num + .5F)
3873
3677
18144
17955
Thus, by examining the release build results for the device, it can be concluded that on the current generation of devices it is most efficient to use casting in C# and Convert.ToInt32 in VB. In C#, casting proved to be over 106 times faster, whereas in VB, Convert.ToInt32 was nearly 3 times faster.
//C#
float temp = 3.6f;
int rounded1 = (int)(temp + .5f);
int rounded2 = Convert.ToInt32(temp);
'VB
Dim temp As Single = 3.6F
Dim rounded1 As Integer = Fix(temp + .5F)
Dim rounded2 As Integer = Convert.ToInt32(temp)
There is a known bug with using Control.Invoke while a modal dialog is displayed through ShowDialog. Download the latest .NET Compact Framework Service Pack to fix this:
Culture information can be retrieved through Reflection:
//C#
using System.Reflection;
using System.Globalization;
Assembly asm = Assembly.GetExecutingAssembly();
CultureInfo ci = asm.GetName().CultureInfo;
'VB
Imports System.Reflection
Imports System.Globalization
Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim ci As CultureInfo = asm.GetName().CultureInfo
Using Digest authentication instead of Basic or NTLM authentication is the recommended solution. Digest authentication is supported by the .NET Compact Framework.
There is a known issue with setting Blocking to false on a blocking socket. There are three paradigms when programming with sockets (both managed and native) and it is strongly recommended that developers use asynchronous I/O functions as a solution to this problem. Select is very inefficient in managed code, and the cost of the regular case exception is also high. The three paradigms are detailed below, with the last being the preferred method.
Use blocking calls on blocking sockets (i.e. send(), recv(), etc). This is the simplest method, and probably the most commonly used. The functions perform their operation synchronously, tying up the currently executing thread. This is most acceptable for clients, and multithreaded servers (Microsoft does not recommend using one thread per client because threads are very expensive on Windows). Note that though the operations may block for an unspecified period of time (i.e. until the operation can be completed), they will most often complete immediately.
Use non-blocking sockets in conjunction with select or poll. Use the same "blocking" functions, but put the socket in a special mode that prohibits it from blocking. In the cases when the function would block, it returns an error code (in managed code, this throws an exception). You can then use poll or select to wait until a point in time at which an operation would complete (select allows you to manage more than one socket, so you can handle multiple clients on only one thread), and then perform the operation. At the time these calls were created, there were no threads in operating systems, so this was the only way to do things. Using this mechanism has by far the absolute worst performance you could possibly get from socket programming. This problem is exacerbated in managed code, since you now expect an exception to be thrown, which is an even bigger performance hit.
Use asynchronous I/O functions on the socket. This is your best all around solution. It allows you to perform operations asynchronously, and you get notified by callbacks. In general, the performance is better than using non-blocking I/O with select, and this is the recommendation for how to do things (at least, in native code).
This is a known issue in the version 1 of the .NET Compact Framework. To work around the issue, you can P/Invoke setsockopt with SO_REUSEADDR set to true.
There is a known issue in the .NET Compact Framework. You can work around this by setting HttpWebRequest.AllowStreamWriteBuffering to True and commenting out your setting of the ContentLength.
An error of type SQLCeException with a Native Error Code of 27750 occurs. This error corresponds to "Cannot load sscemw20.dll or ssceca20.dll is missing or not registered."
Resolution Load the library at the start of the application, even if you do nothing with it. To load the library, you can load the SqlCeEngine object.
This document provides an overview of the development tasks that were unique to the Pocket TaskVision sample application. Pocket PC devices have much lower storage capacities, memory and lower-end processors than desktop systems—storage and performance considerations are mentioned throughout the document. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/wnf_pkttaskvision.asp
Learn how to create .NET Compact Framework-based games. Learn the key requirements for writing games targeting small devices and see that the .NET Compact Framework can handle them with ease. Include advanced performance-tuning techniques that you can use to push the limits of your game. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfgaming.asp
Learn how to use a managed DateTimePicker class in your Pocket PC projects. The .net精简框架 provides managed classes for most controls but the DateTimePicker class is not supported. Wrapper classes that P/Invoke the native control can be found on the Web, but this sample provides a purely managed DateTimePicker class that you can use in your Pocket PC projects. The class and sample application are provided in C# and Visual Basic .NET. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfdatetimepicker.asp
This article discusses the Pocket PC Signature sample application. The sample includes a client that runs on the Pocket PC that sends signature data over TCP sockets to a server that is running on the desktop. Data is encrypted and decrypted using the cryptography services. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/PPCSignatureApp.asp
Symptom: When a Windows CE device is connected through ActiveSync, deployment and debugging (F5 and Ctrl-F5) fail with the typical error message "There were deployment errors. Continue?"
Cause: Due to the fact that ActiveSync 3.5 and 3.6 do not provide accurate information about the device instruction set, ConMan (Visual Studio .NET 2003 component for device connectivity) cannot use the information returned by ActiveSync to map a Windows CE device to the right .NET Compact Framework target (MIPSII, ARMV4 etc).
Workaround: Install and run the separately provided Windows CE Configuration Add-In. For ARMV4 devices, an alternative is to select Pocket PC Device at the beginning of the deployment.
Symptom: After running the Windows CE Device Configuration Add-In, the user can deploy without debugging (Ctrl-F5) to an ActiveSync connected CEPC (or any x86 based Windows CE device) but cannot debug (F5). Non-x86 based Windows CE devices do not have this problem.
Cause: Debugging Windows CE Emulators (also x86 based) uses a TCP port that conflicts with the one used by ActiveSync for debugging x86 based devices. To support Emulator debugging, it is necessary to disable debugging for CEPC and other x86 devices by default.
Workaround: The following workaround will allow debugging of x86 devices but disable debugging with the Windows CE Emulator. The workaround requires the files WinCEx86Device.reg, and ProxyPorts.reg which can be found in the SDK utilities directory: "<VSROOT>\CompactFrameworkSDK\WinCE Utilities\WinCE Proxy Ports Reg".
Import WinCEx86Device.reg to the registry. Now you will be able to debug x86 devices, but debugging with the Windows CE Emulator will fail.
To restore Emulator debugging, remove the registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\ProxyPorts" and then import ProxyPorts.reg.
Symptom: After F5, all the files including the application executables and .net精简框架 cab files are copied to the device, and the IDE reports success on launching the application, but nothing is launched on the device. If you check the \windows folder of the device, toolhelp.dll does not exist.
Cause: Some device images may not include toolhelp.dll which is required by SDE debugging.
Workaround: Copy toolhelp.dll from Windows CE SDK to the \windows folder of the device. This file can be found in the corresponding target directory for each device.
For example, for an ARM device: "<VSROOT>\CompactFrameworkSDK\WinCE Utilities\ToolHelp\WinCE4\armv4".
Cause: It is a known issue that the .NET Framework method System.Net.Dns.GetHostName throws an exception when there are more than 50 network protocols installed on the current machine. The method is called by ConMan and the exception causes failure of the ConMan transport initialization.
Workaround: Uninstall the network protocols that are not actually needed. One way to do this is to go to Device Manager (Right-click on "My Computer", select Properties->Hardware and press the "Device Manager" button) and remove unused entries under "Network adapters". Another way is to uninstall applications that have installed protocols. Those applications can be found from the output of EnumProtocles.exe.
Symptom: The "deployment error" message box occurs on Ctrl-F5 or F5 after ActiveSync has been uninstalled. Uninstalling ActiveSync typically happens when the user upgrades ActiveSync, e.g. from version 3.5 to 3.6.
Cause: ConMan relies on some registry values under "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\ProxyPorts" for deployment and debugging through ActiveSync. These registry values are removed when ActiveSync is uninstalled.
The device may need to be un-cradled/re-cradled or soft rebooted to make the fix take effect. It is recommended that users never uninstall ActiveSync. To upgrade ActiveSync, you should directly install the new version which will correctly override the old one.
Symptom: When the machine name or the logon user name contains any character that is not on the current code page or below ASCII value 0x20, deployment to devices fails while deployment to the Emulator works fine.
Cause: ConMan security authentication internally uses the machine name and user name in the ASCII form which is not able to handle characters in the categories described above.
Workaround: Do not use characters described above in the machine name or the user name. If the user is not clear what characters belong to those categories, it is always safe to use just English alphabetic letters and numbers.
Symptom: If the current logon user is not in the Administrators group, deployment to devices always fails even though the user is in the Visual Studio Developers group and the Debugger Users group. On the other hand deployment/debugging to the Emulator works fine.
Cause: During device deployment, the ConMan code internally opens a file in a mode that requires administrator privileges.
Workaround: Add the current user to the Administrators group or limit the deployment target to the Emulator only.
Symptom: When deploying an application to a Pocket PC 2002/2003 device or Emulator, the deployment fails with an output message similar to "Could not write to output file 'SmartDeviceApplication1.exe' - Sharing violation". This usually happens after the same application has previously deployed to the same device or Emulator.
Cause: By default the X button on an application for Pocket PC 2002 or 2003 (not 2000) does not close the process. It only "minimizes" the application window. When the user tries to deploy the application again, the "minimized" instance will cause the sharing violation and fail the deployment. Another possible cause is that the user has forcibly terminated a debug session in the middle.
Workaround: Make sure the application is really closed on the device or Emulator when deploying it again. To see running processes, go to Start->Settings->Memory->Running Programs on the Pocket PC. In case it is intended to have a button to really close an application, create such a button explicitly or set Windows Form's property Minimize Box to False so that the X button will become an OK button for closing the application. If the cause was the manual termination of a debug session, the device or the Emulator may need to be soft rebooted.
Symptom: When the Emulator is used for the first time after the installation of Visual Studio .NET 2003 on a .NET Server machine, a message box pops up titled "Security Alert – Driver Installation". The text of the message starts with "The driver software you are installing for: Virtual PC Application Services has been signed with Authenticode(TM) technology. The publisher's identity has been identified, and the driver has not been modified since it was published". Near the end, it says "However, this driver cannot be tested by Microsoft Windows Hardware Quality Labs to verify its compatibility with this version of Windows. … Do you trust this publisher and want to install the driver?"
Cause: Visual Studio .NET 2003 installs a Virtual PC Application Services Emulator driver which does not pass the validation of .NET Server's Driver Validation system.
Workaround: When the alert message comes up, click the Yes button. Otherwise the Emulator will not work.
Symptom: When the Emulator is being started, a message box appears titled "Emulator for Windows CE" with the text "One or more files from the Emulator for Windows CE installation is missing. Please reinstall Emulator for Windows CE and try again." The connection or deployment to the Emulator will fail after the message box.
Cause: A typical cause is that the Emulator driver is not installed or is corrupted.
Workaround: Go to "Device Manager" (Right-click on "My Computer", select Properties->Hardware and press the "Device Manager" button) and check whether "Virtual PC Application Services" is installed under the System Devices group. If the driver is not installed, install it by running "<VSROOT>\CompactFrameworkSDK\ConnectionManager\Bin\DriverInstall\Smart Devices Emulator.msi".
Note: Verify that the "Virtual PC Application Services" appears after running the msi file. If not then reboot the PC and attempt the installation again.
Symptom: The startup of the Emulator is slow and the deployment eventually fails with a connection error message. The startup mainly includes starting the Emulator image and downloading the .net精简框架 files. The failure happens more often on low-end machines when the Emulator is used for the first time or after "turn off" instead of "saving state" was used on the previous Emulator shutdown.
Cause: Due to some performance issues in the Emulator, its startup may take too long to finish within the ConMan timeout period.
Workaround: If possible, always choose "saving state" instead of "turn off" when closing the Emulator so that the startup will be much faster when the Emulator is started next time. Or before the deployment, pre-start the Emulator by clicking on the "Connect to Device" button which is located next to the Emulator name on the menu bar. Note that the first deployment will still fail after the pre-start. (See details in the issue about this.) In addition, hovering the mouse over the Emulator image may speed up the Emulator startup.
Symptom: When the Emulator is started with the "Connect to Device" button, the first deployment attempt always fails.
Workaround: There is no workaround at this point for the failure of the first deployment after the Emulator is started with the button, but the subsequent deployment attempts should succeed.
Symptom: When a machine has no active network connection, debugging with the Emulator (F5) does not work, while deployment without debugging (Ctrl-F5) works fine.
Cause: Emulator debugging relies on TCP transport.
Workaround: Install the Microsoft Loopback Adapter on the machine.
To install the the Microsoft Loopback Adapter in Microsoft Windows XP
Open Control Panel, choose Add Hardware, and then choose Next.
Choose Yes, I have already connected the hardware, and then choose Next.
From the Installed hardware list, select Add a new hardware device, and then choose Next.
Choose Install the hardware that I manually select from a list (Advanced), and then choose Next.
From the Common hardware types list, select Network adapters, and then choose Next.
From the Manufacturer list, select Microsoft.
From the Network Adapter list, select Microsoft Loopback Adapter, and then choose Next.
Choose Next, and then choose Finish.
To install the Microsoft Loopback Adapter in Microsoft Windows 2000
Open Control Panel, choose Add/Remove Hardware, and then choose Next.
Choose Add/Troubleshoot a device, and then choose Next.
From the Devices list, select Add a new device, and then choose Next.
Choose No, I want to select the hardware from a list, and then choose Next.
From the Hardware types list, select Network adapters, and then choose Next.
From the Manufacturers list, select Microsoft.
From the Network Adapter list, select Microsoft Loopback Adapter, and then choose Next.
Symptom: When the Emulator image is starting up, it stays as a black screen forever. This happens when the Emulator is configured to use at least one COM port.
Cause: By default, the Emulator is not assigned a COM port but the user may change the configuration to assign a COM port to the Emulator. When a COM port assigned to the Emulator is used by ActiveSync or any other running application, the Emulator image loading will fail.
Workaround: Remove all of the COM ports assigned to the Emulator or change them to those not used by ActiveSync or any other running application. To remove or change the COM port configurations in Visual Studio .NET 2003, go to Tools/Options/Devices, choose an Emulator in the Devices window and click the Configure button.
Symptom: When the eMbedded Visual Tools (Platform Builder) Emulator is running, Visual Studio .NET 2003 Emulator deployment fails, and vice versa. Sometimes even after the Visual Studio .NET 2003 Emulator is closed, the eMbedded Visual Tools Emulator still does not work.
Cause: The Visual Studio .NET 2003 Emulator and the eMbedded Visual Tools Emulator internally use two different instances of Emulator.exe which are not compatible with each other. Because they share the same executable name and the exe is a single instance application, while Emulator.exe for eMbedded Visual Tools is running, the one for Visual Studio .NET 2003 cannot be started, and vice versa. Another issue is that when the Visual Studio .NET 2003 Emulator user interface is closed, emulator.exe does not close until the hosting Visual Studio IDE instance is closed. In that case, the eMbedded Visual Tools Emulator cannot be started even though there is no Emulator running visually.
Workaround: Do not use the two Emulators at the same time. If an Emulator cannot be started, check the Task Manager and close any running emulator.exe processes.
Symptom: When a device does not have an ActiveSync connection with the Visual Studio machine but has a valid TCP connection, deployment does not work.
Cause: ConMan relies on an ActiveSync connection to perform some initial settings between the Visual Studio machine and the device. If no ActiveSync connection is available, the setting will have to be performed manually.
Workaround: Use the separately provided Keyman tool to perform the initial device settings with the following steps:
Choose the correct flavor of Keyman.exe (e.g. ARMV4 or SH4, etc) and place it on the device. Launch Keyman and select "FILE->CONNECT". Keyman is now waiting for the VS machine to connect to it via TCP/IP. After the VS machine has connected, and then disconnected, Keyman on the device will automatically exit.
On the VS machine, copy CryptoAPIWrapper.dll from <VSROOT>\ CompactFrameworkSDK\ConnectionManager\bin to the directory of KeymanDesktop.exe. Launch KeymanDesktop.exe.
Click "Save" to save your settings in the current directory where KeymanDesktop.exe is running. The saved defaults will be loaded next time the exe is launched.
Enter the IP Address of the device
Use the recommended value for the Device Port.
Enter the path of the "Target" directory where the ConmanClient bits such as ConManClient.exe reside. The typical value is \ CompactFrameworkSDK\ConnectionManager\Target.
Use the Connect button to connect.
Click on "prep device" after you have connected. On success, you will see a dialog telling you everything went OK.
Use the Disconnect button to disconnect. When you disconnect, the device side bits will automatically exit.
Launch ConManClient.exe on the device. First you must select the flavor of the target device. Also, you must select the location of your "Target" directory in Visual Studio.
Cause Bug in the connection tool after selecting ARMV4I CPU type
Resolution Configure tool for ARMV4T CPU type:
In Visual Studio, on the Tools menu, click "Select Windows CE Device CPU". Note: this menu item is installed by the Windows CE Utilities for Visual Studio .NET 2003 Add-on Pack
Change the device architecture to ARMV4T
Click Configure, then click Close. Restart Visual Studio if prompted
No. The .NET Compact Framework is serviced through operating system updates at the carrier's / OEM's discretion. Therefore, .NET Compact Framework will generally drop as part of larger Windows Mobile for Smartphone operating system updates.
The input mode can be set using GetFocus and SendMessage APIs according to the code below:
'VB
Imports System.Runtime.InteropServices
Public Const EM_SETINPUTMODE As Integer = &HDE
Public Const EIM_SPELL As Integer = 0
Public Const EIM_AMBIG As Integer = 1
Public Const EIM_NUMBERS As Integer = 2
<DllImport("coredll.dll")> _
Public Shared Function GetFocus() As IntPtr
End Function
<DllImport("coredll.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal Message As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
'Sample use setting TextBox to number input
Private Sub txtAmount_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtAmount.GotFocus
Dim hWnd As IntPtr
hWnd = Me.GetFocus()
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS)
txtAmount.SelectionStart = txtAmount.Text.Length
End Sub
//C#
using System.Runtime.InteropServices;
public const uint EM_SETINPUTMODE = 0xDE;
public const uint EIM_SPELL = 0;
public const uint EIM_AMBIG = 1;
public const uint EIM_NUMBERS = 2;
[DllImport("coredll.dll")]
public static extern IntPtr GetFocus();
[DllImport("coredll.dll")]
public static extern int SendMessage(IntPtr hWnd,
uint Message, uint wParam, uint lParam);
// Sample use setting TextBox to number input
private void Form1_Load(object sender, System.EventArgs e)
{
txtAmount.GotFocus +=
new System.EventHandler(txtAmount_GotFocus);
}
private void txtAmount_GotFocus(object sender, System.EventArgs e)
{
IntPtr hWnd;
hWnd = GetFocus();
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS);
txtAmount.SelectionStart = txtAmount.Text.Length;
}
Each version of the .NET Compact Framework is released with a different Win32 File Version number (this is a separate version number from the Assembly Version, which should be the same across all releases of the Version 1 .NET Compact Framework, including Service Packs).
In order to see what version is installed, use File Explorer to navigate to the \Windows directory on the device, and click the file called CGACUTIL. You will get a message box showing you the Win32 File Version of the .NET Compact Framework installed on the device.
One approach would be to create a file share on your development PC, and then connect to that share via File Explorer in the emulator. You may then copy and paste the files from the share to the emulator's local file system. Another approach would be to add the file(s) to a smart device project and set their Build Action(s) property to "Content". See the Visual Studio .NET online documentation for more information on "File Properties":
You must ask the OEM to include it in the device's image. If you are the OEM and you are using Platform Builder 4.2, then including the OS Dependencies for the .NET item automatically causes imgdecmp.dll to be part of the emulator image - if that is not working then refer to cesysgen.bat. Another method is to set the environment variable "__SYSGEN_IMGDECMP=1" to explicitly force the DLL into the image.
This is by design. You must either change the names of the DLLs, or if the DLLs are strong named, place them in the GAC and use Assembly.Load with a full strong name.
This allows ActiveSync to connect to your Emulator session from Visual Studio .NET 2003. Create an ActiveSync session to the 4.2 emulator, this will allow Visual Studio 2003 to consider it a real device (Choose PPC device as the deployment target).
While adding designer support in Visual Studio .NET 2003 for Smart Device custom controls, you may run into the following issues:
Unable to associate an Icon to the Control for showing it in the toolbox at design time
The component, when added to the toolbox, becomes greyed out
Causes
Using a design project separate from the control project. Visual Studio .NET automatically prepends the project default namespace to the bitmap name. The "default namespace" defaults to the project name. This may be a problem because the design project has a slightly different name than the runtime project.
Not setting the correct ToolBoxItemFilterAttribute values
Resolutions
Given the following example: Runtime VS.NET Project: MyProject Class Name: MyProject.MyClass Design VS.NET Project Name: MyProject.Design BitMap name in VS.NET Design Project: Foo.bmp Bitmap name in design assembly: MyProject.Design.MyClass.bmp -- This creates a problem because the bitmap needs the name: MyProject.MyClass.bmp
In the above example, setting the design project's default namespace to "MyProject" rather then "MyProject.Design" should fix the problem.
The easiest way to check the name of the bitmap within the assembly is to run ILDASM and open the Manifest. The embedded resources are listed at the end of the manifest.
If you create a custom component derived from the Component class, your code must include the following statements so that your component appears in the Toolbox:
//C#
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(...);
}
'VB
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawLine(...)
End Sub 'OnPaint
在程序的其他部分,利用控件的一个方法,可以用来建立任意控件的图形对象:
//C#
using System.Drawing;
Graphics g = this.CreateGraphics();
'VB
Imports System.Drawing
Dim g As Graphics = Me.CreateGraphics()
直接画到bitmap位图文件中:
//C#
using System.Drawing;
Bitmap bm = new Bitmap(10,10);
Graphics g = Graphics.FromImage(bm);
'VB
Imports System.Drawing
Dim bm As New Bitmap(10, 10)
Dim g As Graphics = Graphics.FromImage(bm)
//C#
using System.Drawing;
protected override void OnPaint(PaintEventArgs e)
{
string s = "Hello World"
Pen pen = new Pen(Color.Fuchsia);
Font font = new Font("Arial", 18, FontStyle.Regular);
Brush brush = new SolidBrush(Color.Black);
SizeF sSize = e.Graphics.MeasureString(s, font);
Rectangle r = new Rectangle(9, 199,(int)sSize.Width + 1, (int)sSize.Height + 1);
e.Graphics.DrawRectangle(pen, r);
e.Graphics.DrawString(s, font, brush, 10.0f, 200.0f);
base.OnPaint (e);
}
'VB
Imports System.Drawing
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim s As String = "Hello World"
Dim pen As New Pen(Color.Fuchsia)
Dim font As New Font("Arial", 18, FontStyle.Regular)
Dim brush = New SolidBrush(Color.Black)
Dim sSize As SizeF = e.Graphics.MeasureString(s, font)
Dim r As New Rectangle(9, 199, Fix(sSize.Width) + 1, Fix(sSize.Height) + 1)
e.Graphics.DrawRectangle(pen, r)
e.Graphics.DrawString(s, font, brush, 10F, 200F)
MyBase.OnPaint(e)
End Sub 'OnPaint
While there is no inherent support for zooming or stretching a single image, these effects can be achieved quite easily by creating a new Bitmap object with an associated Graphics object and copying the desired portion of the original Bitmap into it. The following sample creates two bitmaps of the same size, where the second contains a zoomed center section of the first, provided the project has an embedded resource named MyImage.bmp. This same technique could be used to stretch images by modifying the source and destination rectangles such that they do not maintain their original aspect ratio.
//C#
using System.Drawing;
using System.Reflection;
Bitmap m_bmpOriginal;
Bitmap m_bmpZoom;
private void Form1_Load(object sender, System.EventArgs e)
{
Assembly asm = Assembly.GetExecutingAssembly();
m_bmpOriginal = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name
+ ".MyImage.bmp"));
// Take the center quarter of m_bmpOriginal
// and create stetch it into m_bmpZoom of the same size
m_bmpZoom = new Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height);
Graphics gZoom = Graphics.FromImage(m_bmpZoom);
Rectangle srcRect = new Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4,
m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2);
Rectangle dstRect = new Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height);
gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel);
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(m_bmpOriginal, 0, 0);
e.Graphics.DrawImage(m_bmpZoom, 125, 0);
base.OnPaint (e);
}
'VB
Imports System.Drawing
Imports System.Reflection
Private m_bmpOriginal As Bitmap
Private m_bmpZoom As Bitmap
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
m_bmpOriginal = New Bitmap(asm.GetManifestResourceStream((asm.GetName().Name _
+ ".MyImage.bmp")))
' Take the center quarter of m_bmpOriginal
' and create stetch it into m_bmpZoom of the same size
m_bmpZoom = New Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height)
Dim gZoom As Graphics = Graphics.FromImage(m_bmpZoom)
Dim srcRect As New Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4, _
m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2)
Dim dstRect As New Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height)
gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub 'Form1_Load
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.DrawImage(m_bmpOriginal, 0, 0)
e.Graphics.DrawImage(m_bmpZoom, 125, 0)
MyBase.OnPaint(e)
End Sub 'OnPaint
Download and install to your desktop development PC a "Developer" version of the service pack (the download title will read something like: "Microsoft® .NET Compact Framework 1.0 SPx Developer Redistributable") from: http://msdn.microsoft.com/mobility/downloads/updates/default.aspx
The next step is to copy the appropriate .NET Compact Framework cab file (as per next paragraph) to the emulator. From within the emulator point File Explorer to a share on your PC and then copy and paste the cab to somewhere on the emulator's file system. Now launch the cab file from File Explorer and answer "Yes" if asked to overwrite anything.
To install SQL Server CE with an application, simply install the proper SQL Server CE CAB files as part of the application's installation. There are two sets of cabs associated with SQL Server CE.
The developer CAB includes Query Analyzer, and error strings. This CAB should not be included with application deployment. It comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
The SQL Server CE CAB, which includes the engine, client agent, and managed extensions for the client agent is required by applications utilizing System.Data.SqlServerCe components. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
Applications that access SQL Server, ie applications utilizing System.Data.SqlClient components should deploy the 'sql' CAB. This CAB also comes in two actual files, one for Pocket PC and one for Windows CE 4.x devices:
sql.ppc3.<processor>.cab sql.wce4.<processor>.cab
All of these CABs are included in the Visual Studio .NET 2003 Professional Edtion install. The default location is:
\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\...
This article expands upon the "Dancing Rectangles" sample by implementing loading and displaying of bitmaps. It also implements some more advanced features such as animated bitmaps, source and destination key transparency, and alpha blending, i.e., translucency. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp
This article expands upon the "Dancing Zombies" sample by implementing drawing of points, lines, and custom 1 bit fonts converted from 8 bit bitmaps. It also implements an input system that overrides the functionality of the hardware buttons and tracks button states. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI3.aspTop of Page
在windows平台(Win32或.NET)下正确关闭应用程序的方法是关闭主窗体(如:Form.Close)。所有主窗体结束时仍存在的窗体需要手工关闭。Any window that's still present after the main message pump ends needs to be manually closed. 好的方法就是在应用程序调用Form.Close或Form.Dispose退出之前,关闭所有窗体。需要记住.NET框架的OnClosing()就是Win32平台下WM_CLOSE的托管版本,而不是WM_DESTROY。
Create a shortcut to your application somewhere under \windows\start menu\programs. When your application is launched from this shortcut an icon for your applications will appear the MRU list.
In order to display a non-full screen Form, ensure that the Form's FormBorderStyle property is set to FormBorderStyle.None. To center the form add the following code to the Form's FormLoad event handler: Set FormBorderStyle to FormBorderStyle.None then:
Once a Form is closed, it is disposed and therefore may be garbage collected by the system so it is not safe to attempt to show a closed Form. An alternative solution is to use Form.Hide and Form.Show to hide and display Forms respectively.
Multi-instancing is not supported by the .NET Compact Framework. The following code sample provides a solution that allows applications to be instanced rather than maximized when an application is launched but a running instance already exists.
Note: The following code is not supported and is not guaranteed to work on all versions of the OS, including future versions.
// C#
using System.Runtime.InteropServices;
using System.Reflection;
private void Form1_Load(object sender, System.EventArgs e)
{
this.Text = string.Format("Form {0}", new Random().Next());
}
[DllImport("CoreDll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("CoreDll")]
public static extern int SetWindowText(IntPtr hWnd, string lpString);
protected override void OnResize(EventArgs e)
{
Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
IntPtr hWnd = FindWindow("#NETCF_AGL_PARK_",
asm.GetModules()[0].FullyQualifiedName);
if (hWnd != IntPtr.Zero)
SetWindowText(hWnd, "#42");
base.OnResize (e);
}
'VB
Imports System.Runtime.InteropServices
Imports System.Reflection
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = String.Format("Form {0}", New Random().Next())
End Sub 'Form1_Load
<DllImport("CoreDll")> _
Public Shared Function FindWindow(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("CoreDll")> _
Public Shared Function SetWindowText(ByVal hWnd As IntPtr, _
ByVal lpString As String) As Integer
End Function
Protected Overrides Sub OnResize(ByVal e As EventArgs)
Dim asm As [Assembly] = System.Reflection.Assembly.GetExecutingAssembly()
Dim hWnd As IntPtr = FindWindow("#NETCF_AGL_PARK_", _
asm.GetModules()(0).FullyQualifiedName)
If hWnd.ToInt32() <> IntPtr.Zero.ToInt32() Then
SetWindowText(hWnd, "#42")
End If
MyBase.OnResize(e)
End Sub 'OnResize
这是一个已经知道的BUG,把右键菜单设置为分割线,将抛出NotSupportedException错误。这个问题是由于WinCE系统有个限制,不允许在已经加入右键菜单的菜单项设置为分隔符,并且菜单的父类是一个控件。在Visual Studio 2003种,设计器分割移动应用代码的方式和PC上的应用程序类似。这是导致此问题的原因。解决的方法是,把右键菜单单独放在InitilizeComponent方法外的地方。
Adding subnodes to all nodes is accomplished by iterating through all of the nodes in the TreeView and adding a new node to each.
//C#
foreach (TreeNode node in treeView1.Nodes)
{
node.Nodes.Add(new TreeNode("SubNode"));
}
'VB
Dim node As TreeNode
For Each node In treeView1.Nodes
node.Nodes.Add(New TreeNode("SubNode"))
Next node
The number of rows and columns in a DataGrid can be determined from the data source itself. For example:
//C#
DataSet ds = new DataSet();
int numRows = ds.Tables[0].Rows.Count;
int numCols = ds.Tables[0].Columns.Count;
'VB
Dim ds As New DataSet()
Dim numRows As Integer = ds.Tables(0).Rows.Count
Dim numCols As Integer = ds.Tables(0).Columns.Count
If the DataGrid is bound to the DataView you can also use DataView.Count.
The tab order of the controls in the .NET Compact Framework correspond directly to the order of the Controls in the Form.Controls collection. Therefore, GetNextControl can be implemented by determining the index of the specified Control and determing its neighbors in the collection.
//C#
public Control GetNextControl(Control ctl, bool forward)
{
int curIndex = this.Controls.IndexOf(ctl);
if (forward)
{
if (curIndex < this.Controls.Count)
curIndex++;
else
curIndex = 0;
}
else
{
if (curIndex > 0)
curIndex--;
else
curIndex = this.Controls.Count - 1;
}
return this.Controls[curIndex];
}
'VB
Public Function GetNextControl(ByVal ctl As Control, _
ByVal forward As Boolean) As Control
Dim curIndex As Integer = Me.Controls.IndexOf(ctl)
If forward Then
If curIndex < Me.Controls.Count Then
curIndex += 1
Else
curIndex = 0
End If
Else
If curIndex > 0 Then
curIndex -= 1
Else
curIndex = Me.Controls.Count - 1
End If
End If
Return Me.Controls(curIndex)
End Function 'GetNextControl
Similar to the NumericUpDown control, the maximum achievable value is the first empty row above the thumb. More specifically, from the editor properties, this equates to:
Icons support transparency, however, there is a known bug in Visual Studio .NET 2003 designer that creates incorrect code and makes icons non-transparent. A work around is to add an icon file to the ImageList outside of InitializeComponent and add the icon files to the project as content or embedded resources. The following code demonstrates this:
//C#
using System.Drawing;
using System.IO;
using System.Reflection;
// Loaded as content example
private void Form1_Load(object sender, System.EventArgs e)
{
this.imageList1.Images.Add(new Icon(File.Open("fullFileName.ico",
FileMode.Open)));
this.toolBar1.Buttons[0].ImageIndex = 0;
}
// Loaded as a resource example
private void Form1_Load(object sender, System.EventArgs e)
{
this.imageList1.Images.Add(new
Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(
".filename.ico")));
this.toolBar1.Buttons[0].ImageIndex = 0;
}
'VB
Imports System.Drawing
Imports System.IO
Imports System.Reflection
' Loaded as content example
Private Sub Form1_Load1(ByVal sender As Object, ByVal e As System.EventArgs)
Me.imageList1.Images.Add(New Icon(File.Open("fullFileName.ico", _
FileMode.Open)))
Me.toolBar1.Buttons(0).ImageIndex = 0
End Sub 'Form1_Load1
' Loaded as a resource example
Private Sub Form1_Load2(ByVal sender As Object, ByVal e As System.EventArgs)
Me.imageList1.Images.Add(New _
Icon([Assembly].GetExecutingAssembly().GetManifestResourceStream( _
".filename.ico")))
Me.toolBar1.Buttons(0).ImageIndex = 0
End Sub 'Form1_Load2
//C#
using System.Runtime.InteropServices;
public class MEMORYSTATUS
{
public uint dwLength;
public uint dwMemoryLoad;
public uint dwTotalPhys;
public uint dwAvailPhys;
public uint dwTotalPageFile;
public uint dwAvailPageFile;
public uint dwTotalVirtual;
public uint dwAvailVirtual;
}
[DllImport("CoreDll.dll")]
public static extern void GlobalMemoryStatus
(
MEMORYSTATUS lpBuffer
);
[DllImport("CoreDll.dll")]
public static extern int GetSystemMemoryDivision
(
ref uint lpdwStorePages,
ref uint lpdwRamPages,
ref uint lpdwPageSize
);
public void Test()
{
uint storePages = 0;
uint ramPages = 0;
uint pageSize = 0;
int res = GetSystemMemoryDivision(ref storePages, ref ramPages, ref pageSize);
MEMORYSTATUS memStatus = new MEMORYSTATUS();
GlobalMemoryStatus(memStatus);
}
'VB
Imports System.Runtime.InteropServices
Public Structure MEMORYSTATUS
Public dwLength As UInt32
Public dwMemoryLoad As UInt32
Public dwTotalPhys As UInt32
Public dwAvailPhys As UInt32
Public dwTotalPageFile As UInt32
Public dwAvailPageFile As UInt32
Public dwTotalVirtual As UInt32
Public dwAvailVirtual As UInt32
End Structure 'MEMORYSTATUS
<DllImport("coredll.dll")> _
Private Shared Sub GlobalMemoryStatus(ByRef ms As MEMORYSTATUS)
End Sub
<DllImport("CoreDll.dll")> _
Public Shared Function GetSystemMemoryDivision( _
ByRef lpdwStorePages As UInt32, _
ByRef lpdwRamPages As UInt32, _
ByRef lpdwPageSize As UInt32) As Integer
End Function
Public Shared Sub Test()
Dim storePages As UInt32
Dim ramPages As UInt32
Dim pageSize As UInt32
Dim res As Integer = GetSystemMemoryDivision(storePages, ramPages, pageSize)
Dim memStatus As New MEMORYSTATUS
GlobalMemoryStatus(memStatus)
End Sub 'Test
//C#
using System.Runtime.InteropServices;
[DllImport("CoreDll")]
public static extern IntPtr FindWindow(string className,string WindowsName);
[DllImport("CoreDll")]
public static extern bool ShowWindow(IntPtr hwnd,int nCmdShow);
const int SW_MINIMIZE = 6;
protected override void OnGotFocus(EventArgs e)
{
IntPtr hwnd = FindWindow(null, this.Text);
ShowWindow(hwnd, SW_MINIMIZE);
base.OnGotFocus(e);
}
'VB
Imports System.Runtime.InteropServices
<DllImport("CoreDll")> _
Public Shared Function FindWindow(ByVal className As String, ByVal WindowsName As String) As IntPtr
End Function
<DllImport("CoreDll")> _
Public Shared Function ShowWindow(ByVal hwnd As IntPtr,ByVal nCmdShow As Integer) As Boolean
End Function
Private Const SW_MINIMIZE As Integer = 6
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
Dim hwnd As IntPtr = FindWindow(Nothing, Me.Text)
ShowWindow(hwnd, SW_MINIMIZE)
MyBase.OnGotFocus(e)
End Sub 'OnGotFocus
//C#
[DllImport("coredll.dll"]
public static extern IntPtr GetCapture();
[DllImport("coredll.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
this.Text = "FindMe";
IntPtr hwnd1 = FindWindow(null, "FindMe");
this.Capture = true;
IntPtr hwnd2 = GetCapture();
this.Capture = false;
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function GetCapture() As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Me.Text = "FindMe"
Dim deskWin As IntPtr = FindWindow(Nothing, "FindMe")
Me.Capture = True
Dim hwnd As IntPtr = GetCapture()
Me.Capture = False
//C#
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceFrequency(ref Int64 lpFrequency);
[DllImport("CoreDll.dll")]
public static extern int QueryPerformanceCounter(ref Int64 lpPerformanceCount);
private void TestTimer()
{
System.Int64 freq = 0;
if (QueryPerformanceFrequency(ref freq) != 0)
{
System.Int64 count1 = 0;
System.Int64 count2 = 0;
if (QueryPerformanceCounter(ref count1) != 0)
{
System.Threading.Thread.Sleep(1200);
QueryPerformanceCounter(ref count2);
System.Int64 time_ms = (count2 - count1) * 1000 / freq;
}
}
}
'VB
<DllImport("CoreDll.dll")> _
Public Shared Function QueryPerformanceFrequency(ByRef lpFrequency As Int64) As Integer
End Function
<DllImport("coredll.dll")> _
Public Shared Function QueryPerformanceCounter(ByRef lpPerformanceCount As Int64) As Integer
End Function
Private Sub TestTimer()
Dim freq As System.Int64 = 0
If QueryPerformanceFrequency(freq) <> 0 Then
Dim count1 As System.Int64 = 0
Dim count2 As System.Int64 = 0
If QueryPerformanceCounter(count1) <> 0 Then
System.Threading.Thread.Sleep(1200)
QueryPerformanceCounter(count2)
Dim time_ms As System.Int64 = (count2 - count1) * 1000 / freq
End If
End If
End Sub 'TestTimer
using System.Runtime.InteropServices;
[DllImport("coredll.dll", SetLastError=true)]
int myFoo(...);
Foo(...)
{
int rc = myFoo(...);
if (rc == false)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Foo failed");
}
}
//C#
[DllImport("coredll.dll", SetLastError=true)]
public static extern long RegOpenKey(
IntPtr hkey,
string lpSubKey,
ref IntPtr hkeyResult
);
public long OpenMySubKey()
{
IntPtr hkey = IntPtr.Zero;
return RegOpenKey(HKEY_CLASSES_ROOT, "MySubKey", ref hkey);
}
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function RegOpenKey(ByVal hkey As IntPtr, ByVal lpSubKey As String, ByRef hkeyResult As IntPtr) As Long
End Function
Public Function OpenMySubKey() As Long
Dim hkey As IntPtr = IntPtr.Zero
Return RegOpenKey(HKEY_CLASSES_ROOT, "MySubKey", hkey)
End Function 'OpenMySubKey
//C#
unsafe
{
byte[] test = new byte[5];
fixed (byte* p = &test[0])
{
*p = 0xff;
}
}
也可以使用GCHandle指向对象。
//C#
using System.Runtime.InteropServices;
byte[] test = new byte[5];
GCHandle hObject = GCHandle.Alloc(test, GCHandleType.Pinned);
IntPtr pObject = hObject.AddrOfPinnedObject();
if(hObject.IsAllocated)
hObject.Free();
'VB
Imports System.Runtime.InteropServices
Dim test(4) As Byte
Dim hObject As GCHandle = GCHandle.Alloc(test, GCHandleType.Pinned)
Dim pObject As IntPtr = hObject.AddrOfPinnedObject()
If hObject.IsAllocated Then
hObject.Free()
End If
最后,可以使用LocalAlloc和Marshalling函数复制内存块得到数据块。
//C#
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalAlloc(uint uFlags, uint uBytes);
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("coredll.dll",SetLastError=true)]
public static extern IntPtr LocalReAlloc(IntPtr hMem, uint uBytes, uint fuFlags);
public const uint LMEM_FIXED = 0;
public const uint LMEM_MOVEABLE = 2;
public const uint LMEM_ZEROINIT = 0x0040;
byte[] test = new byte[5];
IntPtr p = LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT, (uint)test.Length);
if (p == IntPtr.Zero)
{
throw new OutOfMemoryException();
}
else
{
Marshal.Copy(test, 0, p, test.Length);
}
'VB
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalAlloc(ByVal uFlags As UInt32, ByVal uBytes As UInt32) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function LocalReAlloc(ByVal hMem As IntPtr, ByVal uBytes As UInt32, ByVal fuFlags As UInt32) As IntPtr
End Function
Public Const LMEM_FIXED As Integer = 0
Public Const LMEM_MOVEABLE As Integer = 2
Public Const LMEM_ZEROINIT As Integer = &H40
Dim test(4) As Byte
Dim p As IntPtr = LocalAlloc(Convert.ToUInt32(LMEM_FIXED Or LMEM_ZEROINIT), Convert.ToUInt32(test.Length))
If p.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
Else
Marshal.Copy(test, 0, p, test.Length)
End If
There are several issues to consider when determining the case of a MissingMethodException. When this exception occurs you should verify the following:
If targeting Pocket PC 2003 use Microsoft eMbedded Visual C++ 4.0.
If targeting Pocket PC 2000 or 2002 use Microsoft eMbedded Visual Tools 3.0
Verify that the parameters to the function match those of the original
A long in native code is typically 32-bits, whereas in the .NET Compact Framework it is 64-bits
Make sure that the function name is spelled correctly
Verify that the DLL is located correctly - Windows or executing folder of target device
Verify with DUMPBIN that the names of the functions were not mangled on exporting of the DLL (extern "C" fixes this). More information on this topic can be found in section "6.1. How do I call a function that is in a native DLL?" of this FAQ.
Verify that the DLL being imported is not dependant upon other DLLs. A missing dependant DLL will result in the MissingMethodException.
//C#
using System.Runtime.InteropServices;
public struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
[DllImport("coredll.dll")]
public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("coredll.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
// Set the clock ahead one hour
SYSTEMTIME st = new SYSTEMTIME();
GetSystemTime(ref st);
st.wHour = (ushort)(st.wHour + 1 % 24);
SetSystemTime(ref st);
'VB
Imports System.Runtime.InteropServices
Public Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMilliseconds As UInt16
End Structure
<DllImport("coredll.dll")> _
Public Shared Sub GetSystemTime(ByRef lpSystemTime As SYSTEMTIME)
End Sub
<DllImport("coredll.dll")> _
Public Shared Function SetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As UInt32
End Function
'Set the clock ahead one hour
Dim st As New SYSTEMTIME
GetSystemTime(st)
st.wHour = Convert.ToUInt16(((Convert.ToInt32(st.wHour) + 1)) Mod 24)
SetSystemTime(st)
The device can be soft reset through P/Invoking of the KernelIoControl function, as demonstrated in the code below. For more information on how to use the function and extend the functionality of this sample, refer to Visual Studio .NET Help.
Note: On Smartphone devices, this will only work if you are signed with a privileged certificate.
//C#
using System.Runtime.InteropServices;
public const uint FILE_DEVICE_HAL = 0x00000101;
public const uint METHOD_BUFFERED = 0;
public const uint FILE_ANY_ACCESS = 0;
public uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
{
return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
}
[DllImport("Coredll.dll")]
public extern static uint KernelIoControl
(
uint dwIoControlCode,
IntPtr lpInBuf,
uint nInBufSize,
IntPtr lpOutBuf,
uint nOutBufSize,
ref uint lpBytesReturned
);
uint ResetPocketPC()
{
uint bytesReturned = 0;
uint IOCTL_HAL_REBOOT = CTL_CODE(FILE_DEVICE_HAL, 15,
METHOD_BUFFERED, FILE_ANY_ACCESS);
return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0,
IntPtr.Zero, 0, ref bytesReturned);
}
private void Form1_Load(object sender, System.EventArgs e)
{
DialogResult r = MessageBox.Show
(
"Are you sure you want to reset?",
"Test",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2
);
if (r == DialogResult.Yes)
{
ResetPocketPC();
}
}
'VB
Public Const FILE_DEVICE_HAL As Integer = &H101
Public Const METHOD_BUFFERED As Integer = 0
Public Const FILE_ANY_ACCESS As Integer = 0
Public Function CTL_CODE( _
ByVal DeviceType As Integer, _
ByVal Func As Integer, _
ByVal Method As Integer, _
ByVal Access As Integer) As Integer
Return (DeviceType << 16) Or (Access << 14) Or (Func << 2) Or Method
End Function 'CTL_CODE
<DllImport("Coredll.dll")> _
Public Shared Function KernelIoControl _
( _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuf As IntPtr, _
ByVal nInBufSize As Integer, _
ByVal lpOutBuf As IntPtr, _
ByVal nOutBufSize As Integer, _
ByRef lpBytesReturned As Integer _
) As Integer
End Function
Function ResetPocketPC() As Integer
Dim bytesReturned As Integer = 0
Dim IOCTL_HAL_REBOOT As Integer = CTL_CODE(FILE_DEVICE_HAL, _
15, METHOD_BUFFERED, FILE_ANY_ACCESS)
Return KernelIoControl(IOCTL_HAL_REBOOT, IntPtr.Zero, 0, _
IntPtr.Zero, 0, bytesReturned)
End Function 'ResetPocketPC
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim r As DialogResult = MessageBox.Show( _
"Are you sure you want to reset?", _
"Test", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2)
If r = DialogResult.Yes Then
ResetPocketPC()
End If
End Sub 'Form1_Load
This is not supported with the current version of the .NET Compact Framework. You can, however, P/Invoke Pocket PC's notificaiton system to do this. Refer to the following for more information:
This sample demonstrates how to P/Invoke numerous useful native functions that are not directly available through the .NET Compact Framework. A test Form is provided that enumerates all available test procedures and allows the user to select and run them: http://msdn.microsoft.com/library/en-us/dnnetcomp/html/PInvokeLib.asp
//C#
using System.Reflection;
using System.IO;
// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;
// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);
// This adds a file name to the path
String splashImageName = Path.Combine(fullAppPath, "myfile.txt");
'VB
Imports System.IO
Imports System.Reflection
' This is the full directory and exe name
Dim fullAppName As String = [Assembly].GetExecutingAssembly().GetName().CodeBase
' This strips off the exe name
Dim fullAppPath As String = Path.GetDirectoryName(fullAppName)
' This adds a file name to the path
Dim splashImageName As String = Path.Combine(fullAppPath, "myfile.txt")
//C#
using System.Reflection;
Assembly asm = Assembly.GetExecutingAssembly();
Bitmap bmpSprite = new Bitmap(asm.GetManifestResourceStream("AssemblyName.FileName"));
'VB
Imports System.Reflection
Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
Dim bmpSprite As New Bitmap(asm.GetManifestResourceStream("AssemblyName.FileName"))
//C#
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
}
'VB
Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
e.Cancel = True
End Sub 'OnClosing
//C#
public class MyClass
{
protected byte[] m_data = null;
// uint uiDummy
// short sDummy
// This is a bit unsafe so you should throw an
// exception or assert if the byte array length is
// not 6. A safer but less memory efficient approach
// would be to set m_data = new byte[6] and then copy
//bytes to m_data.
public MyClass(byte[] bytes) {m_data = bytes;}
// Get/Set the uint
public uint uiDummy
{
get {return BitConverter.ToUInt32(m_data, 0);}
set
{
Buffer.BlockCopy(BitConverter.GetBytes(value),0,m_data,0,BitConverter.GetBytes(value).Length);
}
}
// Get/Set the short
public short sDummy
{
get {return BitConverter.ToInt16(m_data, 4);}
set
{
Buffer.BlockCopy(BitConverter.GetBytes(value),0,m_data,4,BitConverter.GetBytes(value).Length);
}
}
}
byte[] fromFile = {1,1,1,1,2,2};
MyClass myClass = new MyClass(fromFile);
uint test1 = myClass.uiDummy; // 0x1010101
short test2 = myClass.sDummy; // 0x202
myClass.sDummy = 0x0505; // Test setting the short
uint test4 = myClass.uiDummy; // 0x1010101
short test5 = myClass.sDummy; // 0x505
'VB
Public Class ByteClass
Protected m_data As Byte() = Nothing
' uint uiDummy
' short sDummy
' This is a bit unsafe so you should throw an exception
' or assert if the byte array length is not 6. A safer
' but less memory efficient approach would be to set
' m_data = new byte[6] and then copy bytes to m_data.
Public Sub New(ByVal bytes() As Byte)
m_data = bytes
End Sub 'New
' Get/Set the uint
Public Property uiDummy() As UInt32
Get
Return BitConverter.ToUInt32(m_data, 0)
End Get
Set(ByVal Value As System.UInt32)
Buffer.BlockCopy(BitConverter.GetBytes(Value),0,m_data,0,BitConverter.GetBytes(Value).Length)
End Set
End Property
' Get/Set the short
Public Property sDummy() As Short
Get
Return BitConverter.ToInt16(m_data, 4)
End Get
Set(ByVal Value As Short)
Buffer.BlockCopy(BitConverter.GetBytes(Value),0,m_data,4,BitConverter.GetBytes(Value).Length)
End Set
End Property
End Class 'ByteClass
Dim fromFile As Byte() = {1, 1, 1, 1, 2, 2}
Dim testClass As New ByteClass(fromFile)
Dim test1 As System.UInt32 = testClass.uiDummy ' 0x1010101
Dim test2 As Short = testClass.sDummy ' 0x202
testClass.sDummy = &H505 ' Test short
Dim test4 As System.UInt32 = testClass.uiDummy ' 0x1010101
Dim test5 As Short = testClass.sDummy ' 0x505
//C#
public void HandleMe(object o, EventArgs e) {...}
form.Invoke(new EventHandler(form.HandleMe));
'VB
Public Sub HandleMe(o As Object, e As EventArgs)
End Sub 'HandleMe
form.Invoke(New EventHandler(AddressOf form.HandleMe))
Although the following will compile, it will not work properly:
//C#
public delegate void MyHandler();
public void HandleMeBadly() {...}
form.Invoke(new MyHandler(form.HandleMeBadly));
'VB
Delegate Sub MyHandler()
Public Sub HandleMeBadly()
End Sub 'HandleMeBadly
form.Invoke(New MyHandler(form.HandleMeBadly))
Visual Studio .NET 2003帮助中的C# Programmer's Reference提供了在.net精简框架下使用不安全代码调用GetFileVersionInfo函数。 这个例子带来的问题是,这个函数是由OEM厂商决定的,并且不保证能返回正确结果。
使用Reflection可以获得程序集(Assembly)的版本:
//C#
using System.Reflection;
String ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
'VB
Imports System.Reflection;
Dim ver As String = [Assembly].GetExecutingAssembly().GetName().Version.ToString()
//C#
static void Main(string[] args)
{
// Do your thing here
}
'VB
Shared Sub Main(ByVal args() As String)
'Do your thing here
End Sub
在Visual Studio 2003中测试的时候,可以通过继承开发环境设置命令行的参数。主菜单,选择 Project->Project Name Properties. 在Property Pages对话框中,选择Configuration Properties->Debugging。在 Command Line Arguments 中输入字符串:text box.
//C#
System.IO.FileInfo fi = new System.IO.FileInfo("filename");
// remove readonly attribute
fi.Attributes -= System.IO.FileAttributes.ReadOnly;
System.IO.File.Delete("filename");
'VB
Dim fi As New System.IO.FileInfo("filename")
'remove readonly attribute
fi.Attributes -= IO.FileAttributes.ReadOnly
System.IO.File.Delete("filename")
The existence of a member can be determined at runtime using Reflection. The code below demonstrates how to use Reflection to access the "Width" property and the "GetPixel" method of a Bitmap object. In the case of the "Width" property, the code enumerates all "public" properties with a "get" component and then searches for one named "Width". The "GetPixel" sample demonstrates how one might use Reflection to call a known function where the parameter order is unknown. This sample is set up as if the author knows there is a method named "GetPixel" which takes a pixel location of x,y but does not know the order in which they appear in the parameter list. The sample enumerates the methods and searches for one named "GetPixel" and then enumerates the parameter list to determine whether the first or second parameter is named "X". Keep in mind that, due to the differences in hardware pixel formats, the value returned by GetPixel may be different from that set by SetPixel in this sample.
Reflection provides many powerful tools for determing functionality at runtime so for information, refer to the documentation regarding System.Type and the namespace System.Reflection.
//C#
using System.Reflection;
using System.Drawing;
Bitmap bm = new Bitmap(200, 100);
int width = 0;
// Explicitly set one pixel for testing
int x = 199;
int y = 20;
Color pixColor = Color.Black;
bm.SetPixel(x,y,Color.Magenta);
// Get the "Width" property
PropertyInfo[] propInfo =
bm.GetType().GetProperties(BindingFlags.GetProperty |
BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < propInfo.Length; i++)
{
if (propInfo[i].Name == "Width")
{
width = (int)propInfo[i].GetValue(bm, null);
break;
}
}
// Call the GetPixel method
MethodInfo[] methInfo = bm.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
for (int i = 0; i < methInfo.Length; i++)
{
if (methInfo[i].Name == "GetPixel")
{
ParameterInfo[] paramInfo = methInfo[i].GetParameters();
if (paramInfo.Length == 2)
{
object[] xy = new object[2];
if (paramInfo[0].Name == "x")
{
xy[0] = x;
xy[1] = y;
}
else
{
xy[1] = x;
xy[0] = y;
}
pixColor = (Color)methInfo[i].Invoke(bm, xy);
break;
}
'VB
Imports System.Reflection
Imports System.Drawing
Dim bm As New Bitmap(200, 100)
Dim width As Integer = 0
' Explicitly set one pixel for testing
Dim x As Integer = 199
Dim y As Integer = 20
Dim pixColor As Color = Color.Black
bm.SetPixel(x, y, Color.Magenta)
' Get the "Width" property
Dim propInfo As PropertyInfo() = _
bm.GetType().GetProperties((BindingFlags.GetProperty Or _
BindingFlags.Public Or BindingFlags.Instance))
Dim i As Integer
For i = 0 To propInfo.Length - 1
If propInfo(i).Name = "Width" Then
width = Fix(propInfo(i).GetValue(bm, Nothing))
Exit For
End If
Next i
' Call the SetPixel method
Dim methInfo As MethodInfo() = bm.GetType().GetMethods((BindingFlags.Public _
Or BindingFlags.Instance))
For i = 0 To methInfo.Length - 1
If methInfo(i).Name = "GetPixel" Then
Dim paramInfo As ParameterInfo() = methInfo(i).GetParameters()
If paramInfo.Length = 2 Then
Dim xy(1) As Object
If paramInfo(0).Name = "x" Then
xy(0) = x
xy(1) = y
Else
xy(1) = x
xy(0) = y
End If
pixColor = CType(methInfo(i).Invoke(bm, xy), Color)
Exit For
End If
End If
Next i
There is no Abort method to the Thread class in the .NET Compact Framework so a thread must be aborted by returning from the executing procedure. Typically, an application will notify threads of a closing event by setting a global variable. The main thread will then wait for worker threads to finish processing before closing the application. The following HOWTO article demonstrates how to accomplish this.
Windows Media Player is only available on the Pocket PC 2003 emulator. The Windows Media Player install package is for installation on a hardware device connected through ActiveSync and will not install to the emulator.
The Active Programs list on the Pocket PC enumerates all open Forms. To stop a Form from being displayed in the list, simply set the Form's caption to be an empty string. The following example shows how to keep only the application name in the list while a Form is displayed from within another Form:
//C#
string AppName = "MyApp";
Form1 form1 = new Form1();
this.Text = "";
form1.Text = AppName;
form1.ShowDialog();
this.Text = AppName;
'VB
Dim AppName As String = "MyApp"
Dim form1 As New Form1()
Me.Text = ""
form1.Text = AppName
form1.ShowDialog()
Me.Text = AppName
You can use Reflection to look up a control instance by its name. Here is some sample code:
//C#
private void Form1_Load(object sender, System.EventArgs e)
{
ComboBox c = (ComboBox)this.ControlFromName("combobox1");
c.Items.Add("1");
this.GetControls();
}
private Control ControlFromName(string name)
{
object o = this.GetType().GetField(name,
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase).GetValue(this);
return((Control)o);
}
private void GetControls()
{
System.Reflection.FieldInfo[] fis = this.GetType().GetFields
(
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase
);
foreach(System.Reflection.FieldInfo fi in fis)
{
if (fi.GetValue(this) is Control)
MessageBox.Show(fi.Name);
}
}
'VB
Private Function ControlFromName(ByVal name As String) As Control
Dim o As ObjectDim o As Object
o = Me.GetType().GetField(name, Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase).GetValue(Me)
Return (CType(o, Control))
End Function
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As ComboBox
c = CType(ControlFromName("_combobox1"), ComboBox)
c.Items.Add("1")
Me.GetControls()e.GetControls()
End Sub
Private Sub GetControls()
Dim fis As System.Reflection.FieldInfo()
fis = Me.GetType().GetFields(Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase)
For Each fi As Reflection.FieldInfo In fis
If TypeOf (fi.GetValue(Me)) Is Control Then
MessageBox.Show(fi.Name)
End Ifnd If
Next
End Sub
Create a modeless dialog that behaves like a model dialog.
//C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
public interface IModelessDialogCallback
{
void DialogResultCallback(DialogResult result);
}
public class Test : System.Windows.Forms.Form, IModelessDialogCallback
{
ModelessDialog dlg;
Button bShow;
int counter = 0;
protected override void OnLoad(EventArgs e)
{
this.Text = "Modal(less) Dialog Example";
this.bShow = new Button();
this.bShow.Parent = this;
this.bShow.Bounds = new Rectangle (10, 10, 150, 30);
this.bShow.Text = "Show Dialog";
this.bShow.Click += new EventHandler(this._Click);
this.dlg = new ModelessDialog(this);
}
private void _Click(object o, EventArgs e)
{
this.Enabled = false;
this.bShow.Text = "waiting for dlg";
dlg.Show();
}
public void DialogResultCallback(DialogResult result)
{
MessageBox.Show("dialog returned: " + (result == DialogResult.OK ? "OK" : "Cancel"));
this.Enabled = true;
this.bShow.Text = "Show Dialog:" + ++counter;
}
public static void Main()
{
Application.Run(new Test());
}
}
public class ModelessDialog : Form
{
IModelessDialogCallback parent;
Button bOK, bCancel;
public ModelessDialog(IModelessDialogCallback parent)
{
this.parent = parent;
this.Text = "Modeless Dialog";
this.bOK = new Button();
this.bOK.Parent = this;
this.bOK.Bounds = new Rectangle (10, 10, 150, 30);
this.bOK.Text = "OK";
this.bOK.Click += new EventHandler(this._Click);
this.bCancel = new Button();
this.bCancel.Parent = this;
this.bCancel.Bounds = new Rectangle (10, 50, 150, 30);
this.bCancel.Text = "Cancel";
this.bCancel.Click += new EventHandler(this._Click);
}
private void _Click(object o, EventArgs e)
{
this.Hide();
this.parent.DialogResultCallback(o == this.bOK ? DialogResult.OK : DialogResult.Cancel);
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
this.parent.DialogResultCallback(DialogResult.Cancel);
}
}
'VB
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Collections
Imports System.ComponentModel
Public Interface IModelessDialogCallback
Sub DialogResultCallback(result As DialogResult)
End Interface IModelessDialogCallback'
Public Class Test
Inherits System.Windows.Forms.Form
Implements IModelessDialogCallback
Private dlg As ModelessDialog
Private bShow As Button
Private counter As Integer = 0
Protected Overrides Sub OnLoad(e As EventArgs)
Me.Text = "Modal(less) Dialog Example"
Me.bShow = New Button()
Me.bShow.Parent = Me
Me.bShow.Bounds = New Rectangle(10, 10, 150, 30)
Me.bShow.Text = "Show Dialog"
AddHandler Me.bShow.Click, AddressOf Me._Click
Me.dlg = New ModelessDialog(Me)
End Sub 'OnLoad
Private Sub _Click(o As Object, e As EventArgs)
Me.Enabled = False
Me.bShow.Text = "waiting for dlg"
dlg.Show()
End Sub '_Click
Public Sub DialogResultCallback(ByVal result As DialogResult) Implements _
IModelessDialogCallback.DialogResultCallback
MessageBox.Show(("dialog returned: " + IIf(result = DialogResult.OK, "OK", "Cancel")))
Me.Enabled = True
counter += 1
Me.bShow.Text = String.Format("Show Dialog: {0}", counter)
End Sub 'DialogResultCallback
Public Shared Sub Main()
Application.Run(New Test)
End Sub 'Main
End Class 'Test
Public Class ModelessDialog
Inherits Form
Private myParent As IModelessDialogCallback
Private bOK, bCancel As Button
Public Sub New(parent As IModelessDialogCallback)
Me.myParent = parent
Me.Text = "Modeless Dialog"
Me.bOK = New Button()
Me.bOK.Parent = Me
Me.bOK.Bounds = New Rectangle(10, 10, 150, 30)
Me.bOK.Text = "OK"
AddHandler Me.bOK.Click, AddressOf Me._Click
Me.bCancel = New Button()
Me.bCancel.Parent = Me
Me.bCancel.Bounds = New Rectangle(10, 50, 150, 30)
Me.bCancel.Text = "Cancel"
AddHandler Me.bCancel.Click, AddressOf Me._Click
End Sub 'New
Private Sub _Click(o As Object, e As EventArgs)
Me.Hide()
Me.myParent.DialogResultCallback(IIf(o Is Me.bOK, DialogResult.OK, DialogResult.Cancel))
End Sub '_Click
Protected Overrides Sub OnClosing(e As CancelEventArgs)
e.Cancel = True
Me.Hide()
Me.myParent.DialogResultCallback(DialogResult.Cancel)
End Sub 'OnClosing
End Class 'ModelessDialog
There are two primary methods for rounding numbers:
Convert.ToInt32
Cast or Fix (C# or VB)
Convert.ToInt32 automatically handles rounding, where remainders of .5 and greater cause the number to be rounded up. Casting or using Fix requires adding .5 to the number to ensure that it will round properly, as these methods simply remove the remainder.
Profiling on the emulator and a Compaq iPAQ H3600 series device yielded the following results for 1 million operations of each method, where num is a float set to 3.6F:
Emulator
iPAQ
Operation
Debug (ms)
Release (ms)
Debug (ms)
Release (ms)
C#: Convert.ToInt32(num)
1321
1109
6264
6283
C#: (int)(num + .5F)
170
49
1479
59
VB: Convert.ToInt32(num)
1218
1232
6531
6517
VB: Fix(num + .5F)
3873
3677
18144
17955
Thus, by examining the release build results for the device, it can be concluded that on the current generation of devices it is most efficient to use casting in C# and Convert.ToInt32 in VB. In C#, casting proved to be over 106 times faster, whereas in VB, Convert.ToInt32 was nearly 3 times faster.
//C#
float temp = 3.6f;
int rounded1 = (int)(temp + .5f);
int rounded2 = Convert.ToInt32(temp);
'VB
Dim temp As Single = 3.6F
Dim rounded1 As Integer = Fix(temp + .5F)
Dim rounded2 As Integer = Convert.ToInt32(temp)
There is a known bug with using Control.Invoke while a modal dialog is displayed through ShowDialog. Download the latest .NET Compact Framework Service Pack to fix this:
Culture information can be retrieved through Reflection:
//C#
using System.Reflection;
using System.Globalization;
Assembly asm = Assembly.GetExecutingAssembly();
CultureInfo ci = asm.GetName().CultureInfo;
'VB
Imports System.Reflection
Imports System.Globalization
Dim asm As Assembly = Assembly.GetExecutingAssembly()
Dim ci As CultureInfo = asm.GetName().CultureInfo
Using Digest authentication instead of Basic or NTLM authentication is the recommended solution. Digest authentication is supported by the .NET Compact Framework.
There is a known issue with setting Blocking to false on a blocking socket. There are three paradigms when programming with sockets (both managed and native) and it is strongly recommended that developers use asynchronous I/O functions as a solution to this problem. Select is very inefficient in managed code, and the cost of the regular case exception is also high. The three paradigms are detailed below, with the last being the preferred method.
Use blocking calls on blocking sockets (i.e. send(), recv(), etc). This is the simplest method, and probably the most commonly used. The functions perform their operation synchronously, tying up the currently executing thread. This is most acceptable for clients, and multithreaded servers (Microsoft does not recommend using one thread per client because threads are very expensive on Windows). Note that though the operations may block for an unspecified period of time (i.e. until the operation can be completed), they will most often complete immediately.
Use non-blocking sockets in conjunction with select or poll. Use the same "blocking" functions, but put the socket in a special mode that prohibits it from blocking. In the cases when the function would block, it returns an error code (in managed code, this throws an exception). You can then use poll or select to wait until a point in time at which an operation would complete (select allows you to manage more than one socket, so you can handle multiple clients on only one thread), and then perform the operation. At the time these calls were created, there were no threads in operating systems, so this was the only way to do things. Using this mechanism has by far the absolute worst performance you could possibly get from socket programming. This problem is exacerbated in managed code, since you now expect an exception to be thrown, which is an even bigger performance hit.
Use asynchronous I/O functions on the socket. This is your best all around solution. It allows you to perform operations asynchronously, and you get notified by callbacks. In general, the performance is better than using non-blocking I/O with select, and this is the recommendation for how to do things (at least, in native code).
This is a known issue in the version 1 of the .NET Compact Framework. To work around the issue, you can P/Invoke setsockopt with SO_REUSEADDR set to true.
There is a known issue in the .NET Compact Framework. You can work around this by setting HttpWebRequest.AllowStreamWriteBuffering to True and commenting out your setting of the ContentLength.
An error of type SQLCeException with a Native Error Code of 27750 occurs. This error corresponds to "Cannot load sscemw20.dll or ssceca20.dll is missing or not registered."
Resolution Load the library at the start of the application, even if you do nothing with it. To load the library, you can load the SqlCeEngine object.
This document provides an overview of the development tasks that were unique to the Pocket TaskVision sample application. Pocket PC devices have much lower storage capacities, memory and lower-end processors than desktop systems—storage and performance considerations are mentioned throughout the document. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/wnf_pkttaskvision.asp
Learn how to create .NET Compact Framework-based games. Learn the key requirements for writing games targeting small devices and see that the .NET Compact Framework can handle them with ease. Include advanced performance-tuning techniques that you can use to push the limits of your game. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfgaming.asp
Learn how to use a managed DateTimePicker class in your Pocket PC projects. The .net精简框架 provides managed classes for most controls but the DateTimePicker class is not supported. Wrapper classes that P/Invoke the native control can be found on the Web, but this sample provides a purely managed DateTimePicker class that you can use in your Pocket PC projects. The class and sample application are provided in C# and Visual Basic .NET. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfdatetimepicker.asp
This article discusses the Pocket PC Signature sample application. The sample includes a client that runs on the Pocket PC that sends signature data over TCP sockets to a server that is running on the desktop. Data is encrypted and decrypted using the cryptography services. http://msdn.microsoft.com/library/en-us/dnnetcomp/html/PPCSignatureApp.asp
Symptom: When a Windows CE device is connected through ActiveSync, deployment and debugging (F5 and Ctrl-F5) fail with the typical error message "There were deployment errors. Continue?"
Cause: Due to the fact that ActiveSync 3.5 and 3.6 do not provide accurate information about the device instruction set, ConMan (Visual Studio .NET 2003 component for device connectivity) cannot use the information returned by ActiveSync to map a Windows CE device to the right .NET Compact Framework target (MIPSII, ARMV4 etc).
Workaround: Install and run the separately provided Windows CE Configuration Add-In. For ARMV4 devices, an alternative is to select Pocket PC Device at the beginning of the deployment.
Symptom: After running the Windows CE Device Configuration Add-In, the user can deploy without debugging (Ctrl-F5) to an ActiveSync connected CEPC (or any x86 based Windows CE device) but cannot debug (F5). Non-x86 based Windows CE devices do not have this problem.
Cause: Debugging Windows CE Emulators (also x86 based) uses a TCP port that conflicts with the one used by ActiveSync for debugging x86 based devices. To support Emulator debugging, it is necessary to disable debugging for CEPC and other x86 devices by default.
Workaround: The following workaround will allow debugging of x86 devices but disable debugging with the Windows CE Emulator. The workaround requires the files WinCEx86Device.reg, and ProxyPorts.reg which can be found in the SDK utilities directory: "<VSROOT>\CompactFrameworkSDK\WinCE Utilities\WinCE Proxy Ports Reg".
Import WinCEx86Device.reg to the registry. Now you will be able to debug x86 devices, but debugging with the Windows CE Emulator will fail.
To restore Emulator debugging, remove the registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\ProxyPorts" and then import ProxyPorts.reg.
Symptom: After F5, all the files including the application executables and .net精简框架 cab files are copied to the device, and the IDE reports success on launching the application, but nothing is launched on the device. If you check the \windows folder of the device, toolhelp.dll does not exist.
Cause: Some device images may not include toolhelp.dll which is required by SDE debugging.
Workaround: Copy toolhelp.dll from Windows CE SDK to the \windows folder of the device. This file can be found in the corresponding target directory for each device.
For example, for an ARM device: "<VSROOT>\CompactFrameworkSDK\WinCE Utilities\ToolHelp\WinCE4\armv4".
Cause: It is a known issue that the .NET Framework method System.Net.Dns.GetHostName throws an exception when there are more than 50 network protocols installed on the current machine. The method is called by ConMan and the exception causes failure of the ConMan transport initialization.
Workaround: Uninstall the network protocols that are not actually needed. One way to do this is to go to Device Manager (Right-click on "My Computer", select Properties->Hardware and press the "Device Manager" button) and remove unused entries under "Network adapters". Another way is to uninstall applications that have installed protocols. Those applications can be found from the output of EnumProtocles.exe.
Symptom: The "deployment error" message box occurs on Ctrl-F5 or F5 after ActiveSync has been uninstalled. Uninstalling ActiveSync typically happens when the user upgrades ActiveSync, e.g. from version 3.5 to 3.6.
Cause: ConMan relies on some registry values under "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\ProxyPorts" for deployment and debugging through ActiveSync. These registry values are removed when ActiveSync is uninstalled.
The device may need to be un-cradled/re-cradled or soft rebooted to make the fix take effect. It is recommended that users never uninstall ActiveSync. To upgrade ActiveSync, you should directly install the new version which will correctly override the old one.
Symptom: When the machine name or the logon user name contains any character that is not on the current code page or below ASCII value 0x20, deployment to devices fails while deployment to the Emulator works fine.
Cause: ConMan security authentication internally uses the machine name and user name in the ASCII form which is not able to handle characters in the categories described above.
Workaround: Do not use characters described above in the machine name or the user name. If the user is not clear what characters belong to those categories, it is always safe to use just English alphabetic letters and numbers.
Symptom: If the current logon user is not in the Administrators group, deployment to devices always fails even though the user is in the Visual Studio Developers group and the Debugger Users group. On the other hand deployment/debugging to the Emulator works fine.
Cause: During device deployment, the ConMan code internally opens a file in a mode that requires administrator privileges.
Workaround: Add the current user to the Administrators group or limit the deployment target to the Emulator only.
Symptom: When deploying an application to a Pocket PC 2002/2003 device or Emulator, the deployment fails with an output message similar to "Could not write to output file 'SmartDeviceApplication1.exe' - Sharing violation". This usually happens after the same application has previously deployed to the same device or Emulator.
Cause: By default the X button on an application for Pocket PC 2002 or 2003 (not 2000) does not close the process. It only "minimizes" the application window. When the user tries to deploy the application again, the "minimized" instance will cause the sharing violation and fail the deployment. Another possible cause is that the user has forcibly terminated a debug session in the middle.
Workaround: Make sure the application is really closed on the device or Emulator when deploying it again. To see running processes, go to Start->Settings->Memory->Running Programs on the Pocket PC. In case it is intended to have a button to really close an application, create such a button explicitly or set Windows Form's property Minimize Box to False so that the X button will become an OK button for closing the application. If the cause was the manual termination of a debug session, the device or the Emulator may need to be soft rebooted.
Symptom: When the Emulator is used for the first time after the installation of Visual Studio .NET 2003 on a .NET Server machine, a message box pops up titled "Security Alert – Driver Installation". The text of the message starts with "The driver software you are installing for: Virtual PC Application Services has been signed with Authenticode(TM) technology. The publisher's identity has been identified, and the driver has not been modified since it was published". Near the end, it says "However, this driver cannot be tested by Microsoft Windows Hardware Quality Labs to verify its compatibility with this version of Windows. … Do you trust this publisher and want to install the driver?"
Cause: Visual Studio .NET 2003 installs a Virtual PC Application Services Emulator driver which does not pass the validation of .NET Server's Driver Validation system.
Workaround: When the alert message comes up, click the Yes button. Otherwise the Emulator will not work.
Symptom: When the Emulator is being started, a message box appears titled "Emulator for Windows CE" with the text "One or more files from the Emulator for Windows CE installation is missing. Please reinstall Emulator for Windows CE and try again." The connection or deployment to the Emulator will fail after the message box.
Cause: A typical cause is that the Emulator driver is not installed or is corrupted.
Workaround: Go to "Device Manager" (Right-click on "My Computer", select Properties->Hardware and press the "Device Manager" button) and check whether "Virtual PC Application Services" is installed under the System Devices group. If the driver is not installed, install it by running "<VSROOT>\CompactFrameworkSDK\ConnectionManager\Bin\DriverInstall\Smart Devices Emulator.msi".
Note: Verify that the "Virtual PC Application Services" appears after running the msi file. If not then reboot the PC and attempt the installation again.
Symptom: The startup of the Emulator is slow and the deployment eventually fails with a connection error message. The startup mainly includes starting the Emulator image and downloading the .net精简框架 files. The failure happens more often on low-end machines when the Emulator is used for the first time or after "turn off" instead of "saving state" was used on the previous Emulator shutdown.
Cause: Due to some performance issues in the Emulator, its startup may take too long to finish within the ConMan timeout period.
Workaround: If possible, always choose "saving state" instead of "turn off" when closing the Emulator so that the startup will be much faster when the Emulator is started next time. Or before the deployment, pre-start the Emulator by clicking on the "Connect to Device" button which is located next to the Emulator name on the menu bar. Note that the first deployment will still fail after the pre-start. (See details in the issue about this.) In addition, hovering the mouse over the Emulator image may speed up the Emulator startup.
Symptom: When the Emulator is started with the "Connect to Device" button, the first deployment attempt always fails.
Workaround: There is no workaround at this point for the failure of the first deployment after the Emulator is started with the button, but the subsequent deployment attempts should succeed.
Symptom: When a machine has no active network connection, debugging with the Emulator (F5) does not work, while deployment without debugging (Ctrl-F5) works fine.
Cause: Emulator debugging relies on TCP transport.
Workaround: Install the Microsoft Loopback Adapter on the machine.
To install the the Microsoft Loopback Adapter in Microsoft Windows XP
Open Control Panel, choose Add Hardware, and then choose Next.
Choose Yes, I have already connected the hardware, and then choose Next.
From the Installed hardware list, select Add a new hardware device, and then choose Next.
Choose Install the hardware that I manually select from a list (Advanced), and then choose Next.
From the Common hardware types list, select Network adapters, and then choose Next.
From the Manufacturer list, select Microsoft.
From the Network Adapter list, select Microsoft Loopback Adapter, and then choose Next.
Choose Next, and then choose Finish.
To install the Microsoft Loopback Adapter in Microsoft Windows 2000
Open Control Panel, choose Add/Remove Hardware, and then choose Next.
Choose Add/Troubleshoot a device, and then choose Next.
From the Devices list, select Add a new device, and then choose Next.
Choose No, I want to select the hardware from a list, and then choose Next.
From the Hardware types list, select Network adapters, and then choose Next.
From the Manufacturers list, select Microsoft.
From the Network Adapter list, select Microsoft Loopback Adapter, and then choose Next.
Symptom: When the Emulator image is starting up, it stays as a black screen forever. This happens when the Emulator is configured to use at least one COM port.
Cause: By default, the Emulator is not assigned a COM port but the user may change the configuration to assign a COM port to the Emulator. When a COM port assigned to the Emulator is used by ActiveSync or any other running application, the Emulator image loading will fail.
Workaround: Remove all of the COM ports assigned to the Emulator or change them to those not used by ActiveSync or any other running application. To remove or change the COM port configurations in Visual Studio .NET 2003, go to Tools/Options/Devices, choose an Emulator in the Devices window and click the Configure button.
Symptom: When the eMbedded Visual Tools (Platform Builder) Emulator is running, Visual Studio .NET 2003 Emulator deployment fails, and vice versa. Sometimes even after the Visual Studio .NET 2003 Emulator is closed, the eMbedded Visual Tools Emulator still does not work.
Cause: The Visual Studio .NET 2003 Emulator and the eMbedded Visual Tools Emulator internally use two different instances of Emulator.exe which are not compatible with each other. Because they share the same executable name and the exe is a single instance application, while Emulator.exe for eMbedded Visual Tools is running, the one for Visual Studio .NET 2003 cannot be started, and vice versa. Another issue is that when the Visual Studio .NET 2003 Emulator user interface is closed, emulator.exe does not close until the hosting Visual Studio IDE instance is closed. In that case, the eMbedded Visual Tools Emulator cannot be started even though there is no Emulator running visually.
Workaround: Do not use the two Emulators at the same time. If an Emulator cannot be started, check the Task Manager and close any running emulator.exe processes.
Symptom: When a device does not have an ActiveSync connection with the Visual Studio machine but has a valid TCP connection, deployment does not work.
Cause: ConMan relies on an ActiveSync connection to perform some initial settings between the Visual Studio machine and the device. If no ActiveSync connection is available, the setting will have to be performed manually.
Workaround: Use the separately provided Keyman tool to perform the initial device settings with the following steps:
Choose the correct flavor of Keyman.exe (e.g. ARMV4 or SH4, etc) and place it on the device. Launch Keyman and select "FILE->CONNECT". Keyman is now waiting for the VS machine to connect to it via TCP/IP. After the VS machine has connected, and then disconnected, Keyman on the device will automatically exit.
On the VS machine, copy CryptoAPIWrapper.dll from <VSROOT>\ CompactFrameworkSDK\ConnectionManager\bin to the directory of KeymanDesktop.exe. Launch KeymanDesktop.exe.
Click "Save" to save your settings in the current directory where KeymanDesktop.exe is running. The saved defaults will be loaded next time the exe is launched.
Enter the IP Address of the device
Use the recommended value for the Device Port.
Enter the path of the "Target" directory where the ConmanClient bits such as ConManClient.exe reside. The typical value is \ CompactFrameworkSDK\ConnectionManager\Target.
Use the Connect button to connect.
Click on "prep device" after you have connected. On success, you will see a dialog telling you everything went OK.
Use the Disconnect button to disconnect. When you disconnect, the device side bits will automatically exit.
Launch ConManClient.exe on the device. First you must select the flavor of the target device. Also, you must select the location of your "Target" directory in Visual Studio.
Cause Bug in the connection tool after selecting ARMV4I CPU type
Resolution Configure tool for ARMV4T CPU type:
In Visual Studio, on the Tools menu, click "Select Windows CE Device CPU". Note: this menu item is installed by the Windows CE Utilities for Visual Studio .NET 2003 Add-on Pack
Change the device architecture to ARMV4T
Click Configure, then click Close. Restart Visual Studio if prompted
No. The .NET Compact Framework is serviced through operating system updates at the carrier's / OEM's discretion. Therefore, .NET Compact Framework will generally drop as part of larger Windows Mobile for Smartphone operating system updates.
The input mode can be set using GetFocus and SendMessage APIs according to the code below:
'VB
Imports System.Runtime.InteropServices
Public Const EM_SETINPUTMODE As Integer = &HDE
Public Const EIM_SPELL As Integer = 0
Public Const EIM_AMBIG As Integer = 1
Public Const EIM_NUMBERS As Integer = 2
<DllImport("coredll.dll")> _
Public Shared Function GetFocus() As IntPtr
End Function
<DllImport("coredll.dll")> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal Message As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
'Sample use setting TextBox to number input
Private Sub txtAmount_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtAmount.GotFocus
Dim hWnd As IntPtr
hWnd = Me.GetFocus()
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS)
txtAmount.SelectionStart = txtAmount.Text.Length
End Sub
//C#
using System.Runtime.InteropServices;
public const uint EM_SETINPUTMODE = 0xDE;
public const uint EIM_SPELL = 0;
public const uint EIM_AMBIG = 1;
public const uint EIM_NUMBERS = 2;
[DllImport("coredll.dll")]
public static extern IntPtr GetFocus();
[DllImport("coredll.dll")]
public static extern int SendMessage(IntPtr hWnd,
uint Message, uint wParam, uint lParam);
// Sample use setting TextBox to number input
private void Form1_Load(object sender, System.EventArgs e)
{
txtAmount.GotFocus +=
new System.EventHandler(txtAmount_GotFocus);
}
private void txtAmount_GotFocus(object sender, System.EventArgs e)
{
IntPtr hWnd;
hWnd = GetFocus();
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS);
txtAmount.SelectionStart = txtAmount.Text.Length;
}