如何添加对话框菜单到Win32 DLL? [英] How to add dialog & menu to Win32 DLL?
问题描述
我正在尝试添加一些菜单&对话框中包含按钮&将控件编辑到我的win32 DLL.
我发现必须将资源文件添加到代码中才能生成框.
在MFC中,我们可以通过Class向导轻松完成操作,并添加菜单&对话框代码转换为我们的主代码.在win32 DLL中我如何添加此代码?我们必须手动执行此操作,或者系统自动为我们生成.
例如下面的代码:
Hi,
I am trying add some menu & dialog box include button & edit control to my win32 DLL.
I found I must add resource files to my code to generate box.
In MFC we can do very easy by Class wizard and add menu & dialog box code to our main code. In win32 DLL how I can add this codes? We must do that manually or system generate for us automatically.
For example code same below:
INT_PTR CALLBACK DialogFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HMENU hMenu;
switch(uMsg)
{
case WM_INITDIALOG:
hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MENU1));
SetMenu(hDlg, hMenu);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
case ID_FILE_MESSAGEBOX:
MessageBox(hDlg, "This is sample for Reza", "Test", MB_OK);
break;
case IDC_BUTTON2:
case ID_FILE_EXIT:
SendMessage(hDlg, WM_CLOSE, 0, 0);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hDlg);
return TRUE;
break;
case WM_DESTROY:
PostQuitMessage (0);
return TRUE;
break;
}
return FALSE;
}
是否有与此相关的文章或手册?
问候
Is there any article or manual for this ?
Regards,
推荐答案
实现方法与MFC完全不同.这是我编写的一个示例,该示例使用一个对话框和两个按钮"Exit"和"Say Hi"创建一个简单的DLL,它看起来很复杂,但确实很简单,:
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,:
// 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;
}
- 打开Visual Stdio,我使用VS2005,并创建一个新的C ++ DLL项目,例如DllDialog.
- 右键单击资源文件,添加一个新的资源文件. ,然后
- 添加> 新项目.从资源和资源文件中进行选择,然后将其命名为任意名称,例如resource.rc.
- 转到资源视图并右键单击在您刚刚创建的资源文件上,选择添加资源... 并单击对话框图标,然后单击新建.
- 您应该看到一个带有两个按钮的对话框, 确定"和取消";删除它们并添加两个新按钮:退出"和说声".从提议者中将按钮退出"的ID更改为IDD_BUTTON_EXIT和IDD_BUTTON_SAYHI以表示"Say Hi".
- 找出此ID的值,只需右键单击resource.rc并选择 Resource符号.
- 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.
#define IDD_DIALOGBOX 101
#define IDD_BUTTON_EXIT 1001
#define IDD_BUTTON_SAYHI 1002
该代码是一个DLL,它导出一个名为StartDialog
的函数,您可以通过在应用程序中包含以下头文件来对其进行测试:
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.
这篇关于如何添加对话框菜单到Win32 DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!