正如主题行所解释的那样。
我想在 .NET 中 Button 控件的 Image 属性中实现ImageAlign.MiddleCenter行为
我正在重写按钮的 OnPaint 事件。我使用e.Graphics.DrawImage(Image,oDrawRectagle); 在 Button 控件内绘制图像。然而,它总是出现在按钮的左侧。由于 oDrawRectagle 始终返回 ClientRectangle 的 0,0 坐标。
我希望图像始终对齐ImageAlign.MiddleCenter。我该如何实现它?
谢谢
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
int iHeight;
int iWidth;
if (Image != null)
{
//newSize = MaintainAspectRatio(Image, ClientRectangle.Width, ClientRectangle.Height);
//iHeight = newSize.Height;
//iWidth = newSize.Width;
Rectangle ResizedRectangle = MaintainAspectRatio(Image,ClientRectangle);
iHeight = ResizedRectangle.Height;
iWidth = ResizedRectangle.Width;
}
else
{
iWidth = ClientRectangle.Width;
iHeight = ClientRectangle.Height;
}
g.FillRectangle(new SolidBrush(Color.FromArgb(213, 221, …
Run Code Online (Sandbox Code Playgroud) 我想在 PictureBox 中绘制一些小图片(连续 4 x 32px 图像),所以我应该覆盖 OnPaint 方法还是需要制作扩展 PictureBox 的新组件?我试过这个,它在 Java 中工作,但不是在这里:
this.pictureBox1 = new System.Windows.Forms.PictureBox()
{
protected override void OnPaint(PaintEventArgs e)
{
// If there is an image and it has a location,
// paint it when the Form is repainted.
Graphics g = e.Graphics;
// Draw a string on the PictureBox.
g.DrawString("Test, is that working?",
new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
}
}
Run Code Online (Sandbox Code Playgroud)
InitializeComponent 方法的完整代码:
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Tools));
this.pictureBox1 = …
Run Code Online (Sandbox Code Playgroud) 我想创建一个带有圆角和渐变颜色的自定义组合框。我Button
通过重写OnPaint
方法实现了相同的功能。但是它不起作用ComboBox
。任何帮助将不胜感激。
OnPaint
下面给出了我用于覆盖的代码:
protected override void OnPaint(PaintEventArgs paintEvent)
{
Graphics graphics = paintEvent.Graphics;
SolidBrush backgroundBrush = new SolidBrush(this.BackColor);
graphics.FillRectangle(backgroundBrush, ClientRectangle);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
GraphicsPath graphicsPath = RoundedRectangle(rectangle, cornerRadius, 0);
Brush brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal);
graphics.FillPath(brush, graphicsPath);
rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 100);
graphicsPath = RoundedRectangle(rectangle, cornerRadius, 2);
brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试改进我的应用程序并提高它的性能.基本上,它是一个高级的Graph-class.它绘制了几行并刷新它们.
在确定了几个慢点之后,我想对我的结果进行基准测试.我的绘图卡在~65 FPS(这是完美的,但我是基准测试).我在一个计时器(设置为1毫秒)中使我的对象无效,并使用受保护的覆盖void OnPaint - "way"重绘我的东西.
之后,我将Invalidate()放入受保护的覆盖void OnPaint中.事后FPS设定为几千.但使用该方法将导致空屏幕.
我的问题是:这个问题是故意的吗?这是有道理的,因为高于屏幕刷新率的任何东西都是浪费的力量.但我想为我的基准测试禁用"锁定".
示例代码:
//Timer to refresh
private void timer1_Tick(object sender, EventArgs e)
{
Invalidate();
}
private DateTime date = DateTime.UtcNow;
private long times = 0;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//Drawing
e.Graphics.DrawLine(new Pen(Brushes.Black), 50, 50, Width - 100, Height - 100);
//Required to tell me the framerate
if ((DateTime.UtcNow - date).Seconds > 1)
{
date = DateTime.UtcNow;
Debug.WriteLine(times);
times = 0;
}
times++;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
史蒂夫〜
我模拟地图导航并在面板上绘制生成的地图部分.由于图像闪烁,我必须使用双缓冲.
这是我的面板代码:
public class MapPanel : System.Windows.Forms.Panel
{
public MapPanel()
{
DoubleBuffered = true;
ResizeRedraw = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下方法:
public void panelMap_Paint(object sender, PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
g.DrawImage(mapController.GetCurrentMap(), 0, 0, panelMap.Width, panelMap.Height);
}
}
Run Code Online (Sandbox Code Playgroud)
我不是在说这种方法.我在.Designer.cs中有以下代码:
this.panelMap.Paint += new PaintEventHandler(this.panelMap_Paint);
Run Code Online (Sandbox Code Playgroud)
并在MouseMove中调用Invalidate().我确定发生了这个事件,我已经检查过了.一切似乎都是正确的.
然后图像没有绘制.我的意思是,面板是空的,似乎是透明的或者是默认的控件颜色.但是,如果我关闭双缓冲,图像被正确绘制,但显然,它是闪烁的.你可以帮帮我吗?
我有一个从支票簿派生的控件,我称之为"SettingBooleanButton",但是当任何窗口或对话框被拖动到控件上时,控件会保持拖动的迹象
下一个图像显示了将应用程序窗口拖动到控件上的效果
这是我对OnPaint()的代码块
Public Class SettingBooleanButton
Inherits CheckBox
Private _settingSection As String
Private _settingName As String
Private _associatedSetting As Setting
Public Event StateChange(ByVal affectedSetting As Setting)
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Appearance = Appearance.Button
FlatStyle = FlatStyle.Flat
TextAlign = ContentAlignment.MiddleCenter
AutoSize = False
End Sub
Public Property SettingSection As String
Get
Return _settingSection
End Get
Set(value As String)
_settingSection = value
End Set
End Property
Public …
Run Code Online (Sandbox Code Playgroud)