Tuesday, July 22, 2008

Get List of threads in Windows MObile.

Hi Guys,

it's another nice good morning, because of when i look MS forums last night, one guy asked , how can get list of threads in .net CF. then i tried work out in my home. then it's come sounds is good.

Here code for you.

first i want to create threadentry32 structure.
class THREADENTRY32
{

public uint cntUsage;
public uint dwFlags;
public uint dwSize;
public uint th32AccessKey;
public uint th32CurrentProcessID;
public uint th32OwnerProcessID;
public uint th32ThreadID;
public int tpBasePri;
public int tpDeltaPri;
public THREADENTRY32()
{
}
public THREADENTRY32(byte[] aData)
{
this.dwSize = BitConverter.ToUInt32(aData, 0);
this.cntUsage = BitConverter.ToUInt32(aData, 4);
this.th32ThreadID =BitConverter.ToUInt32(aData, 8);
this.th32OwnerProcessID = BitConverter.ToUInt32(aData, 12);
this.tpBasePri = BitConverter.ToInt32(aData, 0x10);
this.tpDeltaPri = BitConverter.ToInt32(aData, 0x18);
this.dwFlags = BitConverter.ToUInt32(aData, 0x20);
this.th32AccessKey = BitConverter.ToUInt32(aData, 0x24);
this.th32CurrentProcessID = BitConverter.ToUInt32(aData, 40);
}

public byte[] ToByteArray()
{
byte[] aData = new byte[0x2c];
Util.SetUInt(aData, 0, 0x2c);
return aData;
}

}

then i want to get snapshot for thread collections.

if i want to get thread snapshot olny , flag should be 4.


[DllImport("toolhelp.dll", EntryPoint = "CreateToolhelp32Snapshot", SetLastError = true)]
private static extern IntPtr CreateToolhelp32SnapshotAPI(uint flags, uint processid);

[DllImport("toolhelp.dll", EntryPoint = "CloseToolhelp32Snapshot", SetLastError = true)]
private static extern int CloseToolhelp32SnapshotAPI(IntPtr handle);

[DllImport("toolhelp.dll", EntryPoint = "Thread32First", SetLastError = true)]
private static extern int Thread32FirstAPI(IntPtr handle, byte[] te);

[DllImport("toolhelp.dll", EntryPoint = "Thread32Next", SetLastError = true)]
private static extern int Thread32NextAPI(IntPtr handle, byte[] te);

public static THREADENTRY32[] GetThreads(uint processID)
{
ArrayList list = new ArrayList();
IntPtr handle = CreateToolhelp32Snapshot(4, processID);
if (((int) handle) <= 0)
{
throw new Exception("Unable to create snapshot");
}
try
{
byte[] te = new THREADENTRY32().ToByteArray();
for (int i = Thread32First(handle, te); i == 1; i = Thread32Next(handle, te))
{
THREADENTRY32 threadentry = new THREADENTRY32(te);
if ((processID == 0) || (threadentry.th32OwnerProcessID == processID))
{
THREADENTRY32entry = new THREADENTRY32(threadentry);
list.Add(entry);
}
}
}
catch (Exception exception)
{
throw new Exception("Exception: " + exception.Message);
}
CloseToolhelp32Snapshot(handle);
return (THREADENTRY32[]) list.ToArray(typeof(THREADENTRY32));
}


That's all

try and give a feedback.

Saturday, July 19, 2008

Gradient Button in Windows mobile.

Hi Guys,
Again in this post series, today gradient button in windows mobile with managed code.

it's pretty simple,and when are finished it's look cool and nice.

i had posted my article in the www.codeproject.com

here link for you all.

http://www.codeproject.com/KB/mobile/Gradient_bt_NETCF.aspx

Enjoy .

Wednesday, July 16, 2008

Get IP address in Windows MObile

Hi Guys,

Get IP address of the device, when they connected to network.

