Use resources in DLL (1)

xiaoxiao2021-03-06  38

Using resources in DLL (1) The most often seen on the 9CBS Forum About DLL is how to use dialogs in DLL, which is a very common problem with how to use resources in DLL. Here we analyze and solve this problem from both aspects of Win32 DLL and MFC DLL. 1. Win32 DLL Use the dialog in Win32 DLL very simple, you only need to add dialog resources in your DLL, and you can set the control you need above on the dialog. Then use Dialogbox or CreateDialog to create a dialog box and define your own dialog box callback function to process the message received. By following a specific example, learn how to use the dialog box in Win32 DLL, follow these steps:

1) In the VC menu, File-> New New Win32 Dynamic-Link Library Project is named USEDLG, the next step Select A Simple DLL Project.

2) Insert-> Resource in the VC menu Add a IDD_DLG_SHOW Dialog resource, remove the CANCEL button on this Dialog, and only the OK button is left. Add another ID to IDD_ABOUTBOX dialog, which capens is About. Save this resource and name the resource file Usedlg.rc. And add resource.h and usedlg.rc to the project.

3) Contains Resource.h in usedlg.app, and add the following code:

Hinstance hinst = null; hwnd hwnddlg = null;

BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); BOOL CALLBACK AboutProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); extern "C" __declspec (dllexport) void ShowDlg ();

BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {switch (ul_reason_for_call) {case DLL_PROCESS_ATTACH: hinst = (HINSTANCE) hModule; case DLL_PROCESS_DETACH: break;} return TRUE;}

EXTERN "C" __declspec (dllexport) void showdlg () {hwnddlg = createdialog (hinst, makeintresource (IDD_DLG_SHOW), NULL, (DLGPROC) DLGPROC); ShowWindow (hwnddlg, sw_show);}

BOOL CALLBACK DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (LOWORD (wParam) == IDOK) DialogBox (hinst, MAKEINTRESOURCE (IDD_ABOUTBOX), hDlg, (DLGPROC) AboutProc); return TRUE; case WM_CLOSE: DestroyWindow (hDlg); hwndDLG = NULL; return TRUE;} return FALSE;} BOOL CALLBACK AboutProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) {switch ( Message) {copy wm_close: enddialog (hdlg, null); hwnddlg = null; return true;} Return False;

4) Compile to generate usedlg.dll and usedlg.lib.

Next we establish an application that calls this DLL, the steps are as follows:

1) In the VC menu, File-> New has created a new MFC AppWizard (EXE) project named Use, and then click the finish button after you select Dialog Based.

2) Add a button on the main dialog, then double-click this button, pop up the Add Member Function dialog box, directly click OK to enter the void cusedlg :: onbutton1 () function. And add a function call to this function: showdlg () ;.

3) Follow the following code followed by the #include statement:

Extern "c" __declspec (dllexport) void showdlg (); #pragma comment (lib, "debug / buylg")

4) Copy the usedlg.dll and usedlg.lib generated above the Usedlg project to the debug directory of the USE project.

5) Compile to generate use.exe.

Run Use.exe, click the Button1 button, you can see a non-modular dialog box named Dialog. Click the button above to pop up the modal dialog. Run success.

Let's review the process of using the dialog box in Win32 DLL.

In DLL, we define two dialog resources: IDD_DLG_SHOW and IDD_ABOUTBOX, and export the function showdlg. The non-modular dialog IDD_DLG_SHOW is created using the CREATEDIALOG function in the function showdlg, and specifies the callback function DLGProc of the dialog. WM_INITDIALOG, WM_COMMAND, and WM_CLOSE messages are processed in DLGProc to respond to the action made by the user on the dialog. When processing the button action, create an IDD_ABOUTBOX using the Dialogbox function, specify its callback function to AboutProc, and process its corresponding message in the ABOUTPROC. In EXE, we use implicit link to call DLL and use the showdlg function exported in the DLL to call dialogs in the DLL.

Using dialog box in Win32 DLL is as simple, let's take a look at how to use the dialog in the MFC DLL.

转载请注明原文地址:https://www.9cbs.com/read-63270.html

New Post(0)