c#데스크톱 아이콘의 위치와 이름 가져오기
12464 단어 C#
using System;
namespace System.Windows.Forms
{
/// <summary>
///
/// </summary>
class GetIcoPointName
{
#region Api
private const uint LVM_FIRST = 0x1000;
private const uint LVM_GETITEMCOUNT = LVM_FIRST + 4;
private const uint LVM_GETITEMW = LVM_FIRST + 75;
private const uint LVM_GETITEMPOSITION = LVM_FIRST + 16;
[System.Runtime.InteropServices.DllImport("user32.DLL")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.DLL")]
private static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[System.Runtime.InteropServices.DllImport("user32.DLL")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd,
out uint dwProcessId);
private const uint PROCESS_VM_OPERATION = 0x0008;
private const uint PROCESS_VM_READ = 0x0010;
private const uint PROCESS_VM_WRITE = 0x0020;
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
private const uint MEM_COMMIT = 0x1000;
private const uint MEM_RELEASE = 0x8000;
private const uint MEM_RESERVE = 0x2000;
private const uint PAGE_READWRITE = 4;
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint dwFreeType);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool CloseHandle(IntPtr handle);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);
private struct LVITEM //
{
public int mask;
public int iItem;
public int iSubItem;
public int state;
public int stateMask;
public IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public IntPtr lParam;
public int iIndent;
public int iGroupId;
public int cColumns;
public IntPtr puColumns;
}
private int LVIF_TEXT = 0x0001;
/// <summary>
/// , SendMessage
/// </summary>
/// <param name="AHandle"></param>
/// <returns></returns>
private int ListView_GetItemCount(IntPtr AHandle)
{
return SendMessage(AHandle, LVM_GETITEMCOUNT, 0, 0);
}
/// <summary>
///
/// </summary>
/// <param name="AHandle"></param>
/// <param name="AIndex"></param>
/// <param name="APoint"></param>
/// <returns></returns>
private bool ListView_GetItemPosition(IntPtr AHandle, int AIndex, IntPtr APoint)
{
return SendMessage(AHandle, LVM_GETITEMPOSITION, AIndex, APoint.ToInt32()) != 0;
}
#endregion
public string GetSysVer()
{
string Ver = Environment.OSVersion.Version.ToString();
return Ver;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public string[] GetIcoName()
{
// SysListView32
IntPtr vHandle;
//xp Progman ; win7 "WorkerW" spy++
vHandle = FindWindow("Progman", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", null);
int vItemCount = ListView_GetItemCount(vHandle);//
uint vProcessId; // pid
GetWindowThreadProcessId(vHandle, out vProcessId);
//
IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
string[] tempStr = new string[vItemCount];
try
{
for (int i = 0; i < vItemCount; i++)
{
byte[] vBuffer = new byte[256];
LVITEM[] vItem = new LVITEM[1];
vItem[0].mask = LVIF_TEXT;
vItem[0].iItem = i;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM)));
uint vNumberOfBytesRead = 0;
///
WriteProcessMemory(vProcess, vPointer,
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
//
SendMessage(vHandle, LVM_GETITEMW, i, vPointer.ToInt32());
ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM))),
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);
string vText = System.Text.Encoding.Unicode.GetString(vBuffer, 0,
(int)vNumberOfBytesRead).TrimEnd('\0');
tempStr[i] = vText;
}
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
}
return tempStr;
}
public System.Drawing.Point GetIcoPoint(string icoName)
{
// SysListView32
IntPtr vHandle;
//xp Progman ; win7 "WorkerW" spy++
vHandle = FindWindow("Progman", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysListView32", null);
int vItemCount = ListView_GetItemCount(vHandle);//
uint vProcessId; // pid
GetWindowThreadProcessId(vHandle, out vProcessId);
//
IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |
PROCESS_VM_WRITE, false, vProcessId);
IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 4096,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
System.Drawing.Point[] vPoint = new System.Drawing.Point[1];
try
{
for (int i = 0; i < vItemCount; i++)
{
byte[] vBuffer = new byte[256];
LVITEM[] vItem = new LVITEM[1];
vItem[0].mask = LVIF_TEXT;
vItem[0].iItem = i;
vItem[0].iSubItem = 0;
vItem[0].cchTextMax = vBuffer.Length;
vItem[0].pszText = (IntPtr)((int)vPointer + System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM)));
uint vNumberOfBytesRead = 0;
///
WriteProcessMemory(vProcess, vPointer,
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(vItem, 0),
System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM)), ref vNumberOfBytesRead);
//
SendMessage(vHandle, LVM_GETITEMW, i, vPointer.ToInt32());
ReadProcessMemory(vProcess,
(IntPtr)((int)vPointer + System.Runtime.InteropServices.Marshal.SizeOf(typeof(LVITEM))),
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),
vBuffer.Length, ref vNumberOfBytesRead);
string vText = System.Text.Encoding.Unicode.GetString(vBuffer, 0,
(int)vNumberOfBytesRead).TrimEnd('\0');
if (vText == icoName)
{
ListView_GetItemPosition(vHandle, i, vPointer);
ReadProcessMemory(vProcess, vPointer,
System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(vPoint, 0), System.Runtime.InteropServices.Marshal.SizeOf(typeof(System.Drawing.Point)), ref vNumberOfBytesRead);
}
}
}
catch (Exception Ex)
{
throw Ex;
}
finally
{
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
}
return vPoint[0];
}
}
}
사용 예:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Management;
using Shell32;
namespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
GetIcoPointName getPn = new GetIcoPointName();
private void button1_Click(object sender, EventArgs e)
{
foreach (var i in getPn.GetIcoName())
{
textBox1.Text = textBox1.Text+i;
}
ShwoDesk();
}
/// <summary>
///
/// </summary>
private void ShwoDesk()
{
Type shellType = Type.GetTypeFromProgID("Shell.Application");
object shellObject = System.Activator.CreateInstance(shellType);
shellType.InvokeMember("ToggleDesktop", System.Reflection.BindingFlags.InvokeMethod,
null, shellObject, null);
}
private void Form1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = getPn.GetIcoPoint(" ").ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
}
}
저자: qq283868910 2011-12-5 21:26:17
텍스트 링크
읽기: 122 댓글: 0
의견 보기
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.