Show toolbar

2013年10月18日 星期五

Fluent Interface with Prototype

標題:流暢介面搭配Prototype屬性的用法
JavaScript (index.html):
<span id="fatty"></span> V.S. <span id="thin"></span>
<script type="text/javascript">
var Runner = function(e) {
    this.speed = e.speed; //Meters per second
    this.time = null; //Second
    this.distance = null; //Meter
};

Runner.prototype.ready = function(e) {
    this.time = 0; //Cost time
    this.distance = e.starting; //Starting point
    return this;
};

Runner.prototype.run = function(e) {
    this.time += e.second;
    this.distance += this.speed * e.second; // Distance = Speed * Time
    return this;
};

Runner.prototype.halt = function(e) {
    this.time += e.second;
    return this;
};

Runner.prototype.result = function(e) {
    document.getElementById(e.id).innerHTML = "Distance:" + this.distance + " m, Cost time:" + this.time + " sec";
    return this;
};

var fatty = new Runner({speed:2}); //Speed: 2m/s
fatty.ready({starting:10}).run({second:30}).halt({second:5}).run({second:15}).result({id:"fatty"});

var thin = new Runner({speed:2.5}); //Speed: 2.5m/s
thin.ready({starting:0}).run({second:40}).result({id:"thin"});
</script>

範例結果:
V.S.

說明:
此程式使用流暢介面搭配Prototype屬性的方式撰寫,
計算胖與瘦的人100公尺跑步的時間(不計加減速),
條件項目:
1. 胖者速度為每秒2公尺 - Runner({speed:2})
2. 瘦者速度為每秒2.5公尺 - Runner({speed:2.5})
3. 瘦者禮讓胖者由10公尺處起跑 - ready({starting:10})
4. 胖者跑了30秒以後,休息了5秒,再繼續跑了15秒 - run({second:30}).halt({second:5}).run({second:15})
5. 瘦者跑了40秒全程無休息 - run({second:40})

2013年10月17日 星期四

Positive and Countdown Timer

標題:正倒數計時器
JavaScript (index.html):
<span id="loveboxy"></span>
<script type="text/javascript">
var TimerBoxy = function(e) {
    this.id = e.id;
    this.target = new Date(e.target);
};
TimerBoxy.prototype.show = function() {
    var s = Math.abs(parseInt( (new Date().getTime() - this.target.getTime() )/1000,10));
    var m = s/60;
    var h = m/60;
    var D = h/24;
    document.getElementById(this.id).innerHTML = 
    parseInt(D,10) + "日" + parseInt(h%24,10) + "時" + parseInt(m%60,10) + "分" + parseInt(s%60,10) + "秒";
};
TimerBoxy.prototype.start = function() {
    if(!this.timer) {
        var that = this;
        this.show();
        this.timer = window.setInterval(function() {
            that.show();
        }, 1000);
    }
};
TimerBoxy.prototype.stop = function() {
    if(this.timer) {
        window.clearInterval(this.timer);
        this.timer = null;
    }
};
loveboxy = new TimerBoxy({
    id : "loveboxy",
    target : "2011/11/11 11:11:11"
});
loveboxy.start();
</script>
使用範例:


說明:
當目標時間小於當日時間則為正數計時,反之則為倒數計時。
(感謝TonyQ幫忙修正。)

2013年10月8日 星期二

Send Mail with Google Form

標題:Google表單指令碼寄信
Google App Script (sendmail.gs):
function sendmail(e) {
    var currentItemResponses = e.response.getItemResponses();
    var name = currentItemResponses[0].getResponse(); //姓名
    var sex = currentItemResponses[1].getResponse();  //性別
    var mail = currentItemResponses[2].getResponse(); //E-MAIL
    var sexword = (sex=="男")?"先生":"小姐";
    MailApp.sendEmail(mail, "報名成功", name + " " + sexword + "您好,\n" + "恭喜您報名成功。");
}

說明:
用於在表單新增寄信的指令碼。

2013年10月3日 星期四

Folder Access Restrictions in Node.JS

標題:Express資料夾權限控制
Node.JS (main.js):

說明:
使用Express搭配Session的方式進行私用資料夾的權限控制。

2013年8月27日 星期二

MFC draw face and lighting using OpenGL

標題:MFC使用OpenGL繪製面並打光
MFC (OpenGLglutLight.cpp):
#include <afxwin.h>
#include "OpenGLglut.h"
#include "glut.h"
 
class DocBoxy : public CDocument {
public:
    CClientDC *m_pDC;
    HGLRC hrc;
    DECLARE_DYNCREATE(DocBoxy) //宣告run-time類別
};
 
IMPLEMENT_DYNCREATE(DocBoxy, CDocument) //宣告DocBoxy為run-time類別
 
class FrameBoxy : public CFrameWnd {
public:
    afx_msg void OnExit() { //宣告關閉檔案事件
        if(MessageBox( TEXT("Are you sure you want to close this window?"), TEXT("DocViewBoxy"), MB_OKCANCEL | MB_ICONQUESTION ) == 1 ) {
            DestroyWindow();
        }
    }
    afx_msg void OnAbout() { //宣告關於事件
        MessageBox( TEXT("DocViewBoxy Version 1.0\nCopyright (C) 2013, By QQBoxy Huang.\nEmail: gs1458@gmail.com"), TEXT("About"), MB_OK );
    }
 
