非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

C# バーチャルデスクトップ、とりあえずファイルの同期は取れた

      2015/12/29

前回のFormを最背面にできたところからの続きです。

Formを常に最背面にしておくには
http://non-it-salaryman.blogspot.jp/2015/11/c-form.html

バーチャルデスクトップにするためには、指定のフォルダ内のファイルと動機が取れていなければなりません。これは難しくなく、System.IO.Directory.GetFilesでファイルリストを取得すればOK。あとはアイコンを生成すればいいわけです。アイコンはPictureBoxを使っていますが、画像のセンタリングができておらず、文字と共に左寄せ状態。これは今後対策していく必要があります。

まずは、下の図のように指定フォルダ内のファイルと同期がとれるようになりました。今後はこれらをダブルクリックするとファイルが開けるようにしたいと思います。

以下がコード。(長いです。)

MainWindow.cs

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.IO;

class Test{
static void Main(){
Application.Run(new FormMain());
}
}

public class FormMain : Form{
private Panel pane;
private Label label;
private List<IconPanel> iconPaneList;

const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int SC_SIZE = 0xF000;

    //最前面
    [DllImport(“USER32.DLL”, CharSet=CharSet.Auto)]
    private static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport(“USER32.DLL”, CharSet=CharSet.Auto)]
    private static extern System.IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

//Form内で自由に移動
[DllImport(“User32.dll”, EntryPoint = “SendMessage”)]
extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport(“User32.dll”)]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport(“User32.dll”)]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
[DllImport(“User32.dll”)]
public static extern bool SetCapture(IntPtr hWnd);
[DllImport(“user32.dll”)]
public static extern bool ReleaseCapture();

public FormMain(){
this.StartPosition = FormStartPosition.CenterScreen;
this.Width = Screen.PrimaryScreen.Bounds.Width;
this.Height = Screen.PrimaryScreen.Bounds.Height;
this.BackColor = Color.SteelBlue;
this.WindowState = FormWindowState.Maximized;
this.DoubleClick += new EventHandler(FormDoubleClicked);
this.FormBorderStyle = FormBorderStyle.None;
this.ShowInTaskbar = false;

pane = new Panel(){
Size = new Size(200, 150),
BackColor = Color.Blue,
Location = new Point(500, 10),
Parent = this,
};
pane.MouseDown += new MouseEventHandler(this.Panel_MouseDown);

label = new Label(){
Text = “これは付箋紙。製作中…”,
Dock = DockStyle.Fill,
BackColor = Color.LightPink,
BorderStyle = BorderStyle.FixedSingle,
Parent=pane,
};
label.MouseDown += new MouseEventHandler(this.Panel_MouseDown);

this.showIcon();

        System.IntPtr hProgramManagerHandle = FindWindow(null, “Program Manager”);

        // 正しく取得できた場合は、Program Manager を親ウィンドウに設定する
        if (! hProgramManagerHandle.Equals(System.IntPtr.Zero)) {
            SetParent(this.Handle, hProgramManagerHandle);
        }
}

private void showIcon(){
iconPaneList = new List<IconPanel>();

string[] files = Directory.GetFiles(“C:\…\desktop”);

for (int i=0; i< files.Length; i++){
iconPaneList.Add(new IconPanel(files[i]));
iconPaneList[iconPaneList.Count-1].Parent = this;
iconPaneList[iconPaneList.Count-1].setting(i, 10, 10+(70*i));
iconPaneList[iconPaneList.Count-1].MouseDown += new MouseEventHandler(this.Icon_MouseDown);
}
}

private void Panel_MouseDown(object sender, MouseEventArgs e){
SetCapture(pane.Handle);
ReleaseCapture();
SendMessage(pane.Handle, WM_SYSCOMMAND, SC_MOVE | 2, 0);
}

private void Icon_MouseDown(object sender, MouseEventArgs e){
int index = ((IconPanel)sender).getIndex();
SetCapture(iconPaneList[index].Handle);
ReleaseCapture();
SendMessage(iconPaneList[index].Handle, WM_SYSCOMMAND, SC_MOVE | 2, 0);
}

private void FormDoubleClicked(object sender, EventArgs e){
Application.Exit();
}
}

IconPanel.cs

using System;
using System.Drawing;
using System.Windows.Forms;

public class IconPanel : PictureBox{
private int index;

public IconPanel(string path){
this.Size = new Size(80, 60);
this.index = -1;

Icon appIcon = Icon.ExtractAssociatedIcon(path);
this.Image = appIcon.ToBitmap();

Label label = new Label(){
Dock = DockStyle.Bottom,
ForeColor = Color.White,
Parent=this,
};
this.Controls.Add(label);
label.Text = System.IO.Path.GetFileName(path);
}

public void setting(int index, int w, int y){
this.index = index;
this.Location = new Point(w, y);
}

public int getIndex(){ return index; }
}

スポンサーリンク

 - C#応用編, 自作アプリ紹介