Datetimepicker text value changes but value doesn't give the expected value - c#

I have created a class change the appearance of the calander.
The class is based on these previous stackoverflow questions:
Source1: Setting calendar size when overriding DateTimePicker to add week numbers
Source2: Increase Font Size of DateTimePicker Calender in Win7 Aero Theme
This is the class:
class DateTimePickerImpl : DateTimePicker
{
private const int McmFirst = 0x1000;
private const int McmGetminreqrect = (McmFirst + 9);
private const int McsWeeknumbers = 0x4;
private const int DtmFirst = 0x1000;
private const int DtmGetmonthcal = (DtmFirst + 8);
[DllImport("User32.dll")]
private static extern int GetWindowLong(IntPtr handleToWindow, int offsetToValueToGet);
[DllImport("User32.dll")]
private static extern int SetWindowLong(IntPtr h, int index, int value);
[DllImport("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
[DllImport("User32.dll")]
private static extern IntPtr SendMessage(IntPtr h, int msg, int param, int data);
[DllImport("User32.dll")]
private static extern IntPtr GetParent(IntPtr h);
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr h, int msg, int param, ref Rectangle data);
[DllImport("User32.dll")]
private static extern int MoveWindow(IntPtr h, int x, int y, int width, int height, bool repaint);
protected override void OnDropDown(EventArgs e)
{
CalendarFont = new Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
const int offsetToGetWindowsStyles = (-16);
IntPtr pointerToCalenderWindow = SendMessage(Handle, DtmGetmonthcal, 0, 0);
SetWindowTheme(pointerToCalenderWindow, "", "");
int styleForWindow = GetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles);
styleForWindow = styleForWindow | McsWeeknumbers;
Rectangle rect = new Rectangle();
SendMessage(pointerToCalenderWindow, McmGetminreqrect, 0, ref rect);
rect.Width = rect.Width + 30;
rect.Height = rect.Height + 10;
SetWindowLong(pointerToCalenderWindow, offsetToGetWindowsStyles, styleForWindow);
MoveWindow(pointerToCalenderWindow, 0, 0, rect.Width, rect.Height, true);
IntPtr parentWindow = GetParent(pointerToCalenderWindow);
MoveWindow(parentWindow, 0, 0, rect.Width, rect.Height, true);
base.OnDropDown(e);
}
}
Afther implementing this class the visible text of the datetimepicker changes to whatever you choose. But when using dateTimePickerImpl.Value it always returns the it began with (the time the datetimpicker loaded). I already found out by adding a method to the ValueChanged event that this event never happens. I found some similar problems online but these were all solved by setting the checked property of the datetimepicker to true. In my case the checked property is already true.
What did i do to break the datetimepicker?

I had to create the datetime picker manually instead of dragging it from the toolbox into the designer.
DateTimePickerImpl dtp = new DateTimePickerImpl();
dtp.Location = new Point(3, 254);
dtp.Name = "dtp";
dtp.Size = new Size(271, 26);
dtp.TabIndex = 25;
panel1.Controls.Add(dtp);

Related

C# how to stop RichTextBox redraw? [duplicate]

