I'm in the process of creating a rougelike and to assure my game displays correctly I am wanting to change the console font and font size at runtime.
I am very noob to programming and c# so I'm hoping this can be explained in a way I or anyone else can easily implement.
This resource list the full syntax of the CONSOLE_FONT_INFOEX structure:
typedef struct _CONSOLE_FONT_INFOEX {
ULONG cbSize;
DWORD nFont;
COORD dwFontSize;
UINT FontFamily;
UINT FontWeight;
WCHAR FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
Specifically I'm wanting to change the console font to NSimSum and font size to 32 at runtime.
EDIT 1: Could I have it explained how to use the SetCurrentConsoleFontEx function from this post. I don't understand what context the function needs to be in. I tried Console.SetCurrentConsoleFontEx but vs gave me no options.
EDIT 2: this forum post seems to detail a simple method of changing font size but is it specific to c++?
void setFontSize(int FontSize)
{
CONSOLE_FONT_INFOEX info = {0};
info.cbSize = sizeof(info);
info.dwFontSize.Y = FontSize; // leave X as zero
info.FontWeight = FW_NORMAL;
wcscpy(info.FaceName, L"Lucida Console");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, &info);
}
Another alternative that you might look into is to create a winforms app and use a RichTextBox instead. I guess it depends on how much you need to rely on any built-in console features.
Notice the use of some custom functions to make writing colored text easier.
You can try out something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
RichTextBox console = new RichTextBox();
private void Form1_Load(object sender, EventArgs e)
{
console.Size = this.ClientSize;
console.Top = 0;
console.Left = 0;
console.BackColor = Color.Black;
console.ForeColor = Color.White;
console.WordWrap = false;
console.Font = new Font("Consolas", 12);
this.Controls.Add(console);
this.Resize += Form1_Resize;
DrawDiagram();
}
private void DrawDiagram()
{
WriteLine("The djinni speaks. \"I am in your debt. I will grant one wish!\"--More--\n");
Dot(7);
Diamond(2);
WriteLine("....╔═══════╗..╔═╗");
Dot(8);
Diamond(2);
WriteLine("...║..|....║..║.╠══════╦══════╗");
Dot(9);
Diamond(2);
Write("..║.|.....║..║.║ ║.");
Write('&', Color.DarkRed);
Dot(4);
WriteLine("║");
}
private void Dot(int qty)
{
Write('.', qty);
}
private void WriteLine(string text)
{
Write($"{text}\n");
}
private void Diamond(int qty)
{
Write('♦', qty, Color.Blue);
}
private void Write(char character, Color color)
{
Write(character, 1, color);
}
private void Write(char character, int qty)
{
Write(character, qty, Color.White);
}
private void Write(char character, int qty, Color color)
{
Write(new string(character, qty), color);
}
private void Write(string text)
{
Write(text, Color.White);
}
private void Write(string text, Color color)
{
var originalColor = console.SelectionColor;
console.SelectionColor = color;
console.AppendText(text);
console.SelectionColor = originalColor;
}
private void Form1_Resize(object sender, EventArgs e)
{
console.Size = this.ClientSize;
}
}
Output
Have you tried this
using System;
using System.Runtime.InteropServices;
public class Example
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool GetCurrentConsoleFontEx(
IntPtr consoleOutput,
bool maximumWindow,
ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFontEx);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetCurrentConsoleFontEx(
IntPtr consoleOutput,
bool maximumWindow,
CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
private const int STD_OUTPUT_HANDLE = -11;
private const int TMPF_TRUETYPE = 4;
private const int LF_FACESIZE = 32;
private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
public static unsafe void Main()
{
string fontName = "Lucida Console";
IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
if (hnd != INVALID_HANDLE_VALUE) {
CONSOLE_FONT_INFO_EX info = new CONSOLE_FONT_INFO_EX();
info.cbSize = (uint) Marshal.SizeOf(info);
bool tt = false;
// First determine whether there's already a TrueType font.
if (GetCurrentConsoleFontEx(hnd, false, ref info)) {
tt = (info.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE;
if (tt) {
Console.WriteLine("The console already is using a TrueType font.");
return;
}
// Set console font to Lucida Console.
CONSOLE_FONT_INFO_EX newInfo = new CONSOLE_FONT_INFO_EX();
newInfo.cbSize = (uint) Marshal.SizeOf(newInfo);
newInfo.FontFamily = TMPF_TRUETYPE;
IntPtr ptr = new IntPtr(newInfo.FaceName);
Marshal.Copy(fontName.ToCharArray(), 0, ptr, fontName.Length);
// Get some settings from current font.
newInfo.dwFontSize = new COORD(info.dwFontSize.X, info.dwFontSize.Y);
newInfo.FontWeight = info.FontWeight;
SetCurrentConsoleFontEx(hnd, false, newInfo);
}
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct COORD
{
internal short X;
internal short Y;
internal COORD(short x, short y)
{
X = x;
Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct CONSOLE_FONT_INFO_EX
{
internal uint cbSize;
internal uint nFont;
internal COORD dwFontSize;
internal int FontFamily;
internal int FontWeight;
internal fixed char FaceName[LF_FACESIZE];
}
}
Refer https://msdn.microsoft.com/en-us/library/system.console(v=vs.110).aspx for more details
Related
I have seen posts on changing console true type font and console colors (rgb) but nothing on setting or getting the console font size.
The reason I want to change the font size is because a grid is printed to the console, and the grid has many columns, so, it fits better with a smaller font. I'm wondering if it's possible to change it at runtime rather than allowing the default or configured fonts to take priority / override inheritance.
Maybe this article can help you
ConsoleHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ConsoleExtender {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ConsoleFont {
public uint Index;
public short SizeX, SizeY;
}
public static class ConsoleHelper {
[DllImport("kernel32")]
public static extern bool SetConsoleIcon(IntPtr hIcon);
public static bool SetConsoleIcon(Icon icon) {
return SetConsoleIcon(icon.Handle);
}
[DllImport("kernel32")]
private extern static bool SetConsoleFont(IntPtr hOutput, uint index);
private enum StdHandle {
OutputHandle = -11
}
[DllImport("kernel32")]
private static extern IntPtr GetStdHandle(StdHandle index);
public static bool SetConsoleFont(uint index) {
return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index);
}
[DllImport("kernel32")]
private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize,
uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);
[DllImport("kernel32")]
private static extern uint GetNumberOfConsoleFonts();
public static uint ConsoleFontsCount {
get {
return GetNumberOfConsoleFonts();
}
}
public static ConsoleFont[] ConsoleFonts {
get {
ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()];
if(fonts.Length > 0)
GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts);
return fonts;
}
}
}
}
Here is how to use it to list true type fonts for console,
static void Main(string[] args) {
var fonts = ConsoleHelper.ConsoleFonts;
for(int f = 0; f < fonts.Length; f++)
Console.WriteLine("{0}: X={1}, Y={2}",
fonts[f].Index, fonts[f].SizeX, fonts[f].SizeY);
ConsoleHelper.SetConsoleFont(5);
ConsoleHelper.SetConsoleIcon(SystemIcons.Information);
}
Crucial functions: SetConsoleFont, GetConsoleFontInfo and GetNumberOfConsoleFonts. They're undocumented, so use at your own risk.
In this thread I found a much more elegant solution that now works perfectly fine.
ConsoleHelper.cs:
using System;
using System.Runtime.InteropServices;
public static class ConsoleHelper
{
private const int FixedWidthTrueType = 54;
private const int StandardOutputHandle = -11;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct FontInfo
{
internal int cbSize;
internal int FontIndex;
internal short FontWidth;
public short FontSize;
public int FontFamily;
public int FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
//[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)]
public string FontName;
}
public static FontInfo[] SetCurrentFont(string font, short fontSize = 0)
{
Console.WriteLine("Set Current Font: " + font);
FontInfo before = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>()
};
if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before))
{
FontInfo set = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>(),
FontIndex = 0,
FontFamily = FixedWidthTrueType,
FontName = font,
FontWeight = 400,
FontSize = fontSize > 0 ? fontSize : before.FontSize
};
// Get some settings from current font.
if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
{
var ex = Marshal.GetLastWin32Error();
Console.WriteLine("Set error " + ex);
throw new System.ComponentModel.Win32Exception(ex);
}
FontInfo after = new FontInfo
{
cbSize = Marshal.SizeOf<FontInfo>()
};
GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after);
return new[] { before, set, after };
}
else
{
var er = Marshal.GetLastWin32Error();
Console.WriteLine("Get error " + er);
throw new System.ComponentModel.Win32Exception(er);
}
}
}
This way you can just do:
ConsoleHelper.SetCurrentFont("Consolas", 10);
After running the application (Ctrl + F5), right-click the title of the Console (it should say something like C:Windows\system32\cmd.exe) and select properties. Choose the "Font" tab, and you'll see the option to adjust the size.
The console does not support changing font size at runtime. A list of the available methods for modifying the current console windows settings can be found on MSDN. My understanding is that this is because:
The console is not a rich text interface, meaning it cannot display multiple fonts or font sizes.
as Noldorin states, this is something that should be up to the user, for example a person with vision problems may elect for a large fontsize.
I am using Balloon tip text in C# and everything is working as expected. However whenever my notifications appear, there will be subtext that correlates to the project name.
How do I remove this text?
Notification image:
notifyIcon1.Icon = new Icon(SystemIcons.Application, 40, 40);
notifyIcon1.Visible = true;
notifyIcon1.Text = "Application Installation";
notifyIcon1.BalloonTipText = "The App installaion has started.";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipTitle = "Test App Installation";
notifyIcon1.ShowBalloonTip(10000);
Okey so i think i have a start for you atleast. As you can see in the image below i managed to remove the name of the exectuable file that it was running from.
I found a example here how to show Balloon tip like Windows 10 Balloon tip without stretching icon and the example from cokeman19 where you can add your own custom icon. Although this does not seem to work when you dont want the exectuable name at the bottom.
If you want to do some changes i recommend you to look at https://msdn.microsoft.com/en-us/library/windows/desktop/bb773352(v=vs.85).aspx since it provied some nice hints on what to do.
Here is the code that i made a few changes too so we get what we want.
public class NotifyIconLarge : IDisposable
{
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
public static extern int Shell_NotifyIcon(int message, NOTIFYICONDATA pnid);
[DllImport("Comctl32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadIconWithScaleDown(IntPtr hinst, string pszName, int cx, int cy, out IntPtr phico);
[DllImport("user32.dll", SetLastError = true)]
static extern bool DestroyIcon(IntPtr hIcon);
private const int NIF_LARGE_ICON = 0x00000040;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class NOTIFYICONDATA
{
public int cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szInfoTitle;
public int dwInfoFlags;
Guid guidItem;
public IntPtr hBalloonIcon;
}
private IntPtr _windowHandle;
private IntPtr _hIcon;
private bool _added;
private int _id = 1;
private string _tipText;
public NotifyIconLarge(IntPtr windowHandle, string tipText)
{
_windowHandle = windowHandle;
_tipText = tipText;
IntPtr result = LoadIconWithScaleDown(IntPtr.Zero, "", 0, 0, out _hIcon);
UpdateIcon(true);
}
private void UpdateIcon(bool showIconInTray)
{
NOTIFYICONDATA nOTIFYICONDATA = new NOTIFYICONDATA();
nOTIFYICONDATA.uCallbackMessage = 2048;
nOTIFYICONDATA.uFlags = 1;
nOTIFYICONDATA.hWnd = _windowHandle;
nOTIFYICONDATA.uID = _id;
nOTIFYICONDATA.hIcon = IntPtr.Zero;
nOTIFYICONDATA.szTip = null;
if (_hIcon != IntPtr.Zero)
{
nOTIFYICONDATA.uFlags |= 1;
nOTIFYICONDATA.hIcon = _hIcon;
}
nOTIFYICONDATA.uFlags |= 4;
nOTIFYICONDATA.szTip = _tipText;
nOTIFYICONDATA.hBalloonIcon = _hIcon;
if (showIconInTray)
{
if (!_added)
{
Shell_NotifyIcon(0, nOTIFYICONDATA);
_added = true;
}
else
{
Shell_NotifyIcon(1, nOTIFYICONDATA);
}
}
else
{
if (_added)
{
Shell_NotifyIcon(2, nOTIFYICONDATA);
_added = false;
}
}
}
public void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon)
{
NOTIFYICONDATA nOTIFYICONDATA = new NOTIFYICONDATA();
nOTIFYICONDATA.hWnd = _windowHandle;
nOTIFYICONDATA.uID = _id;
nOTIFYICONDATA.uFlags = 16;
nOTIFYICONDATA.uTimeoutOrVersion = timeout;
nOTIFYICONDATA.szInfoTitle = tipTitle;
nOTIFYICONDATA.szInfo = tipText;
switch (tipIcon)
{
case ToolTipIcon.None:
nOTIFYICONDATA.dwInfoFlags = NIF_LARGE_ICON;
break;
case ToolTipIcon.Info:
nOTIFYICONDATA.dwInfoFlags = 1;
break;
case ToolTipIcon.Warning:
nOTIFYICONDATA.dwInfoFlags = 2;
break;
case ToolTipIcon.Error:
nOTIFYICONDATA.dwInfoFlags = 3;
break;
}
int ret = Shell_NotifyIcon(1, nOTIFYICONDATA);
}
public void RemoveFromTray()
{
UpdateIcon(false);
if (_hIcon != IntPtr.Zero)
DestroyIcon(_hIcon);
}
~NotifyIconLarge()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Dispose(bool disposing)
{
RemoveFromTray();
}
}
So first you declare the NotifyIconLarge class somewhere.
private NotifyIconLarge _nil;
Then to create a notify popup you use the following code:
_nil = new NotifyIconLarge(Handle, "Icon Tip");
_nil.ShowBalloonTip(10000, "Balloon Title", "Balloon Text", ToolTipIcon.None);
When you are finished with the tray remove it:
_nil.RemoveFromTray();
Credits to cokeman19 for most of the code
I would like to use the numlock button for something other than numlock. So basically I would like to turn off numlock when it is pressed and keep it off. I can capture the button press but it still toggles on/off. I want it off, always. Any suggestions?
Not sure WHY anyone would like to know WHY I want this to be done but here is the reason: I have a bluetooth numeric keypad that I want to use to control a machine. Does that justify the question?
After a couple hours of research I came across the following code which did the trick:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
class SetNumlockKeyOn
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
internal int type;
internal short wVk;
internal short wScan;
internal int dwFlags;
internal int time;
internal IntPtr dwExtraInfo;
int dummy1;
int dummy2;
internal int type1;
internal short wVk1;
internal short wScan1;
internal int dwFlags1;
internal int time1;
internal IntPtr dwExtraInfo1;
int dummy3;
int dummy4;
}
[DllImport("user32.dll")]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);
public static void SetNumlockOn()
{
if (Control.IsKeyLocked(Keys.NumLock)) return;
const int mouseInpSize = 28;//Hardcoded size of the MOUSEINPUT tag !!!
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;
input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1 = 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;
IntPtr pI = Marshal.AllocHGlobal(mouseInpSize * 2);
Marshal.StructureToPtr(input, pI, false);
int result = SendInput(2, pI, mouseInpSize); //Hardcoded size of the MOUSEINPUT tag !!!
//if (result == 0 || Marshal.GetLastWin32Error() != 0)
// Console.WriteLine(Marshal.GetLastWin32Error());
Marshal.FreeHGlobal(pI);
}
}
Here's an example:
public static class NativeMethods
{
public const byte VK_NUMLOCK = 0x90;
public const uint KEYEVENTF_EXTENDEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0x2;
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public static void SimulateKeyPress(byte keyCode)
{
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
public partial class Form1 : Form
{
private bool protectKeys; // To protect from inifite keypress chain reactions
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (protectKeys)
return;
if (e.KeyCode == Keys.NumLock &&
!(new Microsoft.VisualBasic.Devices.Keyboard().NumLock))
{
protectKeys = true;
NativeMethods.SimulateKeyPress(NativeMethods.VK_NUMLOCK);
protectKeys = false;
}
}
}
"Value does not fall withing expected range" error appears when I run my program with aero glass. I am using the dwmapi.dll. This is the code:
ON Aero.cs
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins margins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
ON AddStudent.cs
private const int PaddingCounter = 0;
private Aero.Margins _newMargins;
private void SetGlassArea()
{
if (Aero.DwmIsCompositionEnabled())
{
_newMargins.Top = Padding.Top;
_newMargins.Bottom = Padding.Bottom;
_newMargins.Left = Padding.Left;
_newMargins.Right = Padding.Right;
Aero.DwmExtendFrameIntoClientArea(this.Handle, ref _newMargins);
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaint(e);
if (Aero.DwmIsCompositionEnabled())
{
e.Graphics.Clear(Color.Black);
Rectangle clientArea = new Rectangle(_newMargins.Left, _newMargins.Top, this.ClientRectangle.Width - _newMargins.Left -_newMargins.Right,
this.ClientRectangle.Height - _newMargins.Top - _newMargins.Bottom);
Brush b = new SolidBrush(this.BackColor);
e.Graphics.FillRectangle(b, clientArea);
}
}
public void ApplyGlassSurface()
{
this.Padding = new Padding(PaddingCounter);
SetGlassArea();
Invalidate();
}
private void AddStudent_Load(object sender, EventArgs e)
{
ApplyGlassSurface();
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo videoCaptureDevice in webcam)
{
comboBoxEx1.Items.Add(videoCaptureDevice.Name);
}
comboBoxEx1.SelectedIndex = 0;
}
The error points to:
Aero.DwmExtendFrameIntoClientArea(this.Handle, ref _newMargins);
Can you please tell me what is wrong with it? I know it should work because I've seen a video and it worked for him. Thanks!
Maybe change DwmExtendFrameIntoClientArea PreserveSig to true.
And change Margins to:
[StructLayout(LayoutKind.Sequential)]
public struct Margins
{
public int leftWidth;
public int rightWidth;
public int topHeight;
public int bottomHeight;
}
In Windows Explorer (at least in Win7) when you hover the mouse over a column header, a filter box with an arrow appears that lets you filter the results in the ListView, so for example you can only show files starting with "A" or files > 128 MB. Can this feature be enabled in the basic ListView control in C# without subclassing or modifying the ListView?
Here's some code to play with. Add a new class to your project and paste the code shown below. Compile. Drop the new ListViewEx control from the top of the toolbox onto your form. In the form constructor, call the SetHeaderDropdown() method to enable the button. Implement the HeaderDropdown event to return a control to display. For example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
listViewEx1.SetHeaderDropdown(0, true);
listViewEx1.HeaderDropdown += listViewEx1_HeaderDropdown;
}
void listViewEx1_HeaderDropdown(object sender, ListViewEx.HeaderDropdownArgs e) {
e.Control = new UserControl1();
}
}
The below code has a flaw, the popup is displayed in a form. Which can't be too small and takes the focus away from the main form. Check this answer on hints how to implement a control that can be displayed as a toplevel window without needing a form. The code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class ListViewEx : ListView {
public class HeaderDropdownArgs : EventArgs {
public int Column { get; set; }
public Control Control { get; set; }
}
public event EventHandler<HeaderDropdownArgs> HeaderDropdown;
public void SetHeaderDropdown(int column, bool enable) {
if (column < 0 || column >= this.Columns.Count) throw new ArgumentOutOfRangeException("column");
while (HeaderDropdowns.Count < this.Columns.Count) HeaderDropdowns.Add(false);
HeaderDropdowns[column] = enable;
if (this.IsHandleCreated) SetDropdown(column, enable);
}
protected void OnHeaderDropdown(int column) {
var handler = HeaderDropdown;
if (handler == null) return;
var args = new HeaderDropdownArgs() { Column = column };
handler(this, args);
if (args.Control == null) return;
var frm = new Form();
frm.FormBorderStyle = FormBorderStyle.FixedSingle;
frm.ShowInTaskbar = false;
frm.ControlBox = false;
args.Control.Location = Point.Empty;
frm.Controls.Add(args.Control);
frm.Load += delegate { frm.MinimumSize = new Size(1, 1); frm.Size = frm.Controls[0].Size; };
frm.Deactivate += delegate { frm.Dispose(); };
frm.StartPosition = FormStartPosition.Manual;
var rc = GetHeaderRect(column);
frm.Location = this.PointToScreen(new Point(rc.Right - SystemInformation.MenuButtonSize.Width, rc.Bottom));
frm.Show(this.FindForm());
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (this.Columns.Count == 0 || Environment.OSVersion.Version.Major < 6 || HeaderDropdowns == null) return;
for (int col = 0; col < HeaderDropdowns.Count; ++col) {
if (HeaderDropdowns[col]) SetDropdown(col, true);
}
}
private Rectangle GetHeaderRect(int column) {
IntPtr hHeader = SendMessage(this.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
RECT rc;
SendMessage(hHeader, HDM_GETITEMRECT, (IntPtr)column, out rc);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
private void SetDropdown(int column, bool enable) {
LVCOLUMN lvc = new LVCOLUMN();
lvc.mask = LVCF_FMT;
lvc.fmt = enable ? LVCFMT_SPLITBUTTON : 0;
IntPtr res = SendMessage(this.Handle, LVM_SETCOLUMN, (IntPtr)column, ref lvc);
}
protected override void WndProc(ref Message m) {
Console.WriteLine(m);
if (m.Msg == WM_NOTIFY) {
var hdr = (NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NMHDR));
if (hdr.code == LVN_COLUMNDROPDOWN) {
var info = (NMLISTVIEW)Marshal.PtrToStructure(m.LParam, typeof(NMLISTVIEW));
OnHeaderDropdown(info.iSubItem);
return;
}
}
base.WndProc(ref m);
}
private List<bool> HeaderDropdowns = new List<bool>();
// Pinvoke
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, ref LVCOLUMN lvc);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, out RECT rc);
[DllImport("user32.dll")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hParent);
private const int LVM_SETCOLUMN = 0x1000 + 96;
private const int LVCF_FMT = 1;
private const int LVCFMT_SPLITBUTTON = 0x1000000;
private const int WM_NOTIFY = 0x204e;
private const int LVN_COLUMNDROPDOWN = -100 - 64;
private const int LVM_GETHEADER = 0x1000 + 31;
private const int HDM_GETITEMRECT = 0x1200 + 7;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct LVCOLUMN {
public uint mask;
public int fmt;
public int cx;
public string pszText;
public int cchTextMax;
public int iSubItem;
public int iImage;
public int iOrder;
public int cxMin;
public int cxDefault;
public int cxIdeal;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct POINT {
public int x, y;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct RECT {
public int left, top, right, bottom;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct NMHDR {
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct NMLISTVIEW {
public NMHDR hdr;
public int iItem;
public int iSubItem;
public uint uNewState;
public uint uOldState;
public uint uChanged;
public POINT ptAction;
public IntPtr lParam;
}
}
It might be tricky to implement the same type of interface, but you could have your ListView respond to the contents of a TextBox by handling the TextBox's TextChanged event and filtering the list based on the contents. If you put the list in a DataTable then filtering will be easy and you can repopulate your ListView each time the filter changes.
Of course this depends on how many items are in your list.