Show toolbar

2012年12月3日 星期一

C# WPF RegEx Check Number

標題:使用RegEx進行數字比對
C# (MainWindow.xaml.cs):
using System;
using System.Windows;
using System.Text.RegularExpressions; //RegEx

namespace WPFRegEx {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
        public void CheckRegEx(string text) {
            Regex regex = new Regex("^[0-9]+$"); //建立RegEx規則
            Match result = regex.Match(text); //字串比對
            if (result.Success) {
                System.Windows.MessageBox.Show("True");
            } else {
                System.Windows.MessageBox.Show("False");
            }
        }
        private void btn_Click(object sender, RoutedEventArgs e) {
            CheckRegEx(tbx.Text);
        }
    }
}
WPF (MainWindow.xaml.cs):
<Window x:Class="WPFRegEx.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox Height="30" Margin="20,20,20,0" Name="tbx" VerticalAlignment="Top" />
        <Button Content="Button" Height="30" Margin="20,70,20,0" Name="btn" VerticalAlignment="Top" Click="btn_Click" />
    </Grid>
</Window>

範例結果:


說明:
在C# WPF使用RegEx判斷字串是否為數字之基礎範例。

2012年11月5日 星期一

C# WPF Call VC++ DLL

標題:C# WPF呼叫C++ DLL動態函式庫
C++ (CppDLL.h):
#pragma once
using namespace System;

namespace CppDLL {
    public ref class MyClass
    {
        public:
        int Multiply(int x, int y)
        {
            return x * y;
        }
    };
}

WPF (MainWindow.xaml):
<Window x:Class="CSharpCaller.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Click Me" Height="30" HorizontalAlignment="Left" Margin="20,20,0,0" Name="btn" VerticalAlignment="Top" Width="100" Click="btn_Click" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="20,70,0,0" Name="tbk" Text="Show up here" VerticalAlignment="Top" Width="100" />
    </Grid>
</Window>

C# (MainWindow.xaml.cs):
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 CSharpCaller
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            CppDLL.MyClass test = new CppDLL.MyClass();
            tbk.Text = test.Multiply(2, 4).ToString();
        }
    }
}

結果:

說明:
使用C# WPF呼叫C++函式庫進行乘法運算的簡單範例,
建立Visual C++ CLR Class Library專案,編譯DLL函式庫,
建立Visual C# WPF Application專案,加入References的DLL檔案,
編譯後即可執行。

2012年10月12日 星期五

Matrix Multiplied

