Download miễn phí Bài giảng lập trình C


Trong trường hợp hiển thịnhiều dòng văn bản, Windows chỉngắt dòng
khi gặp ký tựCR và LF. Đểngắt dòng dài hơn kích thước hình chữnhật
hiển thị, cần thiết lập cờDT_WORDBREAK. Nếu không muốn Windows
cắt bỏcác phần dưra khi vẽchữvượt quá phạm vi khung chữnhật, ta
thêm cờDT_NOCLIP. Nếu muốn ký tựtab (‘\t’ hay 0x09) được diễn
dịch thành ký tựphân cột, cần thêm cờDT_EXPANDTABS. Giá trịmặc
định của tab là 8 khoảng trắng. CờDT_TABSTOP được dùng để đặt lại
giá trịtab. Trong trường hợp này, byte cao của word thấp (bits 15-8)
của uFormat sẽchứa giá trịtab cần thay thế.


Để tải bản Đầy Đủ của tài liệu, xin Trả lời bài viết này, Mods sẽ gửi Link download cho bạn sớm nhất qua hòm tin nhắn.
Ai cần download tài liệu gì mà không tìm thấy ở đây, thì đăng yêu cầu down tại đây nhé:
Nhận download tài liệu miễn phí

Tóm tắt nội dung tài liệu:

