//显示图片
INT CaptureImage(HWND hWnd /*Handle of Main Window*/){
HBITMAP bit1=LoadBitmap(NULL,MAKEINTRESOURCE(IDB_winBit1)); //處理圖片並输入bit1
PAINTSTRUCT ps;
auto hdc=BeginPaint(hWnd,&ps); //开始 paint
HDC compDC1=::CreateCompatibleDC(hdc);
HGDIOBJ hg1=SelectObject(compDC1,bit1); //储存原本的HGIOBJ并 Select 图片(bit1)
if(BitBlt(hdc,10,100,500,250,compDC1,0,0,SRCCOPY)==0){
MessageBox(hWnd,L"Error",NULL,0);
}
//出错 的 处理
SelectObject(compDC1,hg1);
EndPaint(hWnd,&ps);
return 0;
}
上面的C++代码没法在Frame上显示Bitmap
//#include "stdafx.h"
#include "win32 Screen Capture.h"#define MAX_LOADSTRING 100// 全域變數:
HINSTANCE hInst; // 目前執行個體
TCHAR szTitle[MAX_LOADSTRING]; // 標題列文字
TCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類別名稱
HWND winButton1; //button1's handle
HWND winButton2; //button2's handle
HWND winButton3; //button3's handle
HWND winButton4; //button4's handle
int baseX = 10; //base x-cord
int baseY = 10; //base y-cord// 這個程式碼模組中所包含之函式的向前宣告:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK FuncWMCreate(HWND);
INT ClosePrompt(HWND);
INT CaptureImage(HWND);int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此置入程式碼。
MSG msg;
HACCEL hAccelTable; // 初始化全域字串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32SCREENCAPTURE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance); // 執行應用程式初始設定:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
} hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32SCREENCAPTURE)); // 主訊息迴圈:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
} return (int) msg.wParam;
}//
// 函式: MyRegisterClass()
//
// 用途: 註冊視窗類別。
//
// 註解:
//
// 只有當您希望此程式碼能相容比 Windows 95 的
// 'RegisterClassEx' 函式更早的 Win32 系統時,
// 才會需要加入及使用這個函式。
// 您必須呼叫這個函式,讓應用程式取得與它相關的
// 「格式正確」的圖示。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32SCREENCAPTURE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32SCREENCAPTURE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex);
}//
// 函式: InitInstance(HINSTANCE, int)
//
// 用途: 儲存執行個體控制代碼並且建立主視窗
//
// 註解:
//
// 在這個函式中,我們會將執行個體控制代碼儲存在全域變數中,
// 並且建立和顯示主程式視窗。
//
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;
}//
// 函式: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 用途: 處理主視窗的訊息。
//
// WM_COMMAND - 處理應用程式功能表
// WM_PAINT - 繪製主視窗
// WM_DESTROY - 顯示結束訊息然後返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc; switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 剖析功能表選取項目:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
if(ClosePrompt(hWnd)==1 /*close*/ ){
DestroyWindow(hWnd);
}else{/*do nothing, back to main widnow*/}
break;
case IDM_CAPTURE:
{
CaptureImage(hWnd);
break;
}
//button's action
case button1:{
CaptureImage(hWnd);
break;
}
case button2:{UpdateWindow(hWnd);
break;
}
case button3:{Beep(500,3000);
break;
}
case button4:{
if(ClosePrompt(hWnd)==1){
DestroyWindow(hWnd);
}else{}
break;
} default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此加入任何繪圖程式碼...
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
if(ClosePrompt(hWnd)==1 /*close*/ ){
DestroyWindow(hWnd);
}else{/*do nothing, back to main widnow*/}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_CREATE:
{
FuncWMCreate(hWnd);
break;
}
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}// [關於] 方塊的訊息處理常式。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE; case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}else if(LOWORD(wParam)==IDC_accButton1){ /*accept button*/
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}//Handle WM_CREATE Event of Main Window
LRESULT CALLBACK FuncWMCreate(HWND hWnd){
HINSTANCE hInstance;
hInstance = (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
winButton1 = CreateWindow(
TEXT("BUTTON"),
TEXT("Capture"),
WS_VISIBLE | WS_CHILD,
baseX,baseY,
80,25,
hWnd,(HMENU)button1,
hInstance,NULL);
winButton2=CreateWindow(
TEXT("BUTTON"),
TEXT("Redraw"),
WS_VISIBLE | WS_CHILD,
baseX+=80,baseY,
80,25,
hWnd,(HMENU)button2,
hInstance,NULL);
winButton3=CreateWindow(
TEXT("BUTTON"),
TEXT("Beep"),
WS_VISIBLE|WS_CHILD,
baseX+=80,baseY,
80,25,
hWnd,(HMENU)button3,
hInstance,NULL);
winButton4=CreateWindow(
TEXT("BUTTON"),
TEXT("Exit"),
WS_VISIBLE|WS_CHILD,
baseX+=80,baseY,
80,25,
hWnd,(HMENU)button4,
hInstance,NULL);
return (LRESULT) 0;
}//Handle Close Prompt Event
INT ClosePrompt(HWND hWnd){
if(MessageBox(hWnd,TEXT("Do you really want to close the window?"),TEXT("Close?"),MB_YESNO)==IDYES){
return 1;
}else{
return 0;
}
}//显示图片
INT CaptureImage(HWND hWnd /*Handle of Main Window*/){
HBITMAP bit1=LoadBitmap(NULL,MAKEINTRESOURCE(IDB_winBit1)); //處理圖片並输入bit1
PAINTSTRUCT ps;
auto hdc=BeginPaint(hWnd,&ps); //开始 paint
HDC compDC1=::CreateCompatibleDC(hdc);
HGDIOBJ hg1=SelectObject(compDC1,bit1); //储存原本的HGIOBJ并 Select 图片(bit1)
if(BitBlt(hdc,10,100,500,250,compDC1,0,0,SRCCOPY)==0){
MessageBox(hWnd,L"Error",NULL,0);
}
//出错 的 处理
::SelectObject(compDC1,hg1);
EndPaint(hWnd,&ps);
return 0;
}win32 Screen Capture.h#pragma once#include "resource.h"
#ifndef buttonDefines
#define buttonDefines
#define button1 1001
#define button2 1002
#define button3 1003
#define button4 1004
#endif
// 或是經常使用卻很少變更的
// 專案專用 Include 檔案
//#pragma once#include "targetver.h"#define WIN32_LEAN_AND_MEAN // 從 Windows 標頭排除不常使用的成員
// Windows 標頭檔:
#include <windows.h>// C RunTime 標頭檔
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
// TODO: 在此參考您的程式所需要的其他標頭stdafx.cpp// stdafx.cpp : 僅包含標準 Include 檔的原始程式檔
// win32 Screen Capture.pch 會成為先行編譯標頭檔
// stdafx.obj 會包含先行編譯型別資訊#include "stdafx.h"// TODO: 在 STDAFX.H 中參考您需要的任何其他標頭,
// 而不要在這個檔案中參考targetver.h#pragma once// 加上 SDKDDKVer.h 可定義最高可用的 Windows 平台。// 如果要針對先前的 Windows 平台建置應用程式,請加上 WinSDKVer.h,
// 並在加上 SDKDDKVer.h 之前將 _WIN32_WINNT 巨集設為要支援的平台。#include <SDKDDKVer.h>
Resource.h//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by win32 Screen Capture.rc
//
#define IDC_MYICON 2
#define IDD_WIN32SCREENCAPTURE_DIALOG 102
#define IDS_APP_TITLE 103
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_WIN32SCREENCAPTURE 107
#define IDI_SMALL 108
#define IDC_WIN32SCREENCAPTURE 109
#define IDR_MAINFRAME 128
#define IDB_BITMAP1 130
#define IDB_winBit1 130
#define IDC_accButton1 1000
#define IDM_CAPTURE 32773
#define IDC_STATIC -1// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 131
#define _APS_NEXT_COMMAND_VALUE 32776
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
The LoadBitmap function loads the specified bitmap resource from a module's executable file. This function has been superseded by theLoadImage function. HBITMAP LoadBitmap(
HINSTANCE hInstance, // handle to application instance
LPCTSTR lpBitmapName // address of bitmap resource name
);
//
HBITMAP bit1=LoadBitmap(NULL,MAKEINTRESOURCE(IDB_winBit1)); //處理圖片並输入bit1
//
HBITMAP bit1=LoadBitmap(hInst,MAKEINTRESOURCE(IDB_winBit1)); //處理圖片並输入bit1
It does not working.. I've tried LoadImage but it does not work too.