如何在另一个项目中从dll中调用dialogBox(Win32API) [英] how to Invoke dialogBox (Win32API) from within dll in another project
本文介绍了如何在另一个项目中从dll中调用dialogBox(Win32API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,有人可以建议我如何在我的dll项目中定义DialogInvoke函数,这是我的工作代码,我在dll中编译它并想在另一个项目中使用DialogInvoke函数
hello can somebody advice me about how should i define the DialogInvoke function in my dll project, here is my working code, i compiled it in dll and want to use the DialogInvoke function in another project
#include<windows.h>
#include<windowsx.h>
#include<commctrl.h>
#include<shlobj.h>
#include "resource.h"
//Handles for the windows
HWND hdialog;
HINSTANCE ghInstance;
HWND edit;
//Folder Name that has been selected
TCHAR FolderName[MAX_PATH];
TCHAR c[MAX_PATH];
//Return value of the SHGetPathFromIDList
BOOL t;
void SetFont(HWND hwnd,LPTSTR FontName,int FontSize)
{
HFONT hf;
LOGFONT lf={0};
HDC hdc=GetDC(hwnd);
GetObject(GetWindowFont(hwnd),sizeof(lf),&lf);
lf.lfWeight = FW_REGULAR;
lf.lfHeight = (LONG)FontSize;
lstrcpy( lf.lfFaceName, FontName );
hf=CreateFontIndirect(&lf);
SetBkMode(hdc,OPAQUE);
SendMessage(hwnd,WM_SETFONT,(WPARAM)hf,TRUE);
ReleaseDC(hwnd,hdc);
}
int __stdcall BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam,LPARAM lpData)
{
if(uMsg==BFFM_INITIALIZED)
{
RECT ListViewRect,Dialog;
edit=CreateWindowEx(0,"EDIT","",WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,0,100,100,50,hwnd,0,ghInstance,NULL);
HWND caption=CreateWindowEx(0,"STATIC","You have selected the folder :",WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,0,100,100,50,hwnd,0,ghInstance,NULL);
HWND ListView=FindWindowEx(hwnd,NULL,"SysTreeView32",NULL);
GetWindowRect(hwnd,&Dialog);
GetWindowRect(ListView,&ListViewRect);
SetWindowPos(ListView,0,(ListViewRect.left-Dialog.left) ,(ListViewRect.top-Dialog.top )-20,290,170,0);
SetWindowPos(edit,HWND_BOTTOM,(ListViewRect.left-Dialog.left),(ListViewRect.top-Dialog.top )+170,290,18,SWP_SHOWWINDOW);
SetWindowPos(caption,HWND_BOTTOM,(ListViewRect.left-Dialog.left),(ListViewRect.top-Dialog.top )+155,290,14,SWP_SHOWWINDOW);
SetFont(caption,"MS Sans Serif",12);
SetFont(edit,"MS Sans Serif",12);
}
if (uMsg==BFFM_SELCHANGED)
{
t = SHGetPathFromIDList((ITEMIDLIST*)lParam, c);
SetWindowText(edit,c);
}
return 0;
}
BOOL CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
if(message==WM_QUIT||message==WM_CLOSE)
PostQuitMessage(0);
if(message==WM_INITDIALOG)
{
hdialog=hDlg;
RECT rc;
GetWindowRect(hDlg,&rc);
int w=rc.right-rc.left, h=rc.bottom-rc.top;
int cx=GetSystemMetrics(SM_CXSCREEN)/2, cy=GetSystemMetrics(SM_CYSCREEN)/2;
MoveWindow(hDlg,cx-w/2,cy-h/2,w,h,FALSE);
SendMessage(hDlg, WM_COMMAND, IDOK, 0);
SendMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
}
if(message==WM_COMMAND)
{
if((LOWORD(wParam))==IDOK)
{
TCHAR dname[MAX_PATH];
IMalloc *imalloc; SHGetMalloc(&imalloc);
BROWSEINFO bi; ZeroMemory(&bi,sizeof(bi));
bi.hwndOwner=hDlg;
bi.pszDisplayName=dname;
bi.lpszTitle = TEXT("Choose Directory");
#define BIF_NONEWFOLDERBUTTON 0x0200
bi.ulFlags = BIF_NONEWFOLDERBUTTON|BIF_RETURNONLYFSDIRS;
bi.lpfn = BrowseCallbackProc;
ITEMIDLIST *pidl = SHBrowseForFolder(&bi);
if (pidl!=NULL)
MessageBox(hDlg,c,"Selected Folder",0);
// SendMessage(hDlg, WM_COMMAND, IDCANCEL, 0);
imalloc->Free(pidl);
imalloc->Release();
}
if((LOWORD(wParam))==IDCANCEL)
{
PostQuitMessage(0);
EndDialog(hDlg,0);
}
}
return 0;
}
extern "C" __declspec (dllexport) int DialogInvoke ( )
{
DialogBox(ghInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
return 0;
}
//Main window
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//InitCommonControls();
ghInstance=hInstance;
//DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
//DialogInvoke ( ) ;
return 0;
}
是这个编码我想要使用的函数的正确定义?
is this code a proper definition of the function i want to use?
extern "C" __declspec (dllexport) int DialogInvoke ( )
{
DialogBox(ghInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
return 0;
}
问题是对话框没有从另一个项目弹出,但它本身工作正常。
the problem is the dialog doesn't pop up from another project , but it's working fine by itself.
推荐答案
您需要添加DllMain
函数,如 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v = vs.85)的.aspx [ ^ ]在ghInstance
中设置正确的值。由于系统无法找到对话框资源,因此您的调用将失败。
You need to add aDllMain
function, as described in http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx[^] to set the correct value inghInstance
. As it stands your call will fail because the system cannot find the dialog resource.
问题已修复,完整答案在此处
http://cboard.cprogramming.com/c-programming/111737-dialogbox-within-dll-2.html [ ^ ]
problem fixed, the complete answer is here
http://cboard.cprogramming.com/c-programming/111737-dialogbox-within-dll-2.html[^]
好的,谢谢我试试,希望原因在于
ok thanks i'll try,hope the reason lies there
这篇关于如何在另一个项目中从dll中调用dialogBox(Win32API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文