the window loses the
// keyboard focus.
HideCaret(hwndMain);
DestroyCaret();
break;
case WM_CHAR:
switch (wParam)
{
case 0x08: // backspace
case 0x0A: // linefeed
case 0x1B: // escape
MessageBeep((UINT) -1);
return 0;
case 0x09: // tab
Bài giảng: Lập trình C for windows .............................................................................................Trang 34/69
Bài 3:Các thiết bị nhập liệu Trần Minh Thái
// Convert tabs to four consecutive spaces. 77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
for (i = 0; i < 4; i++)
SendMessage(hwndMain, WM_CHAR, 0x20, 0);
return 0;
case 0x0D: // carriage return
// Record the carriage return and position the
// caret at the beginning of the new line.
pchInputBuf[cch++] = 0x0D;
nCaretPosX = 0;
nCaretPosY += 1;
break;
default: // displayable character
ch = (TCHAR) wParam;
HideCaret(hwndMain);
// Retrieve the character's width and output
// the character.
hdc = GetDC(hwndMain);
GetCharWidth32(hdc, (UINT) wParam, (UINT) wParam,
&nCharWidth);
TextOut(hdc, nCaretPosX, nCaretPosY * dwCharY,
&ch, 1);
ReleaseDC(hwndMain, hdc);
// Store the character in the buffer.
pchInputBuf[cch++] = ch;
// Calculate the new horizontal position of the
// caret. If the position exceeds the maximum,
// insert a carriage return and move the caret
// to the beginning of the next line.
nCaretPosX += nCharWidth;
if ((DWORD) nCaretPosX > dwLineLen)
{
nCaretPosX = 0;
pchInputBuf[cch++] = 0x0D;
++nCaretPosY;
}
nCurChar = cch;
ShowCaret(hwndMain);
break;
}
SetCaretPos(nCaretPosX, nCaretPosY * dwCharY);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT: // LEFT ARROW
// The caret can move only to the beginning of
Bài giảng: Lập trình C for windows .............................................................................................Trang 35/69
Bài 3:Các thiết bị nhập liệu Trần Minh Thái
// the current line. 123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
if (nCaretPosX > 0)
{
HideCaret(hwndMain);
// Retrieve the character to the left of
// the caret, calculate the character's
// width, then subtract the width from the
// current horizontal position of the caret
// to obtain the new position.
ch = pchInputBuf[--nCurChar];
hdc = GetDC(hwndMain);
GetCharWidth32(hdc, ch, ch, &nCharWidth);
ReleaseDC(hwndMain, hdc);
nCaretPosX = max(nCaretPosX - nCharWidth, 0);
ShowCaret(hwndMain);
}
break;
case VK_RIGHT: // RIGHT ARROW
// Caret moves to the right or, when a carriage
// return is encountered, to the beginning of
// the next line.
if (nCurChar < cch)
{
HideCaret(hwndMain);
// Retrieve the character to the right of
// the caret. If it's a carriage return,
// position the caret at the beginning of
// the next line.
ch = pchInputBuf[nCurChar];
if (ch == 0x0D)
{
nCaretPosX = 0;
nCaretPosY++;
}
// If the character isn't a carriage
// return, check to see whether the SHIFT
// key is down. If it is, invert the text
// colors and output the character.
else
{
hdc = GetDC(hwndMain);
nVirtKey = GetKeyState(VK_SHIFT);
if (nVirtKey & SHIFTED)
{
crPrevText = SetTextColor(hdc,
RGB(255, 255, 255));
Bài giảng: Lập trình C for windows .............................................................................................Trang 36/69
Bài 3:Các thiết bị nhập liệu Trần Minh Thái
crPrevBk = SetBkColor(hdc, 169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
RGB(0,0,0));
TextOut(hdc, nCaretPosX,
nCaretPosY * dwCharY,
&ch, 1);
SetTextColor(hdc, crPrevText);
SetBkColor(hdc, crPrevBk);
}
// Get the width of the character and
// calculate the new horizontal position of the caret.
GetCharWidth32(hdc, ch, ch, &nCharWidth);
ReleaseDC(hwndMain, hdc);
nCaretPosX = nCaretPosX + nCharWidth;
}
nCurChar++;
ShowCaret(hwndMain);
break;
}
break;
case VK_UP: // UP ARROW
case VK_DOWN: // DOWN ARROW
MessageBeep((UINT) -1);
return 0;
case VK_HOME: // HOME
// Set the caret's position to the upper left
// corner of the client area.
nCaretPosX = nCaretPosY = 0;
nCurChar = 0;
break;
case VK_END: // END
// Move the caret to the end of the text.
for (i=0; i < cch; i++)
{
// Count the carriage returns and save the
// index of the last one.
if (pchInputBuf == 0x0D)
{
cCR++;
nCRIndex = i + 1;
}
}
nCaretPosY = cCR;
// Copy all text between the last carriage
// return and the end of the keyboard input
// buffer to a temporary buffer.
Bài giảng: Lập trình C for windows .............................................................................................Trang 37/69
Bài 3:Các thiết bị nhập liệu Trần Minh Thái
for (i = nCRIndex, j = 0; i < cch; i++, j++) 215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
szBuf[j] = pchInputBuf;
szBuf[j] = TEXT('\0');
// Retrieve the text extent and use it
// to set the horizontal position of the
// caret.
hdc = GetDC(hwndMain);
GetTextExtentPoint32(hdc, szBuf, lstrlen(szBuf), &sz);
nCaretPosX = sz.cx;
ReleaseDC(hwndMain, hdc);
nCurChar = cch;
break;
default:
break;
}
SetCaretPos(nCaretPosX, nCaretPosY * dwCharY);
break;
case WM_PAINT:
if (cch == 0) // nothing in input buffer
break;
hdc = BeginPaint(hwndMain, &ps);
HideCaret(hwndMain);
// Set the clipping rectangle, and then draw the text
// into it.
SetRect(&rc, 0, 0, dwLineLen, dwClientY);
DrawText(hdc, pchInputBuf, -1, &rc, DT_LEFT);
ShowCaret(hwndMain);
EndPaint(hwndMain, &ps);
break;
// Process other messages.
case WM_DESTROY:
PostQuitMessage(0);
// Free the input buffer.
GlobalFree((HGLOBAL) pchInputBuf);
UnregisterHotKey(hwndMain, 0xAAAA);
break;
default:
return DefWindowProc(hwndMain, uMsg, wParam, lParam);
}
return NULL;
}
2. Thiết bị chuột
a. Kiểm tra thiết bị chuột
int GetSystemMetrics(
Bài giảng: Lập trình C for windows .............................................................................................Trang 38/69
Bài 3:Các thiết bị nhập liệu Trần Minh Thái
int nIndex // system metric or configuration setting
);
fMouse = GetSystemMetrics( SM_MOUSEPRESENT );
Giá trị trả về fMouse là TRUE (1) nếu có thiết bị chuột được cài đặt, và
ngược lại bằng FALSE (0) nếu thiết bị chuột không được cài đặt vào
máy.
b. Trong lớp cửa sổ ta định nghĩa con trỏ chuột cho ứng dụng
wndclass.hCursor = LoadCursor ( NULL, IDC_ARROR);
wndclass.style = CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS;
Với thiết bị chuột ta có thể có các hành động như sau:
™ Kích chuột : nhấn và thả một nút chuột.
™ Kích đúp chuột : nhấn và thả chuột nhanh (nhấn 2 lần nhanh).
™ Kéo : di chuyển chuột trong khi vẫn nắm giữ một nút.
c. Thông điệp chuột trong vùng làm việc
Nút Nhấn Thả Nhấn đúp
Trái WM_LBUTTONDOWN WM_LBUTTONUP WM_LBUTTONDBLCLK
Giữa WM_MBUTTONDOWN WM_MBUTTONUP WM_MBUTTONDBLCLK
Phải WM_RBUTTONDOWN WM_MBUTTONUP WM_RBUTTONDBLCLK
d. Giá trị wParam sẽ cho biết trạng thái của nút nhấn, phím Shift, và
phím Ctrl.
MK_LBUTTON Nút chuột trái nhấn
MK_MBUTTON Nút chuột giữa nhấn
MK_RBUTTON Nút chuột phải nhấn
MK_SHIFT Phím Shift được nhấn
MK_CONTROL Phím Ctrl được nhấn
e. Giá trị lParam sẽ cho biết vị trí chuột tại thời điểm phát sinh message.
™ 2 bytes thấp: tọa độ x
™ 2 bytes cao: tọa độ y
f. Ví dụ
Bài giảng: Lập trình C for windows ...........................................................
 

Kiến thức bôn ba

Các chủ đề có liên quan khác

Top