Solved

DRAWTEXT API in C# - hdc usuage

Posted on 2004-04-14
4
2,496 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now