TreeView CheckBoxes=true and ShowNodeToolTips=true GDI objects leak - c#

TreeView leaks resource (2 DC handles and 2 Bitmap handles) when TreeView CheckBoxes=true because of a bug, and I dealt with it:
public class TreeViewCustom : TreeView
{
public new bool CheckBoxes
{
get
{
return base.CheckBoxes;
}
set
{
if (base.CheckBoxes == false)
{
base.CheckBoxes = true;
IntPtr handle = SendMessage(this.Handle, TVM_GETIMAGELIST, new IntPtr(TVSIL_STATE), IntPtr.Zero);
if (handle != IntPtr.Zero)
_checkboxImageList = handle;
}
}
}
/// <summary>
/// The handle to the state ImageList
/// </summary>
private IntPtr _checkboxImageList = IntPtr.Zero;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x203) // double click
m.Result = IntPtr.Zero;
else
base.WndProc(ref m);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
Focus();
SelectedNode = GetNodeAt(e.X, e.Y);
}
}
[DllImport("comctl32.dll", CharSet = CharSet.Auto)]
public static extern bool ImageList_Destroy(IntPtr hImageList);
[DllImport("comctl32.dll", CharSet = CharSet.Auto)]
public static extern bool ImageList_RemoveAll(IntPtr hImageList);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern System.IntPtr SendMessage(IntPtr hWnd, uint Msg,
IntPtr wParam, IntPtr lParam);
const uint TV_FIRST = 0x1100;
const uint TVM_GETIMAGELIST = TV_FIRST + 8;
const int TVSIL_NORMAL = 0;
const int TVSIL_STATE = 2;
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_checkboxImageList != IntPtr.Zero)
{
ImageList_Destroy(_checkboxImageList);
}
base.Dispose(disposing);
this.DestroyHandle();
}
else base.Dispose(disposing);
}
}
And everything is OK, except when treeview.ShowNodeToolTips is set to true, then the resources still leak, even with the fix. I think the tooltip is a problem (ToolTips also leak handles if you don't dispose of them properly)
EDIT:
(Why the leak happens when CheckBoxes = true)
When CheckBoxes is set to true, the style is set to the TreeView, and it can be done only once. This causes an CheckBoxImageList to be created. The list contains 2 bitmaps: the checked and unchecked box, every object in the list has a DC handle and a BITMAP handle (that's why 4 handles are leaked for each instance). When the TreeView is disposed, the list of images isn't destroyed, hence the leak.
(Why the leak happens when ShowNodeToolTips=true - my suspicions, http://reflector.webtropy.com/default.aspx/DotNET/DotNET/8#0/untmp/whidbey/REDBITS/ndp/fx/src/WinForms/Managed/System/WinForms/TreeView#cs/2/TreeView#cs)
The TootTip holds the reference to the control, if the ToolTip is not disposed, the control stays in the memory too. When ShowNodeToolTips = true, the ToolTip is created and associated with the TreeView, and it isn't disposed when TreeView is disposed.
The code I use for testing:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form_Tv form = new Form_Tv();
form.StartPosition = FormStartPosition.CenterParent; ;
form.ShowDialog();
form.Dispose();
GC.Collect();
}
}
public partial class Form_Tv : Form
{
public Form_Tv()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
{
TreeViewCustom tv = new TreeViewCustom();
tv.CheckBoxes = true;
tv.ShowNodeToolTips = true;
this.Controls.Add(tv);
}
this.FormClosed += new FormClosedEventHandler(Form_Tv_FormClosed);
this.ControlRemoved += new ControlEventHandler(Form_Tv_ControlRemoved);
}
void Form_Tv_ControlRemoved(object sender, ControlEventArgs e)
{
e.Control.Dispose();
}
void Form_Tv_FormClosed(object sender, FormClosedEventArgs e)
{
this.Controls.Clear();
}
}
Test results (Windows 7):
Form1 is the main form of the application. After it's shown:
DC: 7 Bitmap: 3
After Form_Tv is shown:
DC: 409 Bitmap: 405
After Form_Tv is closed:
DC: 209 Bitmap: 205
My question:
How can I get a hold of the ToolTip that is set for the TreeView when ShowNodeToolTips is set to true, and how can I properly dispose of it?

