Questo blog è stato creato come diario persoanle

Questo blog è realizzato come diario personale,per provare le mie
capacità di programmazione e altro

mercoledì 10 marzo 2010

Per trasformare un oggetto


¦label.layouttransform¦
¦skewtransform anglex="0" angley="0" centerx="0" centery="0"¦
¦matrixtransform name="myMatrixTransform"¦
¦matrixtransform.matrix¦
¦matrix offsetx="1" offsety="1" m11="3" m12="1"¦
¦/matrix¦
¦/matrixtransform.matrix¦
¦rotatetransform angle="-90"¦¦/rotatetransform¦
¦/matrixtransform¦
¦/skewtransform¦¦/label.layouttransform¦

Testo 3D in WPF

http://msdn.microsoft.com/it-it/magazine/cc163349.aspx

Il rendering di testo

http://msdn.microsoft.com/it-it/magazine/dd263097.aspx

martedì 2 marzo 2010

Per istanziare una classe unica utilizzabile da tutti i controlli:

¦window.resources¦
¦lineargradientbrush key="Colore" colorinterpolationmode="ScRgbLinearInterpolation" mappingmode="RelativeToBoundingBox"¦
¦lineargradientbrush.gradientstops¦
¦gradientstop color="Gainsboro" offset="0"¦
¦gradientstop color="AliceBlue" offset="0.1"¦
¦gradientstop color="White" offset="0.5"¦
¦gradientstop color="AliceBlue" offset="0.9"¦
¦gradientstop color="Wheat" offset="1"¦
¦/gradientstop¦
¦/gradientstop¦
¦lineargradientbrush key="ColoreInizio" colorinterpolationmode="SRgbLinearInterpolation" mappingmode="RelativeToBoundingBox" spreadmethod="Repeat"¦
¦lineargradientbrush.gradientstops¦
¦gradientstop color="White" offset="0"¦
¦gradientstop color="Gainsboro" offset="0.5"¦
¦gradientstop color="WhiteSmoke" offset="0.6"¦
¦gradientstop color="AntiqueWhite" offset="0.7"¦
¦gradientstop color="WhiteSmoke" offset="0.8"¦
¦gradientstop color="Gainsboro" offset="0.9"¦
¦gradientstop color="White" offset="1"¦
¦/gradientstop¦
¦/gradientstop¦
¦/gradientstop¦
...
poi all'interno del controllo
...Background="{StaticResource ResourceKey=Colore}"...

Distanza di Levenshtein

static class LevenshteinDistance
{
    /// 
    /// Compute the distance between two strings.
    /// 
    /// The first of the two strings.
    /// The second of the two strings.
    /// The Levenshtein cost.
    public static int Compute(string s, string t)
    {
        int n = s.Length;
        int m = t.Length;
        int[,] d = new int[n + 1, m + 1];
 
        // Step 1
        if (n == 0)
        {
            return m;
        }
 
        if (m == 0)
        {
            return n;
        }
 
        // Step 2
        for (int i = 0; i <= n; d[i, 0] = i++)
        {
        }
 
        for (int j = 0; j <= m; d[0, j] = j++)
        {
        }
 
        // Step 3
        for (int i = 1; i <= n; i++)
        {
            //Step 4
            for (int j = 1; j <= m; j++)
            {
                // Step 5
                int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
 
                // Step 6
                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }
        // Step 7
        return d[n, m];
    }
}

Ricavare l’icona di sistema dato il percorso di un file

ImageList imageList1 = new ImageList();

IntPtr hImgSmall; //the handle to the system image list

IntPtr hImgLarge; //the handle to the system image list

string fName; // 'the file name to get icon from

SHFILEINFO shinfo = new SHFILEINFO();

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\temp\\";

openFileDialog1.Filter = "All files (*.*)|*.*";

openFileDialog1.FilterIndex = 2;

openFileDialog1.RestoreDirectory = true;

            listView1.SmallImageList = imageList1;

listView1.LargeImageList = imageList1;

if (openFileDialog1.ShowDialog() == DialogResult.OK)

{

fName = openFileDialog1.Filename;

//Use this to get the small Icon

hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,

(uint)Marshal.SizeOf(shinfo),

Win32.SHGFI_ICON |

Win32.SHGFI_SMALLICON);

//Use this to get the large Icon

//hImgLarge = SHGetFileInfo(fName, 0,

//ref shinfo, (uint)Marshal.SizeOf(shinfo),

//Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);

//The icon is returned in the hIcon member of the shinfo

//struct

System.Drawing.Icon myIcon =

System.Drawing.Icon.FromHandle(shinfo.hIcon);

imageList1.Images.Add(myIcon);

//Add file name and icon to listview

ListView1.Items.Add(fName, nIndex++);

}

