none
Vc++ how to convert screen coordinates into word coordinates. RRS feed

  • Question

  • Vc++ how to convert screen coordinates into word coordinates.Use ms word to operate word2016.

    How does xPos compute the wordX?

    How does yPos compute the wordY?

    The image on the screen is inserted into word, and the position is very large.BOOL CWordOperate::AddPicture(const float xPos, const float yPos, const CString &filePath)

    {
    RECT * r = CWordOperate::getWordRect();
    //return 0;
    //SetWordMacro(0, 0,0,0,filePath);
    //return true;
    //std::ofstream outfile; 
    //outfile.open("2.txt", ios::app);    
    CLSID   clsid;
    if (FAILED(::CLSIDFromProgID(L"Word.Application", &clsid)))//Word.Application
    return FALSE;

    IUnknown  *pUnknown  = NULL;
    IDispatch *pDispatch = NULL;
    /*Word::*/_Application app = NULL;
    /*Word::*/Selection    sel = NULL;
    /*Word::*/Documents docs;
    /*Word::*/_Document doc;
    if (FAILED(::GetActiveObject(clsid, NULL, &pUnknown)))
    {
    return FALSE;
    }
    try
    {
    if (FAILED(pUnknown->QueryInterface(IID_IDispatch, (LPVOID *)&app)))
    throw(_T("No IDispatchPtr"));

    pUnknown->Release();
    pUnknown = NULL;
    sel = app.GetSelection();

    if (!sel)
    throw(_T("No Edited Document"));
    }
    catch(_com_error lpErr)
    {
    if(pUnknown != NULL)
    pUnknown->Release();
    if(sel)
    sel.ReleaseDispatch();
    if(app)
    app.ReleaseDispatch();

    return FALSE;
    }

    COleVariant covTrue((short)TRUE), 
    covFalse((short)FALSE), 
    covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

    float wordXpos = 0, wordYpos = 0;
    doc = app.GetActiveDocument();
    /**********************************************************************************************
    ***********************************************************************************************/
    wordXpos = (xPos - 514) / 1.4 + 5;
    wordYpos = (yPos - 270) / 1.344 ;//+ 1;
    //outfile <<"wX=" << wordXpos << "wY = " << wordYpos;

    /*Shapes */
    /*Word::*/Shapes shaps = doc.GetShapes();
    Window win = app.GetActiveWindow();
    Shape shap = shaps.AddPicture(filePath, covTrue, covTrue, &COleVariant((float)10), &COleVariant((float)10), covOptional, covOptional, covOptional);
    long ScrWidth =0,ScrPosHeight = 0, minx,miny;
    win.GetPoint((long*)&minx, (long*)&miny, &ScrWidth,&ScrPosHeight,shap); 

    shap = shaps.AddPicture(filePath, covTrue, covTrue, &COleVariant((float)20), &COleVariant((float)20), covOptional, covOptional, covOptional);
    win.GetPoint((long*)&minx, (long*)&miny, &ScrWidth,&ScrPosHeight,shap); 


    //outfile.close();
    if(pUnknown != NULL)
    pUnknown->Release();
    if(sel)
    sel.ReleaseDispatch();
    if(app)
    app.ReleaseDispatch();
    return TRUE;
    }

    Friday, April 27, 2018 8:21 AM

All replies

  • Hello 油炸的兔子,

    Why do you need convert screen coordinates into word coordinates? What's their relationship? Do you want to convert screen length unit to a word length unit? 

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Saturday, April 28, 2018 4:20 AM