Related

Exception when using console window in C# (Windows form application)

I have a program WFA that also has and command Window. I open the window with AllocConsole(); When I close the console window, I use FreeConsole(); but when I open it again with AllocConsole(); I wanna write and read from it and it throws an exeption.
The code:
namespace WindowsFormsApplication2
{
class classx
{
[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
[DllImport("kernel32")]
public static extern bool AttachConsole();
[DllImport("kernel32")]
public static extern bool GetConsoleWindow();
public static bool z = false;
[DllImport("kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add);
public delegate bool HandlerRoutine(uint dwControlType);
}
public partial class Form1 : Form
{
NotifyIcon icontask;
Icon iconone_active;
Icon iconone_inactive;
/*Icon icontwo;
Icon iconthree;
Icon iconfour;
Icon iconfive;*/
Thread Threadworkermy;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
iconone_active = new Icon(".../iconone_active.ico");
iconone_inactive = new Icon(".../iconone_inactive.ico");
icontask = new NotifyIcon();
icontask.Icon = iconone_active;
icontask.Visible = true;
Threadworkermy = new Thread(new ThreadStart(checkActivityThread));
Threadworkermy.Start();
MenuItem Nameapp = new MenuItem("xr");
MenuItem quitappitem = new MenuItem("quit program");
MenuItem OpenGUI = new MenuItem("Open GUI");
MenuItem Advancedmodewindow = new MenuItem("x");
ContextMenu contextmenu = new ContextMenu();
quitappitem.Click += quitappitem_click;
OpenGUI.Click += OpenGUI_click;
Advancedmodewindow.Click += Advancedmodewindow_click;
contextmenu.MenuItems.Add(Nameapp);
contextmenu.MenuItems[0].Enabled = false;
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(OpenGUI);
contextmenu.MenuItems.Add(Advancedmodewindow);
contextmenu.MenuItems.Add("-");
contextmenu.MenuItems.Add(quitappitem);
icontask.ContextMenu = contextmenu;
icontask.Icon = iconone_active;
icontask.Visible = true;
}
private void Advancedmodewindow_click(object sender, EventArgs e)
{
classx.AllocConsole();
Console.WriteLine("X");
classx.FreeConsole();
}
private void OpenGUI_click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
}
private void quitappitem_click(object sender, EventArgs e)
{
Threadworkermy.Abort();
icontask.Dispose();
this.Close();
}
public void checkActivityThread()
{
try
{
while(true)
{
Thread.Sleep(100);
}
} catch(ThreadAbortException tbe)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
}
}
Exception that it throws out 'System.IO.IOException' in mscorlib.dll
Additional information: The handle is invalid.
To those who will be saying to change the type, I can't. (it needs to be WFA application)
there seems to be an issue with destroying the consolewindow, so you could just hide it.
For hiding the window you need an additional DllImport from user32.dll and change the returnvalue of GetConsoleWindow to IntPtr:
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
Now check if a console-handle already exists. If it does show the console otherwise create the consolewindow:
private void Advancedmodewindow_click(object sender, EventArgs e)
{
IntPtr handle = classx.GetConsoleWindow();
if (handle == IntPtr.Zero)
{
classx.AllocConsole();
handle = classx.GetConsoleWindow();
}
else
{
//shows the window with the given handle
classx.ShowWindow(handle, 8);
}
Console.WriteLine("X");
//hides the window with the given handle
classx.ShowWindow(handle, 0);
}
The original solution can be found here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/cdee5d88-3325-47ce-9f6b-83aa4447f8ca/console-exception-on-windows-8?forum=clr

Can't register for power notification settings in WinForms C#

Here is the code
[DllImport(#"User32", SetLastError = true, EntryPoint = "RegisterPowerSettingNotification",
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr RegisterPowerSettingNotification(IntPtr hRecipient, ref Guid PowerSettingGuid, Int32 Flags);
static Guid GUID_LIDSWITCH_STATE_CHANGE = new Guid(0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3);
private const int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000;
private const int WM_POWERBROADCAST = 0x0218;
const int PBT_POWERSETTINGCHANGE = 0x8013;
[StructLayout(LayoutKind.Sequential, Pack = 4)]
internal struct POWERBROADCAST_SETTING
{
public Guid PowerSetting;
public uint DataLength;
public byte Data;
}
private bool? _previousLidState = null;
public TrayIcon()
{
RegisterForPowerNotifications();
}
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_POWERBROADCAST:
OnPowerBroadcast(m.WParam, m.LParam);
break;
default:
break;
}
base.WndProc(ref m);
}
private void RegisterForPowerNotifications()
{
IntPtr handle = this.Handle;
Debug.WriteLine("Handle: " + handle.ToString()); //If this line is omitted, then lastError = 1008 which is ERROR_NO_TOKEN, otherwise, lastError = 0
IntPtr hLIDSWITCHSTATECHANGE = RegisterPowerSettingNotification(handle,
ref GUID_LIDSWITCH_STATE_CHANGE,
DEVICE_NOTIFY_WINDOW_HANDLE);
Debug.WriteLine("Registered: " + hLIDSWITCHSTATECHANGE.ToString());
Debug.WriteLine("LastError:" + Marshal.GetLastWin32Error().ToString());
}
private void OnPowerBroadcast(IntPtr wParam, IntPtr lParam)
{
if ((int)wParam == PBT_POWERSETTINGCHANGE)
{
POWERBROADCAST_SETTING ps = (POWERBROADCAST_SETTING)Marshal.PtrToStructure(lParam, typeof(POWERBROADCAST_SETTING));
IntPtr pData = (IntPtr)((int)lParam + Marshal.SizeOf(ps));
Int32 iData = (Int32)Marshal.PtrToStructure(pData, typeof(Int32));
if (ps.PowerSetting == GUID_LIDSWITCH_STATE_CHANGE)
{
bool isLidOpen = ps.Data != 0;
if (!isLidOpen == _previousLidState)
{
LidStatusChanged(isLidOpen);
}
_previousLidState = isLidOpen;
}
}
}
private void LidStatusChanged(bool isLidOpen)
{
if (isLidOpen)
{
//Do some action on lid open event
MessageBox.Show("Lid is now open");
}
else
{
//Do some action on lid close event
MessageBox.Show("Lid is now closed");
}
}
}
}
I have no idea what the problem is. I get calls to WndProc function, but nothing happens when the lid is closed or opened. LidStatusChanged is never called.
I have followed this post but that doesn't help as everything matches.
I have no idea what the heck I did wrong. All help is greatly appreciated.
ShowInTaskbar = Visible = false;
The bug is no longer visible in the snippet. It is the ShowInTaskbar property assignment that caused the problem. It is a "difficult" property, it can only be specified in the style flags passed to CreateWindowEx(). So that forces Winforms to destroy the current window and create a new one, it now gets a different Handle value. No more notifications.
You probably got into this trouble by trying to keep the window invisible. Proper way to do that is:
protected override void SetVisibleCore(bool value) {
if (!IsHandleCreated) {
this.CreateHandle();
value = false;
}
base.SetVisibleCore(value);
}
Delete OnLoad(), no longer necessary and not called until the window actually becomes visible. And you want to make sure that, even if the Handle value changes for some reason (there are several "difficult" properties), you still get a notification. Which you do by deleting the code from the constructor and:
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
RegisterForPowerNotifications();
}