    DECLARE_DYNCREATE(FrameBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
 
IMPLEMENT_DYNCREATE(FrameBoxy, CFrameWnd) //宣告FrameBoxy為run-time類別
 
BEGIN_MESSAGE_MAP(FrameBoxy, CFrameWnd) //建立訊息映射表
    ON_COMMAND(ID_FILE_EXIT, OnExit)
    ON_COMMAND(ID_HELP_ABOUT, OnAbout)
END_MESSAGE_MAP()
 
class ViewBoxy : public CView {
public:
    void OnDraw(CDC * aDC) { //必須覆寫的虛擬函數
        DocBoxy *doc = (DocBoxy *)GetDocument();
        ASSERT_VALID(doc);
         
        glLoadIdentity();
 
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glTranslatef(0.0f, 0.0f, -7.0f);
        glRotatef(-35.0f, 1.0f, 0.0f, 0.0f);
        glRotatef(-45.0f, 0.0f, 1.0f, 0.0f);
         
        /*glColor3f(1.0f, 0.0f, 0.0f);
        glBegin(GL_LINES);
            glVertex3f(0.0f, 0.0f, 0.0f);
            glVertex3f(20.0f, 0.0f, 0.0f);
        glEnd();
         */

        glColor3f(1.0f, 1.0f, 1.0f);
        glBegin(GL_TRIANGLES);
         
        glNormal3f(-0.8944272f, 0.4472136f, 0.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
 
        glNormal3f(0.8944272f, 0.4472136f, 0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
 
        glNormal3f(0.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
 
        glNormal3f(0.0f, -1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
 
        glNormal3f(0.0f, 0.4472136f, -0.8944272f);
        glVertex3f(0.0f, 1.0f, 0.0f);
        glVertex3f(1.0f, -1.0f, -1.0f);
        glVertex3f(-1.0f, -1.0f, -1.0f);
 
        glNormal3f(0.0f, 0.4472136f, 0.8944272f);
        glVertex3f(-1.0f, -1.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 1.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);

        //glutWireTeapot(5.0);
        //glutSolidTeapot(5.0);

        glEnd();
 
        glFinish();
        SwapBuffers( doc->m_pDC->GetSafeHdc() );
    }
 
    BOOL PreCreateWindow(CREATESTRUCT& cs) {
        cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        return CView::PreCreateWindow(cs);
    }
     
    BOOL OnEraseBkgnd(CDC* pDC) {
        return true;
    }
 
    void OnDestroy() {
        DocBoxy *doc = (DocBoxy *)GetDocument();
        HGLRC hRC;
        hRC= wglGetCurrentContext();
        wglMakeCurrent(NULL, NULL);
        if (doc->hrc)
            wglDeleteContext(hRC);
        if (doc->m_pDC)
            delete doc->m_pDC;
        CView::OnDestroy();
    }
 
    bool bSetupPixelFormat() {
        DocBoxy *doc = (DocBoxy *)GetDocument();
        static PIXELFORMATDESCRIPTOR pfd = {
            sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
            1,                             // version number
            PFD_DRAW_TO_WINDOW |             // support window
            PFD_SUPPORT_OPENGL |           // support OpenGL
            PFD_DOUBLEBUFFER,              // double buffered
            PFD_TYPE_RGBA,                 // RGBA type
            24,                            // 24-bit color depth
            0, 0, 0, 0, 0, 0,              // color bits ignored
            0,                             // no alpha buffer
            0,                             // shift bit ignored
            0,                             // no accumulation buffer
            0, 0, 0, 0,                    // accum bits ignored
            32,                            // 32-bit z-buffer
            0,                             // no stencil buffer
            0,                             // no auxiliary buffer
            PFD_MAIN_PLANE,                // main layer
            0,                             // reserved
            0, 0, 0                        // layer masks ignored
        };
 
        int pixelformat;
 
        if( (pixelformat = ChoosePixelFormat(doc->m_pDC->GetSafeHdc(), &pfd)) == 0 ) {
            MessageBox(_TEXT("ChoosePixelFormat failed"));
            return false;
        }
 
        if(SetPixelFormat(doc->m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE) {
            MessageBox(_TEXT("SetPixelFormat failed"));
            return false;
        }
 
        return true;
    }
 
    void init() {
        DocBoxy *doc = (DocBoxy *)GetDocument();
        PIXELFORMATDESCRIPTOR pfd; //宣告一個PixelFormat物件
        int n;
        doc->m_pDC = new CClientDC(this); //建立一個視窗繪圖物件
        ASSERT(doc->m_pDC != NULL);
        if (!bSetupPixelFormat()) return; //設定應用所需的像素格式
        n = GetPixelFormat(doc->m_pDC->GetSafeHdc()); //從Windows裡找尋符合我們設定的
        DescribePixelFormat(doc->m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); //PixelFormat的索引值,並將之指定給pfd
        doc->hrc = wglCreateContext(doc->m_pDC->GetSafeHdc()); //將RC物件給DC物件
        wglMakeCurrent(doc->m_pDC->GetSafeHdc(), doc->hrc); //讓這個RC設定為作用中的物件
        glDepthFunc(GL_GREATER);
        glEnable(GL_DEPTH_TEST);
        glClearDepth(-2.0f);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }
 
    void setLight() {
        GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };  //環境光強度
        GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };  //光源漫射強度
        GLfloat LightPosition[]= { 1.0f, 0.0f, 0.0f, 0.0f }; //表示在x方向無限遠
 
        glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
        glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
        glLightfv(GL_LIGHT0, GL_POSITION,LightPosition);
        glEnable(GL_LIGHT0); //使用光源0
        glEnable(GL_LIGHTING); //打光
        glEnable(GL_COLOR_MATERIAL); //啟用材質顏色
    }
 
    int OnCreate(LPCREATESTRUCT lpCreateStruct) {
        if (CView::OnCreate(lpCreateStruct) == -1)
            return -1;
        init();
        setLight();
        return 0;
    }
     
    void OnSize(UINT nType, int cx, int cy) {
        CView::OnSize(nType, cx, cy);
 
        GLfloat nRange=20.f;
        if(cy == 0)
        {  cy = 1;  }
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
 
        if (cx <= cy) {
            glOrtho(-nRange, nRange, -nRange*cy/cx, nRange*cy/cx, -nRange, nRange);
        } else {
            glOrtho(-nRange*cx/cy, nRange*cx/cy, -nRange, nRange, -nRange, nRange);
        }
 
        glMatrixMode(GL_MODELVIEW);
        glViewport(0, 0, cx, cy);
    }
 
    DECLARE_DYNCREATE(ViewBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
 
IMPLEMENT_DYNCREATE(ViewBoxy, CView) //宣告ViewBoxy為run-time類別
 
BEGIN_MESSAGE_MAP(ViewBoxy, CView) //建立訊息映射表
    ON_WM_DESTROY()
    ON_WM_CREATE()
    ON_WM_SIZE()
END_MESSAGE_MAP()
 
class AppBoxy : public CWinApp {
public:
    BOOL InitInstance() {
        CDocument *doc;    //宣告指向文件的指標
        CSingleDocTemplate* DocTemplate; //宣告指向單文件樣版物件的指標
 
        DocTemplate = new CSingleDocTemplate( //建立具有單文件樣版物件
            IDR_MENU, //用於單文件框架之資源的識別子
            RUNTIME_CLASS(DocBoxy), //單文件視窗的Document
            RUNTIME_CLASS(FrameBoxy),    //單文件視窗的視窗框架
            RUNTIME_CLASS(ViewBoxy)      //單文件視窗的View
        );
 
        AddDocTemplate(DocTemplate);   //將單文件樣版物件設定給MyApp
        doc = DocTemplate->CreateNewDocument();    //建立新的文件
 
        m_pMainWnd = DocTemplate->CreateNewFrame( doc, NULL ); //建立一個視窗框架
        DocTemplate->InitialUpdateFrame ( (CFrameWnd*)m_pMainWnd, doc ); //起始化視窗框架物件,並連結View物件
        m_pMainWnd->ShowWindow(SW_SHOW); //顯示視窗
 
        return true;
    }
};
 
AppBoxy appboxy; //建立應用程式物件

說明:
基礎MFC使用OpenGL繪製面並且打光。

MFC draw face using OpenGL

標題:MFC使用OpenGL繪製面
MFC (OpenGLglut.cpp):
#include <afxwin.h>
#include "OpenGLglut.h"
#include "glut.h"
 
class DocBoxy : public CDocument {
public:
    CClientDC *m_pDC;
    HGLRC hrc;
    DECLARE_DYNCREATE(DocBoxy) //宣告run-time類別
};
 
IMPLEMENT_DYNCREATE(DocBoxy, CDocument) //宣告DocBoxy為run-time類別
 
class FrameBoxy : public CFrameWnd {
public:
    afx_msg void OnExit() { //宣告關閉檔案事件
        if(MessageBox( TEXT("Are you sure you want to close this window?"), TEXT("DocViewBoxy"), MB_OKCANCEL | MB_ICONQUESTION ) == 1 ) {
            DestroyWindow();
        }
    }
    afx_msg void OnAbout() { //宣告關於事件
        MessageBox( TEXT("DocViewBoxy Version 1.0\nCopyright (C) 2013, By QQBoxy Huang.\nEmail: gs1458@gmail.com"), TEXT("About"), MB_OK );
    }
 
    DECLARE_DYNCREATE(FrameBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
 
IMPLEMENT_DYNCREATE(FrameBoxy, CFrameWnd) //宣告FrameBoxy為run-time類別
 
BEGIN_MESSAGE_MAP(FrameBoxy, CFrameWnd) //建立訊息映射表
    ON_COMMAND(ID_FILE_EXIT, OnExit)
    ON_COMMAND(ID_HELP_ABOUT, OnAbout)
END_MESSAGE_MAP()
 
class ViewBoxy : public CView {
public:
    void OnDraw(CDC * aDC) { //必須覆寫的虛擬函數
        DocBoxy *doc = (DocBoxy *)GetDocument();
        ASSERT_VALID(doc); //斷言,確認doc是否存在(在發行版本不會執行)
         
        glLoadIdentity();
 
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glTranslatef(0.0f,0.0f,-1.0f);
        glColor3f(0.0f,0.0f,0.0f);
 
        /*glBegin(GL_LINES);
            glVertex3f(0.0f, 1.0, 0.0f);
            glVertex3f(-1.0, -1.0, 0.0f);
             
            glVertex3f(-1.0f, -1.0, 0.0f);
            glVertex3f(1.0f, -1.0, 0.0f);
 
            glVertex3f(1.0f, -1.0f, 0.0f);
            glVertex3f(0.0f, 1.0f, 0.0f);
        glEnd();*/
 
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f,0.0f,0.0f); //Red
            glVertex3f( 0.0f, 1.0f, 0.0f); // Top
            glColor3f(0.0f,1.0f,0.0f); //Green
            glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
            glColor3f(0.0f,0.0f,1.0f); //Blue
            glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
        glEnd();
 
        glFinish(); //繪圖完畢
        SwapBuffers( doc->m_pDC->GetSafeHdc() ); //
    }
 
    BOOL PreCreateWindow(CREATESTRUCT& cs) {
        cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; //加入 WS_CLIPSIBLINGS 及 WS_CLIPCHILDREN 旗標
        return CView::PreCreateWindow(cs); //避免OpenGL畫到別的視窗
    }
     
    BOOL OnEraseBkgnd(CDC* pDC) {
        return true; //不要重繪視窗
    }
 
    void OnDestroy() { //清除記憶體
        DocBoxy *doc = (DocBoxy *)GetDocument();
        HGLRC hRC;
        hRC= wglGetCurrentContext();
        wglMakeCurrent(NULL,  NULL);
        if (doc->hrc)
            wglDeleteContext(hRC);
        if (doc->m_pDC)
            delete doc->m_pDC;
        CView::OnDestroy();
    }
 
    bool bSetupPixelFormat() {
        DocBoxy *doc = (DocBoxy *)GetDocument();
        static PIXELFORMATDESCRIPTOR pfd = {
            sizeof(PIXELFORMATDESCRIPTOR), // 像素的結構大小
            1,                             // 版本號
            PFD_DRAW_TO_WINDOW |           // 緩衝特性:視窗支援
            PFD_SUPPORT_OPENGL |           // 緩衝特性:OpenGL支援
            PFD_DOUBLEBUFFER,              // 緩衝特性:雙精度緩衝
            PFD_TYPE_RGBA,                 // 設定顏色類型為RGBA
            24,                            // 色彩深度:24-bit color depth
            0, 0, 0, 0, 0, 0,              // color bits ignored
            0,                             // no alpha buffer
            0,                             // shift bit ignored
            0,                             // no accumulation buffer
            0, 0, 0, 0,                    // accum bits ignored
            32,                            // 深度緩衝:32-bit z-buffer
            0,                             // no stencil buffer
            0,                             // no auxiliary buffer
            PFD_MAIN_PLANE,                // main layer
            0,                             // reserved
            0, 0, 0                        // layer masks ignored
        };
 
        int pixelformat;
 
        if ( (pixelformat = ChoosePixelFormat(doc->m_pDC->GetSafeHdc(), &pfd)) == 0 ) {
            MessageBox(_TEXT("ChoosePixelFormat failed"));
            return false;
        }
 
        if (SetPixelFormat(doc->m_pDC->GetSafeHdc(), pixelformat, &pfd) == false) {
            MessageBox(_TEXT("SetPixelFormat failed"));
            return false;
        }
 
        return true;
    }
 
    void init() {
        int n;
        DocBoxy *doc = (DocBoxy *)GetDocument();
        PIXELFORMATDESCRIPTOR pfd; //宣告一個PixelFormat物件
        doc->m_pDC = new CClientDC(this); //建立一個視窗繪圖物件
        ASSERT_VALID(doc->m_pDC); //斷言,確認視窗繪圖物件是否存在(在發行版本不會執行)
        if (!bSetupPixelFormat()) return; //設定應用所需的像素格式
        n = GetPixelFormat(doc->m_pDC->GetSafeHdc()); //從Windows裡找尋符合我們設定的
        DescribePixelFormat(doc->m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd); //PixelFormat的索引值,並將之指定給pfd
        doc->hrc = wglCreateContext(doc->m_pDC->GetSafeHdc()); //將RC物件給DC物件
        wglMakeCurrent(doc->m_pDC->GetSafeHdc(), doc->hrc); //讓這個RC設定為作用中的物件
        glDepthFunc(GL_GREATER); //深度資訊
        glEnable(GL_DEPTH_TEST); //深度測試
        glClearDepth(-2.0f); //初始化深度
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //初始化顏色

        //PixelFormat:視窗的所有3D特徵設定,包括緩衝區、 顏色深度等集合的名稱
        //RC:背景渲染,OpenGL裡負責紀錄目前顏色,狀態設定等資訊的物件
    }
 
    int OnCreate(LPCREATESTRUCT lpCreateStruct) {
        if(CView::OnCreate(lpCreateStruct) == -1) { //判斷視窗生成
            return -1;
        }
        init();
        return 0;
    }
     
    void OnSize(UINT nType, int cx, int cy) {
        CView::OnSize(nType, cx, cy);
 
        GLfloat nRange = 5.0f; //比例係數
        if(cy == 0) { //防止分母為零
            cy = 1;
        }
 
        glMatrixMode(GL_PROJECTION); //透視場景
        glLoadIdentity(); //重新設定矩陣
 
        if (cx <= cy) { //調整視窗比例
            glOrtho(-nRange, nRange, -nRange*cy/cx, nRange*cy/cx, -nRange, nRange);
        } else {
            glOrtho(-nRange*cx/cy, nRange*cx/cy, -nRange, nRange, -nRange, nRange);
        }

        glMatrixMode(GL_MODELVIEW); //重新設定觀點矩陣
        glViewport(0, 0, cx, cy); //設定觀點
    }
 
    DECLARE_DYNCREATE(ViewBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};
 
IMPLEMENT_DYNCREATE(ViewBoxy, CView) //宣告ViewBoxy為run-time類別
 
BEGIN_MESSAGE_MAP(ViewBoxy, CView) //建立訊息映射表
    ON_WM_DESTROY()
    ON_WM_CREATE()
    ON_WM_SIZE()
    //ON_WM_KEYDOWN()
    //ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
 
class AppBoxy : public CWinApp
{
public:
    BOOL InitInstance()
    {
        CDocument *doc;    //宣告指向文件的指標
        CSingleDocTemplate* DocTemplate; //宣告指向單文件樣版物件的指標
 
        DocTemplate = new CSingleDocTemplate( //建立具有單文件樣版物件
            IDR_MENU, //用於單文件框架之資源的識別子
            RUNTIME_CLASS(DocBoxy), //單文件視窗的Document
            RUNTIME_CLASS(FrameBoxy),    //單文件視窗的視窗框架
            RUNTIME_CLASS(ViewBoxy)      //單文件視窗的View
        );
 
        AddDocTemplate(DocTemplate);   //將單文件樣版物件設定給MyApp
        doc = DocTemplate->CreateNewDocument();    //建立新的文件
 
        m_pMainWnd = DocTemplate->CreateNewFrame( doc, NULL ); //建立一個視窗框架
        DocTemplate->InitialUpdateFrame ( (CFrameWnd*)m_pMainWnd, doc ); //起始化視窗框架物件,並連結View物件
        m_pMainWnd->ShowWindow(SW_SHOW); //顯示視窗
 
        return true;
    }
};
 
AppBoxy appboxy; //建立應用程式物件

說明:
基礎MFC使用OpenGL繪製面。

2013年8月16日 星期五

Encryption in Node.JS using crypto

標題:在Node.JS中使用加密
Node.JS (encryption.js):

範例輸出:

說明:
crypto模組在Node.JS為原生Module可直接使用,切勿再使用npm安裝或使用相對路徑require,最後為使用Regular expression比對base64格式。

2013年8月5日 星期一

Create Static Splitter Window using MFC

標題:使用MFC建立靜態分割視窗
VC++ (SplitterWindow.cpp):
範例結果:


動態教學:
Open
直接下載:
http://docs.google.com/uc?id=0B8HdyDgIiLuHREdOUmVuSUtoZzA

說明:
在MFC中建立分割視窗,左視窗為CFrameWnd類別可設計表單,右視窗為CView類別可進行繪圖,注意須建立『IDD_FORMVIEW』之Dialog資源。

2013年7月18日 星期四

Data Synchronization Update

標題:同步變更數據
Node.JS (getdata.js):
var server,
    ip   = "127.0.0.1",
    port = 1337,
    http = require("http"),
    url = require("url"),
    fs = require("fs"),
    qs = require("querystring");

var textPath = "p.txt";

server = http.createServer(function (req, res) {
    var polling = {
        timeover : false,
        timeout : null,
        request : function(q) {
            this.timeout = setTimeout(function() {
                polling.timeover = true;
            }, 10000);
            this.action(q);
        },
        action : function(q) {
            if(this.timeover == true) { //確認連線逾時
                q.callback({});
            } else {
                fs.readFile(textPath, "utf-8", function(err, file) {
                    if(q.json.lastTime < qs.parse(file).lastTime) { //有新資料
                        q.callback(qs.parse(file));
                    } else { //駐列輪巡
                        setTimeout(function() { polling.action(q) }, 100);
                    }
                });
            }
        }
    };
    
    switch(url.parse(req.url).pathname) {
        case "/jquery":
            fs.readFile("js/jquery-1.9.1.js", function (err, data) {
                if(err) throw err;
                res.writeHead(200, {"Content-Type": "text/javascript", "Content-Length":data.length});
                res.write(data);
                res.end();
            });
            break;
        case "/index":
            fs.readFile("index.html", function (err, data) {
                if(err) throw err;
                res.writeHead(200, {"Content-Type": "text/html", "Content-Length":data.length});
                res.write(data);
                res.end();
            });
            break;
        case "/sendData":
            var sendData = "";
            var lastTime = new Date().getTime();
            req.setEncoding("utf8");
            req.addListener("data", function(sendDataChunk) {
                sendData += sendDataChunk;
            });
            req.addListener("end", function() {
                sendData += "&lastTime=" + lastTime;
                fs.open(textPath, "w", 0644, function(err, fd) {
                    if(err) throw err;
                    fs.write(fd, sendData, 0, "utf8", function(err) {
                        if(err) throw err;
                        fs.closeSync(fd);
                        var json = JSON.stringify({lastTime: lastTime});
                        res.writeHead(200, {
                            "Content-Type": "text/json",
                            "Content-Length": json.length
                        });
                        console.log(json);
                        res.end(json);
                    })
                });
            });
            break;
        case "/getData":
            var getData = "";
            req.setEncoding("utf8");
            req.addListener("data", function(getDataChunk) {
                getData += getDataChunk;
            });
            req.addListener("end", function() {
                polling.request({
                    json : qs.parse(getData),
                    callback : function(data) {
                        var json = JSON.stringify(data);
                        res.writeHead(200, {
                            "Content-Type": "text/json",
                            "Content-Length": json.length
                        });
                        res.end(json);
                    }
                });
            });
            break;
        default:
            res.writeHead(200, {"Content-Type": "text/html"});
            res.write("Page not found.");
            res.end();
            break;
    }
    
});

server.listen(port);

console.log("Server running at http://" + ip + ":" + port);
HTML (index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-TW" xml:lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<script src="/jquery"></script>
<script type="text/javascript">
$(function() {
    var localurl = window.location.protocol + "//" + window.location.host;
    var lastTime = 0;
    $("#sendBtn").click(function() {
        var username = $("#username").val();
        var xValue = $("#xValue").val();
        var yValue = $("#yValue").val();
        var zValue = $("#zValue").val();
        $.post("/sendData", {user: username, x: xValue, y: yValue, z: zValue},
        function(data) {
            console.log("Message Send on " + data.lastTime);
        }, "json");
    });
    var pollingData = function() {
        $.ajax({
            cache: false,
            dataType: "json",
            type: "POST",
            url: localurl + "/getData",
            data: {lastTime: lastTime},
            error: function (xhr, status, error) {
                document.write("Error");
            },
            success: function (json) {
                //console.log(json);
                if(!$.isEmptyObject(json)) { //判斷內容是否為空
                    console.log("Update...");
                    //lastTime = new Date().getTime();
                    $("#username").val(json.user);
                    $("#xValue").val(json.x);
                    $("#yValue").val(json.y);
                    $("#zValue").val(json.z);
                    lastTime = json.lastTime;
                    pollingData();
                } else { //繼續輪巡
                    console.log("Pulling...");
                    pollingData();
                }
            }
        });
    };
    pollingData();
});
</script>
</head>
<body>
name: <input type="text" id="username" value="QQBoxy" /><br />
x: <input type="text" id="xValue" value="3" /><br />
y: <input type="text" id="yValue" value="4" /><br />
z: <input type="text" id="zValue" value="5" /><br />
<button id="sendBtn">Send</button>
</body>
</html>
Batch (Start.bat):

說明:
使用Node.JS與jQuery進行數據同步變更。

Read STL Model file and export graphics using GD Library

標題:讀取STL模型檔案並使用GD Library匯出圖形
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex>
#include <fstream>
#include "gd/gd.h"
#include "gd/gdfontg.h"
#include "gd/gdfontt.h"
#include "gd/gdfonts.h"
#include "gd/gdfontmb.h"
#include "gd/gdfontl.h"
#pragma comment(lib, "lib/bgd.lib")
using namespace std;

class pos
{
public:
    float x;
    float y;
    float z;
};

class ERR
{
private:
    int kind;
public:
    ERR(int k) : kind(k){}; //建構函式
    char* message()
    {
        switch(kind)
        {
        case 0:
            return "Parameter Error!";
        case 1:
            return "Path Error!";
        case 2:
            return "File Error!";
        case 3:
            return "Array Size is too small.";
        case 4:
            return "STL Format Error!";
        }
    }
};

void draw(gdImagePtr im, pos vArr[], int v, int color)
{
    int i = 0;
    for(i=0;i<v;i+=3)
    {
        gdImageLine(im, (int)vArr[i].x, (int)vArr[i].y, (int)vArr[i+1].x, (int)vArr[i+1].y, color);
        gdImageLine(im, (int)vArr[i+1].x, (int)vArr[i+1].y, (int)vArr[i+2].x, (int)vArr[i+2].y, color);
        gdImageLine(im, (int)vArr[i].x, (int)vArr[i].y, (int)vArr[i+2].x, (int)vArr[i+2].y, color);
    }
}

int main(int argc, char* argv[])
{
    const float zoom = 5;
    const int arrSize = 3000;
    const int width = 500;
    const int height = 500;
    int i = 0, normals = 0, vertexs = 0;
    char pathChar[1024];
    char stlName[1024];
    string lines = "";

    pos *normalArr;
    pos *vertexArr;

    regex pathRegex("([a-zA-Z]\:\\\\(?:[^\\\\]+\\\\)*)(.*)"); //"C:\\temp\\hello.txt"
    regex solid(" *solid.*");
    regex facetNormal(" *facet normal ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)");
    regex outerLoop(" *outer loop");
    regex vertex(" *vertex ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) ([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)");
    regex endLoop(" *endloop");
    regex endfacet(" *endfacet");
    regex endsolid(" *endsolid");
    
    cmatch pathMatch;
    smatch normalMatch;
    smatch vertexMatch;

    try
    {
        if(argc<2) throw ERR(1); //Check Parameter

        strcpy(pathChar, argv[1]); //Get STL Path
        if(!regex_match(pathChar, pathMatch, pathRegex)) throw ERR(1); //Check STL Path

        strcpy(stlName, pathMatch[2].str().c_str()); //Get STL Name

        ifstream File(pathChar);
        if (!File.is_open()) throw ERR(2); //Check File

        normalArr = new pos[arrSize]; //宣告Facet normal動態陣列大小
        vertexArr = new pos[arrSize*3]; //宣告Vertex動態陣列大小

        cout << "Reading Now..." << endl;

        //solid
        if(!File.good())  throw ERR(4);
        getline(File, lines);
        if(!regex_match(lines, solid)) throw ERR(4);

        while(File.good())
        {
            if(normals >= arrSize) throw ERR(3); //Check Array Size

            //facet normal
            getline(File, lines);
            if(regex_match(lines, normalMatch, facetNormal))
            {
                normalArr[normals].x = atof(normalMatch[1].str().c_str()) + width/2;
                normalArr[normals].y = atof(normalMatch[2].str().c_str()) + height/2;
                normalArr[normals].z = atof(normalMatch[3].str().c_str());
                normals++;
            }
            else if(regex_match(lines, endsolid)) //end solid
            {
                break;
            }
            else
            {
                throw ERR(4);
            }
 
            //outer loop
            getline(File, lines);
            if(!regex_match(lines, outerLoop)) throw ERR(4);
 
            //vertex
            for(i=0;i<3;i++)
            {
                getline(File, lines);
                if(regex_match(lines, vertexMatch, vertex))
                {
                    vertexArr[vertexs].x = atof(vertexMatch[1].str().c_str())*zoom + width/2;
                    vertexArr[vertexs].y = atof(vertexMatch[2].str().c_str())*zoom + height/2;
                    vertexArr[vertexs].z = atof(vertexMatch[3].str().c_str())*zoom;
                    vertexs++;
                }
                else
                {
                    throw ERR(4);
                }
            }
 
            //end loop
            getline(File, lines);
            if(!regex_match(lines, endLoop)) throw ERR(4);
 
            //end facet
            getline(File, lines);
            if(!regex_match(lines, endfacet)) throw ERR(4);
        }
        File.close();

        cout << "Facet Normal: " << normals << endl;
        cout << "Vertex: " << vertexs << endl;
        cout << "Done." << endl;

        // --- draw ---
        cout << "Drawing Now..." << endl;

        FILE *out;
        int size;
        char *data;
 
        gdImagePtr im = gdImageCreate(width, height);
     
        int white =  gdImageColorAllocate(im, 255, 255, 255), //Background
            black =  gdImageColorAllocate(im, 0, 0, 0),
            blue =   gdImageColorAllocate(im, 0, 0, 255),
            cyan =   gdImageColorAllocate(im, 0, 255, 255),
            green =  gdImageColorAllocate(im, 128, 255, 0),
            red =    gdImageColorAllocate(im, 255, 0, 0),
            purple = gdImageColorAllocate(im, 255, 0, 255),
            orange = gdImageColorAllocate(im, 255, 128, 0),
            yellow = gdImageColorAllocate(im, 255, 255, 0);
 
        strcpy(pathChar, argv[0]); //Get Execution Path
        if(!regex_match(pathChar, pathMatch, pathRegex)) throw ERR(1); //Check Execution Path
        strcpy(pathChar, pathMatch[1].str().c_str()); //Get Folder Path
        strcat(pathChar, stlName); //Connections Folder Path and STL Name
        strcat(pathChar, ".png"); //Connections Path and Extension

        out = fopen(pathChar, "wb");
        if (!out) throw ERR(1);

        draw(im, vertexArr, vertexs, black);

        data = (char *) gdImagePngPtr(im, &size);
        if (!data) throw ERR(2);

        if (fwrite(data, 1, size, out) != size) throw ERR(3);

        if (fclose(out) != 0) throw ERR(4);
        gdFree(data);
        cout << "Done." << endl;
    }
    catch(ERR e)
    {
        cout << e.message() << endl;
    }
    system("pause");

    return 0;
}

說明:
首先拖曳一個STL模型檔案至執行檔以進行讀取,一邊進行檔案比對一邊將點資料存入類別陣列內,再使用GD Library匯出png圖片檔。

Solving Matrix Inverse using Pointer

標題:使用指標求解n維矩陣
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include <iomanip>
using namespace std;

void print(double *Arr, int m)
{
    int i = 0, j = 0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            cout << *(Arr+i*m+j) << setprecision(2) << "\t";
        }
        cout << endl;
    }
}
void printInverse(double *Matrix, double *Inverse, int m)
{
    int i = 0, j = 0;
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            cout << *(Matrix+i*m+j) << setprecision(2) << "\t";
        }
        cout << "|\t";
        for(j=0;j<m;j++)
        {
            cout << *(Inverse+i*m+j) << setprecision(2) << "\t";
        }
        cout << endl;
    }
}

void readfile(string filePath, double *Matrix)
{
    int m = 0, n = 0, i = 0;
    string lines = "";
    string::const_iterator start;
    match_results<string::const_iterator> matchMatrix; //迭代器
    regex regMatrix("([0-9]+)+");
    ifstream outputFile(filePath);
    if (outputFile.is_open()) //Check File
    {
        while(outputFile.good())
        {
            getline(outputFile, lines);
            start = lines.begin();
            while (regex_search(start, lines.cend(), matchMatrix, regMatrix))
            {
                *(Matrix+i) = atof(matchMatrix[1].str().c_str());
                i++;
                start = matchMatrix[0].second;
            }
            m++;
        }
        outputFile.close();
    }
}
double plusnum(double num)
{
    if(num < 0) return -1 * num;
    return num;
}

void rowSwap(double *Matrix, int ma, int mb, int m)
{
    int i = 0;
    double *Temp = new double[m];
    for(i=0;i<m;i++)
    {
        *(Temp+i) = *(Matrix+ma*m+i);
        *(Matrix+ma*m+i) = *(Matrix+mb*m+i);
        *(Matrix+mb*m+i) = *(Temp+i);
    }
}

void inverse(double *Matrix, double *Inverse, int m)
{
    int i = 0, j = 0, k = 0;
    double temp = 0;
    //*(Matrix + rows * m + columns) 看成 Matrix[rows][columns]
    for(int i=0; i<m; i++)
    {
        for(int j=i+1; j<m; j++)
        {
            if(plusnum(*(Matrix + j * m + i)) > plusnum(*(Matrix + i * m + i)))
            {
                rowSwap(Matrix, i, j, m); // 互換Matrix矩陣兩行
                rowSwap(Inverse, i, j, m); // 互換Inverse矩陣兩行
                //cout << "[" << i << "][" << j << "]Swap:" << endl;
                //printInverse(Matrix, Inverse, m);
            }
        }
        // 消去
        for(int j=0; j<m; j++) //第 i 次消去運算,以第 i 行作為主要行,將其上下各行的第 i 列元素化為零
        {
            if(j != i)
            {
                // 進行消去運算,將第 i 列上下方的元素化為零
                double temp = *(Matrix + j * m + i) / *(Matrix + i * m + i);
                
                // 注意此處 k 可從 i+1 開始
                //for(int k=i+1; k<m; k++)
                for(int k=0; k<m; k++)
                {
                    *(Matrix + j * m + k) -= (*(Matrix + i * m + k) * temp);
                }
                // 注意此處 k 要從 0 開始
                for(int k=0; k<m; k++)
                {
                    *(Inverse + j * m + k) -= (*(Inverse + i * m + k) * temp);
                }
                //cout << "[" << i << "][" << j << "]Matrix:" << endl;
                //printInverse(Matrix, Inverse, m);
            }
        }
    }

    // Matrix 已經是對角矩陣,只要在將其對角線元素化為 1 即可
    // 此時僅需對 Inverse 矩陣作運算
    //cout << "[" << i << "][" << j << "]Inverse:" << endl;
    //printInverse(Matrix, Inverse, m);
    for(int i=0; i<m; i++)
    {
        for(int j=0; j<m; j++)
        {
            *(Inverse + i * m + j) /= *(Matrix + i * m + i);
        }
        //cout << "[" << i << "][" << j << "]Inverse:" << endl;
        //printInverse(Matrix, Inverse, m);
    }
}
 
int main(int argc, char *argv[])
{
    const int m = 4;
    int i = 0, j = 0;
    double Matrix[m][m] = {};
    double Inverse[m][m] = {};

    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i == j)
                Inverse[i][j] = 1;
            else
                Inverse[i][j] = 0;
        }
    }
    
    readfile("matrix.txt", &Matrix[0][0]);

    cout << "Matrix:" << endl;
    print(&Matrix[0][0], m);

    inverse(&Matrix[0][0], &Inverse[0][0], m);
    cout << endl;
 
    cout << "Inverse:" << endl;
    print(&Inverse[0][0], m);

    system("pause");
    return 0;
}
TEXT (matrix.txt):

說明:
先讀取矩陣數據再利用指標的方式求得n維度反矩陣的解。

2013年7月16日 星期二

Check the ASCII format of STL file

標題:檢查ASCII STL格式範例
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <regex> //string lib here

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int i = 0, num = 1;
    const char *filePath = "TriangleASCII.STL";
    string lines = "";

    regex solid(" *solid.*");
    regex facetNormal(" *facet normal .+ .+ .+");
    regex outerLoop(" *outer loop");
    regex vertex(" *vertex .+ .+ .+");
    regex endLoop(" *endloop");
    regex endfacet(" *endfacet");
    regex endsolid(" *endsolid");
    //regex number("[+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?");

    ifstream outputFile(filePath);
    if (outputFile.is_open()) //Check File
    {
        num++;
        //solid
        if(outputFile.good())
        {
            getline(outputFile, lines);
            if(regex_match(lines, solid))
            {
                cout << lines << endl;

                while(outputFile.good())
                {
                    //facet normal
                    getline(outputFile, lines);
                    if(regex_match(lines, facetNormal))
                    {
                        cout << lines << endl;
                        num++;
                    } else if(regex_match(lines, endsolid)) { //end solid
                        cout << lines << endl;
                        cout << "Done." << endl;
                        break;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }

                    //outer loop
                    getline(outputFile, lines);
                    if(regex_match(lines, outerLoop))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }

                    //vertex
                    getline(outputFile, lines);
                    if(regex_match(lines, vertex))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }
                
                    getline(outputFile, lines);
                    if(regex_match(lines, vertex))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }
                
                    getline(outputFile, lines);
                    if(regex_match(lines, vertex))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }

                    //end loop
                    getline(outputFile, lines);
                    if(regex_match(lines, endLoop))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }

                    //end facet
                    getline(outputFile, lines);
                    if(regex_match(lines, endfacet))
                    {
                        cout << lines << endl;
                        num++;
                    } else {
                        cout << "[line " << num << " format error] " << lines << endl;
                        break;
                    }
                }
            } else {
                cout << "[line " << num << " format error] " << lines << endl;
            }
        }
        outputFile.close();
    }

    return 0;
}
說明:
使用Regular Expression檢查ASCII STL檔案格式。

