C# API的问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 08:04:06
我按书上的代码创建了FORM1,在FORM1.CS上打上了书上的代码,之后我想回到FORM1设计界面上出现了错误“加载设计器时遇到一个或多个错误。这些错误在下面列出。一些错误可通过重新生成项目来修复,而另一些错误则需要更改代码。类型 Form1 由同一文件中的几个分部类构成”不知道是什么回事?请解析一下,谢谢,分没关系,只要详细的话可以追加分
我的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace 定时抓屏
{

internal class NativeMethods
{
[DllImport("user32.dll")]
public extern static IntPtr GetDesktopWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern UInt64 BitBlt
(IntPtr hDestDC,
int x, int y, int nWidth, int nHeight,

1.把form1的定义挪到NativeMethods之前
2.要用partial 关键字
为的是把类定义分布在不同的文件
在form设计器自动生成的文件里,已经有了form1的类定义,用的就是partial,所以你必须在自己写的代码也用partial,才能保证两类是同一个定义.
至于出错变多,不要害怕,很多时候是一个错误解决就会少很多错误,partial是必须加的

你建的是什么工程???

为什么
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

这段代码会出现在Form1的类里面???

///照本宣科也要明白意思...
哪些是编译器生成的代码.哪些应该是手写的代码都没分清楚.Main()应该是在Program.cs里面就有了的

FORM1上用了OCX控件没?以前经常遇到。我有一个解决方法,但是不一定有用,只能试试,打开工程以后把里面所有的类和FORM全部关闭。然后再把 重新生成一下 然后关闭工程然后再打开!

1.internal class NativeMethods 把这个类放到其他地方去,因为你这个类放到form1 类的上面去了。或吧
NativeMethods 放下form1的下面去也可以!。2。public class Form1 : System.Windows.Forms.Form吧这个改为 public partial class Form1 : System.Windows.Forms.Form

把internal class NativeMethods 这个内部类移到public void SaveWindowsScreen(string filename) 上一行。

把internal 改成partial 。