|
发表于 2017-10-31 10:02:03
|
显示全部楼层
楼主是要显示图片的意思吗?这里有一个Windows加载BMP文件的Demo,供参考。
- #include <Windows.h>
- #include <Commdlg.h>
- //先声明一下消息处理函数
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
- //重新设定windows窗口客户区域大小
- void ResetClientSize(HWND hWnd,int width, int height)
- {
- RECT rectProgram,rectClient;
- GetWindowRect(hWnd,&rectProgram);
- GetClientRect(hWnd,&rectClient);
- int nWidth = rectProgram.right -rectProgram.left - (rectClient.right -rectClient.left);
- int nHeiht = rectProgram.bottom -rectProgram.top - (rectClient.bottom -rectClient.top);
- nWidth += width;
- nHeiht += height;
- rectProgram.right = nWidth;
- rectProgram.bottom= nHeiht;
- int showToScreenx =GetSystemMetrics(SM_CXSCREEN)/2-nWidth/2;
- int showToScreeny =GetSystemMetrics(SM_CYSCREEN)/2-nHeiht/2;
- MoveWindow(hWnd, showToScreenx,showToScreeny, rectProgram.right, rectProgram.bottom, false);
- InvalidateRect(hWnd,NULL,TRUE);
- }
- // 入口点
- int CALLBACK WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrvInstance,
- LPSTR lpCommandLine,
- int cmdShow)
- {
- char* clsName = "MyApp";
- WNDCLASS wc;
- ZeroMemory(&wc,sizeof(WNDCLASS));
- wc.hInstance = hInstance;
- wc.lpszClassName = clsName;
- wc.lpfnWndProc = MyWindowProc;
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
- wc.style = CS_HREDRAW | CS_VREDRAW;
- RegisterClass(&wc);
- HWND hMainwind = CreateWindow(clsName,"显示BMP图片",WS_OVERLAPPEDWINDOW,
- 300,200,100,100,NULL,NULL,hInstance,NULL);
- if(hMainwind == NULL)
- return 0;
- OPENFILENAME ofn;
- char szFile[MAX_PATH];
- ZeroMemory(&ofn,sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.lpstrFile = szFile;
- ofn.lpstrFile[0] = TEXT('\0');
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter= TEXT("bmp\0*.bmp\0");
- ofn.nFilterIndex = 0;
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.lpstrTitle=TEXT("请选择一个BMP文件");
- ofn.hwndOwner = hMainwind;
- ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- if (GetOpenFileName(&ofn))
- {
- HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFile, IMAGE_BITMAP,
- 0, 0, LR_LOADFROMFILE);
- BITMAP bmp;
- ZeroMemory(&bmp,sizeof(bmp));
- GetObject(hBitmap, sizeof(BITMAP), &bmp);
- ResetClientSize(hMainwind,bmp.bmWidth,bmp.bmHeight);
- ShowWindow(hMainwind,SW_NORMAL);
- UpdateWindow(hMainwind);
-
- MSG msg;
- GetMessage(&msg,NULL,0,0);
- while(msg.message != WM_QUIT)
- {
- if(PeekMessage( &msg,NULL,0,0,PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- else
- {
- HDC hDC = GetDC(hMainwind);
- HBRUSH hBrush = CreatePatternBrush(hBitmap);
- HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);
- Rectangle(hDC, 0, 0, bmp.bmWidth,bmp.bmHeight);
- SelectObject(hDC, hOldBrush);
- DeleteObject(hOldBrush);
- ReleaseDC(hMainwind,hDC);
- }
- }
- DeleteObject(hBitmap);
- }
- return 0;
- }
- LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_DESTROY:
- PostQuitMessage(0);//退出程序
- return 0;
- default:
- break;
- }
- return DefWindowProc(hwnd,msg,wParam,lParam);
- }
复制代码 |
|