Simple drawing using the GD Libary

標題:GD繪圖範例
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include "gd/gd.h"
#include "gd/gdfontg.h"
#include "gd/gdfontt.h"
#include "gd/gdfonts.h"
#include "gd/gdfontmb.h"
#include "gd/gdfontl.h"
#include <math.h>
using namespace std;

#pragma comment(lib, "lib/bgd.lib")

#define DEG 3.14159/180.

class ERR
{
private:
    int kind;
public:
    ERR(int k) : kind(k){};
    char* message()
    {
        switch(kind)
        {
        case 0:
            return "File Error!";
        case 1:
            return "GD Error!";
        case 2:
            return "File Write Error!";
        case 3:
            return "File Close Error!";
        }
    }
};

void drawstar(gdImagePtr image, float origx, float origy, float r, float angle, int color)
{
    int i = 0;
    float ptx[6],pty[6],pttx[6],ptty[6];
    float temp1,temp2,temp3,temp4;

    for(i=0;i<6;i++)
    {
        ptx[i]=origx+r*sin(72.*i*DEG+angle*DEG);
        pty[i]=origy+r*cos(72.*i*DEG+angle*DEG);

        temp1=cos(54.*DEG-72.*DEG*i-angle*DEG);
        temp2=sin(54.*DEG-72.*DEG*i-angle*DEG);
        temp3=sin(18.*DEG);
        temp4=cos(36.*DEG);

        pttx[i]=origx+r*temp3*temp1/temp4;
        ptty[i]=origy+r*temp3*temp2/temp4;
    }
    for(i=0;i<5;i++)
    {
        gdImageLine(image, (int)ptx[i], (int)pty[i], (int)pttx[i], (int)ptty[i], color);
        gdImageLine(image, (int)pttx[i], (int)ptty[i], (int)ptx[i+1], (int)pty[i+1], color);
    }
}

