【问题标题】:using IntPtr form other class使用另一个类的 IntPtr
【发布时间】:2020-10-20 20:29:14
【问题描述】:

我正在为文本框使用自定义光标

这个方法对我来说很好用

[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);
IntPtr Search_cursor = LoadCursorFromFile(Application.StartupPath + "\\SearchCursor.cur");

txt_Vacation_Calculate_Type_Code.Cursor = new Cursor(Search_cursor);

但是我需要使用其他类的这个方法 我创建了 Image_Icon

class Image_Icon
{
[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);

public IntPtr Search_cursor_C()
{
IntPtr Search_cursor = LoadCursorFromFile(Application.StartupPath + "\\SearchCursor.cur");
return (Search_cursor);
}

我正在使用此代码获取光标

txt_Vacation_Calculate_Type_Code.Cursor = new Cursor(Image_Icon.Search_cursor_C);

但看起来有些不对劲 我得到这个错误

严重性代码描述项目文件行抑制状态 错误 CS1503 参数 1:无法从“方法组”转换为“IntPtr” PayRoll C:\Users\Ahmed Abdo\Desktop\WorkNow\PayRoll\PayRoll\PayRoll\Basis\frmEmployeeData.cs 68 Active

如何从其他类访问该类并使用搜索光标

【问题讨论】:

  • 你需要调用 Search_cursor_C 方法......它也应该是静态的,或者你应该从类实例中调用它......还请参加一些 C# 课程......如果你正在获得编译时间错误主要是表示您不懂语言(或打错字)
  • 这能回答你的问题吗? Using Custom Cursor WinForms
  • @Selvin ...好的
  • 此方法在 Windows 7 中不起作用请看这里stackoverflow.com/questions/64432179/…

标签: c#


【解决方案1】:

将你的函数设为静态,并返回Cursor

class Image_Icon
{

    [DllImport("user32.dll")]
    static extern IntPtr LoadCursorFromFile(string lpFileName);

    public static Cursor Search_cursor_C()
    {
        return new Cursor(LoadCursorFromFile(System.IO.Path.Combine(Application.StartupPath, "SearchCursor.cur")));
    }

}

然后像这样使用它:

txt_Vacation_Calculate_Type_Code.Cursor = Image_Icon.Search_cursor_C();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2021-11-12
    相关资源
    最近更新 更多