| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 418 人关注过本帖
标题:[求助]怎么用openfilename函数随意打开图片
只看楼主 加入收藏
刘天天
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-11-5
收藏
 问题点数:0 回复次数:0 
[求助]怎么用openfilename函数随意打开图片

我建立的WIN32工程画图文件...只能指定路径打开文件..能不能随意打开(好象WIN系统里一样有弹出对话框)
同学说用OPENFILENAME 函数完成....貌似我看不懂MSDN
请能帮忙的人帮帮忙哦
以下是我的程序主要部分(菜单需要连接一下呢):

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static POINT start,finish;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
//HPEN newpen,oldpen;
static bool BUTTONDOWN;
static int drawstyle=1;//1ZHIXIAN,2QUXIAN 3JUXING 4TUOYUAN
static HBRUSH brush;
static CHOOSECOLOR color;
static COLORREF pencolor=RGB(0,0,0),brushcolor=RGB(255,255,255);;//TIANCHONG
static LOGBRUSH lb;
static HPEN newpen,oldpen;
static int penstyle=PS_SOLID;
//BITMAP bitmap;
static HDC hdcmem;
static HBITMAP hbitmap;
static int cxbitmap,cybitmap;
static int cxClient,cyClient;
static OPENFILENAME ofn;
BITMAP bitmap;
HANDLE hfile;
BYTE *bits,*bitmapdate;
DWORD filesize,size;
BITMAPFILEHEADER *bitmapfileheader,bitmapheader;
BITMAPINFOHEADER *bitmapinfodeader;
BITMAPINFO *bitmapinfo;
unsigned long bitmapsize,length;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;

case ID_ZHIXIAN:
drawstyle=1;
break;
case ID_QUXIAN:
drawstyle=2;
break;
case ID_JUXING:
drawstyle=3;
break;
case ID_TUOYUAN:
drawstyle=4;
break;

case ID_SHIXIAN:
penstyle=PS_SOLID;
break;
case ID_XUXIAN:
penstyle=PS_DASH;
break;
case ID_DIANXIAN:
penstyle=PS_DASHDOT;
break;


case ID_COLOR:
//color.rgbResult=ref;
if(ChooseColor(&color))
pencolor=color.rgbResult;
break;
case ID_BRUSHCOLOR:
if(ChooseColor(&color))
brushcolor=color.rgbResult;
break;

case ID_OPEN:
hdc=GetDC(hWnd);
hdcmem=CreateCompatibleDC(hdc);

hfile=CreateFile("D:\\",GENERIC_READ,FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);

if(!hfile) return FALSE;
filesize=GetFileSize(hfile,&size);
bits=(BYTE *)malloc(filesize);
if(!bits) return FALSE;
if(!ReadFile(hfile,bits,filesize,&filesize,NULL)) return FALSE;

bitmapfileheader=(BITMAPFILEHEADER *)bits;
bitmapinfodeader=(BITMAPINFOHEADER *)(bitmapfileheader+1);
bitmapinfo=(BITMAPINFO *)(bitmapfileheader+1);
bitmapdate=bits+bitmapfileheader->bfOffBits;

hbitmap=CreateDIBitmap(hdc,bitmapinfodeader,CBM_INIT,
bitmapdate,bitmapinfo,DIB_RGB_COLORS);



hdc=GetDC(hWnd);
hdcmem=CreateCompatibleDC(hdc);

hfile=CreateFile("D:\\",GENERIC_READ,FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);

if(!hfile) return FALSE;
filesize=GetFileSize(hfile,&size);
bits=(BYTE *)malloc(filesize);
if(!bits) return FALSE;
if(!ReadFile(hfile,bits,filesize,&filesize,NULL)) return FALSE;

bitmapfileheader=(BITMAPFILEHEADER *)bits;
bitmapinfodeader=(BITMAPINFOHEADER *)(bitmapfileheader+1);
bitmapinfo=(BITMAPINFO *)(bitmapfileheader+1);
bitmapdate=bits+bitmapfileheader->bfOffBits;

hbitmap=CreateDIBitmap(hdc,bitmapinfodeader,CBM_INIT,
bitmapdate,bitmapinfo,DIB_RGB_COLORS);