int main(int argc, _TCHAR* argv[])
{
    int i = 0;
    FILE *out;
    int size;
    char *data;

    gdImagePtr im = gdImageCreate(500, 500);
    
    int white =  gdImageColorAllocate(im, 255, 255, 255), //Background
        black =  gdImageColorAllocate(im, 0, 0, 0),
        blue =   gdImageColorAllocate(im, 0, 0, 255),
        cyan =   gdImageColorAllocate(im, 0, 255, 255),
        green =  gdImageColorAllocate(im, 128, 255, 0),
        red =    gdImageColorAllocate(im, 255, 0, 0),
        purple = gdImageColorAllocate(im, 255, 0, 255),
        orange = gdImageColorAllocate(im, 255, 128, 0),
        yellow = gdImageColorAllocate(im, 255, 255, 0);

    out = fopen("image.png", "wb");
    try
    {
        if (out) {
            for(i=0;i<72;i++)
            {
                drawstar(im, 250+5*i*sin(i*10*DEG), 250+5*i*cos(i*10*DEG), 30, 180, black);
            }

            data = (char *) gdImagePngPtr(im, &size);
            if (!data) {
                throw ERR(1);
            }
            if (fwrite(data, 1, size, out) != size) {
                throw ERR(2);
            }
            if (fclose(out) != 0) {
                throw ERR(3);
            }
            gdFree(data);
        }
        else
        {
            throw ERR(0);
        }
    }
    catch(ERR e)
    {
        cout << e.message() << endl;
    }

    return 0;
}

