Archive for the '.NETCF' Category

MeasureString for wrapped multi-line text height in .NET Compact Framework

April 7th, 2008

In .NET Compact Framework, Graphics.MeasureString only measures the required width and height for a single line string.  What happens if the text is wrapped?

Several years ago, I used this piece of code to measure a wrapped string height.  Not pretty, but works.  A couple of days ago, I found this code, which utilizes the DrawText API, I think is better, but I did not like the idea of taking a control as input.  Combine these two, I have this code:

static public Size MeasureStringEx(Graphics gr, string text, Font font, Rectangle rect)
{
    RECT bounds = new RECT();
    bounds.left = rect.Left;
    bounds.right = rect.Right;
    bounds.bottom = rect.Bottom;
    bounds.right = rect.Right;

    IntPtr hFont = font.ToHfont();
    IntPtr hdc = gr.GetHdc();
    IntPtr originalObject = CoreDll.SelectObject(hdc, hFont);
    int flags = CoreDll.DT_CALCRECT | CoreDll.DT_WORDBREAK;
    CoreDll.DrawText(hdc, text, text.Length, ref bounds, flags);
    CoreDll.SelectObject(hdc, originalObject);
    gr.ReleaseHdc(hdc);
    return new Size(bounds.right - bounds.left, bounds.bottom - bounds.top);
}

and the CoreDll API imports:

Public struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

[DllImport("coredll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

[DllImport("coredll.dll")] public static extern int DrawText(IntPtr hdc, string lpStr, int nCount, ref RECT lpRect, int wFormat);
public static int DT_CALCRECT = 0x00000400;
public static int DT_WORDBREAK = 0x00000010;

Posted in .NETCF, Mobile | No Comments »

Screen Capture in .NET Compact Framework

March 31st, 2008

Five years ago, if you wanted to do a screen capture on Windows Mobile devices with .NET Compact Framework, you would look at Screen Capture, I did not count the lines, but looked quiet long.  Five years later, I wanted to do the same thing, and my code looked like this:

public static Bitmap ScreenCapture(IntPtr hWnd, Rectangle rect)
{
    IntPtr hBitmap; 
    IntPtr hDC = CoreDll.GetDC(hWnd); 
    IntPtr hMemDC = CoreDll.CreateCompatibleDC(hDC);

    hBitmap = CoreDll.CreateCompatibleBitmap(hDC, rect.Width, rect.Height);

    if (hBitmap != IntPtr.Zero)
    {
        IntPtr hOld = (IntPtr)CoreDll.SelectObject(hMemDC, hBitmap);
        CoreDll.BitBlt(hMemDC, 0, 0, rect.Width, rect.Height, hDC, 0, 0, CoreDll.SRCCOPY);

        CoreDll.SelectObject(hMemDC, hOld);

        CoreDll.DeleteDC(hMemDC);
        CoreDll.ReleaseDC(hWnd, hDC);
        Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
        CoreDll.DeleteObject(hBitmap);


        return bmp;
    }

    return null;
}

 

The CoreDll imports:

[DllImport("Coredll.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImportAttribute("coredll.dll", EntryPoint = "GetWindowDC")]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("Coredll.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("coredll.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("coredll.dll")]
public static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);
[DllImport("coredll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("coredll")]
public static extern int DeleteDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll", EntryPoint = "ReleaseDC")]
internal static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("coredll")]
public static extern IntPtr DeleteObject(IntPtr hObject);

Posted in .NETCF, Mobile | No Comments »



Permalinks By IIS Permalinks