Move form without border through a panel

I been searching around for a solution for my problem but for now I wasn't able to get any sucessfull code for what I want to do. So, I have a form without border that is filled with 2 custom panels, so there is no way to the user click on the frame, thinking in that, I implement a code that when user click on a panel, this will call a function on my form that recive by parameter the event of the mouse.
This is the code of my Panel (note that both of my panels in the frame are the same class, it's just 2 diferent instances)
public class MyPanel : System.Windows.Forms.Panel{
(...)
private void MyPanel_MouseDown(object sender, MouseEventArgs e)
{
BarraSms.getInstance().mouseDown(e);
}
private void MyPanel_MouseMove(object sender, MouseEventArgs e)
{
BarraSms.getInstance().mouseMove(e);
}
}
And this is the code of my form:
public partial class BarraSms : Form
{
private Point mousePoint;
(...)
public void mouseDown(MouseEventArgs e) {
mousePoint = new Point(-e.X, -e.Y);
}
public void mouseMove(MouseEventArgs e) {
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point mousePos = Control.MousePosition;
mousePos.Offset(mousePoint .X, mousePoint .Y);
this.Location = mousePos;
}
}
}
Is there something that I'm missing?
Thank you in advance for the help.
The working code (update), problem solved by: x4rf41
The MyPanel Class :
MouseMove += MyPanel_MouseMove; // added in class constructer
the BarraSms class (Form)
public partial class BarraSms : Form
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public void mouseMove(MouseEventArgs e) {
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
Point loc = this.Location;
writeCoordToBin(loc.X, loc.Y);
}
}
}
There is much better solution for that using the windows api function.
The way you use it, you will have a problem when you move the form very fast and the mouse goes out of the panel. I had the exact same problem.
try this:
using System.Runtime.InteropServices;
and in your Form class
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
public void mouseMove(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, new IntPtr(HT_CAPTION), IntPtr.Zero);
}
}
no need for a mouseDown event for this
Your private methods in MyPanel cannot be called by the framework. You need to declare them as follows:
protected override void OnMouseDown(MouseEventArgs e)
{
var parent = this.Parent as BarraSms;
parent.mouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
var parent = this.Parent as BarraSms;
parent.mouseMove(e);
}
Try this:
public void mouseMove(MouseEventArgs e) {
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Point currentPos = Location;
currentPos.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
this.Location = currentPos;
}
//Or simply use Location.Offset(e.X + mousePoint.X, e.Y + mousePoint.Y);
}
You have to use Location (the current location of the form), not the Control.MousePosition which is the location of mouse on screen.
UPDATE: Looks like you don't even know how to register event handlers, try modifying your panel class like this:
public class MyPanel : System.Windows.Forms.Panel{
//(...)
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
BarraSms.getInstance().mouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
BarraSms.getInstance().mouseMove(e);
}
}