說明:
簡單使用GD Library進行繪圖。

2013年7月15日 星期一

Exception Handling

標題:例外處理的方法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex>
#include <fstream>
using namespace std;

class pos
{
public:
    float x;
    float y;
    float z;
};

class ERR
{
private:
    int kind;
public:
    ERR(int k) : kind(k){}; //建構函式
    char* message()
    {
        switch(kind)
        {
        case 0:
            return "Error A";
        case 1:
            return "Error B";
        case 2:
            return "Error C";
        }
    }
};

void readfile(string path)
{
    string lines = "";
    ifstream File(path);
    if (File.is_open()) //Check File
    {
        while(File.good())
        {
            getline(File, lines);
            cout << lines << endl;
        }
        File.close();
    }
}

int main(int argc, char *argv[])
{
    switch(0) //Select Example
    {
    case 0:
        {
            //Example 1
            int n = 0;
            string str = "Hello";
            try
            {
                cin >> n;
                cout << str.at(n) << endl; //取得字串的第n個字元
            }
            catch (...)
            {
                cout << "Error!" << endl;
            }
            break;
        }
    case 1:
        {
            //Example 2
            float num = 1;
            try
            {
                cout << "num=";
                cin >> num;
                if(num == 0)
                {
                    throw "Infinity!";
                }
                cout << "100/num=" << 100/num << endl;
            }
            catch (const char* message)
            {
                cout << message << endl;
            }
            break;
        }
    case 2:
        {
            //Example 3
            try {
                if(argc<2)
                {
                    throw "Parameter error.";
                }
                char pathChar[1024];
                regex pathRegex("([a-zA-Z]\:\\\\(?:[^\\\\]+\\\\)*.*)"); //"C:\\temp\\hello.txt"
                cmatch pathMatch; //cmatch: Char Match
                strcpy(pathChar, argv[1]); //Pointer to Char
                if(regex_match(pathChar, pathMatch, pathRegex)) //char, cmatch, regexp
                {
                    readfile(pathMatch[1].str());
                }
                else
                {
                    throw "Path Error.";
                }
            }
            catch(const char* message)
            {
                cout << message << endl;
            }
            break;
        }
    case 3:
        {
            //Example 4
            pos p;
            p.x = 1.1f;
            p.y = 2.2f;
            p.z = 3.3f;
            cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
            break;
        }
    case 4:
        {
            //Example 5
            try
            {
                throw ERR(0);
            }
            catch(ERR e)
            {
                cout << e.message() << endl;
            }
            break;
        }
    }

    system("pause");
    return 0;
}

