ixmx

Code snippets

Remove Child Elements from WPF Panel


This Extension Method shows how to remove Rectangle elements from Panel container.
Many objects derive from Panel. There are six defined panel classes (Canvas, DockPanel, Grid, StackPanel, VirtualizingStackPanel, WrapPanel)


public static class MyExtentions
{
    public static void RemoveRectangles(this Panel panel)
    {
        var children = from UIElement c in panel.Children where c is Rectangle select c;
        foreach (UIElement u in children)
        {
            panel.Children.Remove(u);
        }
    }
}

Adding a System wide Hot Key in Win32


ATOM hotkeyID = GlobalAddAtom(_T("MyHotKeyCombination"));

if(!UnregisterHotKey(hWnd,hotkeyID))
{
   if (RegisterHotKey( hWnd, hotkeyID, MOD_ALT, 0x42))  //0x42 is 'b'
   {
       _tprintf(_T("Hotkey 'Alt+b' registered \n"));
   }
}
Handle the WM_HOTKEY message in WndProc
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_HOTKEY:
        {
            _tprintf(_T("WM_HOTKEY received\n"));
        }
        break;
    case WM_COMMAND:

.....

Cleaner


This is a small utility I created to clean any file type recursively in a folder on Windows

Download v1.3 Freeware

You will require .Net 3.5 runtime on your system.

How to use:

  • Start the application and browse to the folder or you can type in the path in the top text box and press enter.
  • It will show the file types in that folder and all sub folders on the right hand side.
  • Select the file type by putting a check against it
  • When you navigate the file type list, you will see all the files of that type on the middle pane.
  • When you are happy to clean, press clean and all the selected file types will be deleted to the Recycle Bin

cannot access protected member declared in class ‘base’


#include "stdafx.h"

class base
{
protected:
	void method1()
	{
	}
};

class der1:public base
{
public:
	void method2(base * p)
	{
		p->method1(); //Error C2248: 'base::method1' :
//cannot access protected member declared in class 'base'
	}
};

int _tmain(int argc, _TCHAR* argv[])
{

	base *p1  = new base();

	der1 *d = new der1();

	d->method2(p1);

	return 0;
}

 

CUDA compiler Problems with 64bit


Error:
nvcc fatal : nvcc cannot find a supported cl version. Only MSVC 8.0 and MSVC 9.0 are supported

Solution:
include this line
compiler-bindir = C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin

in

C:\CUDA\bin64\nvcc.profile

How to create a compressed folder in Windows using Win32 API


void create_zipped_folder(string & folder_name)
{
   if(CreateDirectory(folder_name.c_str(),NULL))
   {
      HANDLE hFile = CreateFile(folder_name.c_str(),
                        GENERIC_READ | GENERIC_WRITE,
                        NULL,
                        NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_BACKUP_SEMANTICS,
                        NULL);

      DWORD dummy;
      USHORT format = COMPRESSION_FORMAT_DEFAULT;
      if(!DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, &format, sizeof(USHORT), NULL, 0, &dummy, NULL))
      {
         ErrorExit(TEXT("DeviceIoControl Failed"));
      }

      CloseHandle(hFile);
   }
}

system() call with paths containing spaces


string f = "C:\\Program Files\\MyProgram.exe"
string o = " a qex "
string o1= "C:\\My Documents\\file.txt"

enclose strings containg file paths with spaces inside a double quote
f = "\"" + f + "\"";
o1= "\"" + o1 + "\"";

cmd = f + o + o1;
enclose cmd inside a double quote as well
cmd ="\"" + cmd + "\"";

system(cmd);