Design of ActiveX Control Using MFC-Step 2

Source: Internet
Author: User
This step was intended to go up, because I felt there were still a lot of scenery above, but an accident occurred, and I was surprised to find the popular SafeArrayCreateEx function on the Internet on msdn, so I decided to continue the horizontal crawling to see how to use SAFEARRAY to wrap the custom structure.
First, we recommend an article.
VB really cannot think of Series 4: VB pointer SafeArray of sunflower collection
The website will not be provided, and you will find it after searching on the Internet. It is definitely not advertisement, but interesting. From the Visual Basic perspective, SAFEARRAY.

Obviously, COleSafeArray does not encapsulate the SafeArrayCreateEx function, so this time we use the API directly.

1. Add the Rects attribute for CLiteGridCtrl, the VARIANT type, or use Get/Set methods. This attribute function is unnecessary. Set and return m_rect Members for all of our CCell instances.
2. Implement Rects attributes

VARIANT CLiteGridCtrl: GetRects ()
{
VARIANT vaResult;
VariantInit (& vaResult );
// TODO: Add your property handler here
IRecordInfo * pRecInfo = NULL;
GetRecordInfoFromGuids (GUID_Lib, 1, 0, LOCALE_USER_DEFAULT, GUID_Rect, & pRecInfo );

SAFEARRAYBOUND sab [2];
Sab [0]. cElements = 10;
Sab [0]. lLbound = 0;
Sab [1]. cElements = 10;
Sab [1]. lLbound = 0;
SAFEARRAY * psa = SafeArrayCreateEx (VT_RECORD, 2, sab, pRecInfo );

Long lindex [2] = {0 };
RECT * prect = NULL;
SafeArrayLock (psa );
For (int I = 0; I <10; I ++ ){
Lindex [0] = I;
For (int j = 0; j <10; j ++ ){
Lindex [1] = j;
SafeArrayPtrOfIndex (psa, lindex, (void **) & prect );
* Prect = m_cells [I] [j]. m_rect;
}
}
SafeArrayUnlock (psa );

VaResult. vt = VT_ARRAY | VT_RECORD;
VaResult. pRecInfo = pRecInfo;
VaResult. parray = psa;
Return vaResult;
}
Void CLiteGridCtrl: SetRects (const variant far & newValue)
{
// TODO: Add your property handler here
COleSafeArray sa (newValue );
ASSERT (sa. GetDim () = 2 );
Long llb1 = 0;
Long lub1 = 0;
Long llb2 = 0;
Long lub2 = 0;
Long l1 = 0;
Long l2 = 0;
Sa. GetLBound (1, & llb1 );
Sa. GetUBound (1, & lub1 );
L1 = lub1-llb1 + 1;
ASSERT (l1 = 10 );
Sa. GetLBound (2, & llb2 );
Sa. GetUBound (2, & lub2 );
L2 = lub2-llb2 + 1;
ASSERT (l2 = 10 );

Long lindex [2] = {0 };
RECT * prect = NULL;
Sa. Lock ();
For (int I = llb1; I <= lub1; I ++ ){
Lindex [0] = I;
For (int j = llb2; j <= lub2; j ++ ){
Lindex [1] = j;
Sa. PtrOfIndex (lindex, (void **) & prect );
M_cells [i-llb1] [j-llb2]. m_rect = prect;
}
}
Sa. Unlock ();
InvalidateControl ();

SetModifiedFlag ();
}
In fact, you only need to copy the code of the Texts attribute and modify it slightly. The GUID_Lib and GUID_Rect here are defined in LiteGridCtrl. cpp, as follows:

Const GUID GUID_Lib =
{0x191618F9, 0xEBF9, 0x4538, {0x9E, 0x10, 0xD9, 0xC5, 0x62, 0x7E, 0xAE, 0xA9 }};
Const GUID GUID_Rect =
{0x6BF5EE0C, 0x373A, 0x4893, {0x89, 0xEB, 0x2C, 0x02, 0x08, 0xD3, 0xD4, 0xEB }};

3. Add the following code in Form_Load of VB:
Private Sub Form_Load ()
Dim str (0 To 9, 0 To 9) As String
Dim stro () As String
Dim I As Integer
Dim j As Integer
For I = 0 To 9
For j = 0 To 9
Str (I, j) = I & ":" & j
Next
Next
LiteGrid1.Texts = str
Stro = LiteGrid1.Texts

Dim v () As Rect
Dim x As Integer
Dim y As Integer
X = 0
Y = 0
V = LiteGrid1.Rects
For I = LBound (v) To UBound (v)
Y = 0
For j = LBound (v, 2) To UBound (v, 2)
V (I, j). Left = x
V (I, j). Top = y
V (I, j). Right = x + 40
V (I, j). bottom = y + 16
Y = y + 20
Next
X = x + 50
Next
LiteGrid1.Rects = v
End Sub
The previous section is the content in the previous step, which is also listed. After running it, you will find that the boxes are not in the same place and separated, it proves that the Get and Set of the Rect attributes are successfully called.

Therefore, it is quite simple.

Originally, we wanted to use SafeArrayAllocDescriptor and SafeArrayAllocData to simulate SafeArrayCreateEx. In fact, it seems that there will be no error. The values of each member of SAFEARRAY are the same in both methods, as shown below:
SAFEARRAY * psa = NULL;
SafeArrayAllocDescriptor (2, & psa );
Psa-> rgsabound [0]. lLbound = 0;
Psa-> rgsabound [0]. cElements = 10;
Psa-> rgsabound [1]. lLbound = 0;
Psa-> rgsabound [1]. cElements = 10;

Psa-> cbElements = sizeof (RECT );
Psa-> fFeatures = FADF_RECORD;

HRESULT hresult = SafeArrayAllocData (psa );
If (FAILED (hresult )){
SafeArrayDestroyDescriptor (psa );
Return vaResult;
}
Unexpectedly, GetRects and SetRects are not correctly executed.
Add
SafeArraySetRecordInfo (psa, pRecInfo );
OK.
So it seems that this IRecordInfo is hidden in the SAFEARRAY structure. It doesn't matter where it is put. It doesn't make sense to study it. If a friend happens to be familiar with this, thank you for your patience.

I almost forgot, because we still use COleSafeArray for parsing, so we didn't use SafeArrayGetVartype. This function does not seem to exist in msdn. You can use it to determine whether it is the VARTYPE you need, it should be useful.
VARTYPE vt;
SafeArrayGetVarType (psa, & vt );
Psa is SAFEARRAY *.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.