說明:
一般及搭配argc、argv、Regular Expression、Class例外處理的方法

Drag to read the file

標題:拖曳方式讀取檔案
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream> //ifstream
#include <sstream> //getline
using namespace std;

int main(const int argc, char *argv[])
{
    /*int i = 0;
    cout << "argc: " << argc << endl;
    for(i=0;i<argc;i++)
    {
        cout << "*argv[" << i << "]: " << argv[i] << endl;
    }*/

    //Path Convert
    char pathChar[1024];
    string pathString = "";
    strcpy(pathChar, argv[1]); //Pointer to Char
    pathString.assign(pathChar); //Char to String

    //ReadFile
    string lines = "";
    ifstream File(pathString);
    if (File.is_open()) //Check File
    {
        while(File.good())
        {
            getline(File, lines);
            cout << lines << endl;
        }
        File.close();
    }

    system("pause");
    return 0;
}
Batch (Start.bat):


說明:
將檔案拖曳至exe執行檔,並藉由argc、argv取得路徑,轉換路徑變數型態後進行讀檔動作。
Batch檔是用來測試多行參數的讀取,須放在與執行檔相同之目錄。

2013年7月10日 星期三

Passing two-dimensional array to a function using pointer #2

標題:使用指標傳值的方式做矩陣相乘運算
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
using namespace std;

void multiply(float *matrixA, float *matrixB, float *matrixC, int p, int q, int r)
{
    int i = 0, j = 0, k = 0;
    float sum = 0.0f;
    for(i=0;i<p;i++)
    {
        for(j=0;j<r;j++)
        {
            sum = 0.0f;
            for (k = 0; k < q; k++)
            {
                sum += *(matrixA + i*q + k) * *(matrixB + k*r + j);
            }
            *(matrixC + i*r + j) = sum;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int p = 4, q = 2, r = 3;
    int i = 0, j = 0;
    float matrixA[p][q] = { //4x2矩陣
        {3, -1},
        {0, 3},
        {1, 4},
        {2, 1}
    };
    float matrixB[q][r] = { //2x3矩陣
        {1, -2, 3},
        {2, 1, 0}
    };
    float matrixC[p][r] = {}; //4x3矩陣
    
    multiply(&matrixA[0][0], &matrixB[0][0], &matrixC[0][0], p, q, r);

    //印出二維陣列資料
    for(i=0;i<p;i++)
    {
        for(j=0;j<r;j++)
        {
            cout << matrixC[i][j] << "\t";
        }
        cout << endl;
    }
    return 0;
}

說明:
將3個二維陣列初始位址及陣列大小傳入函式,並在函式中針對指標內容進行矩陣相乘運算操作。

Get value using Regular Expression

標題:C/C++使用正規表示法取值
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex> //string lib here

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "--- Example 1 ---" << endl;
    //String Match
    string strA = "13393774";
    smatch matchA;
    regex regA("[0-9]+");
    if(regex_match(strA, matchA, regA))
    {
        cout << atof(matchA[0].str().c_str()) << " is number." << endl;
    } else {
        cout << "Not a number." << endl;
    }

    cout << "--- Example 2 ---" << endl;
    //String Match
    string strB = "-4.472136e-001";
    smatch matchB;
    regex regB("[+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?");
    if(regex_match(strB, matchB, regB))
    {
        cout << atof(matchB[0].str().c_str()) << " is number." << endl;
    } else {
        cout << "Not a number." << endl;
    }
    
    cout << "\n--- Example 3 ---" << endl;
    //String Search
    string strC = "   facet normal 0.000000e+000 -4.472136e-001 8.944272e-001";
    smatch matchC;
    regex regC(" *facet normal \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?) \
([+-]?(?=\.[0-9]|[0-9])(?:[0-9]+)?(?:\.?[0-9]*)(?:[eE][+-]?[0-9]+)?)");
    if(regex_match(strC, matchC, regC))
    {
        cout << "Facet normal: (";
        cout << atof(matchC[1].str().c_str()) << ", ";
        cout << atof(matchC[2].str().c_str()) << ", ";
        cout << atof(matchC[3].str().c_str()) << ")" << endl;
    } else {
        cout << "Not Match." << endl;
    }

    return 0;
}

說明:
在C/C++中使用正規表示式取得數值。

Passing two-dimensional array to a function using pointer

標題:使用指標傳入函式的方式進行多維陣列的處理
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
using namespace std;

// 陣列指標位址, 行數, 列數, 乘值
void multiply(float *matrix, int rows, int columns, float num)
{
    int i = 0, j = 0;
    for(i=0;i<rows;i++)
    {
        for(j=0;j<columns;j++)
        {
            *matrix *= num;
            matrix++;
        }
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    const int m = 3, n = 4;
    int i = 0, j = 0;
    float matrixA[m][n] = { //3x4矩陣
        {1, 2, 3, 4},
        {0, 1, 0, 0},
        {0, 0, 1, 0}
    };
    
    //傳入matrixA的記憶體起始儲存位址以進行處理
    multiply(&matrixA[0][0], m, n , 5);

    //印出二維陣列資料
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            cout << matrixA[i][j] << "\t";
        }
        cout << endl;
    }
    return 0;
}

說明:
將陣列初始位址及陣列大小傳入函式,並在函式中針對指標內容進行運算操作。

Passing array to a function

標題:使用一維陣列傳入函式的方式進行一維陣列的處理
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
using namespace std;
 
// 一維陣列, 乘值
float* multiply(float arr[], int m ,float num)
{
    int i = 0;
    for(i=0;i<m;i++)
    {
        arr[i] *= num;
    }
    return arr;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    const int m = 5;
    int i = 0, j = 0;
    float arrA[m] = {1, 2, 3, 4, 5};
    float *arrB = new float[m]; //宣告一動態陣列
     
    arrB= multiply(arrA, m, 5); //傳入一維陣列
 
    for(i=0;i<m;i++) //印出一維陣列資料
    {
        cout << arrB[i] << endl;
    }
    return 0;
}

說明:
直接向函式傳入一維陣列進行處理,再存入另一個陣列輸出。

2013年7月8日 星期一

Using Regular expression in C/C++

標題:C/C++使用正規表示法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <regex> //string lib here

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //String Match
    string strA = "1337";
    regex regA("[0-9]+");
    if(regex_match(strA, regA))
    {
        cout << "A Match." << endl;
    } else {
        cout << "A Not Match." << endl;
    }

    //String Search
    string strB = "cad23342";
    regex regB("[0-9]");
    if(regex_search(strB, regB))
    {
        cout << "B Match." << endl;
    } else {
        cout << "B Not Match." << endl;
    }

    //String Replace
    string strC = "Hello I'm QQBoxy!!";
    regex regC(" ");
    string wordC = "\n";
    cout << regex_replace(strC, regC, wordC) << endl;

    //String Replace
    string strD = "Hello I'm QQBoxy!!";
    regex regD("a|e|i|o|u");
    string wordD = "[$&]";
    cout << regex_replace(strD, regD, wordD) << endl;

    return 0;
}
說明:
在C/C++中正規表示式常用的三種語法。

Two ways to Read and Write Files

