How to declare a Font variable properly? [duplicate]

与世无争的帅哥 提交于 2019-12-02 21:59:56

问题


I would have a quick question : Could anyone tell me what is wrong with this line :

Font ^printFont = gcnew System::Drawing::Font("Arial", 10);

My compiler says "Identifier 'printFont' is unidentified". I also have the namespaces and the dll file included :

 #using <System.Drawing.dll>
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Drawing::Text;
    using namespace System::Drawing::Printing;

PS. Sorry for not pro coding, but I did not take any University level programming in C++/CLR.

Edited :

private: System::Void testCorrection_PrintPage_1(System::Object^  sender, System::Drawing::Printing::PrintPageEventArgs^  e) {

float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = (float)e->MarginBounds.Left;
float topMargin = (float)e->MarginBounds.Top;
String^ line = nullptr; 

Font ^printFont = gcnew System::Drawing::Font("Arial", 10); // error is :" IntelliSense:identifier 'PrintFont' is undefined "
SolidBrush ^myBrush = gcnew SolidBrush(Color::Black);

StreamReader^ streamToPrint;

// Calculate the number of lines per page.
linesPerPage = e->MarginBounds.Height / printFont->GetHeight(e->Graphics);
line = streamToPrint->ReadLine();

// Iterate over the file, printing each line.
while (count < linesPerPage && ((line = streamToPrint->ReadLine()) != nullptr)) {
        yPos = topMargin + (count * printFont->GetHeight(e->Graphics));
        e->Graphics->DrawString(line, printFont, myBrush, leftMargin, yPos, gcnew StringFormat);
        count++;
        line = streamToPrint->ReadLine();
}
}

回答1:


As you have directive

using namespace System::Drawing;

then you can write simply

Font ^printFont = gcnew Font("Arial", 10);

I think the problem is that you did not add reference to System.Drawing to the project



来源:https://stackoverflow.com/questions/23233993/how-to-declare-a-font-variable-properly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!