├── Icons DatePicker ├── calendarDark.png └── calendarWhite.png ├── LICENSE ├── README.md └── RJDatePicker.cs /Icons DatePicker/calendarDark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RJCodeAdvance/Custom-DateTimePicker-WinForm/c4a0b90381c630d2f90b8d2ceccd73a07e39b6dc/Icons DatePicker/calendarDark.png -------------------------------------------------------------------------------- /Icons DatePicker/calendarWhite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RJCodeAdvance/Custom-DateTimePicker-WinForm/c4a0b90381c630d2f90b8d2ceccd73a07e39b6dc/Icons DatePicker/calendarWhite.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Custom-DateTimePicker-WinForm 2 | Windows Form and C# 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJDatePicker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | using System.Windows.Forms; 8 | using System.Drawing; 9 | using System.Drawing.Drawing2D; 10 | 11 | namespace CustomControls.RJControls 12 | { 13 | public class RJDatePicker : DateTimePicker 14 | { 15 | //Fields 16 | //-> Appearance 17 | private Color skinColor = Color.MediumSlateBlue; 18 | private Color textColor = Color.White; 19 | private Color borderColor = Color.PaleVioletRed; 20 | private int borderSize = 0; 21 | 22 | //-> Other Values 23 | private bool droppedDown = false; 24 | private Image calendarIcon = Properties.Resources.calendarWhite; 25 | private RectangleF iconButtonArea; 26 | private const int calendarIconWidth = 34; 27 | private const int arrowIconWidth = 17; 28 | 29 | //Properties 30 | public Color SkinColor 31 | { 32 | get { return skinColor; } 33 | set 34 | { 35 | skinColor = value; 36 | if (skinColor.GetBrightness() >= 0.6F) 37 | calendarIcon = Properties.Resources.calendarDark; 38 | else calendarIcon = Properties.Resources.calendarWhite; 39 | this.Invalidate(); 40 | } 41 | } 42 | 43 | public Color TextColor 44 | { 45 | get { return textColor; } 46 | set 47 | { 48 | textColor = value; 49 | this.Invalidate(); 50 | } 51 | } 52 | 53 | public Color BorderColor 54 | { 55 | get { return borderColor; } 56 | set 57 | { 58 | borderColor = value; 59 | this.Invalidate(); 60 | } 61 | } 62 | 63 | public int BorderSize 64 | { 65 | get { return borderSize; } 66 | set 67 | { 68 | borderSize = value; 69 | this.Invalidate(); 70 | } 71 | } 72 | 73 | //Constructor 74 | public RJDatePicker() 75 | { 76 | this.SetStyle(ControlStyles.UserPaint, true); 77 | this.MinimumSize = new Size(0, 35); 78 | this.Font = new Font(this.Font.Name, 9.5F); 79 | } 80 | 81 | //Overridden methods 82 | protected override void OnDropDown(EventArgs eventargs) 83 | { 84 | base.OnDropDown(eventargs); 85 | droppedDown = true; 86 | } 87 | protected override void OnCloseUp(EventArgs eventargs) 88 | { 89 | base.OnCloseUp(eventargs); 90 | droppedDown = false; 91 | } 92 | protected override void OnKeyPress(KeyPressEventArgs e) 93 | { 94 | base.OnKeyPress(e); 95 | e.Handled = true; 96 | } 97 | protected override void OnPaint(PaintEventArgs e) 98 | { 99 | using (Graphics graphics = this.CreateGraphics()) 100 | using (Pen penBorder = new Pen(borderColor, borderSize)) 101 | using (SolidBrush skinBrush = new SolidBrush(skinColor)) 102 | using (SolidBrush openIconBrush = new SolidBrush(Color.FromArgb(50, 64, 64, 64))) 103 | using (SolidBrush textBrush = new SolidBrush(textColor)) 104 | using (StringFormat textFormat = new StringFormat()) 105 | { 106 | RectangleF clientArea = new RectangleF(0, 0, this.Width - 0.5F, this.Height - 0.5F); 107 | RectangleF iconArea = new RectangleF(clientArea.Width - calendarIconWidth, 0, calendarIconWidth, clientArea.Height); 108 | penBorder.Alignment = PenAlignment.Inset; 109 | textFormat.LineAlignment = StringAlignment.Center; 110 | 111 | //Draw surface 112 | graphics.FillRectangle(skinBrush, clientArea); 113 | //Draw text 114 | graphics.DrawString(" " + this.Text, this.Font, textBrush, clientArea, textFormat); 115 | //Draw open calendar icon highlight 116 | if (droppedDown == true) graphics.FillRectangle(openIconBrush, iconArea); 117 | //Draw border 118 | if (borderSize >= 1) graphics.DrawRectangle(penBorder, clientArea.X, clientArea.Y, clientArea.Width, clientArea.Height); 119 | //Draw icon 120 | graphics.DrawImage(calendarIcon, this.Width - calendarIcon.Width - 9, (this.Height - calendarIcon.Height) / 2); 121 | 122 | } 123 | } 124 | 125 | protected override void OnHandleCreated(EventArgs e) 126 | { 127 | base.OnHandleCreated(e); 128 | int iconWidth = GetIconButtonWidth(); 129 | iconButtonArea = new RectangleF(this.Width - iconWidth, 0, iconWidth, this.Height); 130 | } 131 | protected override void OnMouseMove(MouseEventArgs e) 132 | { 133 | base.OnMouseMove(e); 134 | if (iconButtonArea.Contains(e.Location)) 135 | this.Cursor = Cursors.Hand; 136 | else this.Cursor = Cursors.Default; 137 | } 138 | 139 | //Private methods 140 | private int GetIconButtonWidth() 141 | { 142 | int textWidh = TextRenderer.MeasureText(this.Text, this.Font).Width; 143 | if (textWidh <= this.Width - (calendarIconWidth + 20)) 144 | return calendarIconWidth; 145 | else return arrowIconWidth; 146 | } 147 | } 148 | } 149 | --------------------------------------------------------------------------------