This Blog is dedicated to Sir Johnson Lin
Sample C program to fetch data from SQL database 使用C語言程式讀取資料庫內容
1 //++++++++++++++++++++++++++
2 // SQLprog2.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 #include <windows.h>
7 #include <sqlext.h>
8 #include <sql.h>
9 //#include <cstdlib>
10 #include <iostream>
11 #include <stdio.h>
12
13 ///main_in
14 int main(int argc,char *argv[])
15 {
16 ///declare Handle_Var
17 HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
18 HDBC hDBC = NULL; // Connection handle
19 HSTMT hStmt = NULL; // Statement handle
20
21 ///declare UCHAR_sz
22 //UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "SQL_DAT";
23 //UCHAR szUID[9] = "SQLabc"; // User ID buffer
24 //UCHAR szPasswd[9] = "SQL123"; // Password buffer
25 UCHAR szModel[128]; // Model buffer
26 UCHAR szSqlStr[128]= "select build_date_no from DATECTL";
27
28 ///Var
29 SDWORD cbModel;
30 RETCODE retcode;
31 //char* buf="abc";
32
33 ///File_Var
34 FILE *outfile;
35
36 ///Open outfile
37 outfile=fopen("mid.txt","wb");
38
39 ///SQL alloc memory
40 SQLAllocEnv (&hEnv); // Allocate memory for ODBC Environment handle
41
42 ///SQL alloc connect
43 SQLAllocConnect (hEnv, &hDBC); // Allocate memory for the connection handle
44
45 ///SQLDriver connect
46 SQLTCHAR *srv = (SQLTCHAR *)"SERVER=10.11.168.100;DSN=SQL_DAT;UID=SQLabc;PWD=SQL123";
47 retcode = SQLDriverConnect(hDBC, NULL, srv, SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
48
49 ///if retcode =0_or_1
50 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
51 {
52 ///SQL alloc statement
53 retcode = SQLAllocStmt (hDBC, &hStmt); // Allocate memory for the statement handle
54 fputc(retcode,outfile);
55
56 ///SQL prepare
57 retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr)); // Prepare the SQL statement by assigning it to the statement handle
58 fputc(retcode,outfile);
59
60 ///SQL execute
61 retcode = SQLExecute (hStmt);// Execute the SQL statement handle
62 fputc(retcode,outfile);
63
64 ///Temp declare
65 #define NAME_LEN 10
66 SQLINTEGER sCustID, cbName, cbAge, cbBirthday,xyz ;
67 SQLCHAR szName[NAME_LEN];
68 char sbuf[10];
69
70 ///SQL fetch
71 retcode = SQLFetch(hStmt);
72
73 ///SQL getdata
74 SQLGetData(hStmt,1, SQL_C_CHAR, szName, NAME_LEN, &xyz);
75
76 ///OutFile
77 fputc(retcode,outfile);
78 //fprintf(outfile, "%s", szName );
79 sprintf(sbuf, "%s", szName );
80 fprintf(outfile, "%s", sbuf );
81
82 ///Display
83 MessageBox(NULL, sbuf, "Date in DMY format", MB_OK);
84
85 ///SQL bindcol
86 SQLBindCol (hStmt, 1, SQL_C_CHAR, szModel, sizeof(szModel), &cbModel);// Project only column 1 which is the models
87
88 ///SQL fetch
89 retcode = SQLFetch (hStmt);// Get row of data from the result set defined above in the statement
90
91 ///OutFile
92 fputc(retcode,outfile);
93
94 ///SQL free statement
95 SQLFreeStmt (hStmt, SQL_DROP);// Free the allocated statement handle
96
97 ///SQL disconnect
98 SQLDisconnect (hDBC);// Disconnect from datasource
99
100 ///+
101 }//end if
102
103 ///else= FALSE
104 else
105 {
106 ///Write "ng"
107 fprintf(outfile,"ng");
108
109 ///+
110 }//end else
111
112 ///OutFile
113 std::cout << retcode << "\n";
114 fputc(retcode,outfile);
115
116 ///Close outfile
117 fclose(outfile);
118
119 ///SQL free connect
120 SQLFreeConnect (hDBC);// Free the allocated connection handle
121
122 ///SQL free_env
123 SQLFreeEnv (hEnv);// Free the allocated ODBC environment handle
124
125 ///main_end
126 return 0;
127 } //main_end
128
@ 下載程式碼 Download source code
+++ Link To Home Page Index 連接至目錄首頁 +++
標籤:
data source,
database,
fetch,
SQL
Simple C program to connect SQL database 簡易C語言程式連結資料庫
1 //++++++++++++++++++++++++++
2 // SQLprog1.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 #include <windows.h>
7 #include <sqlext.h>
8 #include <sql.h>
9 //#include <cstdlib>
10 #include <iostream>
11 #include <stdio.h>
12
13 ///main_in
14 int main(int argc,char *argv[])
15 {
16 ///declare Handle_Var
17 HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
18 HDBC hDBC = NULL; // Connection handle
19 HSTMT hStmt = NULL; // Statement handle
20
21 ///declare UCHAR_sz
22 UCHAR szDSN[SQL_MAX_DSN_LENGTH+1] = "SQL_DAT";
23 UCHAR szUID[9] = "SQLabc"; // User ID buffer
24 UCHAR szPasswd[9] = "SQL123"; // Password buffer
25 UCHAR szModel[128]; // Model buffer
26
27 ///Var
28 SDWORD cbModel;
29 RETCODE retcode;
30
31 ///File_Var
32 FILE *outfile;
33
34 ///Open outfile
35 outfile=fopen("mid.txt","wb");
36
37 ///SQL alloc memory
38 SQLAllocEnv (&hEnv); // Allocate memory for ODBC Environment handle
39
40 ///SQL alloc connect
41 SQLAllocConnect (hEnv, &hDBC); // Allocate memory for the connection handle
42
43 ///SQL connect
44 retcode = SQLConnect (hDBC, szDSN, 8, szUID, 5, szPasswd,5);// Connect to the data source "szDSN" using userid and password.
45
46 ///if retcode =0_or_1
47 if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
48 {
49 ///Write "ok"
50 fprintf(outfile,"ok");
51
52 ///+
53 }//end if
54
55 ///else= FALSE
56 else
57 {
58 ///Write "ng"
59 fprintf(outfile,"ng");
60
61 ///+
62 }//end else
63
64 ///Close outfile
65 fclose(outfile);
66
67 ///SQL free connect
68 SQLFreeConnect (hDBC);// Free the allocated connection handle
69
70 ///SQL free_env
71 SQLFreeEnv (hEnv);// Free the allocated ODBC environment handle
72
73 ///main_end
74 return 0;
75 } //main_end
76
@ 下載程式碼 Download source code
+++ Link To Home Page Index 連接至目錄首頁 +++
標籤:
data source,
database,
SQL
Find prime number from 2 to 100 找出範圍內質數
1 //++++++++++++++++++++++++++
2 // FindPrime.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 #include <stdio.h>
7
8 ///main_in
9 int main ()
10 {
11 ///declare
12 int iDIV, iDVSR;
13 #define INPUTMAX 100
14
15 ///DIV =2 ~ INPUTMAX
16 for(iDIV=2; iDIV<INPUTMAX; iDIV++)
17 {
18 ///DVSR=2 ~ DIV/DVSR
19 for(iDVSR=2; iDVSR <= (iDIV/iDVSR); iDVSR++)
20 {
21 ///remainder =zero
22 if((iDIV%iDVSR)==0) break; // if remainder = 0, is factor, not prime then break loop
23
24 ///+
25 }//end for
26
27 ///Factors Existed
28 if(iDVSR > (iDIV/iDVSR))
29 {
30 ///Display Prime
31 printf("Found prime %d \n", iDIV); //iDVSR <= (iDIV/iDVSR) factor occur before loop end
32
33 ///+
34 }//end if
35
36 ///+
37 }//end for
38
39 ///main_end
40 return 0;
41 } //main_end
42
@ 下載程式碼 Download source code
+++ Link To Home Page Index 連接至目錄首頁 +++
標籤:
質數,
prime number
Gaussian elimination(row reduction) to solve a system of linear equations with back-substitution method 高斯消去法解聯立方程式 使用反向代回法
1 //++++++++++++++++++++++++++
2 // SystemEq2.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 //#include<conio.h>
7 #include<bits/stdc++.h>
8
9 ///main_in
10 int main(int argc,char *argv[])
11 {
12 ///declare
13 int i, j, iEqs;
14 float fCof[20][20], x[20];
15
16 ///system cls
17 system("cls");
18
19 ///Prompt input_eqs
20 printf("Enter the number of equations : ");
21
22 ///Keyin _eqs
23 scanf("%d", &iEqs);
24
25 ///Prompt input_cof
26 printf("\nEnter the coefficients of the equations :\n\n");
27
28 ///Xru 0 to colmax-1
29 for(i = 0 ; i < iEqs ; i++)
30 {
31 ///Xru 0 to rowmax-1
32 for(j = 0 ; j < iEqs ; j++)
33 {
34 ///Prompt input_Xij
35 printf("X%d%d = ", i + 1, j + 1);
36
37 ///Keyin COEFFICIENT-i.j
38 scanf("%f", &fCof[i][j]);
39
40 ///+
41 }//end for
42
43 ///Prompt input_Yij
44 printf("Y%d = ", i + 1);
45
46 ///Keyin COEFFICIENT rowmax
47 scanf("%f", &fCof[i][iEqs]);
48
49 ///+
50 }//end for
51
52 ///Reduce Cycle iRdsCyc=0,1 (0~iEQs-1)
53 int iRdsCyc;
54 for(iRdsCyc = 0 ; iRdsCyc < iEqs - 1 ; iRdsCyc++)
55 {
56 ///Renew row count iRenuRowCnt
57 int iRenuRowCnt;
58 for(iRenuRowCnt = iRdsCyc + 1 ; iRenuRowCnt < iEqs ; iRenuRowCnt++)
59 {
60 ///XpnOvrXnn
61 double XpnOvrXnn;
62 XpnOvrXnn = fCof[iRenuRowCnt][iRdsCyc] / fCof[iRdsCyc][iRdsCyc];
63
64 ///Renew row iRenuRow=4,3 =iRdsCyc~iEQs
65 int iRenuRow;
66 for(iRenuRow = iRdsCyc ; iRenuRow < iEqs + 1 ; iRenuRow++)
67 {
68 fCof[iRenuRowCnt][iRenuRow] = fCof[iRenuRowCnt][iRenuRow] - fCof[iRdsCyc][iRenuRow] *XpnOvrXnn ;
69 }
70
71 ///+
72 }//end for
73
74 ///+
75 }//end for Reduce Cycle
76
77 ///Xn=x[EQs-1] =Y/X
78 x[iEqs-1] = fCof[iEqs-1][iEqs] / fCof[iEqs-1][iEqs-1];
79
80 ///[Esq-2]~0
81 double iSops;
82 for(i = iEqs - 2 ; i >= 0 ; i--)
83 {
84 ///Sum Of Product iSops= 0
85 iSops = 0;
86
87 ///Sop loop
88 for(j = i + 1 ; j < iEqs ; j++)
89 {
90 ///x[i] =
91 iSops = iSops + (fCof[i][j] * x[j]);
92 x[i] = (fCof[i][iEqs] - iSops) / fCof[i][i];
93
94 ///+
95 }//end for
96
97 ///+
98 }//end for
99
100 ///Prompt result
101 printf("\nThe result is :\n");
102
103 ///Display result
104 for(i = 0 ; i < iEqs ; i++) {printf("\nsolution of x%d = %.2f", i + 1, x[i]);}
105
106 ///system pause
107 printf("\n\n");
108 system("pause");
109
110 ///main_end
111 return 0;
112 } //main_end
113
@ 下載程式碼 Download source code
Solving a system of linear equations by Gaussian elimination(row reduction plus Matrix Rotation skill) 聯立方程式之高斯消去法併用矩陣旋轉技巧
1 //++++++++++++++++++++++++++
2 // SystemEq.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 //#include<conio.h>
7 #include<bits/stdc++.h>
8
9 ///main_in
10 int main(int argc,char *argv[])
11 {
12 ///declare
13 int i, j, iEqs;
14 float fCof[20][20], x[20];
15 float dfCof[20][40];
16
17 ///system cls
18 system("cls");
19
20 ///Prompt input_eqs
21 printf("Enter the number of equations : ");
22
23 ///Keyin _eqs
24 scanf("%d", &iEqs);
25
26 ///Prompt input_cof
27 printf("\nEnter the coefficients of the equations :\n\n");
28
29 ///Xru 0 to colmax-1
30 for(i = 0 ; i < iEqs ; i++)
31 {
32 ///Xru 0 to rowmax-1
33 for(j = 0 ; j < iEqs ; j++)
34 {
35 ///Prompt input_Xij
36 printf("X%d%d = ", i + 1, j + 1);
37
38 ///Keyin COEFFICIENT-i.j
39 scanf("%f", &dfCof[i][j]);
40 dfCof[i][j+iEqs]=dfCof[i][j];
41
42 ///+
43 }//end for
44
45 ///Prompt input_Yij
46 printf("Y%d = ", i + 1);
47
48 ///Keyin COEFFICIENT rowmax
49 scanf("%f", &dfCof[i][iEqs*2]);
50
51 ///+
52 }//end for
53
54 ///matrix left_shift
55 int iShift;
56 for(iShift = iEqs ; iShift > 0 ; iShift--)
57 {
58 ///Copy Block dfCof2fCof
59 for(i = 0 ; i < iEqs ; i++) for(j = 0 ; j < iEqs ; j++) fCof[i][j] = dfCof[i][j+iShift];
60
61 ///Copy Y_col
62 for(i = 0 ; i < iEqs ; i++) fCof[i][iEqs] = dfCof[i][iEqs*2];
63
64 ///Reduce Cycle iRdsCyc=0,1 (0~iEQs-1)
65 int iRdsCyc;
66 for(iRdsCyc = 0 ; iRdsCyc < iEqs - 1 ; iRdsCyc++)
67 {
68 ///Renew row count iRenuRowCnt
69 int iRenuRowCnt;
70 for(iRenuRowCnt = iRdsCyc + 1 ; iRenuRowCnt < iEqs ; iRenuRowCnt++)
71 {
72 ///XpnOvrXnn
73 double XpnOvrXnn;
74 XpnOvrXnn = fCof[iRenuRowCnt][iRdsCyc] / fCof[iRdsCyc][iRdsCyc];
75
76 ///Renew row iRenuRow=4,3 =iRdsCyc~iEQs
77 int iRenuRow;
78 for(iRenuRow = iRdsCyc ; iRenuRow < iEqs + 1 ; iRenuRow++)
79 {
80 fCof[iRenuRowCnt][iRenuRow] = fCof[iRenuRowCnt][iRenuRow] - fCof[iRdsCyc][iRenuRow] *XpnOvrXnn ;
81 }
82
83 ///+
84 }//end for
85
86 ///+
87 }//end for Reduce Cycle
88
89 ///Xn=x[EQs-1] =Y/X
90 x[iShift-1] = fCof[iEqs-1][iEqs] / fCof[iEqs-1][iEqs-1];
91
92 ///+
93 }//end for
94
95 ///Prompt result
96 printf("\nThe result is :\n");
97
98 ///Display result
99 for(i = 0 ; i < iEqs ; i++) {printf("\nsolution of x%d = %.2f", i + 1, x[i]);}
100
101 ///system pause
102 printf("\n\n");
103 system("pause");
104
105 ///main_end
106 return 0;
107 } //main_end
108
@ 下載程式碼 Download source code
Windows c++ program : MDI text editor. 視窗版 c 語言程式 : 多文件/多視窗文字編輯器
1 //++++++++++++++++++++++++++
2 // EditorMDI.cpp
3 //++++++++++++++++++++++++++
4
5 #include "EditorMDI.h"//Win32 mode , auto include xxx.h
6 ///#include header
7 #include <windows.h>
8 #include <commctrl.h>
9
10 ///define ID/IDC
11 #define ID_STATUSBAR 4997
12 #define ID_TOOLBAR 4998
13 #define ID_MDI_CLIENT 4999
14 #define ID_MDI_FIRSTCHILD 50000
15 #define IDC_CHILD_EDIT 2000
16
17 ///declare CALLBACK
18 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
19 LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
20
21 ///declare vairable
22 char g_szAppName[] = "MyMDIWindow";
23 char g_szChild[] = "MyMDIChild";
24 HINSTANCE g_hInst;
25 HWND g_hMDIClient, g_hStatusBar, g_hToolBar;
26 HWND g_hMainWindow;
27 HWND u_hEdit; // For ChangeFont(u_hEdit, 32);
28 int fontscale = 28;
29
30 ///ChangeFont
31 BOOL ChangeFont(HWND x_hEdit, int Scale)
32 {
33 HFONT hfont;
34 LOGFONT logFont;
35 memset(&logFont, 0, sizeof(logFont));
36 logFont.lfHeight = -1*Scale; // see PS
37 logFont.lfWeight = FW_BOLD;
38 strcpy(logFont.lfFaceName, "Courier New");
39 hfont=CreateFontIndirect(&logFont);
40
41 SendMessage(x_hEdit, WM_SETFONT,(WPARAM)hfont,MAKELPARAM(TRUE, 0));
42 return TRUE;
43 }
44
45 ///LoadFile
46 BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
47 {
48 HANDLE hFile;
49 BOOL bSuccess = FALSE;
50
51 hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
52 if(hFile != INVALID_HANDLE_VALUE)
53 {
54 DWORD dwFileSize;
55 dwFileSize = GetFileSize(hFile, NULL);
56 if(dwFileSize != 0xFFFFFFFF)
57 {
58 LPSTR pszFileText;
59 pszFileText = LPSTR(GlobalAlloc(GPTR, dwFileSize + 1));
60 if(pszFileText != NULL)
61 {
62 DWORD dwRead;
63 if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
64 {
65 pszFileText[dwFileSize] = 0; // eof terminator
66 if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // post string to client
67 }
68 GlobalFree(pszFileText);
69 }
70 }
71 CloseHandle(hFile);
72 }
73 return bSuccess;
74 }
75
76 ///SaveFile
77 BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
78 {
79 HANDLE hFile;
80 BOOL bSuccess = FALSE;
81
82 hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
83 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
84 if(hFile != INVALID_HANDLE_VALUE)
85 {
86 DWORD dwTextLength;
87 dwTextLength = GetWindowTextLength(hEdit);
88 if(dwTextLength > 0)// avoid blank text file
89 {
90 LPSTR pszText;
91 pszText = LPSTR(GlobalAlloc(GPTR, dwTextLength + 1));
92 if(pszText != NULL)
93 {
94 if(GetWindowText(hEdit, pszText, dwTextLength + 1))
95 {
96 DWORD dwWritten;
97 if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
98 bSuccess = TRUE;
99 }
100 GlobalFree(pszText);
101 }
102 }
103 CloseHandle(hFile);
104 }
105 return bSuccess;
106 }
107
108 ///DoFlOpSv
109 BOOL DoFileOpenSave(HWND hwnd, LPSTR pszFileName, BOOL bSave)
110 {
111 OPENFILENAME ofn;
112
113 ZeroMemory(&ofn, sizeof(ofn));
114 pszFileName[0] = 0;
115
116 ofn.lStructSize = sizeof(ofn);
117 ofn.hwndOwner = hwnd;
118 ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0";
119 ofn.lpstrFile = pszFileName;
120 ofn.nMaxFile = MAX_PATH;
121 ofn.lpstrDefExt = "txt";
122
123 if(bSave)
124 {
125 ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
126 if(!GetSaveFileName(&ofn))
127 return FALSE;
128 }
129 else
130 {
131 ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
132 if(!GetOpenFileName(&ofn))
133 return FALSE;
134 }
135 return TRUE;
136 }
137
138 ///WinMain
139 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
140 {
141 ///declare struct
142 MSG Msg;
143 WNDCLASSEX WndClassEx;
144
145 ///InitCommonControls
146 InitCommonControls();//define in MinGW\include\commctrl.h
147
148 ///setting
149 g_hInst = hInstance;
150
151 ///Register WndProc
152 WndClassEx.cbSize = sizeof(WNDCLASSEX);
153 WndClassEx.style = CS_HREDRAW | CS_VREDRAW;
154 WndClassEx.lpfnWndProc = WndProc;
155 WndClassEx.cbClsExtra = 0;
156 WndClassEx.cbWndExtra = 0;
157 WndClassEx.hInstance = hInstance;
158 WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
159 WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
160 WndClassEx.hbrBackground = (HBRUSH)(COLOR_3DSHADOW+1);
161 WndClassEx.lpszMenuName = "MAINMENU"; //xxx.rc need define the same name
162 WndClassEx.lpszClassName = g_szAppName; // = WndProc, Will run 1st CreatWindowEx
163 WndClassEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
164
165 ///register ?
166 if(!RegisterClassEx(&WndClassEx))
167 {
168 MessageBox(0, "Could Not Register Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
169 return -1;
170 }
171
172 ///Register MDIChildWndProc
173 WndClassEx.lpfnWndProc = MDIChildWndProc;
174 WndClassEx.lpszMenuName = NULL;
175 WndClassEx.lpszClassName = g_szChild;
176 WndClassEx.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
177
178 ///register MDI?
179 if(!RegisterClassEx(&WndClassEx))
180 {
181 MessageBox(0, "Could Not Register Child Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
182 return -1;
183 }
184
185 ///Create WindowEx
186 g_hMainWindow = CreateWindowEx
187 (
188 WS_EX_APPWINDOW,
189 g_szAppName, //WinProc=g_szAppName,MDIChildWndProc=g_szChild
190 "MDI File Editor",
191 WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
192 CW_USEDEFAULT,
193 CW_USEDEFAULT,
194 CW_USEDEFAULT,
195 CW_USEDEFAULT,
196 0,
197 0,
198 hInstance,
199 NULL
200 );
201
202 ///create =NULL?
203 if (g_hMainWindow == NULL)
204 {
205 MessageBox(0, "No Window", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);
206 return -1;
207 }
208
209 ///Show Window
210 ShowWindow(g_hMainWindow, nCmdShow);
211
212 ///Update Window
213 UpdateWindow(g_hMainWindow);
214
215 ///Polling GetMsg
216 while(GetMessage(&Msg, NULL, 0, 0) > 0) // =0 mean WM_QUIT, =-1 abnormal
217 {
218 ///Msg for MDI?
219 if (!TranslateMDISysAccel(g_hMDIClient, &Msg))
220 {
221 ///Msg for WndProc
222 TranslateMessage(&Msg);// if no, will diable queue from keyboard
223 DispatchMessage(&Msg);// if no , will disable all mouse,key active, Call WndProc
224
225 ///+
226 }//end if
227
228 ///+
229 }//end while
230
231 ///program quit
232 MessageBox(0, "PGM is closed ! ", "Ending",MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
233 return Msg.wParam;
234
235 ///main_end
236 }//end main
237
238 ///WndProc
239 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //Dispatchmsg(&Msg); will call it indirectly
240 {
241 ///switch msg
242 switch(msg)
243 {
244 ///WM_ CREATE
245 case WM_CREATE:
246 {
247 CLIENTCREATESTRUCT ccs;
248 int iStatusWidths[] = {200, 300, -1};
249 TBADDBITMAP tbab;
250 TBBUTTON tbb[9];
251
252 // Find window menu where children will be listed
253 ccs.hWindowMenu = GetSubMenu(GetMenu(hwnd), 2);
254 ccs.idFirstChild = ID_MDI_FIRSTCHILD;
255 g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient",NULL,WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,hwnd, (HMENU)ID_MDI_CLIENT, g_hInst, (LPVOID)&ccs);
256 ShowWindow(g_hMDIClient, SW_SHOW);
257
258 g_hStatusBar = CreateWindowEx(0, STATUSCLASSNAME, NULL,WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
259 hwnd, (HMENU)ID_STATUSBAR, g_hInst, NULL);
260 SendMessage(g_hStatusBar, SB_SETPARTS, 3, (LPARAM)iStatusWidths);
261 SendMessage(g_hStatusBar, SB_SETTEXT, 2, (LPARAM)"Toolbar & Statusbar Example");
262
263 g_hToolBar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
264 WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
265 hwnd, (HMENU)ID_TOOLBAR, g_hInst, NULL);
266
267 // Send the TB_BUTTONSTRUCTSIZE msg, which is required for
268 // backward compatibility.
269 SendMessage(g_hToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
270
271 tbab.hInst = HINST_COMMCTRL;
272 tbab.nID = IDB_STD_SMALL_COLOR;
273 SendMessage(g_hToolBar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
274
275 ZeroMemory(tbb, sizeof(tbb));
276
277 tbb[0].iBitmap = STD_FILENEW;
278 tbb[0].fsState = TBSTATE_ENABLED;
279 tbb[0].fsStyle = TBSTYLE_BUTTON;
280 tbb[0].idCommand = CM_FILE_NEW;
281
282 tbb[1].iBitmap = STD_FILEOPEN;
283 tbb[1].fsState = TBSTATE_ENABLED;
284 tbb[1].fsStyle = TBSTYLE_BUTTON;
285 tbb[1].idCommand = CM_FILE_OPEN;
286
287 tbb[2].iBitmap = STD_FILESAVE;
288 tbb[2].fsStyle = TBSTYLE_BUTTON;
289 tbb[2].idCommand = CM_FILE_SAVE;
290
291 tbb[3].fsStyle = TBSTYLE_SEP;
292
293 tbb[4].iBitmap = STD_CUT;
294 tbb[4].fsStyle = TBSTYLE_BUTTON;
295 tbb[4].idCommand = CM_EDIT_CUT;
296
297 tbb[5].iBitmap = STD_COPY;
298 tbb[5].fsStyle = TBSTYLE_BUTTON;
299 tbb[5].idCommand = CM_EDIT_COPY;
300
301 tbb[6].iBitmap = STD_PASTE;
302 tbb[6].fsStyle = TBSTYLE_BUTTON;
303 tbb[6].idCommand = CM_EDIT_PASTE;
304
305 tbb[7].fsStyle = TBSTYLE_SEP;
306
307 tbb[8].iBitmap = STD_UNDO;
308 tbb[8].fsStyle = TBSTYLE_BUTTON;
309 tbb[8].idCommand = CM_EDIT_UNDO;
310
311 SendMessage(g_hToolBar, TB_ADDBUTTONS, 9, (LPARAM)&tbb);
312
313 return 0;
314 }
315 //needn't break
316
317 ///WM_ Command
318 case WM_COMMAND:
319 {
320 ///switch LOWORD (wParam)
321 switch(LOWORD(wParam)) //a window macros to single out low 16 bits of 32 bits
322 { //wParam was sent by Dispatchmsg()
323
324 ///CM_ FILE_ EXIT
325 case CM_FILE_EXIT:
326 {
327 PostMessage(hwnd, WM_CLOSE, 0, 0);
328 }
329 break;
330
331 ///CM_ FILE_ NEW
332 case CM_FILE_NEW:
333 {
334 MDICREATESTRUCT mcs;
335 HWND hChild;
336 mcs.szTitle = "[Untitled]";
337 mcs.szClass = g_szChild;
338 mcs.hOwner = g_hInst;
339 mcs.x = mcs.cx = CW_USEDEFAULT;
340 mcs.y = mcs.cy = CW_USEDEFAULT;
341 mcs.style = MDIS_ALLCHILDSTYLES;
342
343 hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
344 if(!hChild){ MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...", MB_ICONEXCLAMATION | MB_OK);}
345 }
346 break;
347
348 ///CM_ FILE_ OPEN
349 case CM_FILE_OPEN:
350 {
351 MDICREATESTRUCT mcs;
352 HWND hChild;
353 char szFileName[MAX_PATH];
354
355 if(!DoFileOpenSave(hwnd, szFileName, FALSE)) break;
356
357 mcs.szTitle = szFileName;
358 mcs.szClass = g_szChild;
359 mcs.hOwner = g_hInst;
360 mcs.x = mcs.cx = CW_USEDEFAULT;
361 mcs.y = mcs.cy = CW_USEDEFAULT;
362 mcs.style = MDIS_ALLCHILDSTYLES;
363
364 hChild = (HWND)SendMessage(g_hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
365
366 if(!hChild)
367 { MessageBox(hwnd, "MDI Child creation failed.", "Oh Oh...",MB_ICONEXCLAMATION | MB_OK);}
368 }
369 break;
370
371 ///CM_ WINDOW_ TILEHORZ
372 case CM_WINDOW_TILEHORZ:
373 {
374 PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_HORIZONTAL, 0);
375 }
376 break;
377
378 ///CM_ WINDOW_ TILEVERT
379 case CM_WINDOW_TILEVERT:
380 {
381 PostMessage(g_hMDIClient, WM_MDITILE, MDITILE_VERTICAL, 0);
382 }
383 break;
384
385 ///CM_ WINDOW_ CASCADE
386 case CM_WINDOW_CASCADE:
387 {
388 PostMessage(g_hMDIClient, WM_MDICASCADE, 0, 0);
389 }
390 break;
391
392 ///CM_ WINDOW_ ARRANGE
393 case CM_WINDOW_ARRANGE:
394 {
395 PostMessage(g_hMDIClient, WM_MDIICONARRANGE, 0, 0);
396 }
397 break;
398
399 ///CM_ About
400 case CM_HELP_ABOUT:
401 MessageBox (NULL, "MDI Text Editor for Windows\n - Win32 API" , "About...", 0);
402 break;
403
404 ///default
405 default:
406 {
407 if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD)
408 {
409 DefFrameProc(hwnd, g_hMDIClient, msg, wParam,lParam);
410 }
411 else
412 {
413 HWND hChild;
414 hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
415 if(hChild)
416 {
417 SendMessage(hChild, WM_COMMAND, wParam, lParam);
418 }
419 }
420 }
421
422 ///+
423 }//end Switch
424
425 ///+
426 }//end Case
427 break;
428
429 ///WM_ SIZE
430 case WM_SIZE:
431 {
432 RECT rectClient, rectStatus, rectTool;
433 UINT uToolHeight, uStatusHeight, uClientAlreaHeight;
434
435 SendMessage(g_hToolBar, TB_AUTOSIZE, 0, 0);
436 SendMessage(g_hStatusBar, WM_SIZE, 0, 0);
437
438 GetClientRect(hwnd, &rectClient);
439 GetWindowRect(g_hStatusBar, &rectStatus);
440 GetWindowRect(g_hToolBar, &rectTool);
441
442 uToolHeight = rectTool.bottom - rectTool.top;
443 uStatusHeight = rectStatus.bottom - rectStatus.top;
444 uClientAlreaHeight = rectClient.bottom;
445
446 MoveWindow(g_hMDIClient, 0, uToolHeight, rectClient.right, uClientAlreaHeight - uStatusHeight - uToolHeight, TRUE);
447 }
448 break;
449
450 ///WM_ CLOSE
451 case WM_CLOSE:
452 {
453 DestroyWindow(hwnd);
454 }
455 break;
456
457 ///WM_ DESTROY
458 case WM_DESTROY:
459 {
460 PostQuitMessage(0);
461 }
462 break;
463
464 ///default
465 default:
466 return DefFrameProc(hwnd, g_hMDIClient, msg,wParam,lParam);
467
468 ///+
469 }//end Switch
470
471 ///+
472 return 0;
473 }//end CALLBACK
474
475 ///MDIChildWP
476 LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //child frame open, be called while mouse move within the frame
477 {
478 ///switch msg
479 switch(msg)
480 {
481 ///WM_ create
482 case WM_CREATE:
483 {
484 char szFileName[MAX_PATH];
485 HWND hEdit;
486
487 hEdit = CreateWindowEx(
488 WS_EX_CLIENTEDGE,
489 "EDIT",
490 "",
491 WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN,
492 CW_USEDEFAULT,
493 CW_USEDEFAULT,
494 CW_USEDEFAULT,
495 CW_USEDEFAULT,
496 hwnd,
497 (HMENU)IDC_CHILD_EDIT,
498 g_hInst,
499 NULL
500 );
501
502 //Sendmsg(hEdit,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(TRUE, 0));
503
504 ChangeFont(hEdit, 28);
505 u_hEdit = hEdit;// pass to ChildProc
506
507 GetWindowText(hwnd, szFileName, MAX_PATH);
508 if(*szFileName != '[')
509 {
510 if(!LoadFile(hEdit, szFileName))
511 {
512 MessageBox(hwnd, "Couldn't Load File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
513 return -1; //cancel window creation
514 }
515 }
516 }
517 break;
518
519 ///WM_ SIZE
520 case WM_SIZE:
521 {
522 if(wParam != SIZE_MINIMIZED) MoveWindow(GetDlgItem(hwnd, IDC_CHILD_EDIT), 0, 0, LOWORD(lParam),HIWORD(lParam), TRUE);
523 }
524 break;
525
526 ///WM_MDI ACTIVATE
527 case WM_MDIACTIVATE:
528 {
529 HMENU hMenu, hFileMenu;
530 BOOL EnableFlag;
531 char szFileName[MAX_PATH];
532 hMenu = GetMenu(g_hMainWindow);
533 if(hwnd == (HWND)lParam){EnableFlag = TRUE;}//being activated
534 else{ EnableFlag = FALSE;} //being de-activated
535
536 EnableMenuItem(hMenu, 1, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
537 EnableMenuItem(hMenu, 2, MF_BYPOSITION | (EnableFlag ? MF_ENABLED : MF_GRAYED));
538
539 hFileMenu = GetSubMenu(hMenu, 0);
540 EnableMenuItem(hFileMenu, CM_FILE_SAVE, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
541 EnableMenuItem(hFileMenu, CM_FILE_SAVEAS, MF_BYCOMMAND | (EnableFlag ? MF_ENABLED : MF_GRAYED));
542
543 DrawMenuBar(g_hMainWindow);
544
545 SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_FILE_SAVE, MAKELONG(EnableFlag, 0));
546 SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_UNDO, MAKELONG(EnableFlag, 0));
547 SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_CUT, MAKELONG(EnableFlag, 0));
548 SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_COPY, MAKELONG(EnableFlag, 0));
549 SendMessage(g_hToolBar, TB_ENABLEBUTTON, CM_EDIT_PASTE, MAKELONG(EnableFlag, 0));
550
551 GetWindowText(hwnd, szFileName, MAX_PATH);
552 SendMessage(g_hStatusBar, SB_SETTEXT, 0, (LPARAM)(EnableFlag ? szFileName : ""));
553 }
554 break;
555
556 ///WM_SET FOCUS
557 case WM_SETFOCUS:
558 {
559 SetFocus(GetDlgItem(hwnd, IDC_CHILD_EDIT));
560 }
561 break;
562
563 ///WM_ Command
564 case WM_COMMAND:
565 {
566 ///switch LOWORD (wParam)
567 switch(LOWORD(wParam)) //a window macros to single out low 16 bits of 32 bits
568 { //wParam was sent by Dispatchmsg()
569
570 ///CM_ FILE_ SAVE
571 case CM_FILE_SAVE:
572 {
573 char szFileName[MAX_PATH];
574
575 GetWindowText(hwnd, szFileName, MAX_PATH);
576 if(*szFileName != '[')
577 {
578 if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
579 {
580 MessageBox(hwnd, "Couldn't Save File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
581 return 0;
582 }
583 }
584 else
585 {
586 PostMessage(hwnd, WM_COMMAND,MAKEWPARAM(CM_FILE_SAVEAS, 0), 0);
587 }
588 }
589 return 0;
590
591 ///CM_ FILE_ SAVEAS
592 case CM_FILE_SAVEAS:
593 {
594 char szFileName[MAX_PATH];
595
596 if(DoFileOpenSave(hwnd, szFileName, TRUE))
597 {
598 if(!SaveFile(GetDlgItem(hwnd, IDC_CHILD_EDIT), szFileName))
599 {
600 MessageBox(hwnd, "Couldn't Save File.", "Error.", MB_OK | MB_ICONEXCLAMATION);
601 return 0;
602 }
603 else
604 { SetWindowText(hwnd, szFileName);}
605 }
606 }
607 return 0;
608
609 ///CM_ FILE_ UNDO
610 case CM_EDIT_UNDO:
611 {
612 SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, EM_UNDO, 0, 0);
613 }
614 break;
615
616 ///CM_ FILE_ CUT
617 case CM_EDIT_CUT:
618 {
619 SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_CUT, 0, 0);
620 }
621 break;
622
623 ///CM_ FILE_ COPY
624 case CM_EDIT_COPY:
625 {
626 SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
627 }
628 break;
629
630 ///CM_ FILE_ paste
631 case CM_EDIT_PASTE:
632 {
633 SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_PASTE, 0, 0);
634 }
635 break;
636
637 ///CM_ FILE_ FONTL
638 case CM_FONT_FONTL:
639 {
640 u_hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
641 fontscale++;
642 ChangeFont(u_hEdit, fontscale);
643 }
644 break;
645
646 ///CM_ FILE_ FONTS
647 case CM_FONT_FONTS:
648 {
649 u_hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
650 fontscale--;
651 ChangeFont(u_hEdit, fontscale);
652 }
653 break;
654
655 ///+
656 }//end Switch
657
658 ///+
659 }//end Case
660 return 0;
661
662 ///+
663 }//end Switch
664
665 ///+
666 return DefMDIChildProc(hwnd, msg, wParam, lParam);
667 }// Call back end
668
@ 下載程式碼 Download source code
訂閱:
文章 (Atom)













