GSI

타이머 사용하기

WPF 2007. 9. 10. 23:15 |

본 글은 http://www.hoons.kr 에 있는 내용을 나름대로 이해되는 부분에 한해서 적은 것입니다.

타이머 사용을 위해서 네임 스페이스를 하나 선언합니다.
using System.Windows.Threading;

타이머 변수를 하나 선언합니다.
DispatcherTimer dt = new DispatcherTimer();

그리고 난수를 하나 생성하게 됩니다.
Random ran = new Random();

이후에 코드에 맞는 해당 값들의 전역 변수들을 선언 하면 됩니다.

dt의 타이머를 사용하기 위해서 초기 설정을 하게 됩니다.
dt.Interval = TimeSpan.FromMilliseconds(30);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();


Tick에 추가한 이벤트 핸들러에 대한 함수 원형을 추가 합니다.
void dt_Tick(object sender, EventArgs e)
{
   .....
}


..... 이상 코드는 여기까지.......

Start()를 하고 나면 계속해서 타이머가 동작 하게 됩니다.
타이머 동작을 멈추고 싶다면 dt.Stop()를 해주면 됩니다.


이후에 참고할 만한 코드는

* 랜덤 함수는 ran.NextDouble() 를 사용해서 double형 값을 가져 올수 있습니다.
   물론 double 말고도 int형을 가져 오는 함수도 존재 합니다.
* Canvas에 있는 오브젝트의 위치를 변경하기 위해서 아래와 같은 코드가 가능해 집니다.
   Canvas.SetLeft(im, currX);
   Canvas.SetTop(im, currY);
   - im은 Canvas에 있는 객체의 Name 이며, currX, currY 는 전역 변수로 가지고 있는
     위치가 되겠습니다.
* 해당 값의 절대값을 사용하기 위해서 아래의 코드를 사용합니다.
    if (Math.Abs(currX - destX) < 1)
    {
        destX = ran.NextDouble() * this.ActualWidth;
        destY = ran.NextDouble() * this.ActualHeight;
    }




Posted by gsi
:

Blend의 튜토리얼 중에 ColorSwitch 라는 예제가 있다.
그곳에서 보면 칼라 스위치는 마우스가 Enter 되면 앞쪽으로 나오게 된다.
이 부분은 SetZIndex 가 담당하게 되는데.
조작 하는 방법이 조금은 다르다.

Blend에서 Grid 내부에 Canvas를 3개를 겹치게 배치 했다가 가정하겠다.
여기서 우리는 원하는 Canvas를 제일 위쪽에 가져다 놓기를 바랄때가 있을것이다.
이때 사용하는게 SetZIndex를 사용하면 된다.

하지만 이것은 Name를 가지는 객체에 포함되어져 있는 함수는 아니기 때문에 아래의
코드를 이용해야 한다.

관련msdn 로컬 주소 : http://msdn2.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx

Canvas.SetZIndex(Step0, 1);

위와 같은 코드를 사용할 수 있다.
Canvas를 객체의 SetZIndex를 변경해준다고 생각하면 되며,
Grid 내부에 Canvas가 여러개 있을때 사용하게 되는 것이다.
즉, Grid 내부의 Canvas를 이동시키고자 할때는

Canvas.SetZIndex()를 사용하면 되는 것이다.

Step0이라는 것은 Blend에서 Canvas의 이름을 의미한다.

그리도 뒤에 오는 1이라는 숫자는
Blend에서 여러개의 객체를 배치 하고 나서 ZIndex를 보면 모두 0인 것을
알수 있다. 이 경우에 0 이지만 배치된 순서대로 화면에 표현되도록 되어 있다.
그래서 1이라는 숫자는 최상단에 배치 한다고 생각 하면된다.

다른 방법으로 3개의 객체가 있다고 했을때 0 ~ 2까지 3개의 ZIndex를
사용해도 된다.

간단하게 하기 위해서는 모두 0이고 최상단에 올려질것만 1로 설정하면
편하게 할 수 있을거 같다.

Posted by gsi
:

