.net精简框架白皮书 MSDN移动和嵌入式开发中心:
http://msdn.microsoft.com/mobility/understanding/articles/default.aspx
1.11. .net精简框架的文档在哪里?
http://msdn.microsoft.com/library/en-us/dv_evtuv/html/etconNETCompactFramework.asp
1.12. 怎样设置才不会在每次调试的时候把所涉及的文件都拷贝到设备上?
在Visual Studio .NET 2003, 右键点击文件并选择属性,把Build的属性设置为None.文件将不再被拷贝.如果文件修改过了,需要重新拷贝,把Build属性改为Content即可.
1.13. 怎样设置模拟器的属性?
在Visual Studio .NET 2003菜单中,选择 Tools->Options. 打开 Device Tools 文件夹,并选择 Devices. 您应该看到一个显示设备列表的对话框.选择您想修改的设备,按Configuration按钮.
现在您应该看到一个包含多个TAB并可以修改设置的对话框,如设置内存和屏幕大小等.
1.14. 怎么调试.net精简框架的应用程序?
微软.net精简框架完全集成在Visual Studio .NET 2003中,在Visual Studio .NET 2003下调试.net精简框架的应用和在Visual Studio .NET 2003下调试其他应用一样。在一个单独的设备上调试程序和在模拟器中调试程序需要注意不同的地方。查看下面文章可以获得更多调试.net精简框架的经验。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/CompactFXDebug.asp
1.15. 怎样开始使用Visual Basic .NET开发.net精简框架的程序?
了解怎样使用Smart Device Extensions (SDE)编写只能设备的应用。这篇文章详细介绍了在.net精简框架上开发、调试、发布的完整过程,并描述了和在.net框架上开发的区别。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/sdeforvb.asp
1.16. 怎样才能把.net精简框架安装到装配集中(GAC)?
http://msdn.microsoft.com/library/en-us/dncfhowto/html/HOWTOGAC.asp
1.17. 当从Visual Studio .NET 2003中发布程序到设备上的时候,怎样处理"Sharing Violation"?
This article illustrates the cause of a sharing violation that prevents the deployment of your application from Visual Studio .NET 2003, and gives instructions on how to work around it.
http://msdn.microsoft.com/library/en-us/dncfhowto/html/HOWTOSharingviolation.asp
1.18. 什么是common language runtime (CLR)?
.net精简框架提供的运行环境叫做CLR,它使程序运行,并且是开发更加容易。以下文章将使您了解更多关于CLR:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcommonlanguageruntimeoverview.asp
1.19. 什么是Global Assembly Cache (GAC)?
每一台装了.net环境的机器都会有一个本机器范围内的缓冲,这就是GAC。GAC中存放的装配会被这台计算机上的一些应用程序共享 。这篇文章将告诉你更多关于GAC的信息:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconglobalassemblycache.asp
1.20. 在.net精简框架下内存管理是怎样的?
自动内存管理是CLR提供的一项服务。CLR的垃圾回收器会管理应用程序内存的分派和回收,以下文章将为您解释:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconautomaticmemorymanagement.asp
1.21. 当我的网络协议数超过50个的时候,为什么不能向发布程序?
请查看本文章的 "11.4. 为什么当网络协议数超过50个的时候,就不能向设备发布程序?" 部分。
1.22. 为什么会有跨平台代码?
.net精简框架和它的执行引擎是.net框架和CLR的子集。缺省的强名称策略,针对.net精简框架编译的程序同样可以在完整的.NET框架上运行,但会有一些重要的异常信息:
- .net精简框架装配使用和.net框架不用的强名称签名,所以CLR可以区分它们。
- 在未来发布的.NET框架和CLR的绑定策略中将使用.NET框架装配代替兼容.NET精简框架参考。这样,在普通情况下,不需要重新连接就能重用组件。 例如:如果您的组件只引用了.net精简框架的 System 和 System.NET 类库,它则不需要重新连接就适合在.net精简框架和完整的.NET框架下运行。
- 如果你引用了.net精简框架特有的功能,如PocketPC特有的用户界面,程序将不能在完整的.net框架上运行。
- 如跨平台的中间组件的开发和发布,微软认为丰富的客户端应用应该利用智能设备特有的功能为用户带来更好的体验。这一味着好的图形用户界面基本上是客户端特有的。
- 尽管微软花费很大精力为不同设备和功能划分了不同的命名空间和装配以避免装配冲突,但在1.0的版本里还是会有不能处理的兼容性问题。在这种情况下,在.net框架上不经意地使用了设备专有的功能将会导致程序在运行时的异常,而不是载入时的异常。
1.23. 这些ARM指令代表什么?
XScale支持ARM v5指令集,同时也向下兼容ARM v4指令集。这里有三种情况:
- ARMv4 -> 只支持32位ARMv4指令
- ARMv4T -> 'T' 表示 Thumb. Thumb 是ARM16位指令模式
- ARMv4I -> 'I' 表示 Interworking. 允许32位和16位指令共存
对于其他ARM处理器:
- StrongARM (SA1110) -> 只支持ARMv4指令
- ARM920T, etc -> 支持所有三种情况
.net精简框架支持三种ARM代码
- ARMv4 for PocketPC 2000 and Pocket PC 2002. 支持所有ARM设备,包括XScale。发布到设备的CAB文件名包含有"arm"字符。
- ARMv4 for Windows CE.NET. 支持由PlatformBuilder使用ARMv4核心编译的Windows CE.NET设备。Pocket PC 2003支持这种代码。发布到设备的CAB文件名包含有"armv4"字符。
- ARMv4T or ARMv4I for Windows CE.NET. 支持由PlatformBuilder使用ARMv4T 或 ARMv4I核心编译的Windows CE.NET设备。发布到设备的CAB文件名包含有"armv4T"字符。
1.24. 怎样写注册表?
Visual Studio .NET 的发布中没有包括Windows CE的远程注册表编辑器。要修改注册键值,可以采用以下方法:
- Microsoft Embedded Visual Tools Remote Registry Editor
- Microsoft Windows CE Platform Builder Remote Registry Editor
- PHM Pocket PC Registry Editor (共享软件,很容易在网上找到)
1.25. How can I prevent .CAB files from being deleted after installation?
Automatic deletion of .CAB files can be prevented by setting the property of the .CAB file(s) to Read Only.
1.26. How can I determine the version of the .NET Compact Framework that is installed on a device?
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.
RTM = 1.0.2268.0
SP1 = 1.0.3111.0
SP2 Recall = 1.0.3226.0
SP2 Beta = 1.0.3227.0
SP2 Final = 1.0.3316.0
To determine the version programmatically you can use System.Environment.Version.ToString().
1.27. How do I copy files to the emulator?
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":
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconfileproperties.asp
Step by step instructions for adding a "Content" file to a smart device project:
- Open or create a smart device project,
- On the View menu, click Solution Explorer,
- In Solution Explorer, right-click on your project, point to Add, and click Add Existing Item. Browse to and add the desired file to the project.
- Right-click on the file that you added, in solution explorer, and click Properties,
- Set the Build Action property to "Content", if it is not already set.
1.28. Why does installation of the PocketPC 2002 SDK fail?
Symptom: The Pocket PC 2002 SDK installer hangs while "registering components."
Cause: An unregestered component causes the installation to hang while attempting to run the emulator.
Workaround: From a console window prompt:
cd \WINNT\system32
regsvr32 atl.dll
1.29. How do I debug a Web service consumed by a Smart Device application?
You need to attach the debugger to the ASP.NET worker process.
Refer to the following link for more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtskdebugginganaspdeployedwebapplication.asp
1.30. What is the footprint of the .NET Compact Framework?
.NET Compact Framework storage size:
- 1.55MB (ROM) on Pocket PC 2000/2002
- 1.35MB (ROM) on Windows Mobile for Pocket PC 2003 or Windows CE .NET Devices
Running RAM requirements:
- .5 MB+ (depends on application)
Typical application sizes:
1.31. How do I include imgdecmp.dll in an emulator image?
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.
1.32. How do I programmatically replace Assemblies in the Global Assembly Cache (GAC)?
One can install and remove Assemblies directly to and from the GAC by programmatically launching cgacutil.
- Remove assemblies from the GAC using the -u option
- Install assemblies to the GAC using the -i option
It is typically safest to remove the Assembly before reinstalling it.
1.33. How can I display Pocket PC applications on my desktop or laptop without needing any device side configuration?
Download the ActiveSync Remote Display from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.34. How can I allow Activesync to connect to an Emulator session from Visual Studio .NET 2003?
See the entry titled "11.17. How can I allow Activesync to connect to an Emulator session from Visual Studio .NET 2003?" of this FAQ.
1.35. How can I copy files to the device currently connected to desktop ActiveSync?
See the entry titled "11.18. How can I copy files to the device currently connected to desktop ActiveSync?" of this FAQ.
1.36. How can I stress test user input?
Download Hopper from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.37. Where can I get a UI version of the Platform Builder Target Control Window?
Download JShell from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.38. Where can I get a Command shell for Pocket PC 2003 devices?
Download PPC Command Shell from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.39. How can I get detailed information about currently running processes?
Download RAPI Debug from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.40. How can I remotely start an application on a Pocket PC from my desktop?
Download RAPI Start from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?FamilyId=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
1.41. Why can't I load different assemblies with the same name?
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.
1.42. How can I force Visual Studio .NET 2003 to connect to newer versions of the Pocket PC 2003 emulator?
Download the Emulator ActiveSync Connection Tool from Windows Mobile Developer Power Toys:
http://www.microsoft.com/downloads/details.aspx?familyid=74473FD6-1DCC-47AA-AB28-6A2B006EDFE9&displaylang=en
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).
1.43. Why don't my custom controls show up properly in the toolbox?
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: ToolBoxItemFilterAttribute("NETCF",ToolBoxItemFilterType.Require)
ToolBoxItemFilterAttribute("System.CF.Windows.Forms", ToolBoxITemFilterType.Custom)
Top of Page
2. 图形
2.1. 怎样建立一个图形对象?
有很多种方法可以建立图形对象,看你怎么用:
在OnPaint中,使用object参数提供的PaintEventArgs参数:
//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)
2.2. 怎样优化GDI+?
以下编码方式有助提高使用Graphics的绘图速度:
- 只建立一个图形对象 (或只使用OnPaint中的 PaintEventArgs)。
- 把所有绘图工作先画到不显示的位图上,再一次性把位图显示出来。
- 只重画变化的部分图象。
- 尽可能在相同的区域上画相同大小的图象。
主要思路:最小化地重画图象。例如,当光标拖过图象时,不需要把整个图重新画一遍。只需要重画光标之前经过的地方。
2.3. 怎样在窗体上画一个图案?
这里有个例子,告诉你怎样把图片画到窗体的背景上:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/bkgndimage.aspx
2.4. 怎样画一个带有透明色的图案?
画一个带有透明色的图象,需要设置ImageAttributes对象的透明色。目前.net精简框架支持单种颜色的透明色。虽然SetColorKey功能可以设置颜色范围,但颜色的最大值和最小值必须相同,不然在运行时会出现ArgumentException的错误:
//C#
using System.Drawing.Imaging;
ImageAttributes attr = new ImageAttributes();
'VB
Imports System.Drawing.Imaging
Dim attr As New ImageAttributes()
以下代码描述了如何根据图象左上角的颜色设置透明色。
//C#
attr.SetColorKey(bmp.GetPixel(0,0), bmp.GetPixel(0,0));
'VB
attr.SetColorKey(bmp.GetPixel(0,0), bmp.GetPixel(0,0))
以下方法可以准确的设置颜色:
//C#
attr.SetColorKey(Color.FromArgb(255,0,255),Color.FromArgb(255,0,255));
attr.SetColorKey(Color.Fuchsia, Color.Fuchsia);
'VB
attr.SetColorKey(Color.FromArgb(255,0,255),Color.FromArgb(255,0,255))
attr.SetColorKey(Color.Fuchsia, Color.Fuchsia)
图象会被重载的Graphics.DrawImage方法重画,并且使用ImageAttributes对象作为一个参数parameter:
//C#
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height,GraphicsUnit.Pixel, attr);
'VB
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height,GraphicsUnit.Pixel, attr)
2.5. 为什么从TextBox上调用CreateGraphics会失败?
只有Form类才支持Control.CreateGraphics().
2.6. 怎样获得屏幕上文字的大小?
使用Graphics的MeasureString方法。以下代码说明如何在文字周围画一个方框:
//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
2.7. Can I set the width of a pen?
Setting the pen width is not available in the .NET Compact Framework. Some alternate solutions include:
- Drawing filled rectangles with the Graphics.FillRectangle method
- Drawing multiple lines next to each other
- Writing a custom graphics routine with GAPI
2.8. How do I zoom an image?
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
2.9. Why can't I load an image?
Ensure that imgdecmp.dll is in the device's Windows directory.
For more information, see the topic "1.31. How do include imgdemp.dll in an emulator image?" of this FAQ.
Top of Page
3. 发布
3.1. 怎样建立一个PocketPC的设置程序?
这篇文章告诉您如何建立一个单独的.msi文件,可以运行并把应用安装到不同的Pocket PC设备上。整个过程都是自动的,所以很容易把所有需要的组件都打包到.msi文件中。包含C#和Microsoft Visual Basic .NET代码。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfdeployment.asp
3.2. 哪个发布包可以包括在我的安装程序中?
您可以为您的用户提供一个最终的发布包,以帮助他们升级设备。您不能拆开这个发布包把内容给您的用户。但是,您可以拆开开发人员的发布包把内容给您的用户。
3.3. 怎样可以建立一个不需要.net框架的安装程序?
这篇文章讨论了如何成功的安装一个Pocket PC的应用:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnppc2k/html/ppc_allation.asp
3.4. 怎样建立不受PocketPC系统约束的CAB安装文件?
您可以建立一个.inf文件生成一个适合任何Pocket PC设备的应用程序安装文件。查看示例代码:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/cabfile.aspx
3.5. 为什么不同的处理器会有不同的CAB安装文件?
每一个CAB文件都包含一小段检测智能设备上的.net精简框架版本的代码。这个功能是处理器/平台特有的,不同的CAB文件根据处理器类型绑定不同的代码。
3.6. How do I create a desktop installer that detects the presence of the .NET Compact Framework on the target device and installs it if necessary?
The article titled "Creating an MSI Package that Detects and Updates the .NET Compact Framework" in the MSDN Library describes a technique that may be used:
http://msdn.microsoft.com/mobility/understanding/articles/default.aspx?pull=/library/en-us/dnnetcomp/html/netcfdepl.asp
3.7. How do I deploy a .NET Compact Framework Service Pack to the emulator?
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.
| Emulator |
CAB File |
| Pocket PC 2002 |
netcf.core.ppc3.x86.cab |
| Windows Mobile 2003 for Pocket PC |
netcf.core.wce4.x86.cab |
| Windows Mobile 2003 for Smartphone |
RAM installs not supported |
3.8. How do I include SQL Server CE with my application installation?
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:
sqlce.dev.ppc3.<processor>.cab
sqlce.dev.wce4.<processor>.cab
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:
sqlce.ppc3.<processor>.cab
sqlce.wce4.<processor>.cab
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\...
3.9. How do I use GAPI to create a graphics engine?
This article describes how to create a DLL that wraps GAPI (Game API), such that it is .NET Compact Framework compliant, and use it to create and optimize a basic graphics library in managed code.
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI1.asp
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.asp
Top of Page
4. 图形用户界面(GUI): 窗体
4.1. 怎样建立一个全屏的窗体?
您必须把WindowState属性设置为最大化。做一个看不见的窗体,如把全屏的图片放到窗体中,您需要把FormBorderStyle设置为None,关掉ControlBox删掉窗体中所有的菜单。
//C#
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.ControlBox = false;
this.Menu = null;
'VB
Me.WindowState = FormWindowState.Maximized
Me.FormBorderStyle = FormBorderStyle.None
Me.ControlBox = False
Me.Menu = Nothing
4.2. 什么时候用窗体的构造器?什么时候用窗体的Load事件?
窗体的load功能是做界面操作的最好方法。典型又安全的做法是在构造器中创建数据和控件的实例。任何包含用户界面的控件或窗体的初始化,都应该在load功能中完成。例如:在窗体的构造器中完成控件对象的建立,然后在load功能中设置控件的位置等 是安全的做法。
4.3. 怎样把最小化按钮换成关闭按钮?
通过开发环境的设计器,可以把窗体的自动最小化模式转变成关闭模式,或者通过变成实现。(x)按钮自动最小化应用程序,(ok)按钮回关闭程序。
在设计器中转化窗体的风格
打开Visual Studio环境的窗体设计器,查看属性。在窗体上右键并选择Properties,在Window Style 部分把MinimizeBox设置为 False。
在代码中转变窗体风格
简单的在窗体的load功能中添加以下一行代码:
//C#
this.MinimizeBox = false;
'VB
Me.MinimizeBox = False
4.4. 怎样在.net精简框架上建立多窗体系统结构?
这篇文章讨论了如何在.net精简框架上为应用程序有效的建立用户界面:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfuiframework.asp
4.5. 怎样提高.net精简框架应用程序的载入速度?
通过下面文章中的优化技巧,减少.net精简框架应用程序的载如时间:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/netcfimproveformloadperf.asp
4.6. 运行时怎样修改窗体的风格?
这篇快速入门教程描述了在pocket pc应用程序中使用代码改变窗体界面:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/ppcformproperties.aspx
4.7. 怎样滚动窗体的内容?
这篇快速入门教程演示了如何使用纵向和横向滚动条 还有如何在窗体中绘制一个图象:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/scrolling.aspx
4.8. 怎样制作一个浮动的窗体?好象所有的窗体总是全屏状态的。
有边界的最顶端的窗体总是全屏的,不能移动或改变大小。没有边界或子窗体可以移动和改变大小。
使用: Form.BorderStyle = BorderStyle.None; //来设置有无边界
4.9. 怎样强迫一个窗体变成最小化?
请查看本问 "6.6. 如何是窗口一直保持最小化? " 部分。
4.10. 我有一个包含很多控件的智能设备窗体,为什么运行的时候出现"NotSupportedException"错误?
如果你的窗体中包含了很多控件,当你运行的时候可能会有NotSupportedException的错误。如果你是在Debug模式下运行的话,你会发现异常是来自于窗体的InitializeComponent部分。这种情况是由于精简框架CLR在编译类的方法时,会有64kb的限制造成的。这意味着CLR把方法(如,InitializeComponent)翻译成中间语言时,返回的机器码不能超过64KB。如果超过了64KB,NotSupportedException将被抛出。这也是为什么容易在Debug模式(F5)下导致错误,而非Debug模式(Ctrl+F5)却不容易出错。因为Debug模式运行时会生成更多容量的调试代码。
除了模拟器,您也可能在智能设备上遇到这个错误,因为即时编译代码根据因CPU类型而异的(如,PocketPC使用的是ARM指令,而模拟器上使用的x86指令)
没有一个准确的数字说明,一个窗体中到底可以包含多少个控件。因为不同控件的产生的代码量不一样。如,一个Button控件,比TabControl控件产生的代码要少。还会因由设置了多少属性而异。包含集合的控件,象ListBox或TreeView,如果在设计时向属性框中填入了很多值,编译时将产生大量代码。同样,设置了Localized的窗体(Localizable属性为true),会比没设置localize的窗体产生更多代码。因为localize需要从资源文件中读取属性值,放在InitializeComponent方法中。
果你遇到这种情况,下面有一些技巧帮助你避免它的发生:
- 把一个窗体的代码分成多个窗体。过多控件的窗体,会影响程序开始时载入的性能。尽可能把用户界面分成两个或多个窗体。
- 不要在设计时填充大的、内部的、带集合的控件。如果你把很多节点集合加到TreeView控件中,这样会在InitializeComponent方法中加入大量代码。尽可能把加入集合的代码移到Form.Load事件中。这样做的缺点是,要在设计时编辑这些集合将变得不容易,但它有助于分割代码。
- 不要把自己的代码添加到InitializeComponent方法中,这对通常的编码都有用,不建议添加、修改设计起生成的代码。这样做会造成设计器不可知的错误。如果你想添加自己的启动代码,你应该在Form.Load事件中做。
- 运行时初始化类似的控件。比如,有12个Button控件,只是文字和位置不同,你应该考虑使用循环来设置属性,而不是在设计时设置属性。在次,如果你自己写代码来实现它,不要把代码放在InitializeComponent方法中。
编辑InitializeComponent方法的缺点是,在InitializeComponent代码外建立对象实例的代码,将不能在设计器重被设计。同样,如果你手动修改了InitializeComponent中的代码,你会发现,设计器可能不再识别你修改的代码。所以以上技巧的前提是,不要修改InitializeComponent中的代码。
4.11. 哪个是正确关闭窗体的方法:Appplication.Exit 还是 Form.Close ?
Application.Exit是类似Win32平台下的PostQuitMessage()硬性退出。收回所有弹出的信息,释放呼叫堆栈,把执行权返回给系统。
在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。
另外,使用form.Close()的话,你的程序可以在OnClosing或OnClosed事件中处理释放资源、关闭文件等操作。如果使用Application.Exit退出,这些事件将不会被触发。
4.12. Why does showing a MessageBox on Windows Mobile 2003 for Smartphone with the Abort, Retry, and Fail set of buttons or the Yes, No, Cancel set of buttons, or the third button as the default button result in a NotSupportedException?
Windows Mobile 2003 for Smartphone only supports 1 or 2 button MessageBoxes.
4.13 How do I get an icon in the Pocket PC Start Menu Most Recently Used (MRU) List?
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.
4.14 How do I center a Form on the screen?
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: //c#
Rectangle screen = Screen.PrimaryScreen.Bounds;
this.Location = new Point((screen.Width - this.Width) / 2,
(screen.Height - this.Height ) / 2);
'VB
Dim theScreen As Rectangle
theScreen = Screen.PrimaryScreen.Bounds()
Me.Location = New Point((theScreen.Width - Me.Width) / 2, _
(theScreen.Height - Me.Height) / 2)
4.15 Why can't I show a Form that has been closed?
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.
4.16 How can I enable multiple instances of an application?
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
Top of Page
5. 图形用户界面 (GUI): 通用
5.1. 怎样建立一个带有图形或支持多行的按钮?
建立一个带有图形或支持多行的按钮需要使用自定义控件。自定义控件能继承button的paint方法,以及其他任何需要的自定义数据。参考以下连接获得更多关于自定义控件的信息:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/customctrlscompactfx.asp
快速入门教程告诉你如何建立一个带图像的按钮:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/picturebutton.aspx
5.2. TextBox.AcceptsReturn在.net精简框架下是怎样工作的?
虽然设置了AcceptsReturn为false,但它还是按true的方式来操作。你可以写一个继承TextBox的类,在KeyPress事件中实现对Enter的处理。
5.3. 当数据载入ComboBox的时候,为什么SelectedIndexChanged事件没有被触发?
这是一个已经知道的问题,将在以后的.net精简框架中发布。
5.4. Show和ShowDialog有什么不同?
ShowDialog会把一个窗体以 模式 方式显示,这是一种独占调用方式,知道窗体关闭才会返回。这个方法将返回一个DialogResult枚举,表示关闭的条件。
Show是一种非独占的调用方式,和显示一个控件一样,可以立刻返回,没有返回参数。显示一个控件意味着Visible属性被设置为true,直到Hide方法被调用,Visible方法才会变为false。
5.5. 为什么我不能建立右键菜单分离器?(separator)
这是一个已经知道的BUG,把右键菜单设置为分割线,将抛出NotSupportedException错误。这个问题是由于WinCE系统有个限制,不允许在已经加入右键菜单的菜单项设置为分隔符,并且菜单的父类是一个控件。在Visual Studio 2003种,设计器分割移动应用代码的方式和PC上的应用程序类似。这是导致此问题的原因。解决的方法是,把右键菜单单独放在InitilizeComponent方法外的地方。
5.6. 运行时把ImageList赋给ToolBar时,为什么图象不出现?
你可以在窗体载入的时候把ImageList分配给ToolBar,但重新应用图像在ToolBar上的顺序。在设置ToolBar的ImageList之前 设置ToolBar按钮的图像顺序是不被支持的。
5.7. 怎样把光标设置成等待状态?
这段代码可以把光标设置成等待光标:
//C#
Cursor.Current = Cursors.WaitCursor;
'VB
Cursor.Current = Cursors.WaitCursor
这段代码可以把光标设置为默认:
//C#
Cursor.Current = Cursors.Default;
'VB
Cursor.Current = Cursors.Default
5.8. 如何在菜单项中显示符号:&?
这项功能还不被.net精简框架支持。使用"&&"不会在菜单项的文字中显示"&"符号。
5.9. 如何制作基于.net精简框架的动画控件?
这篇文章将告诉你如何制作基于.net精简框架的动画控件:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/animationcontrol.asp
5.10. 如何在.net精简框架上制作自定义控件?
学习制作.net精简框架控件,提高您的技巧。(文章附带了自定义控件的示例代码):
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/customctrlscompactfx.asp
5.11. 如何制作基于.net精简框架的带图片的按钮?
这篇文章讨论了建立基于.net精简框架的带图片的按钮:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/ImageButton.asp
5.12. 如何使用.net精简框架下的MessageWindow类库?
学习如何使用.net精简框架 MessageWindow 类建立一个提示图标:
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/messagewindow.asp
这篇快速入门实现了,当鼠标点击矩形自定义控件或点击Panel控件时,使用MessageWindow把消息发送给发送窗体:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/messagewindow.aspx
5.13. 如何向DataGrid中添加行和列?
这篇快速入门教程解释了如何在运行时向DataGrid控件添加或删除行、列:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridadding.aspx
5.14. 如何实现DataGrid的编辑?
.net精简框架中的DataGrid控件提供了几乎.net框架中的DataGrid控件的所有功能。一个主要的区别是.net精简框架中的DataGrid不能在运行时编辑单元。这篇快速入门教程演示了如何通过程序实现编辑单元格的一种方法:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridediting.aspx
5.15. 怎样设置DataGrid的DataSource?
与.net框架的DataGrid的另一个区别是,.net精简框架的DataGrid不支持把DataSource设置为DataSet。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagrid.aspx
5.16. 怎样对DataGrid的列排序?
与.net框架的DataGrid的另一个区别是,.net精简框架的DataGrid不支持在运行时按照列进行排序。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/datagridsorting.aspx
5.17. 怎样对ListView中的内容排序?
.net精简框架不支持ListView.Sort方法,但任然可以排序。这篇快速入门教程定义了一个继承ArrayList.Sort的IComparable接口的方法:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/listviewsort.aspx
5.18. 如何使用 输入板(SIP)控件?
这篇快速入门教程演示了在PocketPC上打开和关闭软输入板(SIP),以及当SIP显示时,tab控件大小也跟随变化:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/inputpanel.aspx
多个窗体应该共享一个输入板对象。可以通过先在主窗体中建立SIP对象,然后把它传给子窗体或暴露SIP对象的一些方法、属性给其他需要使用SIP的窗体。
5.19. 如何自定义事件?
这篇快速入门教程描述了如何继承Button类、重载方法来事现双击事件。这个自定义事件会在按钮被双击时触发,两次点击的间隔时间是SystemInformation.DoubleClickTime 属性的值,以毫秒为单位。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/btndclick.aspx
.net精简框架的控件不支持OnEnter和OnLeave方法,包括Windows.Forms.Control基类。但是,因为支持Control.OnMouseMove方法,您可以通过它和Control.Capture 属性判断鼠标什么时候进入和离开控件。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/enterleave.aspx
5.20. 如何做一个 owner-drawn List Box?
您可以制作一个.net精简框架的owner drawn list box。.net精简框架的ListBox或其他控件不支持DrawMode、DrawItem, 或其他drawing方法,但您可以编程实现。这篇快速入门教程提供一个自定义控件类,建立一个owner-drawn list box,并实现了选择字体的控件的功能。
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/ownerdrawnlistbox.aspx
5.21. 如何做一个多选框判断真假?
这篇快速入门教程提供了在Windows.Forms.CheckBox控件上建立真/假多选框:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/tfcheckbox.aspx
5.22. 设置InputPanel.Enabled = true的时候为什么出现异常?
InputPanel组件需要窗体包含MainMenu控件,而且那个窗体是显示在屏幕上的。
5.23. 为什么自定义控件不会自动继承父类的字体?
这个功能不被.net精简框架所支持。
5.24. 为什么当输入字符时,NumericUpDown 和 DomainUpDown 控件不会引发 ValueChanged 和 SelectedItemChanged 事件?
在代码中改变控件的值 或 按下了上、下箭头才会触发ValueChanged和SelectedItemChanged事件。当用户往控件中输入字符的时候时不会触发的。
5.25. 为什么NumericUpDown控件增长的值不是设置好的值?
当您按了上、下后出现的值,不是增长值的倍数,它将向着那个方向(上或下)直到下一个增长值的倍数的值。
5.26. 为什么StatusBar不能放在窗体的任意位置?一定要在底部?
StatusBar控件只能停靠在窗体的底部,它的大小不能改变。
5.27. 为什么我的控件自动继承了父控件的背景色?
这个功能不被.net精简框架所支持。可以采取的方法是继承OnParentChanged方法手动设置颜色:
//C#
protected override void OnParentChanged(EventArgs e)
{
base.OnParentChanged(e);
this.BackColor = Parent.BackColor;
}
'VB
Protected Overrides Sub OnParentChanged(ByVal e As EventArgs)
MyBase.OnParentChanged(e)
Me.BackColor = Parent.BackColor
End Sub 'OnParentChanged
5.28. 为什么NumericUpDown控件能接受decimal类型的值,但不会显示大于2^16的值?
虽然NumericUpDown控件接受decimal类型的值,但.net精简框架把这个控件的值当作int类型来处理。如,10.23当作10。同样此控件在PocketPC上不接受大于带符号的16位整型。
5.29. 为什么不能在DomainUpDown 中输入文字,而要选择?
DomainUpDown控件不会对输入的文字进行确认(不象完整的.net框架)。如果您先输入了一些文字,再按上、下箭头,它会显示内容改变前的值的下一个值。
5.30. 为什么OpenFileDialog被限制在"My Documents" 文件夹中?
OpenFileDialog的初始化目录被限制在"My Documents"文件夹或它的子文件夹中。这个限制是由PocketPC系统强加的,为了帮助用户在标准目录下管理自己的文档。
5.31. How can I activate the SIP (InputPanel) without a menu?
The SIP can be activated by P/Invoking the function "SipShowIM" as follows. //C#
using System.Runtime.InteropServices;
const uint SIPF_OFF = 0x0;
const uint SIPF_ON = 0x1;
[DllImport("coredll.dll")]
private extern static void SipShowIM(uint dwFlag);
'VB
Imports System.Runtime.InteropServices
Const SIPF_OFF As Integer = &H0
Const SIPF_ON As Integer = &H1
<DllImport("coredll.dll")> _
Private Shared Function SipShowIM(ByVal dwFlag As Integer) As Integer
End Function
5.32. How do I add a subnode to every node in a TreeView?
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
5.33. How do I determine the number of rows or columns in a DataGrid?
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.
5.34. How do I create a owner drawn Listbox?
See the .NET Compact Framework QuickStarts, Implementing Events topic:
http://samples.gotdotnet.com/quickstart/compactframework/doc/btndclick.aspx
5.35. How can I implement Control.GetNextControl under the .NET Compact Framework?
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
5.36. How do I get notified when the user clicks on a treeview node?
TreeView does not support the Click event, however, a workaround is to use the AfterSelect event instead.
5.37. How do I set the title of a fullscreen multiline edit control window?
This is not supported by the current version of the .NET Compact Framework.
5.38. Why don' I see the validItem selected when I set ComboBox.SelectedValue to validItemInCollection?
Setting the SelectedValue property only works if the control is databound.
5.39. How do I detect the location where a 'tap & hold' occurred to bring up a context menu on my custom control?
Handle the ContextMenu.Popup event, and then query the current mouse coordinates using 'Control.MousePosition'.
5.40. Why doesn't the scrollbar value ever get set to the maximum value?
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:
Maximum - (LargeChange + 1).
5.41. How do I tab out of a custom control to the previous control?
Call this.Parent.Controls(this.Parent.GetChildIndex(customcontrol) - 1).Focus() in the KeyDown event handler when a Keys.Up key is detected.
5.42. How do I add Toolbar buttons with transparency?
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
Top of Page
6. 与本地代码(Native Code)互用
6.1. 如何调用本地代码写的DLL中的函数?
本地DLL代码可以通过系统的Invoke (P/Invoke)方法调用。这些文章提供了如何实现调用和更多的调用技巧:
6.2. 如何使用dumpbin.exe帮助描述 P/Invokes?
见本问答的 "6.1. 如何调用本地写的DLL中的函数? " 章节。
6.3. 如何为微软.net精简框架写非托管代码?
见本问答的 "6.1. 如何调用本地写的DLL中的函数? " 章节。
6.4. 如何调用 P/Invoke GetTickCount?
不需要使用P/Invoke调用GetTickCount功能,因为Environment.TickCount就提供了这个功能。
见本问答的 " 7.2. 如何计算精确的时间间隔?" 章节。
6.5. 如何获得系统还有多少可用内存?
您可以调用GetSystemMemoryDivision和GlobalMemorySystem函,数获得程序和存储器间有多少内存是隔离的和已经分配的。
参数的说明可以在API参考文档中找到。
//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
6.6. 如何是窗口一直保持最小化?
- 继承窗体的OnGotFocus方法。
- 找到窗体的窗口句柄。
- 调用ShowWindow(hwnd, SW_MINIMIZE)强制窗体最小化。
//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
6.7. 在微软.net精简框架上调用系统函数时,如何装配数据类型?
见本问答的 "6.1. 如何调用本地写的DLL中的函数? " 章节。
6.8. 如何得到一个窗体或控件的句柄 (HWND) ?
其实有一些使用调用本地代码的方法可以获得控件的句柄HWND。下面列出其中两种,一种使用GetCapture,另一个使用FindWindow。 //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
6.9. 如何使用性能计数器功能?
使用QueryPerformanceFrequency函数和QueryPerformanceCounter函数可以建立精确的计时程序。 这些功能是和设备提供商相关的,如果他们不能执行,那么只能和GetTickCount功能得到一样的结果。如果能执行这些函数,就能保证计时器最准确的运行,比GetTickCounter或Environment.TickCount准确得多。TickCount其实是调用GetTickCounter的。
如果性能计数器是GetTickCount的一个实例,QueryPerformanceFrequency将把1000作为计时频率。如果这些函数不能执行,将得到返回值为0。以下代码演示了如何使用这些函数。//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);
Que