標題:C/C++兩種讀取文字檔的方法
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
//#include <iomanip>
//setprecision(4) "3.1415"
//setfill('Q') "QQ123"
//setw(5) "  123"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int i = 0, j = 0;
    const char *filePath = "pi.xls";

    remove(filePath); //Delete File
    
    //VC++ -----------------------------------------------------

    //Write File
    ofstream outputFile(filePath, fstream::app); //fstream::out fstream::app
    if (outputFile.is_open()) //Check File
    {
        for(j=1;j<10;j++)
        {
            for(i=1;i<10;i++)
            {
                outputFile << i << "*" << j << "=" << i*j << "\t";
            }
            outputFile << endl;
        }
        outputFile.close();
    }

    //Read File
    string lines = "";
    ifstream inputFile(filePath);
    if (inputFile.is_open()) //Check File
    {
        while(inputFile.good())
        {
            getline(inputFile, lines);
            cout << lines << endl;
        }
        inputFile.close();
    }

    //Standard C -----------------------------------------------------

    //Write File
    FILE *writeFile = fopen(filePath, "a");
    if (writeFile) { //Check File
        for(j=1;j<10;j++)
        {
            for(i=1;i<10;i++)
            {
                fprintf(writeFile, "%d*%d=%d\t", i, j, i*j);
            }
            fprintf(writeFile, "\n");
        }
        fclose(writeFile);
    }
    
    //Read File
    char line[100] = ""; // Light!!
    FILE *readFile = fopen(filePath, "r");
    if (readFile) { //Check File
        while(fgets(line, sizeof(line), readFile))
        {
            printf("%s", line);
        }
        fclose(readFile);
    }

    return 0;
}

說明:
使用fstream的方法讀寫檔案,以及使用C的fopen讀寫檔案的方法。

String conversion

標題:C/C++字串轉換
VC++ (main.cpp):
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream> //ostringstream
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //Character Sequences
    //char c[13] = "CharVariable";
    char c[13] = {'C', 'h', 'a', 'r', 'V', 'a', 'r', 'i', 'a', 'b', 'l', 'e', '\0'};

    //Pointer
    char *p = "Pointer";

    //Class
    string s = "String";

    //String to Char
    strcpy(c, s.c_str());
    cout << c << endl;
    
    //Char to String
    s.assign(c);
    cout << s << endl;

    //Pointer to Char
    strcpy(c, p);
    cout << c << endl;

    //Char to Pointer
    p = &c[0];
    cout << p << endl;

    //String to Float
    float num1 = 0.0f;
    s = "233.42";
    num1 = atof(s.c_str());
    cout << num1 << endl;

    //Float to String
    float num2 = 233.42f;
    ostringstream buffer;
    buffer << num2;
    s = buffer.str();
    cout << s << endl;
    
    return 0;
}
說明:
字元陣列、指標字串、VC字串,互相轉換。

2013年6月20日 星期四

Split CString into float CArray

標題:分割CString字串存入浮點數CArray
VC++ (main.cpp):
CArray<float, float &> fArray;
CString str = _T("1, 1.1, 1.2, 1.3, 1.4, 1.5, 1");
int nTokenPos = 0;
CString strToken = str.Tokenize(_T(","), nTokenPos);
while (!strToken.IsEmpty())
{
    fArray.add((float)_ttof(strToken));
    strToken = str.Tokenize(_T(","), nTokenPos);
}


說明:
使用Tokenize分割每個字串後不斷存入CArray直到結束。

2013年6月11日 星期二

PHP echo HTML using EOT

標題:使用EOT自定義分界符印出HTML
PHP (index.php):
<?php
echo <<< EOT
<p style="color: green">test</p>
EOT;
?>

說明:
使用三個小於符號,在EOT後面、EOT;前面不可以有任何字符,EOT可以為任意字串。

2013年5月1日 星期三

Fluent interface in JavaScript

標題:JavaScript的流暢介面寫法
JavaScript (main.js):
var brain = function() {
    this.echo = "I am ";
    this.word = function(name) {
        this.echo += name;
        return this;
    };
    this.say = function() {
        alert(this.echo);
        return this;
    };
};
var brain = new brain();
brain.word("QQBoxy").say();

說明:
在jQuery中常看見流暢介面(Fluent interface)的寫法,
這對程式的理解上有著極大的幫助,
如同範例程式中大腦(brain)要處理文字(word)然後講出(say)我要講的話,
如此一來便能非常直覺了解程式所要執行的動作。

2013年4月26日 星期五

Create Document/View using MFC

標題:MFC之Document/View架構
VC++ (DocView.cpp):
#include <afxwin.h>
#include "DocView.h"

class DocBoxy : public CDocument //Document
{
    //在此撰寫Document內容
    DECLARE_DYNCREATE(DocBoxy) //宣告run-time類別
};

IMPLEMENT_DYNCREATE(DocBoxy, CDocument) //宣告DocBoxy為run-time類別

class FrameBoxy : public CFrameWnd //Frame
{
public:
    afx_msg void OnExit() //宣告關閉檔案事件
    {
        if(MessageBox( TEXT("Are you sure you want to close this window?"), TEXT("DocViewBoxy"), MB_OKCANCEL | MB_ICONQUESTION ) == 1 )
        {
            DestroyWindow();
        }
    }

    //在此撰寫Frame內容
    DECLARE_DYNCREATE(FrameBoxy) //宣告run-time類別
    DECLARE_MESSAGE_MAP() //宣告訊息映射表
};

//宣告FrameBoxy為run-time類別
IMPLEMENT_DYNCREATE(FrameBoxy, CFrameWnd)

//建立訊息映射表
BEGIN_MESSAGE_MAP(FrameBoxy, CFrameWnd)
    ON_COMMAND(ID_FILE_EXIT, OnExit)
END_MESSAGE_MAP()

class ViewBoxy : public CView //View
{
public:
    void OnDraw(CDC * aDC) //必須覆寫的虛擬函數
    {
        for(int i=0;i<10;i++)
        {
            aDC->TextOut(20 * i, 20 * i, L"HelloBoxy!!");
        }
    }
    //在此撰寫View內容
    DECLARE_DYNCREATE(ViewBoxy) //宣告run-time類別
};

IMPLEMENT_DYNCREATE(ViewBoxy, CView) //宣告ViewBoxy為run-time類別

class AppBoxy : public CWinApp
{
public:
    BOOL InitInstance() //程式進入點
    {
        CDocument *doc;
        CSingleDocTemplate* DocTemplate;

        DocTemplate = new CSingleDocTemplate(
            IDR_MENU,
            RUNTIME_CLASS(DocBoxy),   //Document
            RUNTIME_CLASS(FrameBoxy), //Frame
            RUNTIME_CLASS(ViewBoxy)   //View
        );

        AddDocTemplate(DocTemplate);
        doc = DocTemplate->CreateNewDocument();

        m_pMainWnd = DocTemplate->CreateNewFrame( doc, NULL );
        DocTemplate->InitialUpdateFrame ( (CFrameWnd*)m_pMainWnd, doc );
        m_pMainWnd->ShowWindow(SW_SHOW);

        return true;
    }
};

AppBoxy appboxy; //建立應用程式物件
範例結果:


說明:
在Win32模式下建立MFC之Document/View架構之範例程式碼, 須建立『DocView.rc』資源檔及自動產生『DocView.h』並加入Menu指定ID為『IDR_MENU』。

2013年4月25日 星期四

Verify a character is in English or number

標題:使用ASCII判斷字元是英文或數字
C (main.html):
#include <stdio.h>
#include <string.h> //供strlen使用

int main(void)
{
    int i = 0;
    char str[9] = "_09azAZ?";
    for(i=0;i<strlen(str);i++) //檢查字串中的字元是否均為英文
    {
        //使用十進制ASCII碼判斷字元
        if(str[i] >= 48 && str[i] <= 57) //數字的ASCII碼落在48 ~ 57
        {
            printf("%c 是數字\n", str[i]);
        }
        else if (str[i] >= 65 && str[i] <= 90) //大寫的ASCII碼落在65 ~ 90
        {
            printf("%c 是大寫\n", str[i]);
        }
        else if (str[i] >= 97 && str[i] <= 122) //小寫的ASCII碼落在97 ~ 122
        {
            printf("%c 是小寫\n", str[i]);
        }
        else //其它非英數
        {
            printf("%c 非英數\n", str[i]);
        }
    }
    return 0;
}


說明:
使用ASCII碼十進制判斷字串中的字元是英文或數字,其中注意到字元陣列大小為8(字元數)+1(字串結束符)=9。

2013年4月20日 星期六

C++ Inheritance

標題:C++繼承範例
C++ (main.cpp):
#include <iostream>
using namespace std;

class mother {
public:
    void hello()
    {
        cout << "Hello." << endl;
    }
};
class child : public mother
{
    //
};
void main()
{
    child say;
    say.hello();
}
說明:
child繼承mother的基礎範例。

2013年4月12日 星期五

Dynamic Read Text with Iframe

標題:在JavaScript使用Iframe讀取Text
JavaScript (index.html):
<button id="test">Click Me</button>
<script type="text/javascript">
var iframeboxy = {
    current : null,
    queue : [],
    request : function(q) {
        var iframe = document.createElement("iframe");
        iframe.style.display = "none";
        iframe.name = "iframeboxy";
        iframe.src = q.url;
        if(iframe.attachEvent) { //IE
            iframe.attachEvent("onload", function() {
                iframeboxy.callback(iframe);
            });
        } else {
            iframe.onload = function() {
                iframeboxy.callback(this);
            };
        }
        this.queue.push({
            iframe : iframe,
            data : q.data
        });
        if (!this.current) this.action();
    },
    action : function() {
        this.current = null;
        if(this.queue.length) {
            this.current = this.queue.shift();
            document.getElementsByTagName("body")[0].appendChild(this.current.iframe);
        }
    },
    callback : function(e) {
        this.current.data((e.contentDocument) ? e.contentDocument : e.contentWindow.document);
        document.getElementsByTagName("body")[0].removeChild(this.current.iframe);
        this.action();
    }
};