버튼 위치 이동시키기.

WPF 2007. 9. 9. 05:39 |

Button을 이동시키기 위해서는 Margin의 값을 잘 살펴 보면 된다.
이 값이 어떤 Property이고 어떻게 이동해 주어야 하는지 찾아야 한다.

참고로 Button의 Name가 MoveObject 라고 가정하고 시작한다.

Blend에서 Button을 하나 생성한 후에 마우스로 이동해 보자.
그러면 Margin의 값이 바뀌는 것을 알수 있다. 즉 Left, Top의 값이 바뀌게 되는 것이다.
그렇다면 비아인드 코드를 작성 할려면 Margin에 특정 값을 넣어 주면 되는 것이다.

MoveObject.Margin.Left 라는 속성이 있지만 이것은 특정 int, double의 값을 추가할수는 없다.

그래서 cs 파일의 MoveObject.Margin의 툴팁 정보를 보니
FrameworkElement.margin property 라고 나오는 것을 볼 수 있었다.
그래서 msdn에서 FrameworkElement.margin을 찾아 보았다.

아하! Syntax 에 보니
C#
public Thickness Margin { get; set; }
이런 구문을 볼 수 있었다. 바로 Thickness를 하나 생성한 후에 값을 추가 하고
Margin에 대입해 주면 되는거라고 짐작이 될것이다.

그래서 아래와 같은 코드를 사용해서 하면 된다.

Thickness th = new Thickness(marginleft, margintop, 0, 0);
MoveObject.Margin = th;
이상.  (msdn을 잘 활용하자. 거의 모든 코드가 다 있다. ^^)
Posted by gsi
:

랜덤 코드 사용하기

WPF 2007. 9. 9. 05:34 |

네임스페이스 선언 : using System.Threading;

코드 사용방법 :

Random autoRand = new Random();
double marginleft = autoRand.NextDouble() * (this.Width - MoveObject.Width);
double margintop = autoRand.NextDouble() * (this.Height - MoveObject.Height);

Thickness th = new Thickness(marginleft, margintop, 0, 0);
MoveObject.Margin = th;

Next()는 int형 정보를 가져올때 사용한다.
NextDouble()는 double형 정보를 가져 올때 사용한다.

이후에도 seed의 값을 입력 하는 방법도 있으니 참고 하기 바란다.

Posted by gsi
:

UIElement3D extensibility - 3D Video Carousel

Recently I've been working on a screencast, which goes over some advanced aspects of working with Element3D. In the meantime, this sample demonstrates WPF's Orcas Beta 2 UIElement 3D technology, covering the essentials for how you can make your own reusable UIElement3D controls for 3D. I have two in here:

  1. A "KeepCaseUIElement3D", a representation of a real world DVD case, with the added twist of playing video on the inside. It's API consumes Uri's for cover images and a media source, rather than traditional models, meshes, and materials in 3D .
  2. A "MovieCarousel" - a simple 3D carousel layout control which re-orients itself to position the object which was last clicked towards the user.

You will need to operate with the WPF V3.5(Orcas) Beta 2 Bits. You can get the Visual Studio Orcas Beta 2 installation with WPF from here.

A set of streaming WPF tutorial Videos

Concepts you can see in practice with this sample include:

  • Creating interactive 3D controls as interactive containers on non-interactive 3D models (Hint - Leverage existing 3D Xaml with little effort)
  • Creating a Custom Visual3D container type
  • Use of StaticResources from Application.Resources to separate mesh details from overall structure
  • Databinding a mesh texture to consume a local DP exposed as a URI, via a data binding type converter
  • 3D Layout - Cylinder Layout Helper class
  • Playing Video as a material on 3D

If you have any questions on the details of this sample, feel free to send me a message, or leave a relevant comment- odds are fair other folks have similar questions, which could make for interesting future blog postings!

 Thanks!

관련자료 : http://blogs.msdn.com/pantal/archive/2007/08/22/uielement3d-extensibility-dvd-keep-case-player-cylindrical-carousel-layout.aspx

Posted by gsi
: