C#外部方法

using System;
using System.Runtime.InteropServices;
using System.Text;
/*
 * 外部方法是在声明中没有实现的方法,常常是C#之外的编程语言编写的。
 * 1、用extern修饰符标记,在类的声明中没有实现,它的实现被分号取代
 * 2、声明和实现的连接是依赖实现的。常常使用DllImport特性完成
 */
namespace ExternMethod
{
    class MyClass
    {
        [DllImport("kernel32",SetLastError=true)]
        public static  extern int GetCurrentDirectory(int a,StringBuilder b);
    }
    class Program
    {
        static void Main(string[] args)
        {
            const int MaxDirLength = 250;
            StringBuilder sb = new StringBuilder();
            sb.Length = MaxDirLength;
            MyClass.GetCurrentDirectory(MaxDirLength, sb);
            Console.WriteLine(sb);
            Console.ReadKey();
        }
    }
}

转载于:https://www.cnblogs.com/sulong/p/4793842.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您要在C#中创建自绘的ListView控件,可以使用以下步骤: 1. 创建一个新的Windows窗体应用程序项目。 2. 在工具箱中找到ListView控件并将其拖放到窗体上。 3. 在窗体的代码文件中添加以下命名空间: ```csharp using System.Runtime.InteropServices; ``` 4. 在窗体的代码文件中添加以下常量和结构体: ```csharp private const int LVM_FIRST = 0x1000; private const int LVM_SETBKCOLOR = LVM_FIRST + 1; private const int LVM_SETTEXTBKCOLOR = LVM_FIRST + 38; private const int LVM_SETTEXTCOLOR = LVM_FIRST + 36; [StructLayout(LayoutKind.Sequential)] public struct LVITEM { public int mask; public int iItem; public int iSubItem; public int state; public int stateMask; public IntPtr pszText; public int cchTextMax; public int iImage; public IntPtr lParam; public int iIndent; public int iGroupId; public int cColumns; public IntPtr puColumns; } [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); ``` 5. 在窗体的代码文件中添加以下方法: ```csharp private void SetListViewProperties() { // 设置背景颜色 SendMessage(listView1.Handle, LVM_SETBKCOLOR, 0, SystemColors.Window.ToArgb()); // 设置文本背景颜色 SendMessage(listView1.Handle, LVM_SETTEXTBKCOLOR, 0, SystemColors.Window.ToArgb()); // 设置文本颜色 SendMessage(listView1.Handle, LVM_SETTEXTCOLOR, 0, SystemColors.ControlText.ToArgb()); } private void DrawListViewItem(DrawListViewItemEventArgs e) { // 自定义项的绘制方式 // ... } ``` 6. 在窗体的代码文件中添加以下事件处理程序: ```csharp private void Form1_Load(object sender, EventArgs e) { SetListViewProperties(); } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { DrawListViewItem(e); } ``` 7. 在DrawListViewItem方法中实现您自己的绘制逻辑。在这个方法中,您可以使用GDI+绘制ListView控件的项。 以上就是创建自绘的ListView控件的基本步骤。请注意,您需要在DrawListViewItem方法中实现自定义的绘制逻辑以达到您想要的外观效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值