private void GetMyIP()
{
IPHostEntry hostentry = Dns.GetHostEntry(Dns.GetHostName());
if (hostentry != null)
{
IPAddress[] collectionOfIPs = hostentry.AddressList;
MessageBox.Show(collectionOfIPs[0].ToString());
}
}

Thank you

Tuesday, July 15, 2008

Gradient BackGround in Windows mobile

Hi Guys,

Another cool stuff in this series, how make to gradient background in Windows mobile device forms?.

MSDN have answer for above question. because of .NET CF haven't support directly draw Gradient Background, but Win CE API Have support, so we can play with P/Invoke, can make a cool and pretty background.

Just Sample here

http://msdn.microsoft.com/en-us/library/ms229655.aspx

Thank you

Load Image to PictureBox in Windows Mobile

Hi guys,
Another cool stuff, many guys, asking in MS forums, how load the image to picture box from the device directory. it's pretty easy, there have many ways.

1.loan image directly from the location
2. load the image from the memory stream.

So here code for you

1.
pictureBox.Image = new Bitmap (“Full.Path.To.Bitmpap.”);

2.
private void LoadImage(string filePath)
{
using (FileStream reader = new FileStream(filePath, FileMode.Open))
{
byte[] data = new byte[reader.Length];
reader.Read(data, 0, (int)reader.Length);
using (MemoryStream memory = new MemoryStream(data))
{
pictureBox1.Image = new Bitmap(memory);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
}

Thank you

Transparent Label in .NET Compact framework

Hi Guys,

I seen in MS forums lof of guys, asking how to make transparent label.
it's pretty simple, when we use the Graphics object in .NET CF.

Here complete code for create transparent label.

private void DrawLabel (Label label, Graphics gfx)

{

if (label.TextAlign == ContentAlignment.TopLeft)

{

gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Bounds);

}

else if (label.TextAlign == ContentAlignment.TopCenter)

{

SizeF size = gfx.MeasureString(label.Text, label.Font);

float left = ((float) this.Width + label.Left) / 2 - size.Width / 2;

RectangleF rect = new RectangleF(left, (float) label.Top, size.Width, label.Height);

gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);

}

else //is aligned at TopRight

{

SizeF size = gfx.MeasureString(label.Text, label.Font);

float left = (float) label.Width - size.Width + label.Left;

RectangleF rect = new RectangleF(left, (float) label.Top, size.Width, label.Height);

gfx.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), rect);

}

}


Thank you

How to find string within ComboBox in .NET CF

Hi
The comapct framework haven't method for find a string within combobox, but native code have support. So little P/Invoke and get tht features to .NET CF

Here code for you.

public const int CB_FINDSTRINGEXACT = 0x0158;

[DllImport("coredll.dll")]
internal static extern int SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);

private void FindStringExact(string find)
{
int m = SendMessage(comboBox1.Handle, CB_FINDSTRINGEXACT, 0, find + "\0");
if (m > 0)
{
MessageBox.Show(find);
}
else
{
MessageBox.Show("no");
}
}


Thank you

How to create Action Lable in .Net compact framework.

Hi Guys,

Now i'm back in my blog, so i have plan post some cool stuff in my blog every day.

According that, today i will post a code for , how to create Action Lable in .NETCF.

It's pretty simple, Because of we dont need play with P/Invoke. but we want to play with GDI.

Because of i want to create a graphical objects,

Here complete code for you.


using System;
using System.Windows.Forms;
using System.Drawing;

