在Windows应用程序开发中,向TextEdit控件(通常是多行编辑控件)添加垂直滚动条是一个常见的需求。以下是如何在不同的开发环境中实现这一功能的详细步骤。
如果你在使用Win32 API进行开发,可以通过设置控件的样式来添加垂直滚动条。
CreateWindowEx
函数创建一个多行编辑控件,并设置ES_MULTILINE
和WS_VSCROLL
样式。#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Sample Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CREATE: {
HWND hEdit = CreateWindowEx(
0, L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | WS_VSCROLL,
10, 10, 300, 200,
hwnd, (HMENU)1, GetModuleHandle(NULL), NULL
);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
如果你在使用MFC进行开发,可以通过设置编辑控件的样式来添加垂直滚动条。
Multiline
和Vertical Scroll
属性。class CMyDialog : public CDialogEx {
public:
CMyDialog(CWnd* pParent = nullptr);
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MYDIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CEdit m_editCtrl;
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};
BOOL CMyDialog::OnInitDialog() {
CDialogEx::OnInitDialog();
m_editCtrl.SubclassDlgItem(IDC_MYEDIT, this);
m_editCtrl.SetWindowText(_T("This is a multiline edit control with vertical scroll bar."));
return TRUE;
}
如果你在使用WinForms进行开发,可以通过设置TextBox控件的属性来添加垂直滚动条。
Multiline
属性为true
,ScrollBars
属性为Vertical
。public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.Text = "This is a multiline TextBox with vertical scroll bar.";
}
}
领取专属 10元无门槛券
手把手带您无忧上云