2011年3月1日 星期二

WPF 拖放

請看下列文章 Overview:
http://msdn.microsoft.com/zh-tw/library/ms742859.aspx

這篇是範例:
http://www.wpftutorial.net/DragAndDrop.html

Drag&Drop in 6 Steps

  1. Detect a drag as a combinatination of MouseMove and MouseLeftButtonDown
  2. Find the data you want to drag and create a DataObject that contains the format, the data and the allowed effects.
  3. Initiate the dragging by calling DoDragDrop()
  4. Set the AllowDrop property to True on the elements you want to allow dropping.
  5. Register a handler to the DragEnter event to detect a dragging over the drop location. Check the format and the data by calling GetDataPresent() on the event args. If the data can be dropped, set the Effect property on the event args to display the appropriate mouse cursor.
  6. When the user releases the mouse button the DragDrop event is called. Get the data by calling the GetData() method on the Data object provided in the event args. 

Example Code:

XAML  ---------------------------------------------------------

<Window x:Class="WpfDragDrop.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="237" Width="284">
    <Grid>
        <Label Content="DragSource" Height="28" HorizontalAlignment="Left" Margin="29,12,0,0" 
               Name="label1" VerticalAlignment="Top" 
               PreviewMouseLeftButtonDown="label1_PreviewMouseLeftButtonDown" 
               PreviewMouseMove="label1_PreviewMouseMove" />
        
        <Label Content="Label" Height="28" HorizontalAlignment="Left" Margin="79,78,0,0" 
               Name="label2" VerticalAlignment="Top" Drop="label2_Drop" AllowDrop="True" />
    </Grid>
</Window>

C# ---------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDragDrop
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private Point startPoint;

        private void label1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPoint = e.GetPosition(null);
        }

        private void label1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                //// Get the dragged ListViewItem
                //ListView listView = sender as ListView;
                //ListViewItem listViewItem =
                //    FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource);

                //// Find the data behind the ListViewItem
                //object contact = (object)listView.ItemContainerGenerator.
                //    ItemFromContainer(listViewItem);

                // Initialize the drag & drop operation
                DataObject dragData = new DataObject(DataFormats.Text, label1.Content);
                DragDrop.DoDragDrop(label1, dragData, DragDropEffects.Move);
            }
        }

        private void label2_Drop(object sender, DragEventArgs e)
        {
            label2.Content = e.Data.GetData(DataFormats.Text);
        }

        private static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);
            return null;
        }
    }
}


2011年2月13日 星期日

下面是我花了一天的研究成果!

下面這段程式是用 C++ 啟動 .Net Runtime 的範例,利用這範例可以寫出偵測戶端有沒有安裝 .Net Framework 的前導程式。我知道很難理解,由其是沒寫過 C++ 的人...


 參考來源:http://msdn.microsoft.com/zh-tw/library/dd380851.aspx

#include "stdafx.h"

#include <iostream>
#include <Metahost.h>
//#include <mscoree.h>
#include <CorError.h>

#pragma comment(lib, "mscoree.lib")

// LPCWSTR 不可變的寬字元。
// LPWSTR 可變的寬字元,要用 malloc 初始化。

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ICLRMetaHost *pMetaHost = NULL;
//ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
//ICLRDebugging *pCLRDebugging   = NULL;
HRESULT hr;

hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost,
(LPVOID*)&pMetaHost);
//hr = CLRCreateInstance (CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy,
// (LPVOID*)&pMetaHostPolicy);
//hr = CLRCreateInstance (CLSID_CLRDebugging, IID_ICLRDebugging,
// (LPVOID*)&pCLRDebugging);

ICLRRuntimeInfo *clr = NULL;
hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo,(LPVOID *)&clr);
ICLRRuntimeHost *runtime= NULL;

hr = clr->GetInterface(CLSID_CLRRuntimeHost , IID_ICLRRuntimeHost, (LPVOID*)&runtime);

hr = runtime->Start();

//執行 .Net Manage 程式。
//DWORD *retval = 0;
//hr = runtime->ExecuteInDefaultAppDomain(L"C:\\Users\\Yaoming\\Documents\\visual studio 2010\\Projects\\ClrApp\\ClrApp\\bin\\Debug\\ClrApp.dll"
// ,L"ClrApp.Program",L"AppMain",L"",retval);

//取得已安裝的 .Net Runtime 版本字串。
IEnumUnknown *setups = NULL;
hr = pMetaHost->EnumerateInstalledRuntimes(&setups);
ICLRRuntimeInfo *rt; 
ULONG actual;
while((hr = setups->Next(1, (IUnknown**)&rt, &actual)) == S_OK)  
{
DWORD buffer = 20;
LPWSTR ver = (LPWSTR)malloc(buffer * sizeof(LPWSTR));
hr = rt->GetVersionString(ver,&buffer);

if(hr == S_OK)
cout << ver << endl;
else
cout << "Get version fail." << endl;

hr = pMetaHost->GetRuntime(ver, IID_ICLRRuntimeInfo,(LPVOID *)&clr);

if(hr == S_OK)
cout << ver << endl;
else
cout << "Get runtime fail." << endl;
}

if(hr == S_OK)
cout << "S_OK" << endl;
else if (hr == HOST_E_CLRNOTAVAILABLE)
cout << "Clr Not available" << endl;
else if (hr == HOST_E_TIMEOUT)
cout << "Time out " << endl;
else if(hr == HOST_E_NOT_OWNER)
cout << "Not owner" << endl;
else if(hr == HOST_E_ABANDONED)
cout << "abandoned" << endl;
else
cout << "Fail" << endl;

system("pause");

return 0;
}

2011年1月27日 星期四

在封送物件在 AppDomain 之間時,如何讓物件的 Proxy 不要過期?

將 LifeTimeServices 的 LeaseTime 屬性設定成 TimeSpan.Zero 就行了。

System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseTime = TimeSpan.Zero;

2011年1月4日 星期二

.Net 與 Java 在 RSA 金鑰部份如何交換?

這個鳥問題,搞了我好久,最後終於解決了…兩邊無法交換金鑰最主要的問題有兩個,第一個是資料表示法的問題,.第二個是 PKCS#8 問題....不多做說明了,看下面的網頁就有解答了。

PKCS#8 PrivateKeyInfo to CryptoAPI PRIVATEKEYBLOB & XML RSA key Converter

2011年1月2日 星期日

WPF 圖形轉譯概觀

本主題將概要說明 WPF 視覺圖層。 並著重於 WPF 模型中轉譯支援的 Visual 類別角色。

透過建立 ControlTemplate 自訂現有控制項的外觀

WPF 的 UI 彈性很大,缺點是很複雜,不太容易理解,但是了解之後,可以非常自由的做出非常驚人的 UI 效果。

http://msdn.microsoft.com/zh-tw/library/ee230084.aspx

繫結宣告概觀

在 WPF 中資料繫結似乎是非常重要的機制,其中如何宣告繫結是一個重要的細節,下列文件有如何宣告的細詳說明,相信會很有用。

http://msdn.microsoft.com/zh-tw/library/ms752300.aspx