C#记事本程序中,怎么实现查找,替换功能?

2024年11月30日 01:36
有1个网友回答
网友(1):

调用系统查找与替换对话框!
using System.Runtime.interopservices;

[DllImport("comdlg32.dll")]
private static extern IntPtr FindText(ref FINDREPLACE lpfn);

[DllImport("comdlg32.dll")]
private static extern IntPtr ReplaceText(ref FINDREPLACE lpfn);

public struct tagFINDREPLACEW {

/// DWORD->unsigned int
public uint lStructSize;

/// HWND->HWND__*
public System.IntPtr hwndOwner;

/// HINSTANCE->HINSTANCE__*
public System.IntPtr hInstance;

/// DWORD->unsigned int
public uint Flags;

/// LPWSTR->WCHAR*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public string lpstrFindWhat;

/// LPWSTR->WCHAR*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public string lpstrReplaceWith;

/// WORD->unsigned short
public ushort wFindWhatLen;

/// WORD->unsigned short
public ushort wReplaceWithLen;

/// LPARAM->LONG_PTR->int
public int lCustData;

/// LPFRHOOKPROC
public LPFRHOOKPROC lpfnHook;

/// LPCWSTR->WCHAR*
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public string lpTemplateName;
}

/// Return Type: UINT_PTR->unsigned int
///param0: HWND->HWND__*
///param1: UINT->unsigned int
///param2: WPARAM->UINT_PTR->unsigned int
///param3: LPARAM->LONG_PTR->int
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate uint LPFRHOOKPROC(System.IntPtr param0, uint param1, System.IntPtr param2, System.IntPtr param3);

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]