星期五, 10月 12, 2007

WPF Book Control


之前我曾經有用過Flash or 一些作法 來做幾本電子書.........

之前的SilverLight也有一個demo叫Page Turn也蠻炫的


不過今天才發現這本Pure WPF的Book Control

這本才真的比較棒~~ 翻頁不是四四角角的 而是有點柔軟的感覺

裡面可以放一大堆Control, 3D Object, Video, ...etc 應有盡有

而且還有Full Source

還真的不錯 值得一看....







Powered by ScribeFire.

Routed Events In WPF


今天在解一個Bugs.... 就發現先前設計的一個Control有一些功能沒有想的很完善.......

於是又重新再思考了一下..... 發現自以為對WPF裡的Routed Event, 包括三種方式Tunnel, Bubble, 與Direct很熟悉

以為已經掌握到訣竅 但是因為之前的設計不完善 怎麼想就是想到一些漏洞

直到看到下面的兩種Add Handler的方法 我才恍然大悟.... 我未曾懂過....

也難怪WPF的Controls的Event機制並沒有照著普通的.Net的event or delegate方法 而是還有什麼AddHandler, ...etc之類的東西

就是為了要Support Routed Event


全文可以再參考一次msdn WPF Event

唔.... 沒有一直持續設計與思考 我還真的沒發現自己沒真正懂過.....

Design的路還很長.......


C# Method1-------------------------------------------------
void MakeButton()
{
Button b2 = new Button();
b2.AddHandler(Button.ClickEvent, new RoutedEventHandler(Onb2Click));
}
void Onb2Click(object sender, RoutedEventArgs e)
{
//logic to handle the Click event
}

The next example shows the C# operator syntax
(Microsoft Visual Basic .NET has slightly different operator syntax
because of its handling of indirection):

C# Method2-------------------------------------------------

void MakeButton2()
{
Button b2 = new Button();
b2.Click += new RoutedEventHandler(Onb2Click2);
}
void Onb2Click2(object sender, RoutedEventArgs e)
{
//logic to handle the Click event
}


Powered by ScribeFire.

星期一, 10月 08, 2007

GeekInterview


之前我有一個朋友很喜歡蒐集各類很Tricky的小問題 因為現在國外大型公司很喜歡問這些Tricky的問題

例如如何Swap一個Varaible不用記憶體啦~~ 或是...etc

比較偏程式的方面 或許我還有機會可以答的出來..... 但是如果是一些思考性的問題 我幾乎答對率都很低

像是三個燈泡在一間房子裡與外面有三個開關 你可以先在外面動三個開關 (開或關) 然後只能進房子一次 就要決定那一個開關是那一個燈泡

像這類的問題 我幾乎都答不出來 @@||

所以發現網上有一個site在整理這些東西~~ 所以覺得以後如果要面試一些會考這類問題的大公司 還是多多蒐集題庫可能比較保險

:-)


Link --> IT Interview Questions Open Database - GeekInterview.com

Powered by ScribeFire.

Marshaling Nested Data Structures


C++ & C#之間的Interop有許多方式,或是workaround

一個是C++/CLi, 一直是把C++ Class轉成Regular C,然後用Platform Invoke的方式,另一個是把C++包成COM

不過不論如何 都有可能會遇到可以傳遞Structure的問題 上一篇就是基本的Sample

但是Structure可能複雜到Nested怎麼辦.........找到了一篇Blog (Marshaling Nested Data Structure from jaredpar's WebLog)在描述如何解決


看完之後~~ 只能說 真的很麻煩!!!!


Powered by ScribeFire.

Code Snippets : Interop between C# & C++ (Managaed & Unmanaged)


雖然知道用法 卻每次都背不起來~~ 所以特別把關鍵的Sample Code Paste到Blog

以後查詢也會比較方便.....

主要是C++ & C#之間structure該如何傳遞

Source From MSDN

在下列程式碼中,建立了 Managed 結構,將其傳輸到 Unmanaged 記憶體後,再使用 PtrToStructure 方法傳輸回到 Managed 記憶體。
using System;
using System.Runtime.InteropServices;

public struct Point
{
public int x;
public int y;
}

class Example
{

static void Main()
{

// Create a point struct.
Point p;
p.x = 1;
p.y = 1;

Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");

// Initialize unmanged memory to hold the struct.
IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));

try
{

// Copy the struct to unmanaged memory.
Marshal.StructureToPtr(p, pnt, false);

// Create another point.
Point anotherP;

// Set this Point to the value of the
// Point in unmanaged memory.
anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));

Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");

}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}



}

}
在下列程式碼中,示範了如何使用 PtrToStructure 方法,將記憶體的 Unmanaged 區塊封送處理成 Managed 結構。




Powered by ScribeFire.