標題:n階矩陣相乘
C# (MainWindow.xaml.cs):
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 matrix_multiplied
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public int ma = 0;
        public int na = 0;
        public int mb = 0;
        public int nb = 0;
        public MainWindow()
        {
            InitializeComponent();
        }
        public void CreateMatrix()
        {
            int m = 0, n = 0;
            
            //Clear virtual Matrix
            this.MatrixGrid.Children.Clear();
            for (m = 0; m < ma; m++)
                for (n = 0; n < na; n++)
                    this.UnregisterName("MA_" + m.ToString() + "_" + n.ToString());
            for (m = 0; m < mb; m++)
                for (n = 0; n < nb; n++)
                    this.UnregisterName("MB_" + m.ToString() + "_" + n.ToString());

            //Create Matrix A B Textbox
            ma = Convert.ToInt32(mA.Text);
            na = Convert.ToInt32(nA.Text);
            mb = Convert.ToInt32(mB.Text);
            nb = Convert.ToInt32(nB.Text);
            if (na != mb) //Check Matrix
            {
                MessageBox.Show("Can't do the Multiplication!!");
            }
            else
            {
                //Create Matrix A
                for (m = 0; m < ma; m++)
                {
                    for (n = 0; n < na; n++)
                    {
                        TextBox newtbx = new TextBox();
                        newtbx.Name = "MA_" + m.ToString() + "_" + n.ToString();
                        newtbx.Text = "1.0";
                        newtbx.VerticalAlignment = VerticalAlignment.Top;
                        newtbx.HorizontalAlignment = HorizontalAlignment.Left;
                        newtbx.Margin = new Thickness((10 + 40 * n), (10 + 40 * m), 0, 0);
                        newtbx.Width = 30;
                        newtbx.Height = 30;
                        this.MatrixGrid.Children.Add(newtbx);
                        this.RegisterName(newtbx.Name, newtbx);
                    }
                }

                //Add multiply symbol
                TextBlock newtbk = new TextBlock();
                newtbk.Name = "Multiply";
                newtbk.Text = "×";
                newtbk.Margin = new Thickness((15 + 40 * na), (20 * ma), 0, 0);
                this.MatrixGrid.Children.Add(newtbk);

                //Create Matrix B
                for (m = 0; m < mb; m++)
                {
                    for (n = 0; n < nb; n++)
                    {
                        TextBox newtbx = new TextBox();
                        newtbx.Name = "MB_" + m.ToString() + "_" + n.ToString();
                        newtbx.Text = "1.0";
                        newtbx.VerticalAlignment = VerticalAlignment.Top;
                        newtbx.HorizontalAlignment = HorizontalAlignment.Left;
                        newtbx.Margin = new Thickness((40 + 40 * (n + na)), (10 + 40 * m), 0, 0);
                        newtbx.Width = 30;
                        newtbx.Height = 30;
                        this.MatrixGrid.Children.Add(newtbx);
                        this.RegisterName(newtbx.Name, newtbx);
                    }
                }
            }
        }

        public void DetermineMatrix()
        {
            int m = 0, n = 0, i=0;
            Double sum = 0;
            Double[,] MaArray = new Double[ma, na];
            Double[,] MbArray = new Double[mb, nb];
            Double[,] McArray = new Double[ma, nb];
            
            //Get Number
            for (m = 0; m < ma; m++)
            {
                for (n = 0; n < na; n++)
                {
                    TextBox MA = (TextBox)MatrixGrid.FindName("MA_" + m.ToString() + "_" + n.ToString());
                    MaArray[m, n] = (Double)Convert.ToDouble(MA.Text);
                }
            }
            for (m = 0; m < mb; m++)
            {
                for (n = 0; n < nb; n++)
                {
                    TextBox MB = (TextBox)MatrixGrid.FindName("MB_" + m.ToString() + "_" + n.ToString());
                    MbArray[m, n] = (Double)Convert.ToDouble(MB.Text);
                }
            }

            //Determine Matrix
            for (m = 0; m < ma; m++)
            {
                for (n = 0; n < nb; n++)
                {
                    sum = 0.0;
                    for (i = 0; i < mb; i++)
                    {
                        sum += MaArray[m,i] * MbArray[i,n];
                    }
                    McArray[m,n] = sum;
                }
            }

            //Add equal symbol
            TextBlock newtbk = new TextBlock();
            newtbk.Name = "Anser";
            newtbk.Text = "=";
            newtbk.Margin = new Thickness((50 + 40 * (na + nb)), (20 * ma), 0, 0);
            this.MatrixGrid.Children.Add(newtbk);

            //Create Matrix C
            for (m = 0; m < ma; m++)
            {
                for (n = 0; n < nb; n++)
                {
                    sum = 0.0;
                    for (i = 0; i < mb; i++)
                    {
                        TextBox newtbx = new TextBox();
                        newtbx.Name = "MC" + m.ToString() + "_" + n.ToString();
                        newtbx.Text = Convert.ToString(McArray[m,n]);
                        newtbx.VerticalAlignment = VerticalAlignment.Top;
                        newtbx.HorizontalAlignment = HorizontalAlignment.Left;
                        newtbx.Margin = new Thickness((70 + 40 * (n + na + nb)), (10 + 40 * m), 0, 0);
                        newtbx.Width = 30;
                        newtbx.Height = 30;
                        this.MatrixGrid.Children.Add(newtbx);
                    }
                }
            }
        }

        private void CreateMatrix_Click(object sender, RoutedEventArgs e)
        {
            CreateMatrix();
        }

        private void DetermineMatrix_Click(object sender, RoutedEventArgs e)
        {
            DetermineMatrix();
        }
    }
}
WPF (MainWindow.xaml):
<Window x:Class="matrix_multiplied.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="600">
    <Grid>
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="20,20,0,0" Name="MatrixATitle" Text="Matrix A" VerticalAlignment="Top" Width="122" TextAlignment="Left" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="20,50,0,0" Name="textBlock1" Text="m:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="90,50,0,0" Name="textBlock2" Text="n:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
        <TextBox Height="30" Margin="40,50,0,0" Name="mA" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
        <TextBox Height="30" Margin="110,50,0,0" Name="nA" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
        
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="180,20,0,0" Name="MatrixBTitle" Text="Matrix B" VerticalAlignment="Top" Width="122" TextAlignment="Left" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="180,50,0,0" Name="textBlock3" Text="m:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
        <TextBlock Height="30" HorizontalAlignment="Left" Margin="250,50,0,0" Name="textBlock4" Text="n:" VerticalAlignment="Top" Width="20" DataContext="{Binding}" TextAlignment="Left" />
        <TextBox Height="30" Margin="200,50,0,0" Name="mB" VerticalAlignment="Top" Text="4" HorizontalAlignment="Left" Width="30" />
        <TextBox Height="30" Margin="270,50,0,0" Name="nB" VerticalAlignment="Top" Text="1" HorizontalAlignment="Left" Width="30" />

        <Button Content="Create Matrix" Height="30" Margin="340,50,0,0" Name="CreateMatrixBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" Click="CreateMatrix_Click" />
        <Button Content="Determine" Height="30" Margin="460,50,0,0" Name="DetermineMatrixBtn" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" Click="DetermineMatrix_Click" />
        
        <Grid Margin="10,100,10,10" Name="MatrixGrid" HorizontalAlignment="Stretch">
        </Grid>
    </Grid>
