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 });
   }
}

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.

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));

 

 

 

 

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);