C# 在窗体中启用滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10023043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable scroll bars in windows forms
提问by user1314391
I'm developing a windows forms application. In my application I have anchored controls to forms such that forms can be maximized and controls will get arranged accordingly. This application should support different DPI values.
我正在开发一个 Windows 窗体应用程序。在我的应用程序中,我已将控件锚定到窗体,以便可以最大化窗体并相应地安排控件。此应用程序应支持不同的 DPI 值。
I have set the anchors of some controls to bottom, right, and bottom-right. The AutoScroll property of the forms is set to true. When the DPI value is on default (96) controls work as expected. But the problem is if the screen loads in higher DPI (like 120), even though form scroll bars enabled, controls which are anchored to bottom and bottom-right cannot be seen.
我已经将一些控件的锚点设置为底部、右侧和右下角。表单的 AutoScroll 属性设置为 true。当 DPI 值为默认值 (96) 时,控件按预期工作。但问题是,如果屏幕以更高的 DPI(如 120)加载,即使启用了表单滚动条,也无法看到锚定到底部和右下角的控件。
Could anybody please advise me on this issue?
有人可以就这个问题给我建议吗?
regards, Eranga
问候, 埃兰加
回答by dalip
Create a Panelcalled panel, then do the following:
创建一个Panel被调用的panel,然后执行以下操作:
panel1.autoscroll = true;
panel1.BorderStyle = BorderStyle.FixedSingle;
Use this method to set scroll width and height:
使用此方法设置滚动宽度和高度:
panel1.SetAutoScrollMargin(int x,int y);
回答by Elshan
Set AutoScroll = TrueIn Form Properties
AutoScroll = True在表单属性中设置


回答by violet313
It's an old post but the issue is ongoing and related posts just keep on arriving on SO!
这是一个旧帖子,但问题仍在继续,相关帖子不断出现SO!
I'm doing the necro thing here rather than addressing a more recent question only because it comes top of my google search
我在这里做 necro 的事情而不是解决最近的问题,因为它是我的谷歌搜索的顶部
The question is simple: "why won't the damn scrollbars appear on my ScrollableControl?"
问题很简单:“为什么该死的滚动条不会出现在我的ScrollableControl?”
But there can be no specific, definitive answer. Because the causes are legion. Because whether or not scrollbars appear on a control depends:
但不可能有具体的、确定的答案。因为原因很多。因为滚动条是否出现在控件上取决于:
- not only upon it's own properties settings
- but also upon the state of it's parent control
- and also the states of any child controls.
- 不仅取决于它自己的属性设置
- 但也取决于它的父母控制的状态
- 以及任何子控件的状态。
It's easy to fall into the trap of randomly twiddling prop-values until the cows come home. Or go on the i/webs &hope to find some SOfoos. But oh dear. Here is a handful of related SOposts with an outstanding variety of proposed resolutions:
在奶牛回家之前,很容易陷入随机调整道具值的陷阱。或者去 i/webs &hope 上找到一些SOfoos。但是哦,亲爱的。以下是一些相关的SO帖子,其中包含各种出色的提议决议:
Horizontal Scrollbar is not visible on DataGridView
Horizontal scrollbar not showing on my textbox
How to set scroll bar in Windows form
How to make scrollbars appear in a resizable panel when the contained control is too big for it?
Scrollable Form in c#, AutoScroll=true doesn't work
c# 中的可滚动表单,AutoScroll=true 不起作用
How to get scrollbar in Panel in VB.Net?
There are screen-shots of VS-designerproperty pages (like here) & even some extreme code-based solutions... my favourites:
有VS-designer属性页面的屏幕截图(像这里)甚至一些极端的基于代码的解决方案......我最喜欢的:
Add vertical scroll bar to panel in .NET
how to add Vscroll control to form in Visualbasic.net?
如何在 Visualbasic.net 中添加 Vscroll 控件到窗体?
/sighs/
/叹气/
A general answer
一般答案
..in the form of a minimalgithub solution in order to explore some of the .NET scrollbar voodoos:
..以最小的github 解决方案的形式,以探索一些 .NET 滚动条巫术:
https://github.com/violet313/TestWinForms/tree/Test1-Body-Panel
https://github.com/violet313/TestWinForms/tree/Test1-Body-Panel
It's a Visual Studio 2015solution using the .NET4.52 framework.
这是Visual Studio 2015使用.NET4.52 framework.
In the solution i am trying to create a form that responds to some dynamic text data that is to be displayed. here's the basic lay-out i am ultimately seeking:
在解决方案中,我试图创建一个表单来响应一些要显示的动态文本数据。这是我最终寻求的基本布局:
--------------------------------------------------
| fixed-size form header | |
------------------------------------| side |
| | panel |
| dynamic content panel | stuff |
| | |
--------------------------------------------------
| fixed-size form trailer |
--------------------------------------------------
I want the form to:
我希望表格:
- not be resizable by the user
respond to the dynamic content by:
- shrinking as small as possible down to a pre-determined minimum form-size.
- growing up to a pre-determined maximum form-size; and providing appropriate scroll-bars on the dynamic content panel thereafter.
- 用户不可调整大小
通过以下方式响应动态内容:
- 尽可能缩小到预先确定的最小形状尺寸。
- 长到预先确定的最大形状尺寸;然后在动态内容面板上提供适当的滚动条。
Grab it, go thro each of the (only 9 starting from 95dccc5)commits and then test yourrequirements in a sane & incremental fashion. be sure to branch whenever you make a dubious state-change.
抓住它,通过每个(从 95dccc5 开始只有 9 个)提交,然后以理智和增量的方式测试您的需求。每当您进行可疑的状态更改时,请务必进行分支。
Irl: maybe i'm a thicky but it took me over an hour reading the MSDNs trying (&failing) to figure-out the .NET forms control property contingencies. doing structured trial-and-error this way took me only 20mins to get what i wanted.
Irl:也许我是个胖子,但我花了一个多小时阅读 MSDN,试图(&失败)找出 .NET 表单控制属性意外事件。以这种方式进行结构化试错只花了我 20 分钟就得到了我想要的东西。
y~bwc
y~bwc
i know this here is a yeaz ~but who cares?but i had to get if off my chest. heh :
grrr. having to unlurk &answer this question arises out of my need to profitably take on Microsoft contract work. paymasters can be relatively (from a developer pov) non-technical and, having read lots of stuff including the words: quick, simple, straight-forward, secure, etc, come away with the impression that things .NETish is a stroll in the park. My issue with this is that i would then have difficulty trying to reasonably explain why they might need to pay me for n-day's worth of work in order to get a simple scroll-bar to appear on a responsive form.
On this occasion, i never got that far. lol. i spent a few hours wading thro the MSDN blahs trying to make it happen. and then yawned, gave up, &moved on with a pragmatic implementation. which was accepted. but it's now the w/end and i am an ocd fool who cannot let things be.
我知道这是一个 yeaz 〜但谁在乎?但我不得不离开我的胸膛。
呵呵:嗯。不得不潜伏并回答这个问题是因为我需要从 Microsoft 合同工作中获利。paymasters 可能相对(从开发人员的观点来看)非技术性的,并且在阅读了很多东西,包括诸如:快速、简单、直接、安全等词之后,得出的印象是 .NETish 是在公园。我的问题是,我将难以合理地解释为什么他们可能需要支付我 n 天的工作费用,以便在响应式表单上显示一个简单的滚动条。
在这种情况下,我从未走得那么远。哈哈。我花了几个小时涉水通过 MSDN 废话试图让它发生。然后打了个哈欠,放弃了,并以务实的方式继续前进。被接受了。但现在已经结束了,我是一个不能让事情发生的强迫症傻瓜。
回答by Florian Ritter
I was reading through this pageand i can say it provides an exact and easy solution to your problem!
我正在阅读此页面,我可以说它为您的问题提供了准确而简单的解决方案!
I tested it and it worked well for me.
我测试了它,它对我来说效果很好。
Instruction:
操作说明:
- Add a manifest file to your project (Project--> New Item--> Select the manifest type)
- Add the following XML into your app.manifest (may be another name):
- 将清单文件添加到您的项目(项目-->新建项目--> 选择清单类型)
- 将以下 XML 添加到您的 app.manifest(可能是另一个名称):
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
- Add the following C# code into the class where you call the component initialize (
InitializeComponent();)
- 将以下 C# 代码添加到您调用组件初始化 (
InitializeComponent();) 的类中
[DllImport("shcore.dll")]
static extern int SetProcessDpiAwareness(_Process_DPI_Awareness value);
enum _Process_DPI_Awareness
{
Process_DPI_Unaware = 0,
Process_System_DPI_Aware = 1,
Process_Per_Monitor_DPI_Aware = 2
}
- Now just add this code line above your initialize component method (by default
InitializeComponent();)
- 现在只需在初始化组件方法上方添加此代码行(默认情况下
InitializeComponent();)
SetProcessDpiAwareness(_Process_DPI_Awareness.Process_DPI_Unaware);

