📄 edit.c
字号:
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(ES_MULTILINE | ES_CENTER | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0);
SendMessage(hwEdit, WM_SETTEXT, 0, (LPARAM) "aa");
lo = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 0, 0));
hi = LOWORD(SendMessage(hwEdit, EM_POSFROMCHAR, 1, 0));
mid = lo + (hi - lo) / 2 +1;
for (i = lo; i < mid; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(0 == ret, "expected 0 got %d\n", ret);
}
for (i = mid; i <= hi; i++) {
ret = LOWORD(SendMessage(hwEdit, EM_CHARFROMPOS, 0, (LPARAM) i));
ok(1 == ret, "expected 1 got %d\n", ret);
}
ret = SendMessage(hwEdit, EM_POSFROMCHAR, 2, 0);
ok(-1 == ret, "expected -1 got %d\n", ret);
DestroyWindow(hwEdit);
}
/* Test if creating edit control without ES_AUTOHSCROLL and ES_AUTOVSCROLL
* truncates text that doesn't fit.
*/
static void test_edit_control_5(void)
{
static const char *str = "test\r\ntest";
HWND hWnd;
int len;
hWnd = CreateWindowEx(0,
"EDIT",
str,
0,
10, 10, 1, 1,
NULL, NULL, NULL, NULL);
assert(hWnd);
len = SendMessageA(hWnd, WM_GETTEXTLENGTH, 0, 0);
ok(lstrlenA(str) == len, "text shouldn't have been truncated\n");
DestroyWindow(hWnd);
hWnd = CreateWindowEx(0,
"EDIT",
str,
ES_MULTILINE,
10, 10, 1, 1,
NULL, NULL, NULL, NULL);
assert(hWnd);
len = SendMessageA(hWnd, WM_GETTEXTLENGTH, 0, 0);
ok(lstrlenA(str) == len, "text shouldn't have been truncated\n");
DestroyWindow(hWnd);
}
static void test_margins(void)
{
HWND hwEdit;
RECT old_rect, new_rect;
INT old_left_margin, old_right_margin;
DWORD old_margins, new_margins;
hwEdit = create_editcontrol(WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0);
old_margins = SendMessage(hwEdit, EM_GETMARGINS, 0, 0);
old_left_margin = LOWORD(old_margins);
old_right_margin = HIWORD(old_margins);
/* Check if setting the margins works */
SendMessage(hwEdit, EM_SETMARGINS, EC_LEFTMARGIN, MAKELONG(10, 0));
new_margins = SendMessage(hwEdit, EM_GETMARGINS, 0, 0);
ok(LOWORD(new_margins) == 10, "Wrong left margin: %d\n", LOWORD(new_margins));
ok(HIWORD(new_margins) == old_right_margin, "Wrong right margin: %d\n", HIWORD(new_margins));
SendMessage(hwEdit, EM_SETMARGINS, EC_RIGHTMARGIN, MAKELONG(0, 10));
new_margins = SendMessage(hwEdit, EM_GETMARGINS, 0, 0);
ok(LOWORD(new_margins) == 10, "Wrong left margin: %d\n", LOWORD(new_margins));
ok(HIWORD(new_margins) == 10, "Wrong right margin: %d\n", HIWORD(new_margins));
/* The size of the rectangle must decrease if we increase the margin */
SendMessage(hwEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(5, 5));
SendMessage(hwEdit, EM_GETRECT, 0, (LPARAM)&old_rect);
SendMessage(hwEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(15, 20));
SendMessage(hwEdit, EM_GETRECT, 0, (LPARAM)&new_rect);
ok(new_rect.left == old_rect.left + 10, "The left border of the rectangle is wrong\n");
ok(new_rect.right == old_rect.right - 15, "The right border of the rectangle is wrong\n");
ok(new_rect.top == old_rect.top, "The top border of the rectangle must not change\n");
ok(new_rect.bottom == old_rect.bottom, "The bottom border of the rectangle must not change\n");
/* If we set the margin to same value as the current margin,
the rectangle must not change */
SendMessage(hwEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(10, 10));
old_rect.left = 1;
old_rect.right = 99;
old_rect.top = 1;
old_rect.bottom = 99;
SendMessage(hwEdit, EM_SETRECT, 0, (LPARAM)&old_rect);
SendMessage(hwEdit, EM_GETRECT, 0, (LPARAM)&old_rect);
SendMessage(hwEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN, MAKELONG(10, 10));
SendMessage(hwEdit, EM_GETRECT, 0, (LPARAM)&new_rect);
ok(new_rect.left == old_rect.left, "The left border of the rectangle has changed\n");
ok(new_rect.right == old_rect.right, "The right border of the rectangle has changed\n");
ok(new_rect.top == old_rect.top, "The top border of the rectangle has changed\n");
ok(new_rect.bottom == old_rect.bottom, "The bottom border of the rectangle has changed\n");
DestroyWindow (hwEdit);
}
#define edit_pos_ok(exp, got, txt) \
ok(exp == got, "wrong " #txt " expected %d got %ld\n", exp, got);
#define check_pos(hwEdit, set_height, test_top, test_height, test_left) \
do { \
RECT format_rect; \
int left_margin; \
set_client_height(hwEdit, set_height); \
SendMessage(hwEdit, EM_GETRECT, 0, (LPARAM) &format_rect); \
left_margin = LOWORD(SendMessage(hwEdit, EM_GETMARGINS, 0, 0)); \
edit_pos_ok(test_top, format_rect.top, vertical position); \
edit_pos_ok((int)test_height, format_rect.bottom - format_rect.top, height); \
edit_pos_ok(test_left, format_rect.left - left_margin, left); \
} while(0)
void test_text_position_style(DWORD style)
{
HWND hwEdit;
HFONT font, oldFont;
HDC dc;
TEXTMETRIC metrics;
INT b, bm, b2, b3;
BOOL single_line = !(style & ES_MULTILINE);
b = GetSystemMetrics(SM_CYBORDER) + 1;
b2 = 2 * b;
b3 = 3 * b;
bm = b2 - 1;
/* Get a stock font for which we can determine the metrics */
assert(font = GetStockObject(SYSTEM_FONT));
assert(dc = GetDC(NULL));
oldFont = SelectObject(dc, font);
assert(GetTextMetrics(dc, &metrics));
SelectObject(dc, oldFont);
ReleaseDC(NULL, dc);
/* Windows' edit control has some bugs in multi-line mode:
* - Sometimes the format rectangle doesn't get updated
* (see workaround in set_client_height())
* - If the height of the control is smaller than the height of a text
* line, the format rectangle is still as high as a text line
* (higher than the client rectangle) and the caret is not shown
*/
/* Edit controls that are in a parent window */
hwEdit = create_child_editcontrol(style | WS_VISIBLE, 0);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 0);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 2, 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 10, 0, metrics.tmHeight , 0);
destroy_child_editcontrol(hwEdit);
hwEdit = create_child_editcontrol(style | WS_BORDER | WS_VISIBLE, 0);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, b);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + bm, 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + b2, b, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + b3, b, metrics.tmHeight , b);
destroy_child_editcontrol(hwEdit);
hwEdit = create_child_editcontrol(style | WS_VISIBLE, WS_EX_CLIENTEDGE);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 1);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 2, 1, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 10, 1, metrics.tmHeight , 1);
destroy_child_editcontrol(hwEdit);
hwEdit = create_child_editcontrol(style | WS_BORDER | WS_VISIBLE, WS_EX_CLIENTEDGE);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 1);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 2, 1, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 10, 1, metrics.tmHeight , 1);
destroy_child_editcontrol(hwEdit);
/* Edit controls that are popup windows */
hwEdit = create_editcontrol(style | WS_POPUP, 0);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 0);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 2, 0, metrics.tmHeight , 0);
check_pos(hwEdit, metrics.tmHeight + 10, 0, metrics.tmHeight , 0);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(style | WS_POPUP | WS_BORDER, 0);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, b);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + bm, 0, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + b2, b, metrics.tmHeight , b);
check_pos(hwEdit, metrics.tmHeight + b3, b, metrics.tmHeight , b);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(style | WS_POPUP, WS_EX_CLIENTEDGE);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 1);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 2, 1, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 10, 1, metrics.tmHeight , 1);
DestroyWindow(hwEdit);
hwEdit = create_editcontrol(style | WS_POPUP | WS_BORDER, WS_EX_CLIENTEDGE);
SendMessage(hwEdit, WM_SETFONT, (WPARAM) font, (LPARAM) FALSE);
if (single_line)
check_pos(hwEdit, metrics.tmHeight - 1, 0, metrics.tmHeight - 1, 1);
check_pos(hwEdit, metrics.tmHeight , 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 1, 0, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 2, 1, metrics.tmHeight , 1);
check_pos(hwEdit, metrics.tmHeight + 10, 1, metrics.tmHeight , 1);
DestroyWindow(hwEdit);
}
void test_text_position(void)
{
trace("EDIT: Text position (Single line)\n");
test_text_position_style(ES_AUTOHSCROLL | ES_AUTOVSCROLL);
trace("EDIT: Text position (Multi line)\n");
test_text_position_style(ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL);
}
static BOOL RegisterWindowClasses (void)
{
WNDCLASSA test2;
WNDCLASSA test3;
WNDCLASSA text_position;
test2.style = 0;
test2.lpfnWndProc = ET2_WndProc;
test2.cbClsExtra = 0;
test2.cbWndExtra = 0;
test2.hInstance = hinst;
test2.hIcon = NULL;
test2.hCursor = LoadCursorA (NULL, IDC_ARROW);
test2.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
test2.lpszMenuName = NULL;
test2.lpszClassName = szEditTest2Class;
if (!RegisterClassA(&test2)) return FALSE;
test3.style = 0;
test3.lpfnWndProc = edit3_wnd_procA;
test3.cbClsExtra = 0;
test3.cbWndExtra = 0;
test3.hInstance = hinst;
test3.hIcon = 0;
test3.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
test3.hbrBackground = GetStockObject(WHITE_BRUSH);
test3.lpszMenuName = NULL;
test3.lpszClassName = szEditTest3Class;
if (!RegisterClassA(&test3)) return FALSE;
text_position.style = CS_HREDRAW | CS_VREDRAW;
text_position.cbClsExtra = 0;
text_position.cbWndExtra = 0;
text_position.hInstance = hinst;
text_position.hIcon = NULL;
text_position.hCursor = LoadCursorA(NULL, MAKEINTRESOURCEA(IDC_ARROW));
text_position.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
text_position.lpszMenuName = NULL;
text_position.lpszClassName = szEditTextPositionClass;
text_position.lpfnWndProc = DefWindowProc;
if (!RegisterClassA(&text_position)) return FALSE;
return TRUE;
}
static void UnregisterWindowClasses (void)
{
UnregisterClassA(szEditTest2Class, hinst);
UnregisterClassA(szEditTest3Class, hinst);
UnregisterClassA(szEditTextPositionClass, hinst);
}
START_TEST(edit)
{
hinst = GetModuleHandleA(NULL);
assert(RegisterWindowClasses());
test_edit_control_1();
test_edit_control_2();
test_edit_control_3();
test_edit_control_4();
test_edit_control_5();
test_margins();
test_text_position();
UnregisterWindowClasses();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -