none
Xaml应用能不能从剪贴板获得图像或文本?(C++) RRS feed

答案

  • 可以的,参考一下我同事写的一个博客 http://blogs.msdn.com/b/windows8devsupport/archive/2014/04/17/how-to-use-clipboard-class-in-windows-store-apps-.aspx

    代码是C#的,不过C++是一样的写法。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月4日 5:53
    版主
  • 我已经成功了,你最近回复怎么这么慢啊?

    代码:

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	if (!b->Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::Bitmap)){
    		return;
    	}
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    		create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    			auto m_bm = ref new WriteableBitmap(1, 1);
    			m_bm->SetSource(d);
    			int height = m_bm->PixelHeight, width = m_bm->PixelWidth;
    			wb_ = ref new WriteableBitmap(m_bm->PixelWidth, m_bm->PixelHeight);
    			unsigned int length;
    			byte* temp_m_bm = GetPointerToPixelData(m_bm->PixelBuffer, &length);
    			byte* temp_wb_ = GetPointerToPixelData(wb_->PixelBuffer, &length);
    			for (unsigned int k = 0; k <unsigned int(height); k++){
    				for (unsigned int i = 0; i < unsigned int(width * 4); i += 4){
    					int pos = k * (width * 4) + (i);
    					temp_wb_[pos + 0] = temp_m_bm[pos + 0];
    					temp_wb_[pos + 1] = temp_m_bm[pos + 1];
    					temp_wb_[pos + 2] = temp_m_bm[pos + 2];
    					temp_wb_[pos + 3] = temp_m_bm[pos + 3];
    				}
    			}
    			Pic->Source = wb_;
    		});
    	});

    	auto saving = Windows::Storage::ApplicationData::Current->RoamingSettings;
    	int elseing = 1;//保存
    	if (saving->Values->HasKey("P4::ClipBoardSave")){
    		Object^ saveValue = saving->Values->Lookup("P4::ClipBoardSave");
    		if (saveValue != nullptr){
    			elseing = 0;
    			create_task(Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->
    				GetFileAsync(saveValue->ToString())).then([this](Windows::Storage::StorageFile^ file){
    				if (file != nullptr){
    					create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([this](IRandomAccessStream^ stream){
    						create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream)).then([this](BitmapEncoder^ encoder){
    							Array<unsigned char>^ pixles = ref new Array < unsigned char >{};
    							Windows::Security::Cryptography::CryptographicBuffer::CopyToByteArray(wb_->PixelBuffer, &pixles);
    							encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, wb_->PixelWidth, wb_->PixelHeight, 96.0, 96.0, pixles);
    							encoder->FlushAsync();
    							//保存完毕,开始写入剪贴板
    							int elseing = 1;
    							auto saving = Windows::Storage::ApplicationData::Current->RoamingSettings;
    							if (saving->Values->HasKey("P4::ClipBoardOpen")){
    								Object^ saveValue = saving->Values->Lookup("P4::ClipBoardOpen");
    								if (saveValue != nullptr){
    									elseing = 0;
    									create_task(Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->
    										GetFileAsync(saveValue->ToString())).then([this](Windows::Storage::StorageFile^ file){
    										auto DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    										//	Windows::Storage::Streams::RandomAccessStreamReference^ RASR =
    										//		ref new Windows::Storage::Streams::RandomAccessStreamReference();
    										//	RASR->CreateFromFile(file);
    										//	DP->SetBitmap(RASR);
    										DP->SetBitmap(Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(file));
    
    										Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    										CB->SetContent(DP);
    									});
    								}
    							}
    							if (elseing == 1){//需要得到mruToken
    								auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker();
    								openPicker->FileTypeFilter->Clear();
    								openPicker->FileTypeFilter->Append("*");
    								openPicker->FileTypeFilter->Append(".jpg");
    								create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file){
    									auto DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    									//	Windows::Storage::Streams::RandomAccessStreamReference^ RASR =
    									//		ref new Windows::Storage::Streams::RandomAccessStreamReference();
    									//	RASR->CreateFromFile(file);
    									//	DP->SetBitmap(RASR);
    									DP->SetBitmap(Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(file));
    
    									Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    									CB->SetContent(DP);
    									auto saveValueString = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->Add(file);
    									Windows::Storage::ApplicationData::Current->RoamingSettings->Values->Insert("P4::ClipBoardOpen", saveValueString);
    								});
    							}
    							//粘贴完毕,将要自动返回
    						});
    					});
    				}
    				else{
    					T9->Text = "文件已损坏,请自行修改";
    				}
    			});
    		}
    	}
    	if (elseing == 1){//运行后直接返回,不进行粘贴
    		auto Picker = ref new Windows::Storage::Pickers::FileSavePicker();
    		auto plainTextExtensions = ref new Platform::Collections::Vector<String^>();
    		plainTextExtensions->Append(".jpg");
    		Picker->FileTypeChoices->Insert("JPG File", plainTextExtensions);
    		create_task(Picker->PickSaveFileAsync()).then([this](Windows::Storage::StorageFile^ file){
    			if (file != nullptr){
    				auto saveValueString = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->Add(file);
    				Windows::Storage::ApplicationData::Current->RoamingSettings->Values->Insert("P4::ClipBoardSave", saveValueString);
    				create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([this](IRandomAccessStream^ stream){
    					create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream)).then([this](BitmapEncoder^ encoder){
    						Array<unsigned char>^ pixles = ref new Array < unsigned char >{};
    						Windows::Security::Cryptography::CryptographicBuffer::CopyToByteArray(wb_->PixelBuffer, &pixles);
    						encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, wb_->PixelWidth, wb_->PixelHeight, 96.0, 96.0, pixles);
    						encoder->FlushAsync();
    					});
    				});
    			}
    		});
    	}

    2014年6月9日 14:07

