Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DRAWTEXT API in C# - hdc usuage

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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