Summary of strprog learning in Windows Programming

Source: Internet
Author: User

Task definition: SameProgramMultiple instances share one copy of data. modifications made to the data by one instance affect other instances, data Display changes in one instance cause data display updates in other instances.

Overall analysis: compile a DLL that contains the memory data shared by different instances of the same program. Similar to the document-view mode, the document corresponds to the shared memory data in the DLL, view corresponds to the display window of each instance. This allows an instance to modify shared data and affects other instances. After each modification, the current instance sends a message to all other instances, synchronize the data in each instance window.

KeyCodeAnalysis:

1. Shared data segments

// Create a shared data segment named shared

# Pragma data_seg ("shared") // data must be initialized; otherwise, the compiler will put them in a common uninitialized data segment.

Int itotal = 0; // total number of identified strings

Wchar szstrings [max_strings] [max_length + 1] = {'\ 0'}; // a key shared string array used to display

# Pragma data_seg ()

# Pragma comment (linker, "/section: shared, RWS ")

2. String display functions in various Application Instances

Bool callback getstrcallback (ptstr pstring, cbparam * pcbp)

{

// Output string

Textout (pcbp-> HDC, pcbp-> xtext, pcbp-> ytext,

Pstring, lstrlen (pstring ));

// The output y coordinate increases by one character height and compares it with the maximum row output in the window.

// If the output row has reached the maximum row, execute

If (pcbp-> ytext + = pcbp-> yincr)> pcbp-> Ymax)

{

// Point y coordinate to the first line

Pcbp-> ytext = pcbp-> ystart;

// Add the maximum string width of a line at the X coordinate and compare it with the maximum column that can be output.

// If the output has reached the maximum row and column, false is returned.

If (pcbp-> xtext + = pcbp-> xincr)> pcbp-> xmax)

Return false;

}

Return true;

}

3. Obtain and display the string function in the DLL (the string display function is called internally)

Export int callback getstringsw (getstrcb pfngetstrcallback, pvoid pparam)

{

Bool breturn;

Int I;

For (I = 0; I <itotal; I ++)

{// The string display function in the application instance is called back here, because the display can only be displayed in each instance, and the DLL cannot be displayed, DLL does not know the display parameters or the context of the display in the instance. Therefore, you cannot directly write the displayed code here. Instead, you need to use the functions in the instance so that the instance can do the display on its own, only the user can know how to display the data according to the displayed DC. In fact, the main task of this function is to obtain shared data, because the data is within the control of the DLL.

Breturn = pfngetstrcallback (szstrings [I], pparam );

If (breturn = false)

Return I + 1;

}

Return itotal;

}

4. Global message Registration

// Register a string to change the message. This message can be received by all instances. For details, refer to the msdn

Idatachangemsg = registerwindowmessage (text ("strprogdatachange "));

5. Broadcast messages to other instances (when an operation is performed in the dialog box, the shared data changes. Therefore, messages are broadcast to other instances for refreshing and display)

// Create input dialog box (delete dialog box behavior is similar, no analysis)

If (dialogbox (hinst, text ("enterdlg"), hwnd, & dlgproc)

{

// Add User input strings to the string array of shared memory

If (addstring (szstring ))

// Send the message registered by the program to all windows to notify other programs that use the shared memory.

Postmessage (hwnd_broadcast, idatachangemsg, 0, 0 );

Else

Messagebeep (0 );

}

6. Receive the broadcast message in the window. (when the broadcast message is received, the customer zone will be invalidated immediately, causing refresh)

Default:

// Broadcast messages and refresh the customer Zone

If (Message = idatachangemsg)

Invalidaterect (hwnd, null, true );

7. Update display (corresponding to the modification of shared data, each instance makes an update display)

Case wm_paint:

HDC = beginpaint (hwnd, & PS );

// Initialize the data required by the output string

Cbparam. HDC = HDC;

Cbparam. xtext = cbparam. xstart = cxchar;

Cbparam. ytext = cbparam. ystart = cychar;

Cbparam. xincr = cxchar * max_length;

Cbparam. yincr = cychar;

Cbparam. xmax = cbparam. xincr * (1 + cxclient/cbparam. xincr );

Cbparam. Ymax = cychar * (cyclient/cychar-1 );

// Output string

Getstrings (getstrcb) getstrcallback, (pvoid) & cbparam );

Endpaint (hwnd, & PS );

Return 0;

 

PS: Other considerations include engineering configuration problems and the DLL dependency configuration of applications.

Related Article

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.