WinForms MenuStrip에서 메뉴 항목 텍스트와 바로 가기 키 사이의 간격을 늘리는 간단한 방법이 있습니까? 아래에서 보는 바와 같이, VS 의해 생성 된 경우에도 기본 템플릿은 다른 항목의 바로 가기 키를 넘어 확장 텍스트 "인쇄 미리보기"로, 나쁜 같습니다MenuStrip 바로 가기 키 간격
나는 약간의 간격을 가지고 할 수있는 방법을 찾고 있어요 가장 긴 메뉴 항목과 바로 가기 키 여백의 시작 사이.
WinForms MenuStrip에서 메뉴 항목 텍스트와 바로 가기 키 사이의 간격을 늘리는 간단한 방법이 있습니까? 아래에서 보는 바와 같이, VS 의해 생성 된 경우에도 기본 템플릿은 다른 항목의 바로 가기 키를 넘어 확장 텍스트 "인쇄 미리보기"로, 나쁜 같습니다MenuStrip 바로 가기 키 간격
나는 약간의 간격을 가지고 할 수있는 방법을 찾고 있어요 가장 긴 메뉴 항목과 바로 가기 키 여백의 시작 사이.
쉬운 방법은 더 짧은 메뉴 항목의 간격을 좁히는 것입니다. 예를 들어, 패드는 "새로운"메뉴 항목의 Text 속성은 마지막 여분의 공간을 가지고 있으며, 그래서 " 새로운 "그 동안 추진 할 예정입니다 바로 가기.
업데이트
나는 코드에서이 문제를 자동화하는 당신을 도울 제안.
나는 당신이 당신의 메뉴 스트립에서 모든 메인 메뉴 항목을 통해 이동하고 모든 메뉴 항목의 크기를 조정할 것을 호출 할 수있는 다음과 같은 코드를 작성 : 여기에 코드를시키는의 결과는 당신을위한 작업 않습니다이다 :
// put in your ctor or OnLoad
// Note: the actual name of your MenuStrip may be different than mine
// go through each of the main menu items
foreach (var item in menuStrip1.Items)
{
if (item is ToolStripMenuItem)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)item;
ResizeMenuItems(menuItem.DropDownItems);
}
}
그리고 이러한 일을 할 방법은 다음과 같습니다
private void ResizeMenuItems(ToolStripItemCollection items)
{
// find the menu item that has the longest width
int max = 0;
foreach (var item in items)
{
// only look at menu items and ignore seperators, etc.
if (item is ToolStripMenuItem)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)item;
// get the size of the menu item text
Size sz = TextRenderer.MeasureText(menuItem.Text, menuItem.Font);
// keep the longest string
max = sz.Width > max ? sz.Width : max;
}
}
// go through the menu items and make them about the same length
foreach (var item in items)
{
if (item is ToolStripMenuItem)
{
ToolStripMenuItem menuItem = (ToolStripMenuItem)item;
menuItem.Text = PadStringToLength(menuItem.Text, menuItem.Font, max);
}
}
}
private string PadStringToLength(string source, Font font, int width)
{
// keep padding the right with spaces until we reach the proper length
string newText = source;
while (TextRenderer.MeasureText(newText, font).Width < width)
{
newText = newText.PadRight(newText.Length + 1);
}
return newText;
}
이 가진 문제는이 requ에 공간의 수를 결정하기 어려운 점이다
고정 폭 글꼴이 아닌 문자로 채워지는 것은 문자열의 길이만큼 쉽지 않습니다. 그럼에도 불구하고 +1. – casablanca
내 업데이트 @casablanca를 확인하십시오. –
감사합니다. 나는이 대답을 받아들입니다. 실제로'MeasureText'를 사용하여 끝내기를 원하지는 않았지만 더 좋은 방법을 찾을 수 없었습니다. – casablanca