How to Use MFC Grid control 2.27

Programming/MFCGridCtrl 2016. 8. 12. 16:13 Posted by TanSanC
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

How to Use MFC Grid control 2.27


http://www.codeproject.com/Articles/8/MFC-Grid-control-2-27

1. Download


MFC Grid control 2.27

6 May 2010 CPOL
A fully featured MFC grid control for displaying tabular data. The grid is a custom control derived from CWnd





2. Essential Include Files



Files

To use the Grid control in your project you will need to add a number of files to your project:

gridctrl.cpp, gridctrl.hMain grid control source and header files.
gridcellbase.cpp, gridcellbase.hMain grid cell base class.
gridcell.cpp, gridcell.hMain grid cell default class implementation.
CellRange.hDefinition of CCellID and CCellRange helper classes.
MemDC.hKeith Rule's memory DC helper class.
InPlaceEdit.cpp, InPlaceEdit.hIn-place edit windows source and header files.
GridDropTarget.cpp, GridDropTarget.hGrid control OLE drag and drop target. Only necessary if you don't define GRIDCONTROL_NO_DRAGDROP in gridctrl.h
Titletip.cpp, Titletip.h

Titletips for cells, from Zafir Anjum. Only necessary if you don't define GRIDCONTROL_NO_TITLETIPS in gridctrl.h



3. Essential Include Files Add to Project 







4. Custom Control Add to Dialog




ID : IDC_GRID


Class : MFCGridCtrl (No CGridCtrl)


if you use CGridCtrl, you can see this error








5. Make Member Variable m_Grid



Dlg.h


- Include


#include "GridCtrl.h"


Make Member Variable m_Grid


CGridCtrl m_Grid;




Dlg.cpp


- Include


#include "GridCtrl.h"





6. IDC_GRID Connect with Member Variable




DDX_GridControl(pDX, IDC_GRID, m_Grid);


DDX_GridControl Function defined in GridCtrl.h









336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

C# Class Library


VS2013


새 프로젝트 -> Visual C# -> 클래스 라이브러리 -> 프로젝트 이름(Library 이름이 된다, 예제에서는 ClassLibrary2 이다.) -> 


자동으로 Class1.cs 파일이 만들어진다.


이 Class1.cs 가 DLL 에서 불러쓸 객체의 이름이 된다.



참고 : https://support.microsoft.com/en-us/kb/828736




Class1.cs 를 우클릭하여 이름바꾸기 원하는 객체 이름으로 바꾼다.




예제에서는 SimpleCalc 로 진행한다.



----------------------------------------------------------------


SimpleCalc.cs


----------------------------------------------------------------


namespace ClassLibrary2

{

    public class SimpleCalc

    {

    }

}


----------------------------------------------------------------



interface 를 추가 구현한다.

// Interface declaration.
public interface ICalculator
{
    int Add(int Number1, int Number2);
};



----------------------------------------------------------------


SimpleCalc.cs


----------------------------------------------------------------


namespace ClassLibrary2

{

    // Interface declaration.

    public interface ICalculator

    {

        int Add(int Number1, int Number2);

    };

    public class SimpleCalc

    {

    }

}



----------------------------------------------------------------


interface를 상속 받고

interface 로 부터 상속받은 메서드를 구현한다.


// Interface implementation.
public class ManagedClass:ICalculator
{
    public int Add(int Number1,int Number2)
        {
            return Number1+Number2;
        }
}




----------------------------------------------------------------


SimpleCalc.cs


----------------------------------------------------------------


namespace ClassLibrary2

{

    // Interface declaration.

    public interface ICalculator

    {

        int Add(int Number1, int Number2);

    };

    public class SimpleCalc : ICalculator

    {

        public int Add(int Number1, int Number2)

        {

            return Number1 + Number2;

        }

    }

}




----------------------------------------------------------------


이제


Properties 를 열어서


AssemblyInfo.cs 를 열어보면