全部回复

  • 可以的,参考一下我同事写的一个博客 http://blogs.msdn.com/b/windows8devsupport/archive/2014/04/17/how-to-use-clipboard-class-in-windows-store-apps-.aspx

    代码是C#的,不过C++是一样的写法。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月4日 5:53
    版主
  • 我的代码是:

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    
    	});

    怎样将Windows::Storage::Streams::RandomAccessStreamReference^转换为图像?
    图像是WriteableBitmap类型,也可以是Image控件的Source。
    2014年6月5日 4:22
  • 现在的问题是这样的:

    代码:

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    		create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    			Windows::Storage::Streams::IBuffer^ e;
    			create_task(d->ReadAsync(e, 0 - 1, Windows::Storage::Streams::InputStreamOptions::Partial))
    				.then([this](Windows::Storage::Streams::IBuffer^){//标记1
    				Windows::Storage::Streams::IRandomAccessStream^ h;
    				h->WriteAsync(f);
    				wb_->SetSource(h);
    			});
    		});
    	});
    在程序运行到标记1时,出现错误,自动停止。

    跳转到App.g.hpp里,代码:

    void ::实验::App::InitializeComponent()
    {
    #if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
        UnhandledException += ref new Windows::UI::Xaml::UnhandledExceptionEventHandler(
            [](::Platform::Object^ sender, ::Windows::UI::Xaml::UnhandledExceptionEventArgs^ e)
            {
                (void)sender; // Unused parameter
                if (IsDebuggerPresent())
                {
                    ::Platform::String^ errorMessage = e->Message;
                    __debugbreak();//停止在这一行
                }
            });
    #endif
    }
    怎么回事?

    2014年6月6日 4:35
  • 还有,保存图片似乎也不成功。

    代码:(其中Pic是Image控件)

    	auto PBU = Pic->BaseUri;
    	auto PUBS = PBU->ToString();
    	create_task(Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(PBU))
    		.then([this](Windows::Storage::StorageFile^ SF){
    		Windows::ApplicationModel::DataTransfer::DataPackage^ DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    		DP->SetBitmap(Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(SF));
    		Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    		CB->SetContent(DP);
    	});

    但是Pic->BaseUri并不是图片的Uri。如果用别的方法,那么代码是:

    	Windows::Storage::StorageFile^ SF;
    	Windows::ApplicationModel::DataTransfer::DataPackage^ DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    
    	//需要将Pic或wb_转为Windows::Storage::Streams::RandomAccessStreamReference^
    
    	Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    	CB->SetContent(DP);
    我不知道怎么将图片转换为Windows::Storage::Streams::RandomAccessStreamReference^(wb_是WriteableBitmap^)

    2014年6月8日 3:34
  • 我已经成功了,你最近回复怎么这么慢啊?

    代码:

    	Windows::ApplicationModel::DataTransfer::Clipboard^ a;
    	auto b = a->GetContent();
    	if (!b->Contains(Windows::ApplicationModel::DataTransfer::StandardDataFormats::Bitmap)){
    		return;
    	}
    	create_task(b->GetBitmapAsync()).then([this](Windows::Storage::Streams::RandomAccessStreamReference^ c){
    		create_task(c->OpenReadAsync()).then([this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ d){
    			auto m_bm = ref new WriteableBitmap(1, 1);
    			m_bm->SetSource(d);
    			int height = m_bm->PixelHeight, width = m_bm->PixelWidth;
    			wb_ = ref new WriteableBitmap(m_bm->PixelWidth, m_bm->PixelHeight);
    			unsigned int length;
    			byte* temp_m_bm = GetPointerToPixelData(m_bm->PixelBuffer, &length);
    			byte* temp_wb_ = GetPointerToPixelData(wb_->PixelBuffer, &length);
    			for (unsigned int k = 0; k <unsigned int(height); k++){
    				for (unsigned int i = 0; i < unsigned int(width * 4); i += 4){
    					int pos = k * (width * 4) + (i);
    					temp_wb_[pos + 0] = temp_m_bm[pos + 0];
    					temp_wb_[pos + 1] = temp_m_bm[pos + 1];
    					temp_wb_[pos + 2] = temp_m_bm[pos + 2];
    					temp_wb_[pos + 3] = temp_m_bm[pos + 3];
    				}
    			}
    			Pic->Source = wb_;
    		});
    	});

    	auto saving = Windows::Storage::ApplicationData::Current->RoamingSettings;
    	int elseing = 1;//保存
    	if (saving->Values->HasKey("P4::ClipBoardSave")){
    		Object^ saveValue = saving->Values->Lookup("P4::ClipBoardSave");
    		if (saveValue != nullptr){
    			elseing = 0;
    			create_task(Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->
    				GetFileAsync(saveValue->ToString())).then([this](Windows::Storage::StorageFile^ file){
    				if (file != nullptr){
    					create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([this](IRandomAccessStream^ stream){
    						create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream)).then([this](BitmapEncoder^ encoder){
    							Array<unsigned char>^ pixles = ref new Array < unsigned char >{};
    							Windows::Security::Cryptography::CryptographicBuffer::CopyToByteArray(wb_->PixelBuffer, &pixles);
    							encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, wb_->PixelWidth, wb_->PixelHeight, 96.0, 96.0, pixles);
    							encoder->FlushAsync();
    							//保存完毕,开始写入剪贴板
    							int elseing = 1;
    							auto saving = Windows::Storage::ApplicationData::Current->RoamingSettings;
    							if (saving->Values->HasKey("P4::ClipBoardOpen")){
    								Object^ saveValue = saving->Values->Lookup("P4::ClipBoardOpen");
    								if (saveValue != nullptr){
    									elseing = 0;
    									create_task(Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->
    										GetFileAsync(saveValue->ToString())).then([this](Windows::Storage::StorageFile^ file){
    										auto DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    										//	Windows::Storage::Streams::RandomAccessStreamReference^ RASR =
    										//		ref new Windows::Storage::Streams::RandomAccessStreamReference();
    										//	RASR->CreateFromFile(file);
    										//	DP->SetBitmap(RASR);
    										DP->SetBitmap(Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(file));
    
    										Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    										CB->SetContent(DP);
    									});
    								}
    							}
    							if (elseing == 1){//需要得到mruToken
    								auto openPicker = ref new Windows::Storage::Pickers::FileOpenPicker();
    								openPicker->FileTypeFilter->Clear();
    								openPicker->FileTypeFilter->Append("*");
    								openPicker->FileTypeFilter->Append(".jpg");
    								create_task(openPicker->PickSingleFileAsync()).then([this](Windows::Storage::StorageFile^ file){
    									auto DP = ref new Windows::ApplicationModel::DataTransfer::DataPackage();
    									//	Windows::Storage::Streams::RandomAccessStreamReference^ RASR =
    									//		ref new Windows::Storage::Streams::RandomAccessStreamReference();
    									//	RASR->CreateFromFile(file);
    									//	DP->SetBitmap(RASR);
    									DP->SetBitmap(Windows::Storage::Streams::RandomAccessStreamReference::CreateFromFile(file));
    
    									Windows::ApplicationModel::DataTransfer::Clipboard^ CB;
    									CB->SetContent(DP);
    									auto saveValueString = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->Add(file);
    									Windows::Storage::ApplicationData::Current->RoamingSettings->Values->Insert("P4::ClipBoardOpen", saveValueString);
    								});
    							}
    							//粘贴完毕,将要自动返回
    						});
    					});
    				}
    				else{
    					T9->Text = "文件已损坏,请自行修改";
    				}
    			});
    		}
    	}
    	if (elseing == 1){//运行后直接返回,不进行粘贴
    		auto Picker = ref new Windows::Storage::Pickers::FileSavePicker();
    		auto plainTextExtensions = ref new Platform::Collections::Vector<String^>();
    		plainTextExtensions->Append(".jpg");
    		Picker->FileTypeChoices->Insert("JPG File", plainTextExtensions);
    		create_task(Picker->PickSaveFileAsync()).then([this](Windows::Storage::StorageFile^ file){
    			if (file != nullptr){
    				auto saveValueString = Windows::Storage::AccessCache::StorageApplicationPermissions::MostRecentlyUsedList->Add(file);
    				Windows::Storage::ApplicationData::Current->RoamingSettings->Values->Insert("P4::ClipBoardSave", saveValueString);
    				create_task(file->OpenAsync(FileAccessMode::ReadWrite)).then([this](IRandomAccessStream^ stream){
    					create_task(BitmapEncoder::CreateAsync(BitmapEncoder::JpegEncoderId, stream)).then([this](BitmapEncoder^ encoder){
    						Array<unsigned char>^ pixles = ref new Array < unsigned char >{};
    						Windows::Security::Cryptography::CryptographicBuffer::CopyToByteArray(wb_->PixelBuffer, &pixles);
    						encoder->SetPixelData(BitmapPixelFormat::Bgra8, BitmapAlphaMode::Ignore, wb_->PixelWidth, wb_->PixelHeight, 96.0, 96.0, pixles);
    						encoder->FlushAsync();
    					});
    				});
    			}
    		});
    	}

    2014年6月9日 14:07
  • 多谢对论坛的支持,最近有点忙,不好意思,之后会继续给大家提供建议的,虽然我对C++不是很懂,不过这段时间我也在学习:)

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2014年6月10日 1:40
    版主
  • 对了,怎么样才能按照博客上的方法将图像(WriteableBitmap)写入剪贴板啊?(我原来的做法是通过保存文件然后写入,但是我希望更改这种方法)

    C++和C#的解决方案都可以

    2015年2月17日 7:39