Solved

DRAWTEXT API in C# - hdc usuage

Posted on 2004-04-14
4
2,506 Views
Last Modified: 2008-07-11
I am trying to write text into a rectangle with text (which contains carraige return and line feed characters) as a multi line text. I am having problems in writing it. Code below

Graphics g = e.Graphics;
const int DT_CENTER = 0x1;
Win32.RECT rect = text. new Win32.RECT();
rect.left = r.Left+leftOffset;
rect.top =r.Top + r.Height;
rect.bottom = r.Top ;
rect.right =r.Left+leftOffset+textWidth;

IntPtr asd = g.GetHdc();
//win32.SetRec ??
Win32.DrawTextA(asd, sText, 255,ref rect, DT_CENTER);
g.ReleaseHdc(asd);

Kindly let me know how to deal with hdc and forming of RECT.
0
Comment
Question by:rajaloysious
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10830779
What result do you get? I don't see any problem in this code.
0
 
LVL 8

Author Comment

by:rajaloysious
ID: 10830794
I do not get the tex printed in the rectangle...
0
 
LVL 4

Assisted Solution

by:caner_elci
caner_elci earned 100 total points
ID: 10830897
Is your sText's length is really 255? Why don't you use sText.Length instead of 255? Also there is a "text." before "new Win32.RECT()" statement, which is a typing mistake I think?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 10830917
This code works for me:

        [DllImport ("User32.dll")]
        public static extern void DrawTextA(IntPtr hDC,
            [MarshalAs(UnmanagedType.LPStr)] string lpString,
            int nCount,
            ref RECT rect,
            int uFormat
            );

        private const int DT_CENTER = 1;


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



        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            RECT rect = new RECT();
            rect.left = 10;
            rect.top = 10;
            rect.bottom = 200;
            rect.right = 200;

            String s = "Hello\nWorld";

            IntPtr asd = e.Graphics.GetHdc();

            DrawTextA(asd, s, s.Length, ref rect, DT_CENTER);
       
            e.Graphics.ReleaseHdc(asd);

        }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question