Solved

DRAWTEXT API in C# - hdc usuage

Posted on 2004-04-14
4
2,520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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