</Window>

結果:

說明:
使用WPF撰寫計算n階矩陣相乘印出結果。

首先按下Create Matrix按鈕虛擬建立需要的TextBox陣列數量,
再按下Determine去擷取經過RegisterName後的虛擬TextBox中的值,
並計算出矩陣相乘的結果後虛擬印出。

2012年8月28日 星期二

VtkSDI draw 3D in MFC

標題:VtkSDI 3D 繪製範例
C++ (VtkSDIView.h):
// VtkSDIView.h : interface of the CVtkSDIView class
//

#pragma once

// Include the required header files for the vtk classes we are using
#include <vtkRenderer.h>
#include <vtkWin32OpenGLRenderWindow.h>
#include <vtkWin32RenderWindowInteractor.h>
 
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkGlyph3D.h>
#include <vtkElevationFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCubeAxesActor2D.h>

class CVtkSDIView : public CView
{
protected: // create from serialization only
    CVtkSDIView();
    DECLARE_DYNCREATE(CVtkSDIView)

// Attributes
public:
    CVtkSDIDoc* GetDocument() const;

// Operations
public:

// Overrides
public:
    virtual void OnDraw(CDC* pDC);  // overridden to draw this view
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
    virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
    virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
    virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);

// Implementation
public:
    void Pipeline ( void );
    virtual ~CVtkSDIView();
    vtkWin32OpenGLRenderWindow *renWin;
    vtkRenderer *ren;
    vtkWin32RenderWindowInteractor *iren;
            
    vtkSphereSource *sphere;
    vtkPolyDataMapper *sphereMapper;
    vtkElevationFilter *sphereElevation;
    vtkActor *sphereActor;
    vtkConeSource *cone;
    vtkGlyph3D *glyph;
    vtkPolyDataMapper *spikeMapper;
    vtkActor *spikeActor;
    vtkCubeAxesActor2D *sphereAxis;

#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
    afx_msg void OnFilePrintPreview();
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
};

#ifndef _DEBUG  // debug version in VtkSDIView.cpp
inline CVtkSDIDoc* CVtkSDIView::GetDocument() const
   { return reinterpret_cast<CVtkSDIDoc*>(m_pDocument); }
#endif

C++ (VtkSDIView.cpp):
// VtkSDIView.cpp : implementation of the CVtkSDIView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "VtkSDI.h"
#endif

#include "VtkSDIDoc.h"
#include "VtkSDIView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CVtkSDIView

IMPLEMENT_DYNCREATE(CVtkSDIView, CView)

