DataGridView: detect click on empty cells (Forms)

To detect a mouse click on empty cells add the mouseClick event.

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
            DataGridView.HitTestInfo ht = dataGridView1.HitTest(e.X, e.Y);
            if (ht != null)
            {
		  //e.RowIndex and e.ColumnIndex can be used.
	     }
}       
Lastest update in September 2017, inital post in September 2017

DataGridView: No selection mode (Forms)

To prevent selection in a DataGridView add the SelectionChanged event

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
	//Just to be sure this will not become recursive 
	if (dataGridView.SelectedRows.Count > 0) dataGridView.ClearSelection();  
}
Lastest update in February 2021, inital post in September 2017

Enable double buffering without sub classing (Forms)

To enable double buffering on a control without subclassing it, use the method below. Double buffering is disabled in terminal server sessions since it will lower performance instead of increasing it.

public static void setDoubleBuffering(Control control, bool state)
{
   if (!System.Windows.Forms.SystemInformation.TerminalServerSession)
   {
      typeof(Control).InvokeMember("DoubleBuffered",
      System.Reflection.BindingFlags.NonPublic |
      System.Reflection.BindingFlags.Instance  |
      System.Reflection.BindingFlags.SetProperty,
      null,
      control, new object[] { state });
   }
}
Lastest update in September 2017, inital post in September 2017

Implement clone for a derived class.

In a derived class of a clonable class (implementing ICloneable) it is strongly advised
to implement the clone override. Obviously, this is only needed if new members have been
added.

for example:

class A : B
{
   int value1;
   public override object Clone()
   {
      A instance = (A)base.Clone();
      instance.value1 = value1;
      return (instance);
   }
}

The method Clone is being called from the context of A and creates an A object. Coming from a
C++ background this looks weird since you would expect a class of type B to be created. After
the initial Clone the extra member value1 has to be initialized as shown in the example.

Lastest update in September 2017, inital post in September 2017

Dynamically load a Windows API function

Sometimes an API function is nice to have but will break compatibility with previous versions of Windows. It is possible to load functions dynamically.

For example, we will use the function SHDefExtractIconW. Using it will break Windows 2000 compatibility. Whether that is a problem I’ll leave that up to you.

The windows prototype for this function is:

HRESULT SHDefExtractIcon(
_In_ LPCTSTR pszIconFile,
int iIndex,
_In_ UINT uFlags,
_Out_opt_ HICON *phiconLarge,
_Out_opt_ HICON *phiconSmall,
UINT nIconSize
);

Most API functions use the calling definition __stdcall. Omitting this will lead to stack errors or malfunctioning functions. A variable of the function prototype must be made. It will have the following form:

HRESULT (__stdcall *SHDefExtractIcon)(
_In_ LPCTSTR pszIconFile,
int iIndex,
_In_ UINT uFlags,
_Out_opt_ HICON *phiconLarge,
_Out_opt_ HICON *phiconSmall,
UINT nIconSize
);

Note that the name SHDefExtractIcon is free to choose since this is just a variable declaration.

You can also remove the variable names, leaving them will make it somewhat self-documenting.

Now load the DLL using:

HMODULE module = LoadLibrary(L”shell32.dll”);

and the function using:

SHDefExtractIcon = ( HRESULT(__stdcall *)(LPCTSTR, int, UINT, HICON *, HICON *, UINT))
GetProcAddress(module,”SHDefExtractIconW”);

Here the variable names are removed for clarity. note that API functions accepting string arguments are often available in two variant e.g. MessageBoxA and MessageBoxW, you can use both. The compiler will create an define MessageBox to either one depending on the compiler setting, Multibyte (A) or Unicode(W).

GetProcAddress will return NULL if the function can not be found.

Now the functionn can be called:

(*SHDefExtractIcon)(L”c:\\Windows\\System32\\shell32.dll”, 0,0, &largeIcon, &smallIcon, MAKELPARAM(256, 0));

 

 

 

 

Lastest update in September 2017, inital post in September 2017

Get large icon from a dll or executable (XP+)

To get a large icon from a DLL or executable use SHDefExtractIcon from the shell32.lib

HRESULT SHDefExtractIcon(
_In_ LPCTSTR pszIconFile,
int iIndex,
_In_ UINT uFlags,
_Out_opt_ HICON *phiconLarge,
_Out_opt_ HICON *phiconSmall,
UINT nIconSize
);

This function is supported since Windows XP.

Icon size contains the required size in pixels. Use MAKELPARAM(large icon size, small icon size) macro to get the correct value. Icons will be resized to the requested size. Use 0 to get the default size.

Sample code:

SHDefExtractIcon(L”c:\\Windows\\System32\\shell32.dll”, 0,0, &iconLarge, &iconsSmall, MAKELPARAM(256, 0));

//To check the size, colordeph
ICONINFO iconInfo = { 0 };
GetIconInfo(iconLarge, &iconInfo);
BITMAP bitmap = { 0 };
GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bitmap);

DestroyIcon(smallIcon);
DestroyIcon(largeIcon);

Lastest update in September 2017, inital post in September 2017

Determine whether a point is above or below a vector (2D)

Determine whether point P is above or below a vector through points A, B

Point above vector

z = (B_x - A_x) (P_y - A_y) - (B_y - A_y) (P_x - A_x)

Possible values for z:

0  On the vector

> 0 Above the vector

< 0 Below the vector

The function above is the ‘2D cross product’, therefore z is the surface of vector \overline{AB} and \overline{AP}. Dividing the surface by the length of vector \overline{AB} (\vert \overline{AB}\vert) gives the ‘signed distance’ of the point perpendicular to the point P.

Lastest update in January 2022, inital post in December 2015

Get the distance between a point and vector (2D)

Determine the distance of point P and vector through points P1, P2

Distance point – vector

dist = \dfrac{\left | (B_x - A_x) (P_y - A_y) - (B_y - A_y) (P_x - A_x) \right |}{||\overline{AB}||}

This is a ‘2D cross product’ of \overline{AB} and \overline{AP}. Since the cross product is the parallelogram surface of the two vectors, dividing it by the length of \overline{AB} to get the distance.

\left\|\overline{AB}\right\| = \sqrt{(B_x - A_x)^2 + (B_y - A_y)^2}

Lastest update in January 2022, inital post in November 2015

Settings

Settings can be stored using

SharedPreferences settings;
settings = PreferenceManager.getDefaultSharedPreferences(context);

This will access settings stored for the context which is typically an Activity! To share settings cross Activities use context.getApplicationContext() instead of context. This will share the data,

settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

Lastest update in August 2013, inital post in August 2013