I am painting a control from a buffer. My code for painting is it:
protected override void OnPaint(PaintEventArgs e)
{
if (surface != null)
{
using (Bitmap Pintar = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height))
{
BitmapData bmd = Pintar.LockBits(new Rectangle(0, 0, e.ClipRectangle.Width, e.ClipRectangle.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
int DtX = e.ClipRectangle.X;
int DtY = e.ClipRectangle.Y;
Console.WriteLine(DtX + " - " + bmd.Width + " || " + DtY + " - " + bmd.Height);
unsafe
{
byte* PunteroL = (byte*)bmd.Scan0;
byte* PunteroS = (byte*)(surface.Buffer + (DtX * 4 + surface.Width * DtY * 4));
for (int Y = 0; Y < bmd.Height; Y++)
{
byte* PunteroLT = (byte*)(PunteroL + (bmd.Width * Y * 4));
byte* PunteroST = (byte*)(PunteroS + (surface.Width * Y * 4));
for (int X = 0; X < bmd.Width; X++)
{
byte* PunteroLS = (byte*)(PunteroLT + (X * 4));
byte* PunteroSS = (byte*)(PunteroST + (X * 4));
PunteroLS[0] = PunteroSS[0];
PunteroLS[1] = PunteroSS[1];
PunteroLS[2] = PunteroSS[2];
}
}
}
Pintar.UnlockBits(bmd);
e.Graphics.DrawImage(Pintar, DtX, DtY, Pintar.Width, Pintar.Height);
}
}
}
The problem here is when I resize the window I got an error "Tryng access to protected memory" and this is because of pointers..
I wanna know if there is any way to leave (or block) the OnPaint event while the user is resizing the view..
Thank! =)
What worked best for me in a similar situation was the solution described in this SO thread.
In an nutshell, it is suggested to add to your control the following import:
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
And the following methods:
public static void SuspendDrawing( Control parent )
{
SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}
public static void ResumeDrawing( Control parent )
{
SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
parent.Refresh();
}
Then all you have to do is call SuspendDrawing before the re-size takes place and ResumeDrawing after.
Update:
Personally I prefer using it as an extension method, thus making it available for all controls I use without duplicating code or inheriting from base class...
public static class MyExtensionClass
{
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
private const int WM_SETREDRAW = 11;
public static void SuspendDrawing(this Control ctrl )
{
var parent = ctrl.Parent;
if(parent != null)
{
SendMessage(parent.Handle, WM_SETREDRAW, false, 0);
}
}
public static void ResumeDrawing(this Control ctrl )
{
var parent = ctrl.Parent;
if(parent != null)
{
SendMessage(parent.Handle, WM_SETREDRAW, true, 0);
parent.Refresh();
}
}
}
Try to implement a event handler on Form.ResizeBegin, in which you will remove your OnPaint handler from your control.
Then you would just have to restore it in Form.ResizeEnd.
You have to get the ResizeBegin and the ResizeEnd events.
The code in there should look like:
private void OnResizeEnd(object sender, EventArgs e)
{
Paint += OnPaint;
}
private void OnResizeBegin(object sender, EventArgs e)
{
Paint -= OnPaint;
}
Related
I'm trying to integrate a unity application (.exe) inside a WPF XAML application. I've managed to get the unity window to open inside the WPF window, but it's stuck in the top left corner of the window and does not resize when I resize the WPF window.
Here is my code, (the unity application is called unityWindow.exe):
MainWindow.xaml
<Window x:Class="UnityApplicationIntegration.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UnityApplicationIntegration"
mc:Ignorable="d"
Title="MainWindow" MinHeight="488" MinWidth="815">
<Grid x:Name="unityContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Threading;
namespace UnityApplicationIntegration
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
[DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
[DllImport("user32.dll")]
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private Process _process;
private IntPtr _unityHWND = IntPtr.Zero;
private const int WM_ACTIVATE = 0x0006;
private readonly IntPtr WA_ACTIVE = new IntPtr(1);
private readonly IntPtr WA_INACTIVE = new IntPtr(0);
//Frame p = MainWindow.Instance.floatingFrame;
Grid UnityContent;
bool initialized = false;
public MainWindow()
{
InitializeComponent();
UnityContent = unityContent;
//MainWindow.Instance.MainWindowClosing += Application_Exit;
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += attemptInit;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
void attemptInit(object sender, EventArgs e)
{
if (initialized)
return;
HwndSource source = (HwndSource)HwndSource.FromVisual(UnityContent);
Debug.WriteLine("attempting to get handle...");
if (source == null)
{
Debug.WriteLine("Failed to get handle source");
return;
}
IntPtr hWnd = source.Handle;
try
{
_process = new Process();
_process.StartInfo.FileName = "UnityWindow.exe";
_process.StartInfo.Arguments = "-parentHWND " + hWnd.ToInt32() + " " + Environment.CommandLine;
_process.StartInfo.UseShellExecute = true;
_process.StartInfo.CreateNoWindow = true;
_process.Start();
_process.WaitForInputIdle();
// Doesn't work for some reason ?!
//hWnd = _process.MainWindowHandle;
EnumChildWindows(hWnd, WindowEnum, IntPtr.Zero);
//unityHWNDLabel.Text = "Unity HWND: 0x" + unityHWND.ToString("X8");
Debug.WriteLine("Unity HWND: 0x" + _unityHWND.ToString("X8"));
// TODO: rename. What are the Args?
UnityContentResize(this, EventArgs.Empty);
initialized = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ".\nCheck if Container.exe is placed next to UnityGame.exe.");
}
}
private void ActivateUnityWindow()
{
SendMessage(_unityHWND, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
}
private void DeactivateUnityWindow()
{
SendMessage(_unityHWND, WM_ACTIVATE, WA_INACTIVE, IntPtr.Zero);
}
private int WindowEnum(IntPtr hwnd, IntPtr lparam)
{
_unityHWND = hwnd;
ActivateUnityWindow();
return 0;
}
private void UnityContentResize(object sender, EventArgs e)
{
MoveWindow(_unityHWND, 0, 0, (int)UnityContent.Width, (int)UnityContent.Height, true);
Debug.WriteLine("RESIZED UNITY WINDOW TO: " + (int)UnityContent.Width + "x" + (int)UnityContent.Height);
ActivateUnityWindow();
}
// Close Unity application
private void ApplicationExit(object sender, EventArgs e)
{
try
{
_process.CloseMainWindow();
Thread.Sleep(1000);
while (!_process.HasExited)
_process.Kill();
}
catch (Exception)
{
}
}
private void UnityContentActivate(object sender, EventArgs e)
{
ActivateUnityWindow();
}
private void UnityContentDeactivate(object sender, EventArgs e)
{
DeactivateUnityWindow();
}
}
}
I've tried placing the unity window inside a content presenter like so:
<ContentPresenter x:Name="unityContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ContentPresenter>
But resizing still didn't work.
I've read in many places to use WindowsFormsHost but since this is not a forms application, this does not work.
Could anyone tell me where I'm going wrong?
Thanks for your time.
EDIT:
I managed to get the grid with the unity window to resize by changing:
MoveWindow(_unityHWND, 0, 0, (int)UnityContent.Width, (int)UnityContent.Height, true);
to
MoveWindow(_unityHWND, 0, 0, (int)UnityContent.ActualWidth, (int)UnityContent.ActualHeight, true);
But now, after the first resize I get blank space on the sides of the unity window. I need it to fill all available space. Please see the image.
I fixed this issue by doing 2 things:
By changing MoveWindow(_unityHWND, 0, 0, (int)UnityContent.Width, (int)UnityContent.Height, true);
to
MoveWindow(_unityHWND, 0, 0, (int)UnityContent.ActualWidth, (int)UnityContent.ActualHeight, true);
The unity window was not the same scale as the WPF window, so I had to add the following to mainwindow.xaml.cs
double scaleX, scaleY;
if (s != null)
{
scaleX = s.CompositionTarget.TransformToDevice.M11;
scaleY = s.CompositionTarget.TransformToDevice.M22;
}
var actualHeight = (int)UnityContent.ActualHeight * scaleY;
var actualWidth = (int)UnityContent.ActualWidth * scaleX;
MoveWindow(_unityHWND, 0, 0, (int)actualWidth, (int)actualHeight, true);
I've made a custom TextBox class to have a text box with custom border in my application, based on this other SO post. If I set any of the new custom properties in the form designer, they appear momentarily, until I change the control focus, and when I run the application, the new border settings are not displayed. I did update my form's InitializeComponent method, so that the text box initializes a new BorderedTextBox instead of TextBox. Does anyone know what's wrong here?
public class BorderedTextBox : TextBox
{
private Color _borderColor = Color.Black;
private int _borderWidth = 2;
private int _borderRadius = 5;
public BorderedTextBox() : base()
{
InitializeComponent();
this.Paint += this.BorderedTextBox_Paint;
}
public BorderedTextBox(int width, int radius, Color color) : base()
{
this._borderWidth = Math.Max(1, width);
this._borderColor = color;
this._borderRadius = Math.Max(0, radius);
InitializeComponent();
this.Paint += this.BorderedTextBox_Paint;
}
public Color BorderColor
{
get => this._borderColor;
set
{
this._borderColor = value;
DrawTextBox();
}
}
public int BorderWidth
{
get => this._borderWidth;
set
{
if (value > 0)
{
this._borderWidth = Math.Min(value, 10);
DrawTextBox();
}
}
}
public int BorderRadius
{
get => this._borderRadius;
set
{ // Setting a radius of 0 produces square corners...
if (value >= 0)
{
this._borderRadius = value;
this.DrawTextBox();
}
}
}
private void BorderedTextBox_Paint(object sender, PaintEventArgs e) => DrawTextBox(e.Graphics);
private void DrawTextBox() => this.DrawTextBox(this.CreateGraphics());
private void DrawTextBox(Graphics g)
{
Brush borderBrush = new SolidBrush(this.BorderColor);
Pen borderPen = new Pen(borderBrush, (float)this._borderWidth);
Rectangle rect = new Rectangle(
this.ClientRectangle.X,
this.ClientRectangle.Y,
this.ClientRectangle.Width - 1,
this.ClientRectangle.Height - 1);
// Clear text and border
g.Clear(this.BackColor);
// Drawing Border
g.DrawRoundedRectangle(
borderPen,
(0 == this._borderWidth % 2) ? rect.X + this._borderWidth / 2 : rect.X + 1 + this._borderWidth / 2,
rect.Y,
rect.Width - this._borderWidth,
(0 == this._borderWidth % 2) ? rect.Height - this._borderWidth / 2 : rect.Height - 1 - this._borderWidth / 2,
(float)this._borderRadius);
}
#region Component Designer generated code
/// <summary>Required designer variable.</summary>
private System.ComponentModel.IContainer components = null;
/// <summary>Clean up any resources being used.</summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
/// <summary>Required method for Designer support - Don't modify!</summary>
private void InitializeComponent() => components = new System.ComponentModel.Container();
#endregion
}
You need to override WndProc:
private const int WM_PAINT = 0x000F;
protected override void WndProc( ref Message m ) {
if(m.Msg == WM_PAINT ) {
base.WndProc( ref m );
Graphics gr = this.CreateGraphics();
//draw what you want
gr.Dispose();
return;
}
base.WndProc( ref m );
}
Works fine without any issues. It draws in client area though. A complete version drawing a custom border, textbox need to have border:
[DllImport( "user32.dll" )]
static extern IntPtr GetWindowDC( IntPtr hWnd );
[DllImport( "user32.dll" )]
static extern bool ReleaseDC( IntPtr hWnd, IntPtr hDC );
[DllImport( "gdi32.dll" )]
static extern bool FillRgn( IntPtr hdc, IntPtr hrgn, IntPtr hbr );
[DllImport( "gdi32.dll" )]
static extern IntPtr CreateRectRgn( int nLeftRect, int nTopRect, int nRightRect,
int nBottomRect );
[DllImport( "gdi32.dll" )]
static extern IntPtr CreateSolidBrush( uint crColor );
[DllImport( "gdi32.dll" )]
static extern bool DeleteObject( IntPtr hObject );
private const int WM_NCPAINT = 0x0085;
private const int WM_PAINT = 0x000F;
private const int RGN_DIFF = 0x4;
private int p_border = 2;
protected override void WndProc( ref Message m ) {
if(m.Msg == WM_PAINT ) {
base.WndProc( ref m );
IntPtr hdc = GetWindowDC( this.Handle ); //gr.GetHdc();
IntPtr rgn = CreateRectRgn( 0, 0, this.Width, this.Height );
IntPtr brush = CreateSolidBrush( 0xFF0000 ); //Blue : B G R
CombineRgn( rgn, rgn, CreateRectRgn( p_border, p_border, this.Width - p_border,
this.Height - p_border ), RGN_DIFF );
FillRgn( hdc, rgn, brush );
ReleaseDC( this.Handle, hdc );
DeleteObject( rgn );
DeleteObject( brush );
m.Result = IntPtr.Zero;
return;
}
if( m.Msg == WM_NCPAINT ) {
return;
}
base.WndProc( ref m );
}
Winforms TextBox is a legacy control I think even from way back before the .Net framework.
It doesn't support owner-drawing and as one can see on MSDN neither Paint not OnPaint are documented to work.
Yes, you can code them, and yes, they will have some effect. But TextBox doesn't play by the normal rules and will mess up your drawing without triggering a paint event.
Possibly you can hook yourself into the windows message queue (WndProc) but it is generally not recommended, especially for something like adorning it with a border.
Usually nesting a TextBox in a Panel and letting the Panel draw a nice Border is the simplest solution..
I used a custom control which is created by other posts. I tried to modify it to fit my requirements but I'm stuck in those areas which we cannot edit it.
This is my Modified Control Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CControls
{
public partial class TapControl : TabControl
{
#region "Properties"
private int _hotTabIndex = -1;
private int HotTabIndex
{
get { return _hotTabIndex; }
set
{
if (_hotTabIndex != value)
{
_hotTabIndex = value;
this.Invalidate();
}
}
}
private int CloseButtonHeight
{
get { return FontHeight; }
}
#endregion
public TapControl()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.DrawMode = TabDrawMode.OwnerDrawFixed;
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.White, 0, 0, ClientRectangle.Right,ClientRectangle.Top);
e.Graphics.DrawLine(Pens.Red, 0, ItemSize.Height+2, ClientRectangle.Right-1, ItemSize.Height+2);
base.OnPaint(e);
for (int id = 0; id < this.TabCount; id++)
DrawTabContent(e.Graphics, id);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
for (int id = 0; id < this.TabCount; id++)
DrawTabBackground(pevent.Graphics, id);
}
protected override void OnCreateControl()
{
base.OnCreateControl();
this.OnFontChanged(EventArgs.Empty);
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
IntPtr hFont = this.Font.ToHfont();
SendMessage(this.Handle, WM_SETFONT, hFont, new IntPtr(-1));
SendMessage(this.Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
this.UpdateStyles();
}
private void DrawTabContent(Graphics graphics, int id)
{
bool selectedOrHot = id == this.SelectedIndex || id == this.HotTabIndex;
bool vertical = this.Alignment >= TabAlignment.Left;
Image tabImage = null;
if (this.ImageList != null)
{
TabPage page = this.TabPages[id];
if (page.ImageIndex > -1 && page.ImageIndex < this.ImageList.Images.Count)
tabImage = this.ImageList.Images[page.ImageIndex];
if (page.ImageKey.Length > 0 && this.ImageList.Images.ContainsKey(page.ImageKey))
tabImage = this.ImageList.Images[page.ImageKey];
}
Rectangle tabRect = GetTabRect(id);
Rectangle contentRect = vertical ? new Rectangle(0, 0, tabRect.Height, tabRect.Width) : new Rectangle(Point.Empty, tabRect.Size);
contentRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ItemSize.Width, ItemSize.Height);
Rectangle textrect = contentRect;
textrect.Width -= FontHeight;
if (tabImage != null)
{
textrect.Width -= tabImage.Width;
textrect.X += tabImage.Width;
}
Color frColor = id == SelectedIndex ? Color.Black : this.ForeColor;
Color bkColor = id == SelectedIndex ? Color.White : FindForm().BackColor;
using (Bitmap bm = new Bitmap(contentRect.Width, contentRect.Height))
{
using (Graphics bmGraphics = Graphics.FromImage(bm))
{
TextRenderer.DrawText(bmGraphics, this.TabPages[id].Text, this.Font, textrect, frColor, bkColor);
//if (selectedOrHot)
//{
Rectangle closeRect = new Rectangle(contentRect.Right - CloseButtonHeight, 0, CloseButtonHeight, CloseButtonHeight);
closeRect.Offset(-2, (contentRect.Height - closeRect.Height) / 2);
DrawCloseButton(bmGraphics, closeRect);
//}
if (tabImage != null)
{
Rectangle imageRect = new Rectangle(Padding.X, 0, tabImage.Width, tabImage.Height);
imageRect.Offset(0, (contentRect.Height - imageRect.Height) / 2);
bmGraphics.DrawImage(tabImage, imageRect);
}
}
if (vertical)
{
if (this.Alignment == TabAlignment.Left)
bm.RotateFlip(RotateFlipType.Rotate270FlipNone);
else
bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
graphics.DrawImage(bm, tabRect);
}
}
private void DrawCloseButton(Graphics graphics, Rectangle bounds)
{
using (Font closeFont = new Font("Tahoma", Font.Size, FontStyle.Bold))
TextRenderer.DrawText(graphics, "x", closeFont, bounds, Color.Gray, Color.Transparent, TextFormatFlags.HorizontalCenter | TextFormatFlags.NoPadding | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter);
}
private void DrawTabBackground(Graphics graphics, int id)
{
if (id == SelectedIndex)
graphics.FillRectangle(Brushes.White, GetTabRect(id));
else if (id == HotTabIndex)
{
Rectangle rc = GetTabRect(id);
rc.Width--;
rc.Height--;
graphics.DrawRectangle(Pens.DarkGray, rc);
}
}
private const int TCM_ADJUSTRECT = 0x1328;//(TCM_FIRST + 40);
protected override void WndProc(ref Message m)
{
if (m.Msg == TCM_SETPADDING)
{
m.LParam = MAKELPARAM(this.Padding.X + CloseButtonHeight / 2, this.Padding.Y);
}
if (m.Msg == WM_MOUSEDOWN && !this.DesignMode)
{
Point pt = this.PointToClient(Cursor.Position);
Rectangle closeRect = GetCloseButtonRect(HotTabIndex);
if (closeRect.Contains(pt) && HotTabIndex != 0)
{
TabPages.RemoveAt(HotTabIndex);
m.Msg = WM_NULL;
}
}
if (m.Msg == TCM_ADJUSTRECT && !this.DesignMode)
{
RECT rc = new RECT();
rc.Left -= 3;
rc.Right += 1;
rc.Top -= 1;
rc.Bottom += 1;
Marshal.StructureToPtr(rc, m.LParam, true);
}
base.WndProc(ref m);
}
private struct RECT{
public int Left, Top, Right, Bottom;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
TCHITTESTINFO HTI = new TCHITTESTINFO(e.X, e.Y);
HotTabIndex = SendMessage(this.Handle, TCM_HITTEST, IntPtr.Zero, ref HTI);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
HotTabIndex = -1;
}
private IntPtr MAKELPARAM(int lo, int hi)
{
return new IntPtr((hi << 16) | (lo & 0xFFFF));
}
private Rectangle GetCloseButtonRect(int id)
{
Rectangle tabRect = GetTabRect(id);
Rectangle closeRect = new Rectangle(tabRect.Left, tabRect.Top, CloseButtonHeight, CloseButtonHeight);
switch (Alignment)
{
case TabAlignment.Left:
closeRect.Offset((tabRect.Width - closeRect.Width) / 2, 0);
break;
case TabAlignment.Right:
closeRect.Offset((tabRect.Width - closeRect.Width) / 2, tabRect.Height - closeRect.Height);
break;
default:
closeRect.Offset(tabRect.Width - closeRect.Width, (tabRect.Height - closeRect.Height) / 2);
break;
}
return closeRect;
}
#region Interop
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, ref TCHITTESTINFO lParam);
[StructLayout(LayoutKind.Sequential)]
private struct TCHITTESTINFO
{
public Point pt;
public TCHITTESTFLAGS flags;
public TCHITTESTINFO(int x, int y)
{
pt = new Point(x, y);
flags = TCHITTESTFLAGS.TCHT_NOWHERE;
}
}
[Flags()]
private enum TCHITTESTFLAGS
{
TCHT_NOWHERE = 1,
TCHT_ONITEMICON = 2,
TCHT_ONITEMLABEL = 4,
TCHT_ONITEM = TCHT_ONITEMICON | TCHT_ONITEMLABEL
}
private const int WM_NULL = 0x0;
private const int WM_SETFONT = 0x30;
private const int WM_FONTCHANGE = 0x1D;
private const int WM_MOUSEDOWN = 0x201;
private const int TCM_FIRST = 0x1300;
private const int TCM_HITTEST = TCM_FIRST + 13;
private const int TCM_SETPADDING = TCM_FIRST + 43;
#endregion
}
}
and in the Below Picture, the result and required modifications
I want to print the content of my RichTextBox (eintragRichTextBox)
I have now this code:
private void druckenPictureBox_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument documentToPrint = new PrintDocument();
printDialog.Document = documentToPrint;
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.Print();
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
}
}
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
float LinesPerPage = 0;
float YPosition = 0;
int Count = 0;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string Line = null;
Font PrintFont = this.eintragRichTextBox.Font;
SolidBrush PrintBrush = new SolidBrush(Color.Black);
LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);
while (Count < LinesPerPage && ((Line = reader.ReadLine()) != null))
{
YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));
e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());
Count++;
}
if (Line != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
PrintBrush.Dispose();
}
But it always prints me a blank site :(.. Anyone an Idea, why it is not working?
Or has someone a better code/Idea how I could achieve the printing?
I tried How to print the content of a RichTextBox control and it works like a charm.
You just have to create a custom RichTextBox and then it will print the whole text so fast and also keeps the style on the paper!
Code for creating custom RichTextBox
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;
namespace RichTextBoxPrintCtrl
{
public class RichTextBoxPrintCtrl:RichTextBox
{
//Convert the unit used by the .NET framework (1/100 inch)
//and the unit used by Win32 API calls (twips 1/1440 inch)
private const double anInch = 14.4;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE
{
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
[StructLayout(LayoutKind.Sequential)]
private struct FORMATRANGE
{
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM_USER = 0x0400;
private const int EM_FORMATRANGE = WM_USER + 57;
[DllImport("USER32.dll")]
private static extern IntPtr SendMessage (IntPtr hWnd , int msg , IntPtr wp, IntPtr lp);
// Render the contents of the RichTextBox for printing
// Return the last character printed + 1 (printing start from this point for next page)
public int Print( int charFrom, int charTo,PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);
IntPtr hdc = e.Graphics.GetHdc();
FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo; //Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page
IntPtr res = IntPtr.Zero;
IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam= IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);
//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);
//Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc);
//Return last + 1 character printer
return res.ToInt32();
}
}
}
The main source includes PrintDialog, PrintPreviewDialog, PrintDocument, and PageSetupDialog, but it works fine just with one PrintDocument control. So I've removed some extra codes just to shorten the useful part. But if you're interested to using them all, you can find the full code in the linked page.
private PrintDocument _printDocument = new PrintDocument();
private int _checkPrint;
public Form1()
{
InitializeComponent();
_printDocument.BeginPrint += _printDocument_BeginPrint;
_printDocument.PrintPage += _printDocument_PrintPage;
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog=new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
_printDocument.Print();
}
private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Print the content of RichTextBox. Store the last character printed.
_checkPrint = rchEditor.Print(_checkPrint, rchEditor.TextLength, e);
// Check for more pages
e.HasMorePages = _checkPrint < rchEditor.TextLength;
}
private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
{
_checkPrint = 0;
}
got it..
on this Place:
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.Print();
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
}
I changed to:
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
documentToPrint.Print();
}
Now it works fine..
Also if someone needs to print content of a RichTextBox, you can use my code..
It seems your code has a problem, it reprints the first page only because of the
StringReader reader = new StringReader(eintragRichTextBox.Text);
in your DocumentToPrint_PrintPage()
use that!
StringReader reader;
int prov = 0;
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (prov == 0)
{
prov = 1;
reader = new StringReader(eintragRichTextBox.Text);
}
float LinesPerPage = 0;
float YPosition = 0;
int Count = 0;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string Line = null;
....
I am building a Windows Forms applicaton in C# and have a TrackBar on my Form. How can I compute the (pixel) position of the tip of the tracker? I would like to draw a line from there to another point on my form.
Additionally I also would like to compute the lowest and highest possible x position of the tip.
The native Windows control that is wrapped by TrackBar has an awkward restriction, you cannot get the positions for the first and last tic marks. You can however get the display rectangles for the channel and the slider. This class returns them:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
class MyTrackBar : TrackBar {
public Rectangle Slider {
get {
RECT rc = new RECT();
SendMessageRect(this.Handle, TBM_GETTHUMBRECT, IntPtr.Zero, ref rc);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
}
public Rectangle Channel {
get {
RECT rc = new RECT();
SendMessageRect(this.Handle, TBM_GETCHANNELRECT, IntPtr.Zero, ref rc);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
}
private const int TBM_GETCHANNELRECT = 0x400 + 26;
private const int TBM_GETTHUMBRECT = 0x400 + 25;
private struct RECT { public int left, top, right, bottom; }
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern IntPtr SendMessageRect(IntPtr hWnd, int msg, IntPtr wp, ref RECT lp);
}
Here is a sample usage in a form, it draws a line from the first tick mark to the slider pointer:
private void myTrackBar1_ValueChanged(object sender, EventArgs e) {
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
var chan = this.RectangleToClient(myTrackBar1.RectangleToScreen(myTrackBar1.Channel));
var slider = this.RectangleToClient(myTrackBar1.RectangleToScreen(myTrackBar1.Slider));
e.Graphics.DrawLine(Pens.Black, chan.Left + slider.Width / 2, myTrackBar1.Bottom + 5,
slider.Left + slider.Width / 2, myTrackBar1.Bottom + 5);
}