[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DRAWTEXT API in C# - hdc usuage

Posted on 2004-04-14
4
Medium Priority
?
2,586 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 300 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

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