document.getElementById("test").onclick = function() {
    iframeboxy.request({
        url : "robots.txt",
        data : function(e) {
            alert(e.documentElement.getElementsByTagName("pre")[0].innerHTML);
        }
    });
};
</script>

範例結果:


說明:
使用純JavaScript動態在Head產生Iframe後,呼叫函式取得Text資料。 支援Chrome、FireFox、IE10,由於Browser本身限制僅能讀取Local檔案。

2013年1月31日 星期四

C# Multi List

標題:C#多維List寫法
C# (Program.cs):
using System;
using System.Collections.Generic;

namespace MultiList
{
    class Program
    {
        static void Main(string[] args)
        {
            //建立一維List
            List<String> titles = new List<String>() {
                "ChatBoxy",
                "CodeBoxy",
                "ToolBoxy"
            };

            //建立一維List
            List<String> websites = new List<String>() {
                "http://chatboxy.blogspot.com/",
                "http://codeboxy.blogspot.com/",
                "http://toolboxy.blogspot.com/"
            };
            
            //建立多維List關係
            List<List<String>> options = new List<List<String>> {
                titles,
                websites
            };

            //新增QQBoxy資料
            titles.Add("QQBoxy");
            websites.Add("http://qqboxy.blogspot.com/");

            //移除ChatBoxy資料
            titles.Remove("ToolBoxy");
            websites.Remove("http://toolboxy.blogspot.com/");

            //移除第0筆資料
            titles.RemoveAt(0);
            websites.RemoveAt(0);

            //印出資料
            foreach (List<String> value in options)
            {
                foreach (var v in value)
                {
                    Console.WriteLine(v);
                }
            }
        }
    }
}

結果:


說明:
C#多維List新增移除資料後印出內容。

C# WPF Confirm Options

標題:使用ShowDialog顯示Confirm視窗並回傳RadioButton的Option名稱
XAML (MainWindow.xaml):
<Window x:Class="WPFConfirmOptions.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="200">
    <Grid>
        <Button Content="Confirm Option" Name="confirmBtn" Click="confirmBtn_Click" />
    </Grid>
</Window>

C# (MainWindow.xaml.cs):
using System;
using System.Windows;
using System.Collections.Generic; //List

namespace WPFConfirmOptions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void confirmBtn_Click(object sender, RoutedEventArgs e)
        {
            var options = new List<Confirm.Option>();
            options.Add(new Confirm.Option
            {
                Name = "Test1",
                Description = "Test Description 1."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test2",
                Description = "Test Description 2."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test3",
                Description = "Test Description 3."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test4",
                Description = "Test Description 4."
            });
            options.Add(new Confirm.Option
            {
                Name = "Test5",
                Description = "Test Description 5."
            });
            Confirm confirm = new Confirm("Confirm", "Hello World", options);
            confirm.ShowDialog();
            if (confirm.DialogResult.HasValue && confirm.DialogResult.Value)
            {
                System.Windows.MessageBox.Show(confirm.CheckedName.ToString());
            }
        }
    }
}

XAML (Confirm.xaml):
<Window x:Class="WPFConfirmOptions.Confirm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Name="Windows" Title="Confirm" Height="230" Width="300">
    <Grid>
        <TextBlock Height="20" HorizontalAlignment="Center" Margin="0,10,0,0" Name="tbk_StlTypeTitle" Text="Confirm" VerticalAlignment="Top" Width="120" TextAlignment="Center" />
        <GroupBox Header="Options" Margin="0,30,0,50" Name="gbx">
            <Grid Name="OptionsGrid"></Grid>
        </GroupBox>
        <Grid Height="50" Width="240" HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <Button Content="OK" Height="30" Width="100" Name="btn_OK" VerticalAlignment="Center" HorizontalAlignment="Left" Click="btn_OK_Click" />
            <Button Content="Cancel" Height="30" Width="100" Name="btn_Cancel" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btn_Cancel_Click" />
        </Grid>
    </Grid>
</Window>

C# (Confirm.xaml.cs):
using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic; //List
using System.Linq;

namespace WPFConfirmOptions
{
    /// <summary>
    /// Interaction logic for StlTypeConfirm.xaml
    /// </summary>
    public partial class Confirm : Window
    {
        public Confirm(string header, string message, List<Option> options)
        {
            InitializeComponent();
            this.Header = header;
            this.Message = message;
            this.Options = options;
            AddRadioButton();
        }

        public string Header //Set Head Title
        {
            set
            {
                Windows.Title = value;
            }
        }

        public string Message //Set Content Description
        {
            set
            {
                tbk_StlTypeTitle.Text = value;
            }
        }

        public List<Option> Options;

        public String CheckedName = "";

        public String ResultName
        {
            get
            {
                return CheckedName;
            }
        }

        public class Option
        {
            public string Name { get; set; }
            public string Description { get; set; }
        }

        public void AddRadioButton()
        {
            int key = 0;
            foreach (Option value in Options) // Create Options
            {
                RadioButton radioButton = new RadioButton();
                radioButton.Name = value.Name;
                radioButton.Content = value.Description;
                radioButton.Height = 20;
                radioButton.Width = 250;
                radioButton.Margin = new Thickness(0, (20 + 30 * key), 0, 0);
                radioButton.HorizontalAlignment = HorizontalAlignment.Center;
                radioButton.VerticalAlignment = VerticalAlignment.Top;
                radioButton.IsChecked = false;
                this.OptionsGrid.Children.Add(radioButton);
                this.RegisterName(radioButton.Name, radioButton);
                key++;
            }
            Windows.Height = 200 + 30 * key;
            return;
        }

        private void btn_OK_Click(object sender, RoutedEventArgs e)
        {
            foreach (Option value in Options)
            {
                RadioButton radiobutton = (RadioButton)OptionsGrid.FindName(value.Name);
                if (radiobutton.IsChecked == true)
                {
                    CheckedName = radiobutton.Name;
                    DialogResult = true;
                    return;
                }
            }
            System.Windows.MessageBox.Show("No Select.");
        }

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            return;
        }
    }
}

結果:

說明:
使用Add Window方式設計WPF Confirm視窗, 主視窗可設定加入多組選項包含名稱及描述, 並在ShowDialog後的Confirm視窗產生標題、說明及選項, 最後按下確認回傳RadioButton的Option名稱。

2013年1月30日 星期三

CSharp Goes to

標題:C#使用goes to(=>)(等於大於)語法範例
C# (Program.cs):
using System;
using System.Linq;

namespace GoesTo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] words = { "Welcome", "to", "CodeBoxy" };
            string shortestWord = words.OrderBy(w => w.Length).FirstOrDefault(); //Find shortest word
            int shortestWordLength = words.Min(w => w.Length); //Find the length of the shortest word
            Console.WriteLine(shortestWord + "\n" + shortestWordLength);
        }
    }
}

結果:


說明:
簡單使用goes to(=>)搜尋字串長度最小值。

2013年1月25日 星期五

C# Regex Double Backslash

標題:C#使用雙反斜線表示特殊字元
C# (Main.cs):
Regex solidVcg = new Regex("^\\s*solid\\svcg$");

說明:
在C#撰寫特殊字元如\s時,
若只有一個反斜線Visual Studio C#會報錯,
如下圖:

這時候必須使用雙反斜線來表示就能正常執行。

2013年1月24日 星期四

C# WPF Using List

標題:C# WPF使用List功能
C# (MainWindow.xaml.cs):
using System;
using System.Windows;

namespace WPFVectorList
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public System.Collections.Generic.List<int> arr = new System.Collections.Generic.List<int>();

        private void btn_show_Click(object sender, RoutedEventArgs e)
        {
            tbk.Text = "";
            foreach (int i in arr)
            {
                tbk.Text += i + "\n";
            }
        }

        private void btn_add_Click(object sender, RoutedEventArgs e)
        {
            arr.Add(Int32.Parse(tbx.Text));
        }
    }
}

WPF (MainWindow.xaml):
<Window x:Class="WPFVectorList.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>
        <TextBlock Margin="12,12,12,65" Name="tbk" />
        <Button Content="ADD" Height="40" HorizontalAlignment="Left" Margin="118,259,0,0" Name="btn_add" VerticalAlignment="Top" Width="100" Click="btn_add_Click" />
        <Button Content="Show" Height="40" HorizontalAlignment="Left" Margin="224,0,0,12" Name="btn_show" VerticalAlignment="Bottom" Width="100" Click="btn_show_Click" />
        <TextBox Height="40" HorizontalAlignment="Left" Margin="12,0,0,12" Name="tbx" VerticalAlignment="Bottom" Width="100" Text="1" />
    </Grid>
</Window>

說明:
在C# WPF使用System.Collections.Generic的List實現在陣列Push新值的功能。

MSDN:
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx