如何在Rectangle中绘制文本时修剪文本?

时间:2017-05-16 13:22:04

标签: c# winforms graphics drawing

我尝试使用下面的代码

在矩形内绘制文本
e.Graphics.DrawString(“1234562.021”, drawFont, drawBrush, drawRect);

以下是

enter image description here

案文的权利已隐藏。但是当文本宽度超过矩形时,我需要隐藏文本的左侧,如下所示

enter image description here

请提供任何建议。

感谢Adavnce,

2 个答案:

答案 0 :(得分:2)

您应该使用TextRenderer绘制屏幕上的任何文本。您可以使用标记来完成对齐问题:

using (Pen bigPen = new Pen(Color.Black, 3)) {
  e.Graphics.DrawRectangle(bigPen, drawRect);
}
using (Font drawFont = new Font(SystemFonts.DefaultFont, FontStyle.Bold)) {
  TextRenderer.DrawText(e.Graphics, "1234562.021", drawFont, drawRect,
                        Color.Black, Color.Empty,
                        TextFormatFlags.Right | TextFormatFlags.VerticalCenter);
}

结果:

enter image description here

答案 1 :(得分:1)

您可以从头开始删除字符,而当前字符串的大小不适合矩形宽度。

试试这个:

StringFormater

它适用于我。

您是否尝试使用正确对齐的.DrawString并将其传递到import sys import pygame from pygame.locals import * pygame.init() window = pygame.display.set_mode((700,700)) window.fill((25, 25, 25)) grille=pygame.image.load('Tic-tac-toe.png') window.blit(grille,(50,50)) XX = pygame.Surface((150, 150), pygame.SRCALPHA) pygame.draw.line(XX, (255, 255, 255), (0, 0), (150,150), 5) pygame.draw.line(XX, (255, 255, 255), (150, 0), (0, 150), 5) o = pygame.Surface((150, 150), pygame.SRCALPHA) pygame.draw.circle(o, (255, 255 , 0), (75, 75), 75, 5) def pvploop(): GRID = [0,0,0,0,0,0,0,0,0] gagner =True player = 0 clock = pygame.time.Clock() jouer = True value=1 while jouer and not gagner: gameDisplay.fill(25,25,25) message_to_screen("Game over, press C to play again or Q to quit",) pygame.display.update() for event in pygame.event.get(): if event.type==pygame.QUIT: jouer = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: jouer = False if event.key == pygame.K_c: pvploop() while jouer and gagner: for event in pygame.event.get(): if event.type == pygame.QUIT: jouer = False elif GRID[0]==2 and GRID[1]==2 and GRID[2]==2: print('O a gagne') gagner =False elif GRID[3]==2 and GRID[4]==2 and GRID[5]==2: print('O a gagne') elif GRID[6]==2 and GRID[7]==2 and GRID[8]==2: print('O a gagne') elif GRID[0]==2 and GRID[3]==2 and GRID[6]==2: print('O a gagne') elif GRID[1]==2 and GRID[4]==2 and GRID[7]==2: print('O a gagne') elif GRID[2]==2 and GRID[5]==2 and GRID[8]==2: print('O a gagne') elif GRID[0]==2 and GRID[4]==2 and GRID[8]==2: print('O a gagne') elif GRID[2]==2 and GRID[4]==2 and GRID[6]==2: print('O a gagne') elif GRID[0]==2 and GRID[3]==2 and GRID[6]==2: print('O a gagne') elif GRID[0]==1 and GRID[1]==1 and GRID[2]==1: print (' X a gagne') elif GRID[3]==1 and GRID[4]==1 and GRID[0]==1: print (' X a gagne') elif GRID[6]==1 and GRID[7]==1 and GRID[8]==1: print (' X a gagne') elif GRID[0]==1 and GRID[1]==1 and GRID[2]==1: print (' X a gagne') elif GRID[3]==1 and GRID[4]==1 and GRID[5]==1: print (' X a gagne') elif GRID[6]==1 and GRID[7]==1 and GRID[8]==1: print (' X a gagne') elif GRID[0]==1 and GRID[3]==1 and GRID[6]==1: print (' X a gagne') elif GRID[1]==1 and GRID[4]==1 and GRID[7]==1: print (' X a gagne') elif GRID[2]==1 and GRID[5]==1 and GRID[8]==1: print (' X a gagne') elif GRID[3]==1 and GRID[6]==1 and GRID[0]==1: print (' X a gagne') elif GRID[4]==1 and GRID[7]==1 and GRID[1]==1: print (' X a gagne') elif GRID[5]==1 and GRID[8]==1 and GRID[2]==1: print (' X a gagne') elif GRID[0]==1 and GRID[4]==1 and GRID[8]==1: print (' X a gagne') elif GRID[2]==1 and GRID[4]==1 and GRID[6]==1: print (' X a gagne') elif GRID[4]==1 and GRID[8]==1 and GRID[0]==1: print (' X a gagne') elif GRID[4]==1 and GRID[6]==1 and GRID[2]==1: print (' X a gagne') elif GRID[0]==1 and GRID[8]==1 and GRID[4]==1: print (' X a gagne') elif GRID[2]==1 and GRID[6]==1 and GRID[4]==1: print (' X a gagne') elif GRID[0]==1 and GRID[1]==1 and GRID[2]==1: print (' X a gagne') elif GRID[3]==1 and GRID[4]==1 and GRID[5]==1: print (' X a gagne') elif GRID[6]==1 and GRID[7]==1 and GRID[8]==1: print (' X a gagne') elif GRID[0]==1 and GRID[3]==1 and GRID[6]==1: print (' X a gagne') elif GRID[1]==1 and GRID[4]==1 and GRID[7]==1: print (' X a gagne') elif GRID[2]==1 and GRID[5]==1 and GRID[8]==1: print (' X a gagne') elif event.type == MOUSEBUTTONDOWN: if player == 0: image = XX else: image = o x, y = pygame.mouse.get_pos() if x<232: if y<234 and GRID[0] == 0 : window.blit(image,(75,75)) player += 1 GRID[0] = value elif 234<y<425 and GRID[3]==0: window.blit(image,(75,275)) player += 1 GRID[3] = value elif y>425 and GRID[6] == 0 : window.blit(image,(75,468)) player += 1 GRID[6] = value elif 232<x<419: if y<234 and GRID[1] == 0 : window.blit(image,(275,75)) player += 1 GRID[1] = value elif 234<y<425 and GRID[4] == 0 : window.blit(image,(275,275)) player += 1 GRID[4] = value elif y>425 and GRID[7] == 0 : window.blit(image,(275,468)) player += 1 GRID[7] = value elif x>419: if y<234 and GRID[2] == 0 : window.blit(image,(465,75)) player += 1 GRID[2] = value elif 234<y<425 and GRID[5]==0: window.blit(image,(465,275)) player += 1 GRID[5] = value elif y>425 and GRID[8] == 0 : window.blit(image,(465,468)) player += 1 GRID[8] = value player %= 2 value=player+1 print GRID pygame.display.flip() clock.tick(30) pygame.quit() sys.exit() 的末尾?