This question already has answers here:
RichTextBox syntax highlighting in real time--Disabling the repaint
(3 answers)
Closed 2 years ago.
I'm doing a text editor based on RichTextBox. It has to handle complex formatting (BIU, colored text, etc). The problem is that all formatting tools are selection based, e.g. i have to select piece of text, format it, select next, etc.
it takes time, and it is visible for user.
is there a way to turn-off RichTextBox redraw, then do formatting, then turn-on redraw?
Or maybe any other way to handel complex formatting quickly?
Decision found and it's working.
Wrote a wrap-class using this code
Class itself:
public class RichTextBoxRedrawHandler
{
RichTextBox rtb;
public RichTextBoxRedrawHandler (RichTextBox _rtb)
{
rtb = _rtb;
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, ref Point lParam);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, int wParam, IntPtr lParam);
const int WM_USER = 1024;
const int WM_SETREDRAW = 11;
const int EM_GETEVENTMASK = WM_USER + 59;
const int EM_SETEVENTMASK = WM_USER + 69;
const int EM_GETSCROLLPOS = WM_USER + 221;
const int EM_SETSCROLLPOS = WM_USER + 222;
private Point _ScrollPoint;
private bool _Painting = true;
private IntPtr _EventMask;
private int _SuspendIndex = 0;
private int _SuspendLength = 0;
public void SuspendPainting()
{
if (_Painting)
{
_SuspendIndex = rtb.SelectionStart;
_SuspendLength = rtb.SelectionLength;
SendMessage(rtb.Handle, EM_GETSCROLLPOS, 0, ref _ScrollPoint);
SendMessage(rtb.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
_EventMask = SendMessage(rtb.Handle, EM_GETEVENTMASK, 0, IntPtr.Zero);
_Painting = false;
}
}
public void ResumePainting()
{
if (!_Painting)
{
rtb.Select(_SuspendIndex, _SuspendLength);
SendMessage(rtb.Handle, EM_SETSCROLLPOS, 0, ref _ScrollPoint);
SendMessage(rtb.Handle, EM_SETEVENTMASK, 0, _EventMask);
SendMessage(rtb.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
_Painting = true;
rtb.Invalidate();
}
}
}
Usage:
RichTextBoxRedrawHandler rh = new RichTextBoxRedrawHandler(richTextBoxActually);
rh.SuspendPainting();
// do things with richTextBox
rh.ResumePainting();

Increase Font Size of DateTimePicker Calender in Win7 Aero Theme

I want to change the font size of calender control in Win7 to make it touch screen compatible. The theme in my machine is Aero. CalendarFont property does not have any effect on the Aero theme.
So I have overrided OnDropDown method to disable theme for the calander control. Now the font has changed, but the calender window size is not changed. The following image shows the window I am seeing
The code is given below. What should I do to increase the size of the calender window?
protected override void OnDropDown(EventArgs e)
{
IntPtr pointerToCalenderWindow = SendMessage(Handle, DtmGetmonthcal,0,0);
// Disble Theme
SetWindowTheme(pointerToCalenderWindow, "", "");
var rect = new Rectangle();
SendMessage(pointerToCalenderWindow, McmGetminreqrect, 0, ref rect);
MoveWindow(pointerToCalenderWindow,0,0,rect.Right + 2, rect.Bottom + 2, true);
base.OnDropDown(e);
}
private const int McmFirst = 0x1000;
private const int McmGetminreqrect = (McmFirst + 9);
private const int McsWeeknumbers = 0x4;
private const int DtmFirst = 0x1000;
private const int DtmGetmonthcal = (DtmFirst + 8);
private const int WMPAINT = 0x000F;
[DllImport("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
[DllImport("User32.dll")]
private static extern IntPtr SendMessage(IntPtr h,
int msg,
int param,
int data);
[DllImport("User32.dll")]
private static extern int MoveWindow(IntPtr h,
int x,
int y,
int width,
int height,
bool repaint);
It seems that this question is about the same problem as yours:
The answer states that there are actually two windows used for the calendar part (an 'inner' and an 'outer' one) and that you need to set the size for the outer window correctly.

Possible to control other Programs/Windows [duplicate]

This question already has answers here:
How do you programmatically resize and move windows with the Windows API?
(5 answers)
Closed 9 years ago.
I was wondering whether it is possible to resize programs other than the actual application itself. IE, I want to resize and move Word and my application to fill 70% and 30% of the screen respectively.
Private Sub MinimiseButton_Copy_Click(sender As Object, e As RoutedEventArgs) Handles MinimiseButton_Copy.Click
Me.Left = SystemParameters.PrimaryScreenWidth - Me.Width + 14
Me.Top = -14
Me.Height = SystemParameters.PrimaryScreenHeight
Dim parry As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("winword")
Dim word As System.Diagnostics.Process = parry(0)
SetWindowPos(word.Handle, 0, 0, 0, SystemParameters.PrimaryScreenWidth - Me.Width, SystemParameters.PrimaryScreenHeight - 28, &H10)
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto)> Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, W As Integer, H As Integer, uFlags As UInteger) As Boolean
End Function
That is indeed possible, try this function:
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int WPARAM, int LPARAM);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int, Y, int cx, int cy, uint uFlags);
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_NEXTWINDOW = 0xF040;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
public static readonly IntPtr HWND_TOP = new IntPtr(0);
public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
public const UInt32 TOPMOST_FLAGS = 0x0002 | 0x0001;
Public void resisezeWindow(String procesname, int Width, int Height, Boolean bringtofront)
{
foreach (Process proc in Process.GetProcesses())
{
IntPtr id = proc.MainWindowHandle;
Rectangle rect = new Rectangle();
GetWindowRect(id, ref rect);
if (proc.MainWindowTitle.Contains(procesname))
{
PostMessage(proc.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
MoveWindow(id, 0, 0, Width, Height, true);
if(bringtofront) SetWindowPos(id, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
proc.Refresh();
}
}
}
If there is a problem, please notify me!

How to open word document inside form in windows application

i need to open a word document in a panel control of Windows Forms Application to view/edit file and save.
i use this statement :
[DllImport("user32.dll")]
public static extern int FindWindow(string strclassName, string strWindowName);
[DllImport("user32.dll")]
static extern int SetParent(int hWndChild, int hWndNewParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
static extern bool SetWindowPos(
int hWnd, // handle to window
int hWndInsertAfter, // placement-order handle
int X, // horizontal position
int Y, // vertical position
int cx, // width
int cy, // height
uint uFlags // window-positioning options
);
[DllImport("user32.dll", EntryPoint = "MoveWindow")]
static extern bool MoveWindow(
int hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint
);
const int SWP_DRAWFRAME = 0x20;
const int SWP_NOMOVE = 0x2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOZORDER = 0x4;
const int SWP_FRAMECHANGED = 0x20;
ToolsComponents.MSWord word = new ToolsComponents.MSWord();
private void toolStripButton2_Click(object sender, EventArgs e)
{
word.CreateWordDocument();
word.OpenFile(#"C:\Users\ME\Documents\test.docx", true);
int wordWnd = FindWindow("Opusapp", null);
if (wordWnd != 0)
{
int ret = SetParent(wordWnd, pnlShowForm.Handle.ToInt32());
//int ret2 = FindWindow("Opusapp", null);
//ret = SetParent(wordWnd, pnlShowForm.Handle.ToInt32());
SetWindowPos(wordWnd, pnlShowForm.Handle.ToInt32(), 0, 0, pnlShowForm.Bounds.Width - 20, pnlShowForm.Bounds.Height - 20, SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
MoveWindow(wordWnd, -5, -33, pnlShowForm.Bounds.Width + 10, pnlShowForm.Bounds.Height + 57, true);
}
}
private void frmDocumentManager_FormClosing(object sender, FormClosingEventArgs e)
{
if (word != null)
{
word.CloseDoc(true);
word.Quit();
}
but this is not a good solution and have problem in runtime.
in sometimes MS word and document started outside the form and i can't control this.

C# - How to print aspect ratio / full page

I am printing the CHART control on button click:
chart1.SaveImage(ms, ChartImageFormat.Bmp);
Bitmap bm = new Bitmap(ms);
PrintDocument doc = new PrintDocument();
doc.PrintPage += (s, ev) =>
{
ev.Graphics.DrawImage(bm, Point.Empty); // adjust this to put the image elsewhere
ev.HasMorePages = false;
};
doc.DefaultPageSettings.Landscape = true;
doc.Print();
How do I force it to print the control so that it fits to the size of the page (preserving the aspect ratio)?
There are at least two different ways to do it, both include scaling the image to be printed to fit the page size of the selected printer:
1) using .NET facilities (haven't tested it myself, lifted from this post):
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Image i = pictureBox1.Image;
float newWidth = i.Width * 100 / i.HorizontalResolution;
float newHeight = i.Height * 100 / i.VerticalResolution;
float widthFactor = newWidth / e.MarginBounds.Width;
float heightFactor = newHeight / e.MarginBounds.Height;
if(widthFactor>1 | heightFactor > 1)
{
if(widthFactor > heightFactor)
{
newWidth = newWidth / widthFactor;
newHeight = newHeight / widthFactor;
}
else
{
newWidth = newWidth / heightFactor;
newHeight = newHeight / heightFactor;
}
}
e.Graphics.DrawImage(i, 0, 0, (int)newWidth, (int)newHeight);
}
}
2) P/Invoke'ing Windows API calls from the GDI and flat GDI (this is much more complex but faster and you can pass a plain byte array of a bitmap file (read file as byte[]), provide an email to me if need this code):
private static extern bool ClosePrinter(IntPtr hPrinter);
private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
private static extern int SetJob(IntPtr hPrinter, int JobId, int Level, ref byte pJob, int Command_Renamed);
private static extern int GdiplusStartup(out IntPtr token, ref StartupInput input, out StartupOutput output);
private static extern int GdiplusShutdown(IntPtr token);
internal static extern int GdipLoadImageFromStream([In, MarshalAs(UnmanagedType.Interface)]IStream stream, out IntPtr image);
internal static extern int GdipDisposeImage(IntPtr image);
static internal extern int GdipCreateFromHDC2(IntPtr hDC, IntPtr hDevice, out IntPtr graphics);
static internal extern int GdipDeleteGraphics(IntPtr graphics);
static internal extern int GdipReleaseDC(IntPtr graphics, IntPtr hdc);
internal static extern int GdipGetImageDimension(IntPtr image, out float width, out float height);
internal static extern int GdipGetDpiX(IntPtr graphics, out float dpi);
internal static extern int GdipGetDpiY(IntPtr graphics, out float dpi);
static internal extern int GdipDrawImageRectI(IntPtr graphics, IntPtr image, int x, int y, int width, int height);
private static extern IntPtr CreateDC([MarshalAs(UnmanagedType.LPStr)] string lpszDriver, [MarshalAs(UnmanagedType.LPStr)] string lpszDevice, [MarshalAs(UnmanagedType.LPStr)] string lpszOutput, IntPtr lpInitData);
private static extern bool DeleteDC(IntPtr hdc);
private static extern int StartDoc(IntPtr hdc, DOCINFO lpdi);
private static extern int EndDoc(IntPtr hdc);
private static extern int StartPage(IntPtr hdc);
private static extern int EndPage(IntPtr hdc);
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
Here's what I did to get this working. What's annoying is that the chart control will only draw its contents as the same size that it is sized to on the screen. The only way I found to get around this is to manually resize it. And then, since it may not like being resized when it's on a form, this also involved temporarily removing it from the form.
So with all that, I end up with something like this:
Chart ChartBox { get; private set; }
...
var oldParent = ChartBox.Parent;
var oldSize = ChartBox.Size;
ChartBox.Parent = null;
ChartBox.Size = new Size(width, height);
ChartBox.Printing.Print();
ChartBox.Parent = oldParent;
ChartBox.Size = oldSize;
Depending on your form, you may need to save and restore other properties of the chart control as well.

Categories

Resources