显示图像在Visual c++ windows应用程序
Display Image in Visual c++ windows application
我是visual c++ Windows应用程序编程的初学者,我想在屏幕上显示图像,但我不知道如何。有人能帮我吗?
我在网上找到的大部分东西都是关于我不感兴趣的MFC应用程序。我希望这个应用程序尽可能简单,所以我对使用任何额外的库如OpenCV或使用Direct2D, OpenGl不感兴趣,这并不奇怪。
BitBlt,在MSDN上查找。也可参见http://www.winprog.org/tutorial/bitmaps.html.
只需使用以下代码。它是丑陋的,但足以用于调试
#include <windows.h>
#include <tchar.h>
HBITMAP hBitmap;
HDC localDC;
HBITMAP hOld;
BITMAP qB;
HDC hDC; // Handle (virtual memory pointer) to drawing characteristics
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:{
//MessageBox(hwnd,_T("Window Procedure Received WM_CREATE Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_LBUTTONDOWN: {
//MessageBox(hwnd,_T("Window Procedure Received WM_LBUTTONDOWN Message!"),_T("Message Report!"),MB_OK);
return 0;
}
case WM_PAINT: { //At program start up the whole window is invalid so must be drawn.
//TCHAR tmpText[]=_T("TempText");
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd,&ps);
BOOL qRetBlit = ::BitBlt(hDC,0,0,qB.bmWidth,qB.bmHeight,localDC,0,0,SRCCOPY);
// Draw text on top of the image
//int iBkMode=SetBkMode(hDC,TRANSPARENT); // Save Background Mode characteristic of drawing context
//TextOut(hDC,40,20,tmpText,(int)_tcslen(tmpText)); // Draw Text
//SetBkMode(hDC,iBkMode); // Return Drawing Context To Original State
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY: {
::SelectObject(localDC,hOld);
::DeleteDC(localDC);
::DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int iShow){
TCHAR szClassName[]=_T("Name");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName = szClassName; //Important Field! Character string identifying window class
wc.lpfnWndProc = fnWndProc; //Important Field! Function Pointer. Address of Window Procedure
wc.cbSize = sizeof (WNDCLASSEX); //Those top two fields I just listed are very important. The
wc.style = 0; //others are of course necessary too, but fully understanding all
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION); //the implications of the .szClassName and .lpfnWndProc fields will
wc.hInstance = hInstance; //go a long way to helping you understand Win32 coding. The
wc.hIconSm = 0; //.hBrushBackground field will be the color of the Window's
wc.hCursor = LoadCursor(NULL,IDC_ARROW); //background. The .cbWndExtra field is very useful as it allows
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; //you to associate object (Window) data to the instantiated Window's
wc.cbWndExtra = 0; //internal structure, i.e., accomodate member data.
wc.cbClsExtra = 0;
wc.lpszMenuName = NULL;
RegisterClassEx(&wc);
hBitmap = (HBITMAP)::LoadImage(NULL, (LPCSTR)"P:/Resources/testing/a.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
GetObject(reinterpret_cast<HGDIOBJ>(hBitmap),sizeof(BITMAP),reinterpret_cast<LPVOID>(&qB));
localDC = ::CreateCompatibleDC(hDC);
hOld = (HBITMAP)::SelectObject(localDC,hBitmap);
hWnd = CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,0,0,qB.bmWidth,qB.bmHeight,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0)){
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return (int)messages.wParam;
}
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 在Linux for Windows上编译C++代码时出错
- 在Windows上用C++裁剪HBITMAP
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 在Windows中以.exe的形式运行c++
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 无法在windows控制台中为C++程序提供必要的输入
- 在Windows中查找扬声器输出的当前音量级别
- Windows.h与GLFW.h的接口
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在Linux和C++中的Windows上,散列字符串值会产生不同的输出
- Active Directory:从网络服务帐户下运行的Windows服务调用ADsOpenObject时失败
- c++17文件系统::recursive_directory迭代器()在mac上没有给出这样的目录,但在windows上
- 无法使用Qt Creator在Windows中构建yaml-cpp