public struct SHFILEINFO

{

public IntPtr hIcon;

public IntPtr iIcon;

public uint dwAttributes;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]

public string szDisplayName;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]

public string szTypeName;

};

class Win32

{

public const uint SHGFI_ICON = 0x100;

public const uint SHGFI_LARGEICON = 0x0; // 'Large icon

public const uint SHGFI_SMALLICON = 0x1; // 'Small icon

[DllImport("shell32.dll")]

public static extern IntPtr SHGetFileInfo(string pszPath,

uint dwFileAttributes,

ref SHFILEINFO psfi,

uint cbSizeFileInfo,

uint uFlags);

}

Crea un’applicazione con una singola istanza

static class Program

{

private static System.Threading.Mutex appMutex;

[STAThread]

static void Main()

{

if (PrimoAvvio)

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new Form1());

}

}

private static bool PrimoAvvio

{

get

{

bool primoAvvio = false;

appMutex = new System.Threading.Mutex(true, "NomeApp", out primoAvvio);

return primoAvvio;

}

}

}

Spegnimento del PC

[DllImport("user32.dll")]
static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

[STAThread]
static void Main(string[] args)
{
ExitWindowsEx(ExitWindows.LogOff, ShutdownReason.MajorOther & ShutdownReason.MinorOther);
//this will cause the computer to logoff.

}

Interfaccia che consente di ottenere il nome dell'eseguibile che viene attivato con il doppio click

Si inizia inserendo la chiamata alla funzione esterna:

C#

using System.Runtime.InteropServices;

[DllImport("Shell32.dll")]

protected static extern int FindExecutable(

string lpFile, string lpDirectory,StringBuilder lpResult);

Inserire una funzione “wrapper” a cui passare il nome del file da controllare (esempio c:\myfile.txt) che restituisce una stringa contenente il nome dell’applicazione che la apre.

C#

public static string FindAssociatedApplication(string pFileName)

{

StringBuilder NomeEseguibile = new StringBuilder(1024);

if (FindExecutable(pFileName,String.Empty, NomeEseguibile) > 32)

return NomeEseguibile.ToString();

else

return "";

}

Application.DoEvents su WPF

using System;

using System.Windows;

using System.Windows.Threading;

namespace Sheva.Windows

{

///

/// Designates a Windows Presentation Foundation application model with added functionalities.

///

public class WpfApplication : Application

{

private static DispatcherOperationCallback exitFrameCallback = new

DispatcherOperationCallback(ExitFrame);

///

/// Processes all UI messages currently in the message queue.

///

public static void DoEvents()

{

// Create new nested message pump.

DispatcherFrame nestedFrame = new DispatcherFrame();

// Dispatch a callback to the current message queue, when getting called,

// this callback will end the nested message loop.

// note that the priority of this callback should be lower than the that of UI event messages.

DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(

DispatcherPriority.Background, exitFrameCallback, nestedFrame);

// pump the nested message loop, the nested message loop will

// immediately process the messages left inside the message queue.

Dispatcher.PushFrame(nestedFrame);

// If the "exitFrame" callback doesn't get finished, Abort it.

if (exitOperation.Status != DispatcherOperationStatus.Completed)

{

exitOperation.Abort();

}

}

private static Object ExitFrame(Object state)

{

DispatcherFrame frame = state as DispatcherFrame;

// Exit the nested message loop.

frame.Continue = false;

return null;

}

}

}

Grafici in Wpf

Ecco un'articolo su come creare i grafici in wpf

http://msdn.microsoft.com/it-it/magazine/ee413725.aspx

lunedì 1 marzo 2010

Creare un evento personalizzato

public delegate void Nome_EventHandler(...parametri...);

public class Class
{
public event Nome_EventHandler nome;
protected virtual void Onnome(...parametri...)
{
nome(...parametri...);
}
public void Codice()
{
Onnome(...parametri...);
}
}

Appunti di programmazione

Ecco un pezzo di codice utile se si vuole utilizzare la grafica di Windows Aero su Xp (finziona solo per le applicazioni in Wpf e con windows Xp service Pack 3)

Nell'App.xaml copiare questa riga di codice

¦application.resources¦
¦resourcedictionary source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"¦
¦/resourcedictionary¦