[C#]デスクトップのアイコンの移動

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Fox
記事: 1
登録日時: 11年前

[C#]デスクトップのアイコンの移動

#1

投稿記事 by Fox » 11年前

デスクトップのアイコンを移動しようとしているのですが
うまく移動させることができません。

コード:

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace test
{
    public class ListViewEx : ListView
    {
        public ListViewEx()
        {
            this.AutoArrange = false;
        }

        [DefaultValue(false)]
        public new bool AutoArrange
        {
            get
            {
                return base.AutoArrange;
            }
            set
            {
                base.AutoArrange = value;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct tagPOINT
        {
            int x;
            int y;
            public tagPOINT(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
        }
        [DllImport("user32.dll")]
        private extern static int SendMessage
          (IntPtr hWnd, uint message, int index, ref tagPOINT p);

        private const uint LVM_FIRST = 0x1000;
        private const uint LVM_SETITEMPOSITION32 = LVM_FIRST + 49;

        public void SetItemPosition(IntPtr hwnd,int index, Point point)
        {
            //if (index < 0 || this.Items.Count <= index)
            //    throw new ArgumentOutOfRangeException("index");

            tagPOINT p = new tagPOINT(point.X, point.Y);
            Console.WriteLine("移動します");
            SendMessage(hwnd, LVM_SETITEMPOSITION32, index, ref p);
        }
    }

    class TestClass
    {
        

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string strclassName, string strWindowName);

        [DllImport("user32", EntryPoint = "FindWindowEx")]
        public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);

        static void Main(string[] args)
        {
            IntPtr hWnd;

            hWnd = FindWindow("Progman", "Program Manager");
            hWnd = FindWindowEx(hWnd, IntPtr.Zero, "SHELLDLL_DefView", null);
            hWnd = FindWindowEx(hWnd, IntPtr.Zero, "SysListView32", null);  //デスクトップ

            ListViewEx p = new ListViewEx();
            p.SetItemPosition(hWnd,1, new Point(60,60));
            Console.WriteLine("終わりました");
            Console.ReadKey();
        }
    }
}

※テストなので移動するのは最初のものだけになっています

上のコードを実行して見るとデスクトップから指定のアイコンが消えてなくなっていたり、
エクスプローラを再起動しますなどのメッセージが出てきて止まってしまいます。

これらのエラーはどうしたら治るのかご教授お願いいたします。

OS :windows7

“C言語何でも質問掲示板” へ戻る