namespace RaveSoftBlog
{
///
/// Arrow label button control.
///

public class ActionLabel : Control
{
class Const
{
public static Color DisableColor = Color.FromArgb(150,150,80);
public static Color PushedColor = Color.FromArgb(160,100,0);
public static Color ForeColor = Color.FromArgb(90,90,45);
public static Color BulletColor = Color.FromArgb(180,180,110);
public const string FontName = "Arial";
public const int FontSize = 10;
public const int BulletSize = 8;
}

// internal fields
bool m_pushed;
Rectangle m_rcHitArea;
Point[] m_bulletPts;

// gdi objects
Bitmap m_bmp;
Font m_font;
Pen m_penPushed, m_penFore, m_penDisabled;
Brush m_brushPushed, m_brushFore, m_brushDisabled;

// ctor
public ActionLabelControl()
{
// colors
this.ForeColor = Const.ForeColor;

// gdi objects
CreateGdiObjects();
}

protected override void OnPaint(PaintEventArgs e)
{
// draw to memory bitmap
CreateMemoryBitmap(e.Graphics);
Graphics g = Graphics.FromImage(m_bmp);
DrawLabel(g);

// blit memory bitmap to screen
e.Graphics.DrawImage(m_bmp, 0, 0);
}

protected override void OnPaintBackground(PaintEventArgs e)
{
// don't pass to base since we paint everything, avoid flashing
}

protected override void OnEnabledChanged(EventArgs e)
{
// redraw when enabled state changes
Invalidate();
}

// draw label and arrow
private void DrawLabel(Graphics g)
{
// background
g.Clear(Parent.BackColor);

// determine what pen and brush to use
Pen pen = m_pushed ? m_penPushed :
(this.Enabled ? m_penFore : m_penDisabled);

Brush brush = m_pushed ? m_brushPushed :
(this.Enabled ? m_brushFore : m_brushDisabled);

// draw solid arrow if enabled
if (this.Enabled)
g.FillPolygon(brush, m_bulletPts);

g.DrawPolygon(pen, m_bulletPts);

// label text
Size textSize = g.MeasureString(Text, m_font).ToSize();
g.DrawString(Text, m_font, brush,
Const.BulletSize+8, (this.Height - textSize.Height)/2);
}

protected override void OnMouseDown (MouseEventArgs e)
{
base.OnMouseDown(e);
this.Capture = true;

// see if clicked on label text or arrow
if (m_rcHitArea.Contains(e.X, e.Y))
{
m_pushed = true;
Invalidate();
}
}

protected override void OnMouseUp (MouseEventArgs e)
{
base.OnMouseUp(e);
this.Capture = false;
m_pushed = false;
Invalidate();
}

protected override void OnClick(EventArgs e)
{
if (m_pushed)
base.OnClick(e);
}

private void CreateGdiObjects()
{
// gdi objects
m_font = new Font(Const.FontName, Const.FontSize, FontStyle.Bold);
m_penPushed = new Pen(Const.PushedColor);
m_penFore = new Pen(this.ForeColor);
m_penDisabled = new Pen(Const.DisableColor);
m_brushPushed = new SolidBrush(Const.PushedColor);
m_brushFore = new SolidBrush(this.ForeColor);
m_brushDisabled = new SolidBrush(Const.DisableColor);
}

private void CreateMemoryBitmap(Graphics g)
{
// see if need to create gdi objects
if (m_bmp == null || m_bmp.Width != this.Width || m_bmp.Height != this.Height)
{
// memory bitmap
m_bmp = new Bitmap(this.Width, this.Height);

// bullet points
int halfHeight = this.Height/2;
m_bulletPts = new Point[3];
m_bulletPts[0] = new Point(0, halfHeight-(Const.BulletSize/2));
m_bulletPts[1] = new Point(Const.BulletSize, halfHeight);
m_bulletPts[2] = new Point(0, halfHeight+(Const.BulletSize/2));

// hit area
Size textSize = g.MeasureString(Text, m_font).ToSize();
m_rcHitArea = new Rectangle(0,
(this.Height - textSize.Height)/2,
textSize.Width + Const.BulletSize + 8,
textSize.Height);
}
}
}
}

Thank you

System Center Mobile device

Hi Guys,

Another good news, the Microsoft lounge the new blog for SCMD.

i hope it's help to mobile related developer with MS.

http://blogs.technet.com/scmdm/

Thank you