BEGIN_MESSAGE_MAP(CVtkSDIView, CView)
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CVtkSDIView::OnFilePrintPreview)
    ON_WM_CONTEXTMENU()
    ON_WM_RBUTTONUP()
    ON_WM_SIZE()
    ON_WM_ERASEBKGND()
    ON_WM_CREATE()
END_MESSAGE_MAP()

// CVtkSDIView construction/destruction

CVtkSDIView::CVtkSDIView()
{
    // Create the the renderer, window and interactor objects.
    this->ren = vtkRenderer::New();
    this->renWin = vtkWin32OpenGLRenderWindow::New();
    this->iren = vtkWin32RenderWindowInteractor::New();
            
    // Create the the objects used to form the visualisation.
    this->sphere = vtkSphereSource::New();
    this->sphereElevation = vtkElevationFilter::New();
    this->sphereMapper = vtkPolyDataMapper::New();
    this->sphereActor = vtkActor::New();
    this->cone = vtkConeSource::New();
    this->glyph = vtkGlyph3D::New();
    this->spikeMapper = vtkPolyDataMapper::New();
    this->spikeActor = vtkActor::New();
    this->sphereAxis = vtkCubeAxesActor2D::New();
}

CVtkSDIView::~CVtkSDIView()
{
    // Delete the the renderer, window and interactor objects.
    this->ren->Delete();
    this->iren->Delete();
    this->renWin->Delete();
 
    // Delete the the objects used to form the visualisation.
    this->sphere->Delete();
    this->sphereElevation->Delete();
    this->sphereMapper->Delete();
    this->sphereActor->Delete();
    this->cone->Delete();
    this->glyph->Delete();
    this->spikeMapper->Delete();
    this->spikeActor->Delete();
    this->sphereAxis->Delete();
}

BOOL CVtkSDIView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    return CView::PreCreateWindow(cs);
}

// CVtkSDIView drawing

void CVtkSDIView::OnDraw(CDC* pDC)
{
    CVtkSDIDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    if ( !this->iren->GetInitialized() )
    {
        CRect rect;

        this->GetClientRect(&rect);
        this->iren->Initialize();
        this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);

        this->ren->ResetCamera();
 
    }

    // Invoke the pipeline
    Pipeline();

    if ( pDC->IsPrinting() )
    {
        this->BeginWaitCursor();

        // Obtain the size of the printer page in pixels.
        int cxPage = pDC->GetDeviceCaps(HORZRES);
        int cyPage = pDC->GetDeviceCaps(VERTRES);

        // Get the size of the window in pixels.
        int *size = this->renWin->GetSize();
        int cxWindow = size[0];
        int cyWindow = size[1];
        float fx = float(cxPage) / float(cxWindow);
        float fy = float(cyPage) / float(cyWindow);
        float scale = min(fx,fy);
        int x = int(scale * float(cxWindow));
        int y = int(scale * float(cyWindow));
        this->renWin->SetupMemoryRendering(cxWindow, cyWindow, pDC->GetSafeHdc());
        this->renWin->Render();
        HDC memDC = this->renWin->GetMemoryDC();
        StretchBlt(pDC->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow,cyWindow,SRCCOPY);
        this->renWin->ResumeScreenRendering();

        this->EndWaitCursor();

    }
    else
    {
        this->renWin->Render();
    }
}


// CVtkSDIView printing


void CVtkSDIView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
    AFXPrintPreview(this);
#endif
}

BOOL CVtkSDIView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // default preparation
    return DoPreparePrinting(pInfo);
}

void CVtkSDIView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add extra initialization before printing
}

void CVtkSDIView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add cleanup after printing
}

void CVtkSDIView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
    ClientToScreen(&point);
    OnContextMenu(this, point);
}

void CVtkSDIView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CVtkSDIView diagnostics

#ifdef _DEBUG
void CVtkSDIView::AssertValid() const
{
    CView::AssertValid();
}

void CVtkSDIView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CVtkSDIDoc* CVtkSDIView::GetDocument() const // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVtkSDIDoc)));
    return (CVtkSDIDoc*)m_pDocument;
}
#endif //_DEBUG


// CVtkSDIView message handlers


void CVtkSDIView::OnSize(UINT nType, int cx, int cy)
{
    CView::OnSize(nType, cx, cy);

    CRect rect;
    this->GetClientRect(&rect);
    this->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);
}


