Reposted] Use resources in DLL (recommended) (1)

xiaoxiao2021-03-06  37

The most often seen 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 Newly created a Win32 Dynamic-Link Library project 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) contained in the UseDlg.app resource.h, 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 ( Id D_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 {CASE WM_CLOSE: Enddialog (HDLG, NULL); hwnddlg = null; return true;} Return false;} 4) Compiles to generate usdlg.dll and subside .lg.lib. Next we build an application that calls this DLL, the steps are as follows: 1) In the VC menu, File-> New has newly created a new MFC AppWizard (EXE) project, next to the next selection Dialog Based, click the finish button.

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

New Post(0)