033 在DLL中产生对话框的方法二(Win32 DLL)

Post date: 2015/4/20 上午 09:11:46

// DllDialog.cpp #define _WIN32_WINNT 0x0501 #define WIN32_LEAN_AND_MEAN #include <windows.h> #define MESSAGE_MAP(MessageParam, MessageCallback)\ if(message == MessageParam)\ return MessageCallback(hwnd, message, wParam, lParam) #define MESSAGE_MAP_COMMAND(Identifier, MessageCallback)\ if((message == WM_COMMAND) && (LOWORD(wParam) == Identifier))\ return MessageCallback(hwnd, message, wParam, lParam); #define IDD_DIALOGBOX 101 #define IDD_BUTTON_EXIT 1001 #define IDD_BUTTON_SAYHI 1002 #define DllExport __declspec(dllexport) #define STDCALL __cdecl #if __cplusplus extern "C" { #endif DllExport INT_PTR STDCALL StartDialog(VOID); #if __cplusplus }; #endif // FIXME: There is a better way by passing the hInstance as a parameter to StartDialog HINSTANCE hInstance = 0; BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { if(ul_reason_for_call == DLL_PROCESS_ATTACH) hInstance = (HINSTANCE) hModule; return TRUE; } INT_PTR StartDialog(VOID) { return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOGBOX), NULL, DialogProc); } BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { MESSAGE_MAP(WM_INITDIALOG, OnDialogInit); MESSAGE_MAP(WM_DESTROY, OnDialogDestroy); MESSAGE_MAP(WM_CLOSE, OnDialogClose); MESSAGE_MAP_COMMAND(IDD_BUTTON_EXIT, OnDialogButtonExitClicked); MESSAGE_MAP_COMMAND(IDD_BUTTON_SAYHI, OnDialogButtonSayHiClicked); return FALSE; } BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { return TRUE; } BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PostQuitMessage(0); return TRUE; } BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { DestroyWindow(hwnd); return TRUE; } BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { OnDialogClose(hwnd, message, wParam, lParam); return TRUE; } BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBox(hwnd, TEXT("Hi :D!"), TEXT("DLL"), MB_OK); return TRUE; }

// header.h #ifndef __HEADER_H__ #define __HEADER_H__ #include <windows.h> #define STDCALL __cdecl typedef INT_PTR (STDCALL *StartDialogFunc)(VOID); #endif /* __HEADER_H__ */

// foo.cpp #define _WIN32_WINNT 0x0501 #define WIN32_LEAN_AND_MEAN #include <windows.h> #include "header.h" INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HMODULE hDll; StartDialogFunc StartDialog; hDll = LoadLibrary(TEXT("DllDialog.dll")); if(!hDll) { MessageBox(NULL, TEXT("Cannot load DllDialog.dll"), NULL, MB_OK); return -1; } StartDialog = (StartDialogFunc) GetProcAddress(hDll, "StartDialog"); StartDialog(); return 0; }

//########################################## 原文

The way to it is quite different than MFC. Here is an example I have written for creating a simple DLL with a dialog and two buttons "Exit" and "Say Hi", It may looks complex but it's really simple,:

Hide Expand Copy Code// DllDialog.cpp #define _WIN32_WINNT 0x0501 #define WIN32_LEAN_AND_MEAN #include "stdafx.h" #include <windows.h> #define MESSAGE_MAP(MessageParam, MessageCallback)\ if(message == MessageParam)\ return MessageCallback(hwnd, message, wParam, lParam) #define MESSAGE_MAP_COMMAND(Identifier, MessageCallback)\ if((message == WM_COMMAND) && (LOWORD(wParam) == Identifier))\ return MessageCallback(hwnd, message, wParam, lParam); #define IDD_DIALOGBOX 101 #define IDD_BUTTON_EXIT 1001 #define IDD_BUTTON_SAYHI 1002 #define DllExport __declspec(dllexport) #define STDCALL __cdecl #if __cplusplus extern "C" { #endif DllExport INT_PTR STDCALL StartDialog(VOID); #if __cplusplus }; #endif // FIXME: There is a better way by passing the hInstance as a parameter to StartDialog HINSTANCE hInstance = 0; BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { if(ul_reason_for_call == DLL_PROCESS_ATTACH) hInstance = (HINSTANCE) hModule; return TRUE; } INT_PTR StartDialog(VOID) { return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOGBOX), NULL, DialogProc); } BOOL CALLBACK DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { MESSAGE_MAP(WM_INITDIALOG, OnDialogInit); MESSAGE_MAP(WM_DESTROY, OnDialogDestroy); MESSAGE_MAP(WM_CLOSE, OnDialogClose); MESSAGE_MAP_COMMAND(IDD_BUTTON_EXIT, OnDialogButtonExitClicked); MESSAGE_MAP_COMMAND(IDD_BUTTON_SAYHI, OnDialogButtonSayHiClicked); return FALSE; } BOOL OnDialogInit(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { return TRUE; } BOOL OnDialogDestroy(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PostQuitMessage(0); return TRUE; } BOOL OnDialogClose(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { DestroyWindow(hwnd); return TRUE; } BOOL OnDialogButtonExitClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { OnDialogClose(hwnd, message, wParam, lParam); return TRUE; } BOOL OnDialogButtonSayHiClicked(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { MessageBox(hwnd, TEXT("Hi :D!"), TEXT("DLL"), MB_OK); return TRUE; }

  • Open Visual Stdio, I'm using VS2005, and create a new C++ DLL project named, for example, DllDialog.
  • Add a new resource file by right clicking on the Resource Files and then
  • Add > New Item. Choose from Resource the Resource Files and name it what ever you like, for example, resource.rc.
  • Go to the resource view and right click on the resource file you've just created and choose Add Resource ...and click on the dialog icon and click new.
  • You should see a dialog with two buttons, "OK" and "Cancel"; delete them and add two new buttons, "Exit" and "Say Hi". From the propitiates change the ID of the button "Exit" to IDD_BUTTON_EXIT and IDD_BUTTON_SAYHI for "Say Hi".
  • Find out the value of this IDs, just right click on resource.rc and choose Resource Symbols.

Make sure you change these definitions so they match yours:

#define IDD_DIALOGBOX 101 #define IDD_BUTTON_EXIT 1001 #define IDD_BUTTON_SAYHI 1002

That code is a DLL that exports a function named StartDialog, you could test it by including this header file in your application:

// header.h #ifndef __HEADER_H__ #define __HEADER_H__ #include <windows.h> #define STDCALL __cdecl typedef INT_PTR (STDCALL *StartDialogFunc)(VOID); #endif /* __HEADER_H__ */

and call that using dynamic loading for example:

// foo.cpp #define _WIN32_WINNT 0x0501 #define WIN32_LEAN_AND_MEAN #include <windows.h> #include "header.h" INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HMODULE hDll; StartDialogFunc StartDialog; hDll = LoadLibrary(TEXT("DllDialog.dll")); if(!hDll) { MessageBox(NULL, TEXT("Cannot load DllDialog.dll"), NULL, MB_OK); return -1; } StartDialog = (StartDialogFunc) GetProcAddress(hDll, "StartDialog"); StartDialog(); return 0; }

I hope this is helpful.

Good luck.

Hide Copy Code

Hide Copy Code

Hide Copy Code