free(bits);
InvalidateRect(hWnd,NULL,true);
break;


case ID_SAVE:
hdc=GetDC(hWnd);
hdcmem=CreateCompatibleDC(hdc);
GetObject(hbitmap,sizeof(BITMAP),(LPSTR)&bitmap);
bitmapinfo=(BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER));
hfile=CreateFile("D:\\1.bmp",GENERIC_READ|GENERIC_WRITE,(DWORD)0,
(LPSECURITY_ATTRIBUTES)NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL);

if(!hfile) return FALSE;

bitmapinfodeader=(BITMAPINFOHEADER *)bitmapinfo;
bitmapinfodeader->biBitCount=bitmap.bmBitsPixel;
bitmapinfodeader->biHeight=bitmap.bmHeight;
bitmapinfodeader->biWidth=bitmap.bmWidth;
bitmapinfodeader->biPlanes=bitmap.bmPlanes;
bitmapinfodeader->biClrUsed=0;
bitmapinfodeader->biClrImportant=0;
bitmapinfodeader->biCompression=BI_RGB;
bitmapinfodeader->biSizeImage=0;
bitmapinfodeader->biXPelsPerMeter=0;
bitmapinfodeader->biYPelsPerMeter=0;
bitmapinfodeader->biSize=sizeof(BITMAPINFOHEADER);

bitmapheader.bfType=0x4d42;
bitmapheader.bfReserved1=0;
bitmapheader.bfReserved2=0;
bitmapheader.bfOffBits=(DWORD)sizeof(BITMAPINFOHEADER)+(DWORD)sizeof(BITMAPFILEHEADER);
bitmapsize=bitmap.bmWidthBytes*bitmapinfodeader->biHeight*bitmap.bmPlanes;
bitmapheader.bfSize=(DWORD)sizeof(BITMAPINFOHEADER)+(DWORD)sizeof(BITMAPFILEHEADER)+bitmapsize;

bits=(BYTE *)malloc(bitmapsize);

if(!GetDIBits(hdc,hbitmap,0,(WORD)bitmap.bmHeight,bits,bitmapinfo,DIB_RGB_COLORS==ERROR_INVALID_PARAMETER)) return FALSE;

WriteFile(hfile,&bitmapheader,sizeof(BITMAPFILEHEADER),&length,NULL);
WriteFile(hfile,bitmapinfodeader,sizeof(BITMAPINFOHEADER),&length,NULL);
WriteFile(hfile,bits,bitmapsize,&length,NULL);

CloseHandle(hfile);
free(bits);
free(bitmapinfo);
break;

case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;


case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
break;

case WM_CREATE:
hdc=GetDC(hWnd);
cxbitmap=GetSystemMetrics(SM_CXMAXIMIZED);
cybitmap=GetSystemMetrics(SM_CYMAXIMIZED);
hdcmem=CreateCompatibleDC(hdc);
hbitmap=CreateCompatibleBitmap(hdc,cxbitmap,cybitmap);
SelectObject(hdcmem,hbitmap);
PatBlt(hdcmem,0,0,cxbitmap,cybitmap,WHITENESS);


color.hwndOwner=hWnd;
color.lStructSize=sizeof(color);
color.Flags=CC_ANYCOLOR|CC_RGBINIT;
color.lpCustColors=&lb.lbColor;

lb.lbColor=RGB(0,0,0);
lb.lbHatch=0;
lb.lbStyle=0;
BUTTONDOWN=FALSE;
break;

case WM_LBUTTONDOWN:

start.x=LOWORD(lParam);
start.y=HIWORD(lParam);
finish.x=start.x;
finish.y=start.y;
BUTTONDOWN=TRUE;
break;




case WM_LBUTTONUP:

BUTTONDOWN=FALSE;
break;

case WM_MOUSEMOVE:

if(BUTTONDOWN)
{
switch(drawstyle){

case 1:
hdc=GetDC(hWnd);
SetROP2(hdc,R2_NOTXORPEN);
SetROP2(hdcmem,R2_NOTXORPEN);

newpen=CreatePen(penstyle,1,pencolor);
SelectObject(hdc,newpen);

oldpen=(HPEN)SelectObject(hdcmem,newpen);

MoveToEx(hdc,start.x,start.y,NULL);
MoveToEx(hdcmem,start.x,start.y,NULL);
LineTo(hdc,finish.x,finish.y);
LineTo(hdcmem,finish.x,finish.y);

//SelectObject(hdc,oldpen);
finish.x=LOWORD(lParam);
finish.y=HIWORD(lParam);
MoveToEx(hdc,start.x,start.y,NULL);
MoveToEx(hdcmem,start.x,start.y,NULL);
LineTo(hdc,finish.x,finish.y);
LineTo(hdcmem,finish.x,finish.y);

SetROP2(hdc,R2_COPYPEN);
SetROP2(hdcmem,R2_COPYPEN);

SelectObject(hdcmem,oldpen);

ReleaseDC(hWnd,hdc);
DeleteObject(newpen);
break;

case 2:
hdc=GetDC(hWnd);
newpen=CreatePen(penstyle,1,pencolor);
SelectObject(hdc,newpen);
oldpen=(HPEN)SelectObject(hdcmem,newpen);

finish.x=LOWORD(lParam);
finish.y=HIWORD(lParam);
MoveToEx(hdc,start.x,start.y,NULL);
MoveToEx(hdcmem,start.x,start.y,NULL);
LineTo(hdc,finish.x,finish.y);
LineTo(hdcmem,finish.x,finish.y);
start.x=finish.x;
start.y=finish.y;
SelectObject(hdcmem,oldpen);
ReleaseDC(hWnd,hdc);
DeleteObject(newpen);
break;

case 3:
hdc=GetDC(hWnd);
SetROP2(hdc,R2_NOTXORPEN);
SetROP2(hdcmem,R2_NOTXORPEN);
newpen=CreatePen(penstyle,1,pencolor);
SelectObject(hdc,newpen);
oldpen=(HPEN)SelectObject(hdcmem,newpen);
brush=CreateSolidBrush(brushcolor);
SelectObject(hdc,brush);
SelectObject(hdcmem,brush);
Rectangle(hdc,start.x,start.y,finish.x,finish.y);
Rectangle(hdcmem,start.x,start.y,finish.x,finish.y);
finish.x=LOWORD(lParam);
finish.y=HIWORD(lParam);
Rectangle(hdc,start.x,start.y,finish.x,finish.y);
Rectangle(hdcmem,start.x,start.y,finish.x,finish.y);
SetROP2(hdc,R2_COPYPEN);
SetROP2(hdcmem,R2_COPYPEN);
ReleaseDC(hWnd,hdc);
DeleteObject(newpen);
break;

case 4:
hdc=GetDC(hWnd);
SetROP2(hdc,R2_NOTXORPEN);
SetROP2(hdcmem,R2_NOTXORPEN);
newpen=CreatePen(penstyle,1,pencolor);
SelectObject(hdc,newpen);
oldpen=(HPEN)SelectObject(hdcmem,newpen);
brush=CreateSolidBrush(brushcolor);
SelectObject(hdc,brush);
SelectObject(hdcmem,brush);
Ellipse(hdc,start.x,start.y,finish.x,finish.y);
Ellipse(hdcmem,start.x,start.y,finish.x,finish.y);
finish.x=LOWORD(lParam);
finish.y=HIWORD(lParam);
Ellipse(hdc,start.x,start.y,finish.x,finish.y);
Ellipse(hdcmem,start.x,start.y,finish.x,finish.y);
SetROP2(hdc,R2_COPYPEN);
SetROP2(hdcmem,R2_COPYPEN);
SelectObject(hdcmem,oldpen);
ReleaseDC(hWnd,hdc);
DeleteObject(newpen);
break;

}
}
break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);


//hbitmap=LoadBitmap(hInst,(LPCTSTR)IDB_BITMAP1);
SelectObject(hdcmem,hbitmap);

BitBlt(hdc,0,0,cxClient,cyClient,hdcmem,0,0,SRCCOPY);
RECT rt;
GetClientRect(hWnd, &rt);

EndPaint(hWnd, &ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

搜索更多相关主题的帖子: openfilename 函数 
2007-11-12 19:46
快速回复:[求助]怎么用openfilename函数随意打开图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027344 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved