• 0

[C# Tip] Converting ContentAlignment to StringAlignment


Question

Recently, while writing my own custom button, I needed to convert the TextAlign property to something I can use in DrawString. This initially proofed not as simple as I thought because TextAlign uses the ContentAlignment type, and DrawString uses the far simple StringAlignment.

I spent a good half a day scouring the internet, and I really couldn't believe what I found. It seems that the ONLY way that anyone had found how to do this relatively simple task, was with a switch statement! Laboriously checking which TextAlignment was being used, and manually setting the .LineAlignment and .Alignment properties of the StringFormat object before sending it to graphics.DrawString.

Experience has taught me that, in C#, there are usually many solutions that will work, but most of them are complete and utter garbage, and it struck me that there's no way that a long assed switch statement was the right solution. So I had a look at the actual values of the enums for ContentAlignment and StringAlignment, and realised that really, the solution is actually very simple; you just need to use a little math.

Convert ContentAlignment to StringAlignment

StringFormat cFormat;
Int32 lNum =  (Int32)Math.Log((Double)this.TextAlign, 2);
cFormat.LineAlignment = (StringAlignment)(lNum / 4);
cFormat.Alignment = (StringAlignment)(lNum % 4);

...you then just use the cFormat object in your DrawString.

That's it! Simple really, isn't it?

  • Like 1

3 answers to this question

Recommended Posts

  • 0

DrawString is part of the Graphics namespace and is used when drawing strings on to any graphics object. In my particular case, an owner drawn WinForms button. It's not really anything to do directly with XNA at all, it's GDI territory.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Microsoft 365 will soon disable outdated authentication protocols for file access by Usama Jawad On a fairly regular basis, Microsoft disables outdated protocols that are used to access its services. In the past few years, the company has deprecated Basic Auth in Exchange Online and cut access to Outlook for third-party apps relying on this protocol. Now, it has decided to get rid of old authentication protocols for file access across Microsoft 365 services. As reported by Bleeping Computer, Microsoft has posted a message on its Microsoft 365 Admin Center. Starting from mid-July 2025, the company will begin disabling legacy authentication protocols used to access files across Microsoft 365 and Office apps, SharePoint, and OneDrive. Essentially, applications or services which use the Relying Party Suite (RPS) or FrontPage Remote Procedure Call (FPRPC) will to perform browser-based authentication to perform open operations on Office files will no longer be able to do so. As expected, this is primarily being done to improve the cybersecurity posture of various services. Microsoft states that RPS can be brute-forced and phished with relative ease as it is fairly outdated. Similarly, FPRPC is typically used for remote web page authoring and it is susceptible to exploitation through various vulnerabilities too. As such, both of these protocols will be disabled by default starting from mid-July 2025, with the rollout of this change targeting completion by August 2025. The Redmond tech giant will update the protocol baseline by default without mandating any licensing changes for customers. In addition, once these modifications are rolled out, Microsoft 365 will require admin consent to get third-party access to files and sites. IT admins can view the guidance available here to configure admin consent workflows. Microsoft says that these changes align with the principles of its Secure Future Initiative (SFI). Earlier today, it announced the rollout of improved security defaults for Windows 365 citing the same reasons too.
    • It does and it can... I took an i3 board and upgraded it to my FX8350... no issues, just put in new drivers over the top that Windows didn't. Not the issue for me, (though I eventually did do a new install from 23H2 to 24H2)... I was on 22H2 at the time. The issue is activation. You may get hit with having to activate again.
  • Recent Achievements

    • First Post
      TIGOSS earned a badge
      First Post
    • Week One Done
      slackerzz earned a badge
      Week One Done
    • Week One Done
      vivetool earned a badge
      Week One Done
    • Reacting Well
      pnajbar earned a badge
      Reacting Well
    • Week One Done
      TBithoney earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      709
    2. 2
      ATLien_0
      284
    3. 3
      Michael Scrip
      220
    4. 4
      +FloatingFatMan
      199
    5. 5
      Steven P.
      131
  • Tell a friend

    Love Neowin? Tell a friend!