2 bichs bichs 于 2013.11.30 11:02 提问

两个窗口穿参数,指针数据丢失

在头文件里面声明了一个
struct Param
{
BITMAPINFO *pinfo;
BYTE *pbit;
DWORD h;
DWORD w;
};结构体
程序绝大部分如下
#include "stdafx.h"
#include
#include
#include "resource.h"
#include "MainDlg.h"
#include "SHOWDLG.h"
#include
#include

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
}

 return FALSE;

}

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
HWND chuli=GetDlgItem(hwnd,IDC_chuli);
ComboBox_InsertString(chuli,-1,TEXT("傅里叶变换"));
ComboBox_InsertString(chuli,-1,TEXT("什么二值化"));
ComboBox_InsertString(chuli,-1,TEXT("获取边缘梯度图像"));
ComboBox_InsertString(chuli,-1,TEXT("还有什么阈值"));
return TRUE;
}

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
HWND chuli=GetDlgItem(hwnd,IDC_chuli);
int n;
OPENFILENAME ofn;
char szFile[MAX_PATH];
DWORD m_nHeight, m_nWidth;
HWND h_static=GetDlgItem(hwnd,IDC_s);
HDC hdc_static=GetDC(h_static);
Param params;

 switch(id)
 {
     case IDC_OK://为什么不能开同一张图片两次?

{
ZeroMemory(&ofn,sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = TEXT("ALL\0*.*\0Text\0*.TXT\0BMP\0*.bmp\0JPG\0*.jpg");
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = TEXT('\0');//艹,不加这一句还打不开,不明所以
ofn.nFilterIndex = 3;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.hwndOwner = hwnd;

ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if(GetOpenFileName(&ofn))
{
if(3==ofn.nFilterIndex)
{
MessageBox(hwnd,ofn.lpstrFile,TEXT("问好"),MB_OK);
HANDLE Hbmp=CreateFile(ofn.lpstrFile,GENERIC_READ |GENERIC_WRITE, 0, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE m_Hbmp=CreateFileMapping(Hbmp,NULL,PAGE_READWRITE,0,0x1400000,TEXT("maptest"));
if( INVALID_HANDLE_VALUE==m_Hbmp)
{
MessageBox(hwnd,TEXT("映像创建失败"),TEXT("error"),MB_OK);
return;
}
LPVOID lpBase = MapViewOfFile(m_Hbmp, FILE_MAP_ALL_ACCESS, 0, 0, 0);
//params = lpBase;
BITMAPFILEHEADER pFileHeader;
BITMAPINFO *pInfoHeader;
pFileHeader = (BITMAPFILEHEADER
)lpBase;
BYTE pBits = (BYTE)lpBase + pFileHeader->bfOffBits;
pInfoHeader = (BITMAPINFO*)((BYTE*)lpBase + sizeof(BITMAPFILEHEADER));
m_nHeight = pInfoHeader->bmiHeader.biHeight;
m_nWidth = pInfoHeader->bmiHeader.biWidth;
HBITMAP hBitmap = CreateCompatibleBitmap(hdc_static, m_nWidth, m_nHeight);
SelectObject(hdc_static, hBitmap);
params.w = m_nWidth;
params.h = m_nHeight;
params.pinfo = pInfoHeader;//结构体指针的赋值有问题
params.pbit = pBits;
int nRet = SetDIBitsToDevice(hdc_static,0,0,m_nWidth,m_nHeight,0,0,0,m_nHeight,pBits,pInfoHeader,DIB_RGB_COLORS);
InvalidateRect((HWND)Hbmp, NULL, TRUE);
DeleteObject(hBitmap);
ReleaseDC(hwnd,hdc_static);
//UnmapViewOfFile(lpBase);
CloseHandle(m_Hbmp);

}

}

}
break;
case IDC_DOIT:
{
n=ComboBox_GetCurSel(chuli);
HINSTANCE hinstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
while(n<0)
{
MessageBox(hwnd,TEXT("请选择处理方式"),TEXT("提示"),MB_OKCANCEL);
break;
}
if(n<0)
{
break;
}

switch(n)
{
//LPARAM par;
//par=(long
)&params;
case 0:
{
DialogBoxParam(hinstance,MAKEINTRESOURCE(IDD_SHOWDLG),NULL,SHOWDLG_Proc,(long)&params);
MessageBox(NULL,TEXT("傅里叶变换"),TEXT("你选择了"),MB_OK);

}
break;
case 1:
{
MessageBox(NULL,TEXT("什么二值化"),TEXT("你选择了"),MB_OK);
DialogBoxParam(hinstance,MAKEINTRESOURCE(IDD_SHOWDLG),NULL,SHOWDLG_Proc,300);
}
break;
case 2:
{
MessageBox(NULL,TEXT("获取边缘梯度图像"),TEXT("你选择了"),MB_OK);
DialogBoxParam(hinstance,MAKEINTRESOURCE(IDD_SHOWDLG),NULL,SHOWDLG_Proc,300);
}
break;
case 3:
{
MessageBox(NULL,TEXT("还有什么阈值"),TEXT("你选择了"),MB_OK);
DialogBoxParam(hinstance,MAKEINTRESOURCE(IDD_SHOWDLG),NULL,SHOWDLG_Proc,300);
}
break;
default:
break;

}
}
break;
default:
break;
}
}

void Main_OnClose(HWND hwnd)
{
EndDialog(hwnd, 0);
}
然后在另一个窗口函数里面
BOOL SHOWDLG_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
Param * pa = (Param*)lParam ;//DC没有问题,传进来的指针指的地方不对
SetDIBitsToDevice(h_sdc,0,0,pa->w,pa->h,0,0,0,pa->h,pa->pbit,pa->pinfo,DIB_RGB_COLORS);
return TRUE;
}
调试发现在红色标记语句执行后params里面的数据丢失(之前调试看的数据都正常),这句UnmapViewOfFile(lpBase);不释放就好了,但是传递给另一个窗口的时候结构体里面的数据就全没了,定义全局变量也没用,现在想做的就是在两个窗口里面都显示同一个图片,以后再做图像处理,小弟都是自学的,MFC不太熟悉,想请各位大神指教一下,感激不尽!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!