2014-11-03 3 views
1

가능한 한 실제 화면 크기에 관계없이
위의 두 숫자를 서로 표시하려면
으로 표시하고 싶습니다. (게다가 clumsyness)이 코드레이블 텍스트가 너무 낮게 표시됩니다.

 //getting screen size and setting window to maximized 
     Rectangle screenEdge = Screen.PrimaryScreen.Bounds; 
     this.Width = screenEdge.Width; 
     this.Height = screenEdge.Height; 
     this.FormBorderStyle = FormBorderStyle.FixedToolWindow; 
     this.WindowState = FormWindowState.Maximized; 

     //using 90% of width and 40% (times two) of height 
     int lWidth = (int)(this.Width * 0.9); 
     int lHeight = (int)(this.Height * 0.4); 

     //horiz. spacing: remainder, divided up for left and right 
     int lLeft = (this.Width - lWidth)/2; 
     //vert. spacing: remainder divided for top, bottom and between 
     int lTop = (this.Height - (2 * lHeight))/3 ; 

     //the labels holding the numbers 
     lSoll = new Label(); 
     lIst = new Label(); 

     //setting label lSoll to calc'd dimensions, adding & aligning text 
     lSoll.Left = lLeft; 
     lSoll.Width = lWidth; 
     lSoll.Top = lTop; 
     lSoll.Height = lHeight; 
     Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height); 
     Font sFSized = new Font(sollFont.FontFamily, lSoll.Height); 
     lSoll.Font = sFSized; 
     lSoll.TextAlign = ContentAlignment.MiddleCenter; 
     lSoll.ForeColor = Color.Blue; 
     lSoll.BackColor = Color.White; 
     updateSollText(42); 

     //same as above, just a bit lower 
     lIst.Left = lLeft; 
     lIst.Width = lWidth; 
     lIst.Top = lTop * 2 + lSoll.Height; 
     lIst.Height = lHeight; 
     Font istFont = new Font(FontFamily.GenericSansSerif, lIst.Height); 
     Font iFSized = new Font(istFont.FontFamily, lIst.Height); 
     lIst.Font = iFSized; 
     lIst.TextAlign = ContentAlignment.TopCenter; 
     lIst.ForeColor = Color.Red; 
     lIst.BackColor = Color.White; 
     updateIstText(39); 

문제 : 라벨
텍스트 부분적 즉 보이지 레이블의 하한, 아래에 표시되고, 바닥에서 스크린을 참조.
나는 내 계산을 두 번 확인했는데 1 포인트 (반올림)의 반올림 오류 이외에는 모두 작동해야한다는 것을 알았습니다.
필자는 글꼴 높이를 레이블 높이보다 작게 만들려고했지만 약간은 도움이되었지만 확실히 수정되지는 않았습니다.
사실 내가 textalign이 그것을 커버해야하지만, 그것이 그것이 무엇을 위해 있기 때문에 사실.
또한 왼쪽 반면, 아무것도 변경하지 않은 textAlign과의 높이 완 (낮은 중간 위) chaning/중앙/오른쪽 차이점이 원인이 될 수 무엇

Screen of misaligned numbers

예상 어떻게해야합니까?

+1

레이블에는 다른 컨트롤과 겹치지 않도록 항상 가장자리와 글꼴 사이에 약간의 패딩이 있습니다. 코드에서 글꼴 크기를 레이블의 높이로 설정하면 마녀는 결코 적합하지 않음을 의미합니다. 글꼴을 돌려서 글꼴의 높이를 계산하고 레이블의 autoEllipsis 속성을 true로 설정하면 텍스트에 자동 크기 조절이 적용되지 않는 이유는 무엇입니까? 서식의 전체 너비를 포함 시키려면 레이블의 너비를 직접 변경해야합니다. –

답변

3

글꼴의 기본 측정 단위는 픽셀이 아니라 점입니다. 예를 들어 기본 DPI 설정이 96 일 경우 9 포인트 글꼴은 9 * 96/72 = 12 픽셀을 차지합니다. 그래서 당신이 요구하는 글꼴이 너무 크고 적합하지 않습니다.

해결 방법은 간단합니다. GraphicsUnit 인수를 사용하는 Font 생성자 오버로드로 원하는 측정 단위를 지정할 수 있습니다. 수정 :

Font sollFont = new Font(FontFamily.GenericSansSerif, lSoll.Height, GraphicsUnit.Pixel); 
Font sFSized = new Font(sollFont.FontFamily, lSoll.Height, GraphicsUnit.Pixel); 
+0

72가 어디서 왔는지 궁금 해서요. 그 다음에는 인치 당 72 포인트를 찾았습니다. (http://stackoverflow.com/questions/139655/convert-pixels-to-points) – Jeb

+0

완벽하게 작동합니다! 감사. – Mark

관련 문제