Skip to content

Instantly share code, notes, and snippets.

@tomazursic
Last active December 24, 2022 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomazursic/f4e8a4cb731e1e2a11e75ebfbe320d42 to your computer and use it in GitHub Desktop.
Save tomazursic/f4e8a4cb731e1e2a11e75ebfbe320d42 to your computer and use it in GitHub Desktop.
Arange icons on windows desktop into falus
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
namespace ArrangeByPenis
{
public partial class Form1 : Form
{
const uint MEM_COMMIT = 0x1000;
const uint PAGE_READWRITE = 4;
const uint LVM_GETITEMCOUNT = 4100;
const uint LVM_GETITEMPOSITION = 4112;
const uint LVM_SETITEMPOSITION = 4111;
const uint LVM_GETITEMTEXT = 4141;
const ushort LVIF_TEXT = 1;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, out int lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int dwSize, out int lpNumberOfBytesWritten);
public Form1()
{
InitializeComponent();
}
private IntPtr CalculateCoordinates(int x, int y)
{
return (IntPtr)(x + (y << 16));
}
private void Arrange()
{
}
private IntPtr listviewWindow;
private void Form1_Load(object sender, EventArgs e)
{
// Check whether we're running under Vista or XP
Version version = Environment.OSVersion.Version;
if (version.Major != 6 && version.Major != 5)
{
MessageBox.Show("This application only works under Windows Vista or XP, and not "+ version.ToString());
Application.Exit();
}
// Find a handle to the listview that makes up the desktop
IntPtr programmanagerWindow = FindWindow(null, "Program Manager");
IntPtr desktopWindow = FindWindowEx(programmanagerWindow, IntPtr.Zero, "SHELLDLL_DefView", null);
listviewWindow = FindWindowEx(desktopWindow, IntPtr.Zero, "SysListView32", null);
}
private void ArrangeByPenis(IntPtr listviewWindow)
{
Size monitorSize = SystemInformation.PrimaryMonitorSize;
Graph g = new Graph();
// Convert the desktop listview handle to a handle reference so we can later use it in unsafe code
HandleRef desktopReference = new HandleRef(null, listviewWindow);
// Get the number of icons that's currently on the desktop
int iconCount = (int)SendMessage(desktopReference, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
double step = g.End / (double)iconCount;
double v = 0.0;
for (int i = 0; i < iconCount; i++)
{
Point p = g.getPoint(v, monitorSize.Width, monitorSize.Height);
SendMessage(desktopReference, LVM_SETITEMPOSITION, (IntPtr)i, CalculateCoordinates(p.x, p.y));
v += step;
}
}
struct LVITEM {
public UInt32 mask;
public int iItem;
public int iSubItem;
public UInt32 state;
public UInt32 stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public UInt32 lParam;
int iIndent;
int iGroupId;
uint cColumns;
IntPtr puColumns;
IntPtr piColFmt;
int iGroup;
};
private void StoreIconPositions(IntPtr listviewWindow)
{
// Get the process handle to the currently running explorer
Process[] processes = Process.GetProcessesByName("explorer");
LinkedList<IconInfo> iconInfos = new LinkedList<IconInfo>();
foreach (Process process in processes)
{
// Convert the desktop listview handle to a handle reference so we can later use it in unsafe code
HandleRef desktopReference = new HandleRef(null, listviewWindow);
// Allocate some memory in explorer's space so we can use that as temporary storage
IntPtr ptr = VirtualAllocEx(process.Handle, IntPtr.Zero, 8, MEM_COMMIT, PAGE_READWRITE);
IntPtr ipc_iconlabel = VirtualAllocEx(process.Handle, IntPtr.Zero, 100, MEM_COMMIT, PAGE_READWRITE);
IntPtr ipc_buffer = VirtualAllocEx(process.Handle, IntPtr.Zero, 300, MEM_COMMIT, PAGE_READWRITE);
// Get the number of icons that's currently on the desktop
int iconCount = (int)SendMessage(desktopReference, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
for (int i = 0; i < iconCount; i++)
{
// Send a window message to the desktop listview to get it to store the icon's position in our borrowed memory space
IntPtr x2 = SendMessage(desktopReference, LVM_GETITEMPOSITION, (IntPtr)i, (IntPtr)ptr);
// Copy the contents of the remote allocated array into our own memory space
byte[] b = new byte[8];
int f;
unsafe
{
fixed (byte* bp = b)
{
ReadProcessMemory(process.Handle, ptr, (IntPtr)bp, 8, out f);
}
}
int xpos = b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
int ypos = b[4] + (b[5] << 8) + (b[6] << 16) + (b[7] << 24);
LVITEM iconlabel = new LVITEM();
iconlabel.iSubItem = 0;
iconlabel.cchTextMax = 256;
iconlabel.mask = LVIF_TEXT;
iconlabel.pszText = ipc_buffer;
// Test code
/*string s1 = listView1.Items[0].Text;
unsafe
{
HandleRef xReference = new HandleRef(null, listView1.Handle);
LVITEM* bp = &iconlabel;
byte[] bx = new byte[300];
fixed (byte* bxp = bx)
{
iconlabel.pszText = (IntPtr)bxp;
IntPtr x23 = SendMessage(xReference, LVM_GETITEMTEXT, (IntPtr)0, (IntPtr)bp);
}
}*/
unsafe
{
LVITEM* bp = &iconlabel;
WriteProcessMemory(process.Handle, ipc_iconlabel, (IntPtr)bp, 100, out f);
}
int iconNameLength = (int)SendMessage(desktopReference, LVM_GETITEMTEXT, (IntPtr)i, ipc_iconlabel);
// Copy the contents of the remote allocated array into our own memory space
byte[] b2 = new byte[300];
unsafe
{
fixed (byte* bp = b2)
{
ReadProcessMemory(process.Handle, ipc_buffer, (IntPtr)bp, 300, out f);
}
}
char[] c2 = new char[iconNameLength];
for (int j = 0; j < iconNameLength; j++)
c2[j] = (char)b2[j];
String iconName = new String(c2);
IconInfo ii = new IconInfo(iconName, xpos, ypos);
iconInfos.AddLast(ii);
}
string folder = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Arrange\\";
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
Stream stream = File.Open(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Arrange\\backup.dat", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, iconInfos);
stream.Close();
}
}
private void RestoreIconPositions(IntPtr listviewWindow)
{
Stream stream = File.Open(System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Arrange\\backup.dat", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
LinkedList<IconInfo> iconInfos = (LinkedList<IconInfo>)bformatter.Deserialize(stream);
stream.Close();
// Get the process handle to the currently running explorer
Process[] processes = Process.GetProcessesByName("explorer");
foreach (Process process in processes)
{
// Convert the desktop listview handle to a handle reference so we can later use it in unsafe code
HandleRef desktopReference = new HandleRef(null, listviewWindow);
// Allocate some memory in explorer's space so we can use that as temporary storage
IntPtr ptr = VirtualAllocEx(process.Handle, IntPtr.Zero, 8, MEM_COMMIT, PAGE_READWRITE);
IntPtr ipc_iconlabel = VirtualAllocEx(process.Handle, IntPtr.Zero, 100, MEM_COMMIT, PAGE_READWRITE);
IntPtr ipc_buffer = VirtualAllocEx(process.Handle, IntPtr.Zero, 300, MEM_COMMIT, PAGE_READWRITE);
// Get the number of icons that's currently on the desktop
int iconCount = (int)SendMessage(desktopReference, LVM_GETITEMCOUNT, IntPtr.Zero, IntPtr.Zero);
for (int i = 0; i < iconCount; i++)
{
int f;
LVITEM iconlabel = new LVITEM();
iconlabel.iSubItem = 0;
iconlabel.cchTextMax = 256;
iconlabel.mask = LVIF_TEXT;
iconlabel.pszText = ipc_buffer;
unsafe
{
LVITEM* bp = &iconlabel;
WriteProcessMemory(process.Handle, ipc_iconlabel, (IntPtr)bp, 100, out f);
}
int iconNameLength = (int)SendMessage(desktopReference, LVM_GETITEMTEXT, (IntPtr)i, ipc_iconlabel);
// Copy the contents of the remote allocated array into our own memory space
byte[] b2 = new byte[300];
unsafe
{
fixed (byte* bp = b2)
{
ReadProcessMemory(process.Handle, ipc_buffer, (IntPtr)bp, 300, out f);
}
}
char[] c2 = new char[iconNameLength];
for (int j = 0; j < iconNameLength; j++)
c2[j] = (char)b2[j];
String iconName = new String(c2);
foreach (IconInfo ii in iconInfos)
{
if (ii.name == iconName)
{
SendMessage(desktopReference, LVM_SETITEMPOSITION, (IntPtr)i, CalculateCoordinates(ii.x, ii.y));
break;
}
}
}
}
}
private void saveButton_Click(object sender, EventArgs e)
{
StoreIconPositions(listviewWindow);
}
private void arrangeButton_Click(object sender, EventArgs e)
{
ArrangeByPenis(listviewWindow);
}
private void button2_Click(object sender, EventArgs e)
{
RestoreIconPositions(listviewWindow);
}
private void cancelButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ArrangeByPenis
{
class Graph
{
class Node
{
public double sample;
public double x;
public double y;
public Node(double s, double _x, double _y)
{
sample = s;
x = _x;
y = _y;
}
}
LinkedList<Node> points;
double end;
public double End
{
get { return end; }
}
public Graph()
{
points = new LinkedList<Node>();
points.AddLast(new Node(0.0, 0.75, 0.78));
points.AddLast(new Node(1.0, 0.75, 0.86));
points.AddLast(new Node(3.0, 0.60, 0.9));
points.AddLast(new Node(4.0, 0.56, 0.83));
points.AddLast(new Node(5.5, 0.59, 0.64));
points.AddLast(new Node(6.5, 0.69, 0.53));
points.AddLast(new Node(8.5, 0.69, 0.22));
points.AddLast(new Node(10.5, 0.72, 0.08));
points.AddLast(new Node(11, 0.75, 0.04));
points.AddLast(new Node(11.5, 0.78, 0.08));
points.AddLast(new Node(13.5, 0.81, 0.22));
points.AddLast(new Node(15.5, 0.81, 0.53));
points.AddLast(new Node(16.5, 0.91, 0.64));
points.AddLast(new Node(18.0, 0.94, 0.83));
points.AddLast(new Node(19.0, 0.91, 0.9));
points.AddLast(new Node(21.0, 0.75, 0.86));
points.AddLast(new Node(21.0, 0.69, 0.32));
points.AddLast(new Node(22.0, 0.75, 0.29));
points.AddLast(new Node(23.0, 0.81, 0.32));
end = 23.0;
}
public Point getPoint(double v, int width, int height)
{
Node lastNode = null;
foreach (Node n in points)
{
if (n.sample == v)
return new Point((int)(n.x * width), (int)(n.y * height));
else if (n.sample > v)
{
double f = (v - lastNode.sample) / (n.sample - lastNode.sample);
double x = lastNode.x + f * (n.x - lastNode.x);
double y = lastNode.y + f * (n.y - lastNode.y);
return new Point((int)(x * width), (int)(y * height));
}
lastNode = n;
}
throw new Exception("unexpected argument on getPoint: " + v);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace ArrangeByPenis
{
[Serializable()]
class IconInfo : ISerializable
{
public string name;
public int x;
public int y;
public IconInfo(string n, int _x, int _y)
{
name = n;
x = _x;
y = _y;
}
public IconInfo(SerializationInfo info, StreamingContext ctxt)
{
name = (string)info.GetValue("name", typeof(string));
x = (int)info.GetValue("x", typeof(int));
y = (int)info.GetValue("y", typeof(int));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("name", name);
info.AddValue("x", x);
info.AddValue("y", y);
}
}
}
namespace ArrangeByPenis
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.saveButton = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.arrangeButton = new System.Windows.Forms.Button();
this.cancelButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// saveButton
//
this.saveButton.Location = new System.Drawing.Point(12, 12);
this.saveButton.Name = "saveButton";
this.saveButton.Size = new System.Drawing.Size(155, 23);
this.saveButton.TabIndex = 0;
this.saveButton.Text = "&Save current Icon Layout";
this.saveButton.UseVisualStyleBackColor = true;
this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(188, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(155, 23);
this.button2.TabIndex = 1;
this.button2.Text = "&Restore saved icon layout";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// arrangeButton
//
this.arrangeButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.arrangeButton.Location = new System.Drawing.Point(12, 57);
this.arrangeButton.Name = "arrangeButton";
this.arrangeButton.Size = new System.Drawing.Size(155, 23);
this.arrangeButton.TabIndex = 2;
this.arrangeButton.Text = "Arrange by &Penis";
this.arrangeButton.UseVisualStyleBackColor = true;
this.arrangeButton.Click += new System.EventHandler(this.arrangeButton_Click);
//
// cancelButton
//
this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Location = new System.Drawing.Point(268, 57);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Size = new System.Drawing.Size(75, 23);
this.cancelButton.TabIndex = 3;
this.cancelButton.Text = "Cancel";
this.cancelButton.UseVisualStyleBackColor = true;
this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(355, 92);
this.Controls.Add(this.cancelButton);
this.Controls.Add(this.arrangeButton);
this.Controls.Add(this.button2);
this.Controls.Add(this.saveButton);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Arrange";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button saveButton;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button arrangeButton;
private System.Windows.Forms.Button cancelButton;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ArrangeByPenis
{
class Point
{
public int x;
public int y;
public Point(int _x, int _y)
{
x = _x;
y = _y;
}
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace ArrangeByPenis
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
@miramardesign
Copy link

i dont think this is a bat file but a c# file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment