winform重绘滚动条
时间: 2023-02-11 07:24:55 浏览: 173
Windows Forms 中的重绘滚动条可以通过调用滚动条控件的 Refresh() 方法来实现。例如,对于一个名为 "scrollBar1" 的滚动条控件,可以使用 scrollBar1.Refresh() 来重绘它。
除此之外,还可以在 Scrollbar 值更新后,手动调用 Invalidate() 方法重绘滚动条
需要注意的是,如果您想要在更改滚动条的值时重绘滚动条,则需要在更改值之后调用 Refresh() 或 Invalidate() 方法。
相关问题
winform 重绘滚动条
要重绘 Winform 中的滚动条,你可以使用以下步骤:
1. 找到你想要重绘的滚动条控件的句柄,可以使用 `FindWindowEx` 函数来获取。
2. 使用 `GetWindowDC` 函数获取该控件的设备上下文句柄。
3. 使用 `SetScrollInfo` 函数设置该控件的滚动条信息,例如滚动条范围、滚动条位置等。
4. 使用 `RedrawWindow` 函数重绘该控件。
以下是一个简单的示例代码,可以参考:
```c#
// 找到滚动条控件的句柄
IntPtr hScrollBar = FindWindowEx(this.Handle, IntPtr.Zero, "ScrollBar", null);
// 获取滚动条控件的设备上下文句柄
IntPtr hDC = GetWindowDC(hScrollBar);
// 设置滚动条信息
SCROLLINFO si = new SCROLLINFO();
si.cbSize = (uint)Marshal.SizeOf(si);
si.fMask = (uint)ScrollInfoMask.SIF_ALL;
si.nMin = 0;
si.nMax = 100;
si.nPos = 50;
si.nPage = 10;
SetScrollInfo(hScrollBar, (int)ScrollBarDirection.SB_CTL, ref si, true);
// 重绘滚动条控件
RedrawWindow(hScrollBar, IntPtr.Zero, IntPtr.Zero, RedrawWindowFlags.RDW_INVALIDATE);
// 释放设备上下文句柄
ReleaseDC(hScrollBar, hDC);
```
需要注意的是,上述代码中的一些函数和结构体需要先进行定义或导入,例如:
```c#
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll", SetLastError = true)]
static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetScrollInfo(IntPtr hWnd, int nBar, ref SCROLLINFO lpsi, bool bRedraw);
[StructLayout(LayoutKind.Sequential)]
struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
enum ScrollInfoMask : uint
{
SIF_RANGE = 0x0001,
SIF_PAGE = 0x0002,
SIF_POS = 0x0004,
SIF_DISABLENOSCROLL = 0x0008,
SIF_TRACKPOS = 0x0010,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
}
enum ScrollBarDirection : int
{
SB_CTL = 2,
}
```
winform重绘DATAGRIDVIEW滚动条
以下是在.NET WinForms中重绘DataGridView滚动条的步骤和代码示例:
1. 创建一个自定义的DataGridView控件,继承自DataGridView。
2. 在自定义控件中重写OnPaint方法,并在其中调用基类的OnPaint方法。
3. 在OnPaint方法中,使用Graphics对象绘制滚动条的样式和颜色。
代码示例:
```csharp
class MyDataGridView : DataGridView
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 绘制垂直滚动条
Rectangle verticalScrollBarRect = new Rectangle(
this.Width - SystemInformation.VerticalScrollBarWidth,
this.ColumnHeadersHeight,
SystemInformation.VerticalScrollBarWidth,
this.Height - this.ColumnHeadersHeight);
e.Graphics.FillRectangle(Brushes.Gray, verticalScrollBarRect);
// 绘制水平滚动条
Rectangle horizontalScrollBarRect = new Rectangle(
this.RowHeadersWidth,
this.Height - SystemInformation.HorizontalScrollBarHeight,
this.Width - this.RowHeadersWidth,
SystemInformation.HorizontalScrollBarHeight);
e.Graphics.FillRectangle(Brushes.Gray, horizontalScrollBarRect);
}
}
```
阅读全文
相关推荐