Use of NativeWindow for ComboBox causes exception in Dispose-method

In C# Windows.Forms I want to intercept the paste-windowmessage for a combobox. As this doesn't work by overriding the WndProc-method of the combobox, because I would need to override the WndProc of the textbox inside the combobox, I decided to create a custom class of type NativeWindow which overrides the WndProc. I assign the handle and release it, when the combobox-handle gets destroyed. But when Dispose for the combobox is called the problem is that I get an InvalidOperationException saying that an invalid cross-thread operation occured and that the combobox was accessed from a thread other than the thread it was created on. Any ideas what is going wrong here?
In the following you'll see, how my classes look like:
public class MyCustomComboBox : ComboBox
{
private WinHook hook = null;
public MyCustomComboBox()
: base()
{
this.hook = new WinHook(this);
}
private class WinHook : NativeWindow
{
public WinHook(MyCustomComboBox parent)
{
parent.HandleCreated += new EventHandler(this.Parent_HandleCreated);
parent.HandleDestroyed += new EventHandler(this.Parent_HandleDestroyed);
}
protected override void WndProc(ref Message m)
{
// do something
base.WndProc(ref m);
}
private void Parent_HandleCreated(object sender, EventArgs e)
{
MyCustomComboBox cbx = (MyCustomComboBox)sender;
this.AssignHandle(cbx.Handle);
}
private void Parent_HandleDestroyed(object sender, EventArgs e)
{
this.ReleaseHandle();
}
}
}
Per Hans' suggestion, I modified the code to use CB_GETCOMBOBOXINFO from one of his own examples.
public class PastelessComboBox : ComboBox {
private class TextWindow : NativeWindow {
[StructLayout(LayoutKind.Sequential)]
private struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private struct COMBOBOXINFO {
public Int32 cbSize;
public RECT rcItem;
public RECT rcButton;
public int buttonState;
public IntPtr hwndCombo;
public IntPtr hwndEdit;
public IntPtr hwndList;
}
[DllImport("user32.dll", EntryPoint = "SendMessageW", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessageCb(IntPtr hWnd, int msg, IntPtr wp, out COMBOBOXINFO lp);
public TextWindow(ComboBox cb) {
COMBOBOXINFO info = new COMBOBOXINFO();
info.cbSize = Marshal.SizeOf(info);
SendMessageCb(cb.Handle, 0x164, IntPtr.Zero, out info);
this.AssignHandle(info.hwndEdit);
}
protected override void WndProc(ref Message m) {
if (m.Msg == (0x0302)) {
MessageBox.Show("No pasting allowed!");
return;
}
base.WndProc(ref m);
}
}
private TextWindow textWindow;
protected override void OnHandleCreated(EventArgs e) {
textWindow = new TextWindow(this);
base.OnHandleCreated(e);
}
protected override void OnHandleDestroyed(EventArgs e) {
textWindow.ReleaseHandle();
base.OnHandleDestroyed(e);
}
}

WPF Minimize on Taskbar Click

I have a WPF application that by stakeholder requirement must have a WindowStyle="None", ResizeMode="NoResize" and AllowTransparency="True". I know that by not using the Windows chrome, you have to re-implement many of the OS window-handling features. I was able to create a working custom minimize button, however I was not able to re-implement the feature where Windows minimize the application when you click on the Taskbar icon at the bottom of your screen.
The user requirement is such that the application should minimize on taskbar icon click and restore on clicking again. The latter has never stopped working but I have not been able to implement the former. Here is the code that I am using:
public ShellView(ShellViewModel viewModel)
{
InitializeComponent();
// Set the ViewModel as this View's data context.
this.DataContext = viewModel;
this.Loaded += new RoutedEventHandler(ShellView_Loaded);
}
private void ShellView_Loaded(object sender, RoutedEventArgs e)
{
var m_hWnd = new WindowInteropHelper(this).Handle;
HwndSource.FromHwnd(m_hWnd).AddHook(WindowProc);
}
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == NativeMethods.CS_DBLCLKS)
{
this.WindowState = WindowState.Minimized;
// handled = true
}
return IntPtr.Zero;
}
/// <summary>
/// http://msdn.microsoft.com/en-us/library/ms646360(v=vs.85).aspx
/// </summary>
internal class NativeMethods
{
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int SC_CLOSE = 0xF060;
public const int WM_SYSCOMMAND = 0x0112;
public const int WS_SYSMENU = 0x80000;
public const int WS_MINIMIZEBOX = 0x20000;
public const int CS_DBLCLKS = 0x8;
NativeMethods() { }
}
Use ResizeMode="CanMinimize". This will allow you to minimize to the taskbar.
I have used this code in the past to minimize/maximize Windows using WPF's WindowStyle=None
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
AdjustWindowSize();
}
private void AdjustWindowSize()
{
if (this.WindowState == WindowState.Maximized)
{
this.WindowState = WindowState.Normal;
}
else
{
this.WindowState = WindowState.Maximized;
}
}
private void FakeTitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Left)
{
if (e.ClickCount == 2)
{
AdjustWindowSize();
}
else
{
Application.Current.MainWindow.DragMove();
}
}
}
I just realized that if ResizeMode=NoResize than this happens, if it is equal to CanResize than you do not disable the OS feature to minimize via Taskbar icon click. I'm voting to close this question.

Categories

Resources