0

(問題を説明する短いスクリーンキャスト)

クリップボードの内容を確認できるクリップボード プログラムを作成しています。

次のようになります。 ここに画像の説明を入力

オンザフライで正常にコピーできるようです。

問題は、クリップボード内の以前の img/txt に戻って使用できるようにしたいということです。それは、チェックマーク ボタンを使用するときです。

それは機能しますが、唯一の問題は、使用している画像/リストボックスのリストに2回コピーすることです。リストボックス/ピクチャボックスを初期化するときにも発生します。

コードは次のとおりです。

namespace Clipboard_Wizard
{
public partial class FormMain : Form
{
    //register the program in the clipboard viewer chain
    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

    private const int WM_DRAWCLIPBOARD = 0x0308;        // WM_DRAWCLIPBOARD message
    private IntPtr _clipboardViewerNext;                // Our variable that will hold the value to identify the next window in the clipboard viewer chain

    private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        ChangeClipboardChain(this.Handle, _clipboardViewerNext);        // Removes our from the chain of clipboard viewers when the form closes.
    }

    List<Image> img = new List<Image>();
    int ImgCount = 0;
    int ImgIndex = -1;
    Image tmp;

    public FormMain()
    {
        InitializeComponent();

        _clipboardViewerNext = SetClipboardViewer(this.Handle);      // Adds our form to the chain of clipboard viewers.

        //set listbox/picturebox to whatever already on clipboard
        if (Clipboard.ContainsImage())
        {
            tmp = Clipboard.GetImage();
            pictureBox1.Image = tmp;
            img.Add(tmp);
            ImgCount++;
            ImgIndex++;
            btnSlctImg.Enabled = true;
            label3.Text = "Image 1 / 1";
        }
        else if (Clipboard.ContainsText())
        {
            listBox1.Items.Add(Clipboard.GetText());
        }
    }

    // clears everything from the form and the clipboard
    private void btnClear_Click(object sender, EventArgs e)
    {
        Clipboard.Clear();
        listBox1.Items.Clear();
        img.Clear();
        pictureBox1.Image = null;
        ImgCount = 0;
        ImgIndex = -1;
        btnSlctImg.Enabled = false;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        /*if (Clipboard.ContainsImage())
            {
                tmp = Clipboard.GetImage();
                pictureBox1.Image = tmp;
                img.Add(tmp);
                ImgCount++;
                ImgIndex = ImgCount - 1;  

            }
            else if (Clipboard.ContainsText())
            {
                listBox1.Items.Add(Clipboard.GetText());
                listBox1.TopIndex = listBox1.Items.Count - 1;
            }*/
    }



    private void btnUp_Click(object sender, EventArgs e)
    {
        if(ImgIndex == -1)
        {
            MessageBox.Show("No image.");
        }
        else if (ImgIndex < ImgCount - 1)
        {
            ImgIndex++;
            pictureBox1.Image = img[ImgIndex];
            label3.Text = "Image " + (ImgIndex + 1).ToString() + " / " + ImgCount.ToString() ;
        }
        else
        {
            MessageBox.Show("This is the last image.");
        }
    }

    private void btnDown_Click(object sender, EventArgs e)
    {
        if(ImgIndex == -1)
        {
            MessageBox.Show("No image.");
        }
        else if (ImgIndex > 0)
        {
            ImgIndex--;
            pictureBox1.Image = img[ImgIndex];
            label3.Text = "Image " + (ImgIndex + 1).ToString() + " / " + ImgCount.ToString();
        }
        else
        {
            MessageBox.Show("This is the first image.");
        }
    }

    private void btnDeselect_Click(object sender, EventArgs e)
    {
        listBox1.SelectedIndex = -1;
    }

    //sets clipboard to selected txt from listbox
    private void btnSlct_Click(object sender, EventArgs e)
    {
        string slctTxt = listBox1.SelectedItem.ToString();
        if (slctTxt != null || slctTxt != "")
        {
            Clipboard.SetText(slctTxt);
        }
    }

    //sets clipboard to selected image
    private void btnSlctImg_Click(object sender, EventArgs e)
    {
        Image slctImg = pictureBox1.Image;
        if (slctImg != null)
        {
            Clipboard.SetImage(slctImg); 
        }

    }

    private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex != -1)
        {
            btnSlctTxt.Enabled = true;
        }
        else
        {
            btnSlctTxt.Enabled = false;
        }
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);    // Process the message 

        if (m.Msg == WM_DRAWCLIPBOARD)
        {
            //btnUpdate.PerformClick();

            IDataObject iData = Clipboard.GetDataObject();      // Clipboard's data

            if (iData.GetDataPresent(DataFormats.Text))
            {
                string text = (string)iData.GetData(DataFormats.Text);      // Clipboard text
                listBox1.Items.Add(text);
                listBox1.TopIndex = listBox1.Items.Count - 1;
            }
            else if (iData.GetDataPresent(DataFormats.Bitmap))
            {
                tmp = (Bitmap)iData.GetData(DataFormats.Bitmap);   // Clipboard image
                pictureBox1.Image = tmp;
                img.Add(tmp);
                ImgCount++;
                ImgIndex = ImgCount - 1;
                label3.Text = "Image " + ImgCount.ToString() + " / " + ImgCount.ToString();
                btnSlctImg.Enabled = true;  
            }
        }
    }

}
}

更新: Clipboard.SetImage(...)orを呼び出すたびに問題があるようですClipboard.SetText(...)- それは 2 回実行されます。それでも理由はわかりません。

4

2 に答える 2