Show toolbar

2013年4月26日 星期五

Create Document/View using MFC

標題:MFC之Document/View架構
VC++ (DocView.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<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檔案。