C++ 我是否可以动态更改使用C+;创建的对话框窗口的字体大小+;在VisualStudio中?
比方说,如果我使用Visual Studio从C++/MFC项目中的资源创建对话框窗口,我可以从资源编辑器更改对话框的字体和大小。我的问题是如何从程序中执行相同的操作 以下是几个屏幕截图: 常规尺寸: 14号:C++ 我是否可以动态更改使用C+;创建的对话框窗口的字体大小+;在VisualStudio中?,c++,visual-studio,fonts,dialog,font-size,C++,Visual Studio,Fonts,Dialog,Font Size,比方说,如果我使用Visual Studio从C++/MFC项目中的资源创建对话框窗口,我可以从资源编辑器更改对话框的字体和大小。我的问题是如何从程序中执行相同的操作 以下是几个屏幕截图: 常规尺寸: 14号: 另外,我可以想象,一旦创建了对话框窗口,就无法更改字体大小,但是在创建之前呢?没有测试过这一点,但似乎可以使用WinAPISendDlgItemMessage: hFont = // obtain handle to a font object SendDlgItemMessage(
另外,我可以想象,一旦创建了对话框窗口,就无法更改字体大小,但是在创建之前呢?没有测试过这一点,但似乎可以使用WinAPI
SendDlgItemMessage
:
hFont = // obtain handle to a font object
SendDlgItemMessage(hwnd, IDC_OF_YOUR_CONTROL, WM_SETFONT, (WPARAM)hfFont, TRUE);
用拨号键的标识符替换控件的IDC。创建12pt“Times New Roman”字体的示例代码:
资料来源:
编辑:我不知道这些WinAPI调用是如何映射到MFC的,但可能有类似的方法可用。正如我在评论中所指出的,您要做的并不是特别容易。有很多角落里的箱子和小东西会回来咬你 但是,如果您想尝试,可以使用更改对话框子窗口的字体。只需构造要使用的字体,将其分配给相应的控件,然后单击tada
祝你好运…哇,我不知道事情这么复杂。下面是我提出的改变字体大小和字体的解决方案。它适用于任何对话框,无需调整单个对话框控件的大小: 对于MFC项目:
//Header .h file
static INT_PTR OpenDialogWithFont(CWnd* pParentWnd, LPCTSTR lpszResourceID, LPCTSTR pstrFontFaceName = NULL, WORD wFontPtSz = 0, BOOL* pbOutResultFontApplied = NULL);
static BYTE* AdvanceThrough_sz_Or_Ord(BYTE* pData);
static BYTE* AdvanceThrough_String(BYTE* pData, CString* pOutStr = NULL);
然后,实施本身:
INT_PTR OpenDialogWithFont(CWnd* pParentWnd, LPCTSTR lpszResourceID, LPCTSTR pstrFontFaceName, WORD wFontPtSz, BOOL* pbOutResultFontApplied)
{
//Open dialog box with the 'lpszResourceID'
//'pParentWnd' = parent window class
//'pstrFontFaceName' = Font face name to use, or NULL to use original font
//'wFontPtSz' = point size of the font, or 0 to use original font size
//'pbOutResultFontApplied' = if not NULL, receives TRUE if font was applied, or FALSE if dialog was shown with original font
//RETURN:
// = One of the values returned by CDialog::DoModal
INT_PTR nResDlg = -1;
BOOL bAppliedFont = FALSE;
BYTE* pCNewData = NULL;
LPCTSTR m_lpszTemplateName = MAKEINTRESOURCE(lpszResourceID);
HINSTANCE hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
if(hInst)
{
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
HGLOBAL hDialogTemplate = LoadResource(hInst, hResource);
if(hDialogTemplate)
{
LPCDLGTEMPLATE lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
DWORD dwszDialogTemplate = SizeofResource(hInst, hResource);
if(lpDialogTemplate &&
dwszDialogTemplate)
{
//Template to use
LPCDLGTEMPLATE lpDialogTemplateToUse = lpDialogTemplate;
//See if it's an extended dialog structure
DLGTEMPLATEEX_PART1* pDTX1 = (DLGTEMPLATEEX_PART1*)lpDialogTemplate;
if(pDTX1->signature == 0xFFFF &&
pDTX1->dlgVer == 1)
{
//Now get thru variable length elements
BYTE* pData = (BYTE*)(pDTX1 + 1);
//sz_Or_Ord menu;
pData = AdvanceThrough_sz_Or_Ord(pData);
//sz_Or_Ord windowClass;
pData = AdvanceThrough_sz_Or_Ord(pData);
//title
CString strTitle;
pData = AdvanceThrough_String(pData, &strTitle);
//Now pointsize of the font
//This member is present only if the style member specifies DS_SETFONT or DS_SHELLFONT.
if(pDTX1->style & (DS_SETFONT | DS_SHELLFONT))
{
//Font size in pts
BYTE* pPtr_FontSize = pData;
WORD ptFontSize = *(WORD*)pData;
pData += sizeof(WORD);
WORD wFontWeight = *(WORD*)pData;
pData += sizeof(WORD);
BYTE italic = *(BYTE*)pData;
pData += sizeof(BYTE);
BYTE charset = *(BYTE*)pData;
pData += sizeof(BYTE);
//Font face name
CString strFontFaceName;
BYTE* pPtr_FontFaceName = pData;
pData = AdvanceThrough_String(pData, &strFontFaceName);
//Remember the end of the struct (that we care about)
BYTE* pPtr_EndStruct = pData;
//Get size of the end data chunk
int ncbszEndChunk = dwszDialogTemplate - (pPtr_EndStruct - (BYTE*)lpDialogTemplate);
if(ncbszEndChunk >= 0)
{
//Now we can modify the struct
//Get new type face name (or use the old one)
CString strNewFontFaceName = pstrFontFaceName ? pstrFontFaceName : strFontFaceName;
//Calculate the new struct size
int ncbSzNewData = dwszDialogTemplate -
strFontFaceName.GetLength() * sizeof(WCHAR) +
strNewFontFaceName.GetLength() * sizeof(WCHAR);
//Reserve mem
pCNewData = new BYTE[ncbSzNewData];
if(pCNewData)
{
BYTE* pNewData = pCNewData;
//Copy in chunks
memcpy(pNewData, lpDialogTemplate, pPtr_FontFaceName - (BYTE*)lpDialogTemplate);
pNewData += pPtr_FontFaceName - (BYTE*)lpDialogTemplate;
//Then put our font face name
memcpy(pNewData, strNewFontFaceName.GetString(), (strNewFontFaceName.GetLength() + 1) * sizeof(WCHAR));
pNewData += (strNewFontFaceName.GetLength() + 1) * sizeof(WCHAR);
//And add the ending chunk
memcpy(pNewData, pPtr_EndStruct, ncbszEndChunk);
pNewData += ncbszEndChunk;
//Check memory allocation
if(pNewData - pCNewData == ncbSzNewData)
{
//Are we setting the font size?
if(wFontPtSz != 0)
{
WORD* pwFontSz = (WORD*)(pCNewData + (pPtr_FontSize - (BYTE*)lpDialogTemplate));
if(*pwFontSz != wFontPtSz)
{
//Set flag
bAppliedFont = TRUE;
}
//Set new font size
*pwFontSz = wFontPtSz;
}
//Did we have a specified font face too
if(pstrFontFaceName)
bAppliedFont = TRUE;
//Use our adjusted template
lpDialogTemplateToUse = (LPCDLGTEMPLATE)pCNewData;
}
else
{
ASSERT(NULL);
}
}
}
}
}
//Try to load it from the template
CDialog abt;
if(abt.InitModalIndirect(lpDialogTemplateToUse, pParentWnd))
{
//And show the modal dialog
nResDlg = abt.DoModal();
}
}
}
}
//Free memory
if(pCNewData)
{
delete[] pCNewData;
pCNewData = NULL;
}
if(pbOutResultFontApplied)
*pbOutResultFontApplied = bAppliedFont;
return nResDlg;
}
自定义结构定义:
#pragma pack(push, 1) // exact fit - no padding
struct DLGTEMPLATEEX_PART1{
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
short x;
short y;
short cx;
short cy;
};
#pragma pack(pop)
以下是解析可变大小成员的辅助方法:
BYTE* AdvanceThrough_sz_Or_Ord(BYTE* pData)
{
//'pData' = Points to a variable-length array of 16-bit elements that identifies a menu
// resource for the dialog box. If the first element of this array is 0x0000,
// the dialog box has no menu and the array has no other elements. If the first
// element is 0xFFFF, the array has one additional element that specifies the
// ordinal value of a menu resource in an executable file. If the first element
// has any other value, the system treats the array as a null-terminated Unicode
// string that specifies the name of a menu resource in an executable file.
//RETURN:
// = Following address
ASSERT(pData);
WORD* pWArr = (WORD*)pData;
if(*pWArr == 0x0000)
{
//No other elements
pWArr++;
}
else if(*pWArr == 0xFFFF)
{
//Next element is menu ID
pWArr++;
pWArr++;
}
else
{
//Unicode ASIIZ string
WCHAR z;
do
{
z = *pWArr;
pWArr++;
}
while(z != 0);
}
return (BYTE*)pWArr;
}
BYTE* AdvanceThrough_String(BYTE* pData, CString* pOutStr)
{
//'pData' = Points to null-terminated Unicode string
//'pOutStr' = if not NULL, receives the string scanned
//RETURN:
// = Pointer to the first BYTE after the string
ASSERT(pData);
WCHAR* pWStr = (WCHAR*)pData;
WCHAR z;
do
{
z = *pWStr;
pWStr++;
}
while(z != 0);
if(pOutStr)
{
int nLn = pWStr - (WCHAR*)pData;
memcpy(pOutStr->GetBufferSetLength(nLn), pData, nLn * sizeof(WCHAR));
pOutStr->ReleaseBuffer();
}
return (BYTE*)pWStr;
}
下面是您如何称呼它的:
BOOL bResAppliedFontCorrection;
int nResultDlg = OpenDialogWithFont(this,
MAKEINTRESOURCE(IDD_ABOUTBOX),
_T("Algerian"),
16,
&bResAppliedFontCorrection);
对于那些对其工作方式感兴趣的人,该方法会在创建对话框之前修改,从而让操作系统执行所有字体操作。为什么要这样做?你到底想达到什么目的?@NikBougalis:让最终用户控制放大或缩小窗口。有时窗口对于最终用户的显示大小来说太大,所以我可以自动缩小字体。还有很多其他用途。。。我很惊讶你居然会问这个问题。你将很难通过对话来实现这一点。让所有的控制装置重新通电并不是什么大问题。但是,计算适当的字体大小,这样文本就不会被剪裁和对齐。如果你想要这种类型的“回流”用户界面,那么对话框就不是你想要的。这将在每个控件的基础上工作,并且将是-*ss应用于不同对话框的一大难题。只是好奇,为什么不为每个子控件应用此字体?这不是更容易吗?@AnishRam:如你所说,这样做将涉及调整所有控件的大小和重新定位,以及由于字体大小的变化而导致的父窗口本身。这样做将是一件非常头痛的事。
BOOL bResAppliedFontCorrection;
int nResultDlg = OpenDialogWithFont(this,
MAKEINTRESOURCE(IDD_ABOUTBOX),
_T("Algerian"),
16,
&bResAppliedFontCorrection);