[assembly: ComVisible(false)] 를 찾아서



[assembly: ComVisible(true)]


로 변경한다.



[assembly: ComVisible(true)] 로 바꾸어야 ActiveX 로 사용이 가능하다.





이제 빌드하면


bin/Debug 폴더에


ClassLibrary2.dll 파일이 만들어진다.



궁극적으로 배포를 목적으로 하므로


Release 모드로 바꾸어준다.




빌드를 하게 되면


ClassLibrary2.dll 파일이 만들어진다.



하지만 클래스 내부의 구조가 보이게 하려면


tlb 파일이 필요하다


이 파일은


ClassLibrary2 프로젝트이름을 더블클릭하면 뜨는 속성 창에서


[빌드]


COM Interop 등록 을 체크해준다.


그러면 이제부터 빌드를 하면 tlb 파일도 같이 만들어진다.





tlb 파일과 dll 파일을 C# DLL 을 사용할 프로젝트의 폴더로 복사한다.




예제에서는 MFC 에서 해당 DLL 파일을 불러서 사용해보겠다.



사용할 cpp 파일에서



#import "ClassLibrary2.tlb" raw_interfaces_only

using namespace ClassLibrary2;


를 선언한다.





그 후 사용을 원하는 함수에서




HRESULT hr = CoInitialize(NULL);


// Create the interface pointer.

ICalculatorPtr pICalc(__uuidof(SimpleCalc));


long lResult = 0;


// Call the Add method.

pICalc->Add(5, 10, &lResult);


SetDlgItemInt(IDC_STERM, (int)lResult);


// Uninitialize COM.

CoUninitialize();





이렇게 사용을 하면 된다.



ICalculatorPtr 은 Interface 로 만든  ICalculator 의 스마트포인터이다


ICalculator* 와 같다.





pICalc-> 을 통해 내부에 있는 속성과 메서드에 접근이 가능하다.








336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

ATL 프로젝트로 만든 DLL 을 MFC 프로젝트에서 사용하기



ATL 프로젝트로 만든 DLL 이름을 ComServer.DLL 이라고 가정하겠습니다.


ComServer 대신에 자신이 만든 DLL 을 쓰면 됩니다.



사용하고자하는 ComServer.dll 파일을 프로젝트 디렉토리에 복사한다.


stdafx.h 파일에 


#import "ComServer.dll" no_namespace named_guids


넣고


빌드를 하게 되면


빌드 폴더에 ComServer.tlh , ComServer.tli 가 생성된것을 확인 할 수 있습니다.



ComServer.tlh 파일을 열어 둡니다.




그다음 Dlg 의 OnInitDialog() 같은 곳에 아래의 코드를 삽입한다.


ComTest 는 객체 이름입니다.


try{


UpdateData();


HRESULT rc;


IComTestPtr pComTest; //IComTest *pComTest 도 가능 !!! (IComTest 의 스마트포인터)


rc = CoCreateInstance(CLSID_ComTest, NULL, CLSCTX_INPROC_SERVER, DIID_IComTest, (void**)&pComTest);


if (FAILED(rc)){


MessageBox("Failure in call to CoCreateInstance");


return;


}


pComTest->(사용하고 싶은 속성이나 메서드);


}


catch (_com_error &e){


CString strBuffer;


strBuffer.Format("Code = %08lXnCode meaning = %snSource = %snDescription = %sn",

e.Error(), e.ErrorMessage(), e.Source(), e.Description());

AfxMessageBox(strBuffer);

}






'Programming > COM,ATL' 카테고리의 다른 글

MFC 프로젝트에서 ATL 연동하기  (2) 2016.03.29
ATL/COM 강좌  (0) 2016.03.25
ATL 프로젝트에서 hWnd사용하기 #2  (0) 2016.03.25
ATL 프로젝트에서 hWnd사용하기  (0) 2016.03.25
ocx, dll 라이브러리 등록  (0) 2016.03.15