BOOL CVtkSDIView::OnEraseBkgnd(CDC* pDC)
{
    return TRUE;
}


int CVtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    this->renWin->AddRenderer(this->ren);
    // setup the parent window
    this->renWin->SetParentId(this->m_hWnd);
    this->iren->SetRenderWindow(this->renWin);

    return 0;
}


LRESULT CVtkSDIView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        //case WM_PAINT: 
        case WM_LBUTTONDOWN: 
        case WM_LBUTTONUP: 
        case WM_MBUTTONDOWN: 
        case WM_MBUTTONUP: 
        case WM_RBUTTONDOWN: 
        case WM_RBUTTONUP: 
        case WM_MOUSEMOVE:
        case WM_CHAR:
        case WM_TIMER:
        if (this->iren->GetInitialized())
        {
            return vtkHandleMessage2(this->m_hWnd, message, wParam, lParam, this->iren);
        }
        break;
    }

    return CView::WindowProc(message, wParam, lParam);
}

void CVtkSDIView::Pipeline()
{
    // Construct the sphere
    this->sphere->SetRadius(1);
    this->sphere->SetThetaResolution(18);
    this->sphere->SetPhiResolution(18);
    this->sphere->LatLongTessellationOn();
    // Generate elevations
    this->sphereElevation->SetInput(this->sphere->GetOutput());
    this->sphereElevation->SetLowPoint(0,0,-1);
    this->sphereElevation->SetHighPoint(0,0,1);
    this->sphereElevation->SetScalarRange(-1,1);
    // Link the mapper
    this->sphereMapper->SetInput(this->sphereElevation->GetPolyDataOutput());
    this->sphereMapper->SetColorModeToMapScalars();
    this->sphereMapper->SetScalarRange(-1,1);
    // Link the actor
    this->sphereActor->SetMapper(this->sphereMapper);
    // Add it to the renderer
    this->ren->AddActor(this->sphereActor);
 
    // Construct the cone
    this->cone->SetResolution(8);
 
    // Construct the glyphs on the spherical surface
    this->glyph->SetInput(this->sphere->GetOutput());
    this->glyph->SetSource(this->cone->GetOutput());
    this->glyph->SetVectorModeToUseNormal();
    this->glyph->SetScaleModeToScaleByVector();
    this->glyph->SetScaleFactor(0.1);
    // Link the mapper to the glyph
    this->spikeMapper->SetInput(this->glyph->GetOutput());
    // Link the actor
    this->spikeActor->SetMapper(this->spikeMapper);
    // Add it to the renderer
    this->ren->AddActor(this->spikeActor);
 
    // Add in the cube axis actor
    this->sphereAxis->SetInput(this->sphereElevation->GetOutput());
    this->sphereAxis->SetCamera(this->ren->GetActiveCamera());
    // Add it to the renderer
    this->ren->AddActor(this->sphereAxis);
}

說明:
MFC使用VTK 5.10.0繪製3D模型範例。

2012年8月17日 星期五

Batch Capital To Lower

標題:英文字母大寫轉小寫
Bacth (c2t.bat):
@Echo Off
SetLocal EnableDelayedExpansion
set var=CapitalToLower
echo !var!
for %%a in (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) do (
    set var=!var:%%a=%%a!
)
echo !var!
PAUSE
說明:
使用Batch語法大寫字母轉小寫字母。

2012年7月10日 星期二

C++ String Reverse

標題:C++連續字串反轉
C++ (main.cpp):
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main(void) {
    char str[99];
    int i;
    while(cin.getline(str, 99, '\n')) {
        for (i=strlen(str)-1;i>=0;i--) {
            cout << str[i];
        }
        cout << endl;
    }
    return 0;
}

範例結果:


說明:
基礎連續字串反轉範例。

2012年3月20日 星期二

Unique Array

標題:陣列過濾重複值
JavaScript (index.html):
function unique(arr) {
    var newArr = [];
    var temp = {};
    for(key in arr) {
        var id = arr[key];
        if(!temp[id] && id != "") {
            newArr.push(id);
            temp[id] = true;
        }
    }
    return newArr;
}
alert(unique(new Array("1","2","4","4","1")));

範例結果:


說明:
簡單過濾掉陣列中重複的數值。

2012年3月7日 星期三

Form iframe virtual load post

標題:虛擬表單送出
JavaScript (inidex.html):
<button id="test">test</button>
<div id="show">show</div>
<script type="text/javascript">
function $(id) {
    switch(id.substr(0,1)) {
        case '#':
            return document.getElementById(id.substr(1));
        case '.':
            var elems = document.body.getElementsByTagName('*');
            var target = id.substr(1);
            var result=[];
            for(i=0;j=elems[i];i++) {
                if((j.className).indexOf(target)!=-1) result.push(j);
            }
            return result;
        default:
            return document.getElementsByTagName(id);
    }
}
var loadpost = {
    current : null,
    queue : [],
    request : function(q) {
        //Create iframe
        try { //IE
            var iframe = document.createElement('<iframe name="loadboxy">');
        } catch (e) {
            var iframe = document.createElement('iframe');
        }
        iframe.style.display = 'none';
        iframe.name = 'loadboxy';
        iframe.id = 'loadboxy';
        if(iframe.attachEvent) {
            iframe.attachEvent("onload", function(){
                loadpost.load();
            });
        } else {
            iframe.onload = function(){
                loadpost.load();
            };
        }
        //Create form
        var form = document.createElement('form');
        form.target = 'loadboxy';
        form.action = q.url;
        form.method = 'post';
        form.style.display = 'none';
        for(var key in q.param) {
            if(q.param[key].constructor == Array) {
                for(var i=0;i<q.param[key].length;i++) {
                    var input = document.createElement('input');
                    input.type = 'hidden';
                    input.name = key;
                    input.value = q.param[key][i];
                    form.appendChild(input);
                }
            } else {
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = key;
                input.value = q.param[key];
                form.appendChild(input);
            }
        }
        //Queue
        this.queue.push({
            form : form,
            iframe : iframe,
            flag : 0,
            load : q.load
        });
        if (!this.current) this.action();
    },
    task : function() {
        var iFrame = this.current.iframe;
        var iFrameBody;
        if (iFrame.contentDocument) //FF
            iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
        else if (iFrame.contentWindow) //IE
            iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
        this.current.load(iFrameBody.innerHTML);
    },
    load : function() {
        if(this.current.flag == 1) {
            this.task();
            $('body')[0].removeChild(this.current.iframe);
            $('body')[0].removeChild(this.current.form);
            this.action();
        } else {
            this.current.flag = 1;
            this.send();
        }
    },
    action : function() {
        this.current = null;
        if(this.queue.length) {
            this.current = this.queue.shift();
            $('body')[0].appendChild(this.current.iframe);
        }
    },
    send : function() {
        $('body')[0].appendChild(this.current.form);
        this.current.form.submit();
    }
};
$('#test').onclick = function() {
    loadpost.request({
        url : "action.php",
        param : {
            'num1' : 10,
            'num2' : 20,
            'num[]' : new Array(30, 40)
        },
        load : function(data) {
            $('#show').innerHTML = data;
        }
    });
};
</script>
PHP (action.php):
<?php
$num1 = !empty($_POST['num1']) ? $_POST['num1'] : null;
$num2 = !empty($_POST['num2']) ? $_POST['num2'] : null;
$total = $num1 + $num2;
echo $num1."+".$num2."=".$total;
?>
說明:
純JavaScript產生虛擬form、iframe並發送POST訊息取得php傳回之內容。

2012年2月11日 星期六

JavaScript dollar sign 2

標題:自定義錢字號函式 2
JavaScript (index.html):
function $(id) {
    switch(id.substr(0,1)) {
        case '#':
            return document.getElementById(id.substr(1));
        case '.':
            var elems = document.body.getElementsByTagName('*');
            var target = id.substr(1);
            var result=[];
            for(i=0;j=elems[i];i++) {
                if((j.className).indexOf(target)!=-1) result.push(j);
            }
            return result;
        default:
            return document.getElementsByTagName(id);
    }
}

說明:
自定義一個錢字號指向id之函式, 繼上篇再加入Class與TagName功能, 讓程式更精簡。