Trying to take a screenshot of a window as a bitmap. The code below is creating a properly sized bitmap, but every pixel is black. In other words, GetDIBits is setting imageBuffer to all 0's.
The posted code saves a bitmap for every notepad open and visible on the screen. None of the asserts fail.
The BITMAPFILEHEADER and actual writing to a file is omitted, because the final for loop with asserts shows GetDIBits set imageBuffer to all 0's, so there's no need to examine code after that point.
(In the executable's properties, under Configuration Properties->General, I have Character Set to "Not Set" to avoid unicode necessities.)
#include "stdafx.h"
#include <vector>
#include <sstream>
#include <Windows.h>
#include <iostream>
#include <assert.h>
using namespace std;
BOOL CALLBACK getNotepadWindowsCallback(HWND window, LPARAM notepadWindowsLparam) {
if (NULL != GetParent(window)) {
return true;
}
if (false == IsWindowVisible(window)) {
return true;
}
char text[1024] = { 0 };
GetWindowText(window, text, sizeof(text));
if (NULL == strstr(text, " - Notepad")) {
return true;
}
reinterpret_cast<vector<HWND>*>(notepadWindowsLparam)->push_back(window);
return true;
}
vector<HWND> getNotepadWindows() {
vector<HWND> notepadWindows;
EnumWindows(getNotepadWindowsCallback, reinterpret_cast<LPARAM>(¬epadWindows));
return notepadWindows;
}
int _tmain(int argc, _TCHAR* argv[]) {
for (HWND notepadWindow : getNotepadWindows()) {
HDC notepadWindowDeviceContext = GetDC(notepadWindow);
assert(NULL != notepadWindowDeviceContext);
HDC memoryDeviceContext = CreateCompatibleDC(notepadWindowDeviceContext);
assert(NULL != memoryDeviceContext);
RECT notepadWindowRectangle;
if (0 == GetClientRect(notepadWindow, ¬epadWindowRectangle)) {
assert(true == false);
}
SIZE notepadWindowSize;
notepadWindowSize.cx = notepadWindowRectangle.right - notepadWindowRectangle.left + 1;
notepadWindowSize.cy = notepadWindowRectangle.bottom - notepadWindowRectangle.top + 1;
HBITMAP memoryBitmap = CreateCompatibleBitmap(notepadWindowDeviceContext, notepadWindowSize.cx, notepadWindowSize.cy);
assert(NULL != memoryBitmap);
HBITMAP defaultBitmap = static_cast<HBITMAP>(SelectObject(memoryDeviceContext, memoryBitmap));
assert(NULL != defaultBitmap);
assert(TRUE == BitBlt(memoryDeviceContext, 0, 0, notepadWindowSize.cx, notepadWindowSize.cy, notepadWindowDeviceContext, notepadWindowRectangle.left, notepadWindowRectangle.right, SRCCOPY));
BITMAPINFO bitmapinfo = { 0 };
bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapinfo.bmiHeader.biWidth = notepadWindowSize.cx;
bitmapinfo.bmiHeader.biHeight = notepadWindowSize.cy;
bitmapinfo.bmiHeader.biPlanes = 1;
bitmapinfo.bmiHeader.biBitCount = 4 * 8;
bitmapinfo.bmiHeader.biCompression = BI_RGB;
//bitmapinfo.bmiHeader.biSizeImage, per MSDN, may be set to zero for BI_RGB bitmaps
int imageBufferSize = notepadWindowSize.cx*notepadWindowSize.cy;
int* imageBuffer = new(int[imageBufferSize]);
// doing a memset here to initialize imageBuffer to 0's makes no change - leaving it out makes clear GetDIBits is setting imageBuffer to 0's, because it goes in with random garbage
assert(NULL != SelectObject(memoryDeviceContext, defaultBitmap)); // this must happen before GetDIBits, per MSDN, so memoryBitmap is not selected into a device context
int returnValue = GetDIBits(memoryDeviceContext, memoryBitmap, 0, notepadWindowSize.cy, static_cast<LPVOID>(imageBuffer), &bitmapinfo, DIB_RGB_COLORS);
assert(0 != returnValue);
cout << "returnValue is " << returnValue << endl; // shows proper number of lines is written
for (int i = 0; i < imageBufferSize; ++i) {
assert(0 == imageBuffer[i]);
}
DeleteDC(memoryDeviceContext);
ReleaseDC(NULL, notepadWindowDeviceContext);
}
}
Oops. Helps to give BitBlt notepadWindowRectangle.top instead of notepadWindowRectangle.right for its y1 parameter. Fixing that makes it work.
Related
This code disables 1 display out of 2. The task is to turn it on not from VK_SPACE, but from mouse movement.
I tried to do it through WM_MOUSEMOVE, but so far nothing has worked. I don't really understand how this problem can be implemented.
If there is an opportunity to implement this topic, I will be very grateful.
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 0);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
while (1)
{
if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}
I use GetCursorPos to get the real-time mouse position and perform an energy-saving operation when the mouse is over the edge of a monitor.Here is my code,I commented the code in detail.
#include <iostream>
#include <windows.h>
#include <vector>
#include <lowlevelmonitorconfigurationapi.h>
#include <windowsx.h>
#pragma comment(lib, "Dxva2.lib")
#define KEY_DOWN(key) ((::GetAsyncKeyState(key) & 0x80000) ? 1 : 0)
#define KEY_UP(key) ((::GetAsyncKeyState(key) & 0x80000) ? 0 : 1)
const BYTE PowerMode = 0xD6; // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;
// Monitor description struct
struct MonitorDesc
{
HANDLE hdl;
DWORD power;
};
// Monitor enumeration callback
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<MonitorDesc>* pMonitors = reinterpret_cast<std::vector<MonitorDesc>*>(dwData);
DWORD nMonitorCount;
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
{
PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
{
for (DWORD i = 0; i < nMonitorCount; i++)
{
MonitorDesc desc;
desc.hdl = pMons[i].hPhysicalMonitor;
pMonitors->push_back(desc);
}
}
delete[] pMons;
}
return TRUE;
}
// Switch monitor power
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
if (monitor.power == mode)
return;
SetVCPFeature(monitor.hdl, PowerMode, mode);
monitor.power = mode;
}
int main()
{
// Скрытие консоли
HWND hWnd;
AllocConsole();
hWnd = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 1);
std::vector<MonitorDesc> monitors;
EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));
// Init
for (auto& monitor : monitors)
{
monitor.power = PowerOn;
}
// Here select the first one monitor as example
MonitorDesc targetMonitor = monitors[0];
int currentMonitorID = 0; //need start this exe in monitors[0]
//Mouse position
LONG zx = -1;
LONG zy = -1;
POINT ptB = { 0, 0 };
while (1)
{
/*---------------------------------------------------------------*/
/*- - -*/
/*- - -*/
/*- - -*/
/*- monitors[0] - monitors[1] -*/
/*- - -*/
/*- - -*/
/*- - -*/
/*---------------------------------------------------------------*/
/* {1919,1079} */
LPPOINT xy = &ptB; //Location variables
GetCursorPos(xy); //Gets the current mouse position
//If the mouse moves, (i.e. the current coordinates change to print out the coordinates) print out the coordinates.
if ((zx != xy->x) || (zy != xy->y))
{
//Here you need to test the edge of your monitor[0]
//After Test, delete this and Hide the console by ShowWindow(hWnd, 0)
printf("x=%d,y=%d\n", xy->x, xy->y);
}
//The coordinate in the lower right corner of my monitor is {1919,1079}
if (xy->x > 1919 && currentMonitorID == 0)
{
currentMonitorID = 1;
MonitorSwitch(monitors[1], PowerOn);
MonitorSwitch(monitors[0], PowerOff);
}
else if ( xy->x <= 1919 && currentMonitorID == 1)
{
currentMonitorID = 0;
MonitorSwitch(monitors[0], PowerOn);
MonitorSwitch(monitors[1], PowerOff);
}
/*if (::GetAsyncKeyState('L') == -32767)
{
if (KEY_DOWN(VK_CONTROL) && KEY_DOWN(VK_MENU))
// turn off
if (targetMonitor.power == PowerOn)
MonitorSwitch(targetMonitor, PowerOff);
}
if (::GetAsyncKeyState(VK_SPACE) == -32767)
{
if (KEY_DOWN(VK_SPACE))
// turn on
MonitorSwitch(targetMonitor, PowerOn);
}*/
if (::GetAsyncKeyState('E') == -32767)
{
if (KEY_DOWN(VK_CONTROL))
return 0;
}
}
}
I have a textbox class that works nicely with wider characters such as a,b,c... but with characters like 'f' and 'l' it seems to incorrectly get the sizing of those characters, yet correctly get the sizing of the others? Here is the code for the 'highlighting' of the text for the textbox class, its a bit long ill fix that up later, but should documented enough to understand easily.
void Textbox::Highlight_Text(SDL_Renderer *renderer)
{
if (clickedOn == true){
int currentCharacterWidth = 0;
int currentCharacterHeight = 0;
int totalSize = 0;
SDL_Rect currentCharacterRect;
string currentCharacter, tempText;
if (highlightedCharacters.size() >= 1){ ///To make sure only 1 thing is highlighted, in conjunction with next part
highlighted = true;
}
if (highlighted == true){ /// if a part is highlighted, and is left highlighted, next time clicked, remove the highlighting and redo it
if (EVENTS.mouseClicked == false){
resetHighlightingNextClick = true;
}
}
if (resetHighlightingNextClick == true){
if (highlighted == true){
if (EVENTS.mouseClicked == true){ ///actually remove the highlighting
highlightedCharacters.clear();
indexOfCharactersHighlighted.clear();
highlighted = false;
resetHighlightingNextClick = false;
}
}
}
for (int i=0; i < textboxText.Get_Text().size(); i++){
currentCharacter = textboxText.Get_Text()[i];
TTF_SizeText(textboxText.fonts[textboxText.fontIndex], currentCharacter.c_str(), ¤tCharacterWidth, ¤tCharacterHeight);
///the totalSize added to rectangle is not making it wider, its adjusting its x value offset
currentCharacterRect = {textboxText.x + totalSize, textboxText.y + int(textboxText.textSize*0.1), currentCharacterWidth, currentCharacterHeight};
totalSize += currentCharacterWidth; ///"current" size of text in loop to get x value of specific character clicked on
///If mouse is touching any of the characters in the text
if ( SDL_PointInRect(&EVENTS.mousePos, ¤tCharacterRect) ){
EVENTS.Change_Cursor(SDL_SYSTEM_CURSOR_IBEAM);
if (EVENTS.mouseClicked == true){ ///Clicking on the text to highlight
if (In_Array(highlightedCharacters, currentCharacterRect.x) == false ){
highlightedCharacters.push_back(currentCharacterRect); ///If there is no duplicates
indexOfCharactersHighlighted.push_back(i); ///Get index of text being highlighted, its always in order too
}
if ( currentCharacterRect.x != highlightedCharacters[highlightedCharacters.size()-1].x){ ///So they don't stack up highlights, ie, you can remove them
/// If the mouse is not highlighting the last one, say second last on the right for example, delete the one in front of it (last one)
///Like when highlighting text with mouse, it adapts to how you move it, so it unhighlights text not being highlighted
highlightedCharacters.pop_back();
indexOfCharactersHighlighted.pop_back();
}
}
}
}///End for loop
if (highlighted == true ){
if (EVENTS.backspacePressed == true || EVENTS.currentKey != ""){
tempText = textboxText.Get_Text();
///remove highlighted characters
if (indexOfCharactersHighlighted.size() != 0){
///the range of values highlighted will always be in a sorted order
tempText.erase( Min(indexOfCharactersHighlighted) , Max(indexOfCharactersHighlighted)-Min(indexOfCharactersHighlighted)+1 ); ///erase the range of values highlighted
textboxText.Change_Text(renderer, tempText);
///once removed text, clear every highlighted related thing
highlightedCharacters.clear();
indexOfCharactersHighlighted.clear();
highlighted = false;
resetHighlightingNextClick = false;
EVENTS.backspacePressed = false;
EVENTS.currentKey = "";
}
}
}
} ///End if for clicked on
///fit with scrolling offsets
if (EVENTS.scrolled == true){
for (int p=0; p < highlightedCharacters.size(); p++){
highlightedCharacters[p].y += EVENTS.scrollVal;
}
}
///Drawing the highlighted text
if (highlighted == true && clickedOn == true){
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 55,60,65, 75);
for (int j=0; j < highlightedCharacters.size(); j++){
SDL_RenderFillRect(renderer, &highlightedCharacters[j]);
}
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
}
///when clicked off textbox, clear everything/highlighting
if (clickedOn == false){
highlightedCharacters.clear();
indexOfCharactersHighlighted.clear();
highlighted = false;
}
}
For reference in the font passed in, here is how i obtain it in the text class
fontIndex = textSize-lowestFontSize -1;
///One time setups
if (numOfInstances == 1){
try{
TTF_Init();
//cout << "Initialised ttf" << endl;
}
catch (exception &err){
cout << "Could not initialise ttf for text \"" << text << "\". Error from SDL is: " << TTF_GetError() << ". Error from C++ is: " << err.what() << endl;
}
for (int i=lowestFontSize; i <= highestFontSize; i++){
TTF_Font *currentFont = TTF_OpenFont(fontType.c_str(), i);
if (!currentFont){
cout << "Error with font in text \"" << txt << "\" Error is: " << SDL_GetError() << endl;
}
fonts.push_back(currentFont);
}
}
and so if i pass in, say 25 as my text size, i have my lowestFontSize = 10 and highestFontSize = 100, so the index i need for size 25 would be (25-10 -1 = 14), as indexing begins at 0, which is that first line before i create my static vector of fonts in the text class. Here is a snippet of what i'm trying to explain:
This is clearly working properly.
But now, it is completely inaccurate. If i select a random character towards the end of the text, it is not correctly highlighted, only from the beginning the first one looks pretty much perfect, but then it seems as if the inaccuracy is compounded, hence making the total grey highlighting much wider than it is supposed to be.
The problem with your method is that individual character widths are meaningless. The renderer adjusts them depending on context (their neighbours in the rendered string). So the width of i in the rendered string bit is not necessarily the same as the width of i in the rendered string fil.
The method to find text selection coordinates needs to take context into account.
Say we have the width of the three strings:
prefixWidth is the size of the prefix (the original line of text up to but not including the selection)
selWidth is the width of the selection itself
totalWidth is the width of the prefix and the selection concatenated
(we don't care about the portion after the selection). The first two widths will be close to the third, but will not add up because of the kerning between the last character of the prefix and the first character of the selection. The difference is the correction you need to apply to the X coordinate of the rendered selection. So we need to start rendering selection at the X coordinate which is not prefixWidth, but
prefixWidth + (totalWidth - (prefixWidth + selWidth))
which is the same as
totalWidth - selWidth
so you don't really need to calculate prefixWidth at all.
A full (semi-)working example program below. The arguments are (1) font file full path (2) font size (3) string to render (4) selection start (5) selection length. The selection is rendered on top of the original text, shifted 1 pixel down and right, so it is easy to see if there is any deviation.
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
char* substr(const char* src, int fromChar, int subLen)
{
if (subLen < 0 || fromChar < 0 || fromChar + subLen > (int)strlen(src))
{
fprintf (stderr, "invalid substring\n");
exit (EXIT_FAILURE);
}
char* z = (char*)malloc(subLen);
strncpy(z, src + fromChar, subLen);
z[subLen] = '\0';
return z;
}
void textExtent(TTF_Font* font, const char* text,
int fromChar, int subLen, int* w, int* h)
{
int l = strlen(text);
if (subLen == -1) subLen = l;
if (fromChar < 0 || subLen > l)
{
fprintf (stderr, "Bad text extent\n");
exit (EXIT_FAILURE);
}
char* z = substr(text, fromChar, subLen);
TTF_SizeUTF8(font, z, w, h);
free(z);
}
int textWidth(TTF_Font* font, const char* text,
int fromChar, int subLen)
{
int w, h;
textExtent(font, text, fromChar, subLen, &w, &h);
return w;
};
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
if (argc != 6)
{
fprintf (stderr, "usage: %s font text from length\n", argv[0]);
exit(EXIT_FAILURE);
}
const char* fontpath = argv[1];
int fontSz = atoi(argv[2]);
const char* txt = argv[3];
int from = atoi(argv[4]);
int len = atoi(argv[5]);
int tsize = strlen(txt);
if (from < 0 || from + len >= tsize)
{
fprintf (stderr, "Invalid text portion to highlight\n");
exit (EXIT_FAILURE);
}
if (fontSz < 2 || fontSz > 300)
{
fprintf (stderr, "Invalid font size\n");
exit (EXIT_FAILURE);
}
// open font to render with
TTF_Font * font = TTF_OpenFont(fontpath, fontSz);
if (!font)
{
fprintf (stderr, "Could not open font %s\n", fontpath);
exit (EXIT_FAILURE);
}
// Query text size
int textW, textH;
textExtent(font, txt, 0, -1, &textW, &textH);
SDL_Window * window = SDL_CreateWindow("SDL_ttf in SDL2",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, textW, textH, 0);
// Query selection coords
//
int selWidth = textWidth(font, txt, from, len);
int totalWidth = textWidth(font, txt, 0, from+len);
// Render portions of text
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Color color = { 255, 128, 0, 0 };
SDL_Surface * surface = TTF_RenderUTF8_Blended(font, txt, color);
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
SDL_Rect dstrect = { 0, 0, textW, textH };
SDL_Color color2 = { 0, 128, 255, 128 };
char* s = substr(txt, from, len);
SDL_Surface * surface2 = TTF_RenderUTF8_Blended(font, s, color2);
free(s);
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
SDL_Texture * texture2 = SDL_CreateTextureFromSurface(renderer, surface2);
SDL_SetTextureAlphaMod(texture2, 128);
SDL_SetTextureBlendMode(texture2, SDL_BLENDMODE_BLEND);
SDL_Rect dstrect2 = {totalWidth - selWidth + 1, 1, selWidth, textH };
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dstrect);
SDL_RenderCopy(renderer, texture2, NULL, &dstrect2);
//Update screen
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return 0;
}
I wonder how to solve this problem. I want to check every X seconds if the screen contains an image (for example a red dot) and if so return True. I am very familiar with Python, and there are some easy solutions there. But I haven't found a similar solution yet.
What I basically want to do is:
Take a screenshot
Locate image X on the screenshot
Return bool
Looked into OpenCV and would be possible to solve it that way, but might be a bit overextending. I was thinking about getPixel to loop over all the pixels in the screen. But it's extremely slow.
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HWND runelite = GetForegroundWindow();
HMONITOR monitor = MonitorFromWindow(runelite, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
int r, g, b;
HDC screenshot = GetDC(NULL);
for (int i = 0; i < monitor_height; i++) {
for (int j = 0; j < monitor_width; j++) {
DWORD color = GetPixel(screenshot, j, i);
cout << "Scanning -> X: " << j << " Y: " << i << endl;
r = GetRValue(color);
g = GetGValue(color);
b = GetBValue(color);
if (r == 0 && g == 0 && b == 0) {
cout << "Button found by color!" << endl;
goto end;
}
}
}
end:
ReleaseDC(NULL, screenshot);
return 0;
}
you could greatly increase the speed if you copy your HDCs content to another bitmap and get a pointer to the image data and loop over this.
create a memory bitmap
HDC memDC = CreateCompatibleDC ( hDC );
HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
SelectObject ( memDC, memBM );
then bitblt the screen data to that bitmap via BitBlt and get the bitmap data with GetDIBits.
please also note, that GetDC(NULL) does not make a screenshot, but gets you access to windows live main HDC. drawing to it directly draws on the desktop.
Thatswhy every GetPixel on it does take quite long.
Within the property window of a JPEG image, there is a tab called 'Summary'. Within this tab, there is a field called 'Comments' I would like to write some MFC code which will add a given string to this field e.g "This is a photo".
Does some kind soul out there know how to do this?
Many thanks.
MFC doesn't provide this functionality, however you can use GDI+ for the task. The Image class is capable of reading and writing Exif metadata.
This is almost trivial, and explained under Reading and Writing Metadata. However, since the UserComment metadata tag allows for different character encodings, things get a little more involved. The following code1) implements a command line utility that allows to set (or replace if present) the UserComment field:
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib, "Gdiplus.lib")
int wmain( int argc, const wchar_t* argv[] ) {
// Input format: AddExifComment "<input filename>" "<comment>" "<output filename>"
if (argc != 4)
return -1;
// Initialize GDI+
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken{ 0 };
GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, nullptr );
// Load image
Image* img = new Image(argv[1]);
// Construct Unicode comment
const auto& comment = MakeUnicodeComment(argv[2]);
// Assign the UserComment property
PropertyItem propertyItem;
propertyItem.id = PropertyTagExifUserComment;
propertyItem.length = comment.size();
propertyItem.type = PropertyTagTypeUndefined;
propertyItem.value = (void*)comment.data();
img->SetPropertyItem(&propertyItem);
// Save image
CLSID clsid;
GetEncoderClsid(L"image/jpeg", &clsid);
img->Save(argv[3], &clsid);
// Cleanup
delete img;
GdiplusShutdown(gdiplusToken);
return 0;
}
Constructing an appropriately formatted comment takes a bit of work. This is implemented in the following function:
#include <vector>
using std::vector;
#include <iterator>
using std::back_inserter;
vector<BYTE> MakeUnicodeComment(const wchar_t* text){
// Exif 2.2 header for Unicode (UCS-2): 'U', 'N', 'I', 'C', 'O', 'D', 'E', '\0'
static const char header[]{"UNICODE"};
static const size_t headerSize{ sizeof(header) / sizeof(header[0]) };
// UserComment field contains the 8-byte header followed by UTF-16LE encoded code units
vector<BYTE> buffer;
std::copy(header, header + headerSize, back_inserter(buffer));
// Append comment text (NUL terminator is not required)
auto current = text;
while (*current) {
buffer.push_back(*current & 0xFF);
buffer.push_back((*current >> 8) & 0xFF);
++current;
}
return buffer;
}
Retrieving the JPEG encoder from its MIME type is copied from Retrieving the Class Identifier for an Encoder for reference:
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) {
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j) {
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) {
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
1) Error handling elided for brevity.
OK,Finally I manage to solove the problem!There is the code:
OK,Finally I manage to solove the problem!There is the code:
#include "stdafx.h"
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num= 0;
UINT size= 0;
ImageCodecInfo* pImageCodecInfo= NULL;
GetImageEncodersSize(&num, &size);
if(size== 0)
{
return -1;
}
pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo== NULL)
{
return -1;
}
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j=0; j< num; ++j)
{
if(wcscmp(pImageCodecInfo[j].MimeType, format)== 0)
{
*pClsid= pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j;
}
}
free(pImageCodecInfo);
return -1;
}
// load bitmap from memory,sync way
Bitmap* LoadBitmapFromMemory(const void* memory, DWORD size)
{
Bitmap* bmp = NULL;
IStream* stream = NULL;
if (CreateStreamOnHGlobal(NULL, TRUE, &stream) == S_OK)
{
ULARGE_INTEGER uli;
uli.QuadPart = size;
stream->SetSize(uli);
if (stream->Write(memory, size, NULL) == S_OK)
bmp = new Bitmap(stream);
stream->Release();
}
return bmp;
}
// load bitmap from file,sync way
Bitmap* LoadBitmapFromFile(const TCHAR* file_name)
{
Bitmap* bmp = NULL;
HANDLE file_handle = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file_handle != INVALID_HANDLE_VALUE)
{
DWORD temp = 0;
DWORD file_size = GetFileSize(file_handle, &temp);
if (file_size && !temp) // the file must be less than 4G
{
unsigned char* buffer = new unsigned char[file_size];
if (ReadFile(file_handle, buffer, file_size, &temp, NULL))
bmp = LoadBitmapFromMemory(buffer, temp);
delete [] buffer;
}
CloseHandle(file_handle);
}
return bmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Status stat;
CLSID clsid;
char propertyValue[] = "Fake Photograph";
Bitmap* bitmap = LoadBitmapFromFile(L"E:/sandbox/stone.jpg");
PropertyItem* propertyItem = new PropertyItem;
// Get the CLSID of the JPEG encoder.
GetEncoderClsid(L"image/jpeg", &clsid);
propertyItem->id = PropertyTagCopyright;
propertyItem->length = 16; // string length including NULL terminator
propertyItem->type = PropertyTagTypeASCII;
propertyItem->value = propertyValue;
bitmap->SetPropertyItem(propertyItem);
stat = bitmap->Save(L"E:/sandbox/stone.jpg", &clsid, NULL);
if(stat == Ok)
printf("FakePhoto2.jpg saved successfully.\n");
delete propertyItem;
delete bitmap;
GdiplusShutdown(gdiplusToken);
return 0;
return 0;
}
I got a problem with changing console size. This is my code:
BOOL setConsole(int x, int y)
{
hStdin = GetStdHandle(STD_INPUT_HANDLE);
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE ||
hStdout == INVALID_HANDLE_VALUE)
{
MessageBox(NULL, TEXT("GetStdHandle"),
TEXT("Console Error"), MB_OK);
return false;
}
SMALL_RECT windowSize = {0, 0, x-1, y-1};
// Change the console window size:
SetConsoleWindowInfo(hStdout, TRUE, &windowSize);
COORD c = { x, y};
//Change the internal buffer size:
SetConsoleScreenBufferSize(hStdout, c);
SetConsoleDisplayMode(hStdout,CONSOLE_FULLSCREEN_MODE, &c);
return true;
}
It works perfectly fine, when I try to enlarge the console. When one parameter is smaller than previous one nothing happens. What is wrong?
#edit: after some tests I noticed, that resizing(reducing) is possible if I change one parameter at once. Example(assume console is 100x100)
setConsole(90,90); //dosen't work.
setConsole(90,100);
setConsole(90,90); // works perfectly
WHY?!
SetConsoleScreenBufferSize changes the size of the internal buffer of the console.
Changing it has no effect on the console windows extent.
Call SetConsoleWindowInfo if you need an effect on the visible part of the console (buffer).
The window buffer cannot be smaller than the internal buffer , and decreasing it will also decrease the internal buffer,
but not the other way around.
If you call SetConsoleScreenBufferSize with illegal value in COORDS (e.g. too little height/width) then you get an
error, usually 87 'invalid argument'.
Try this code:
#include <iostream>
#include <windows.h>
using namespace std;
void SetWindow(int Width, int Height)
{
_COORD coord;
coord.X = Width;
coord.Y = Height;
_SMALL_RECT Rect;
Rect.Top = 0;
Rect.Left = 0;
Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
}
int main(void)
{
SetWindow(80,40);
int dx=1,i=5,l=0;
while(l<5)
{
i=i+dx;
if( (i<1) || (i>10)){ dx=-dx; l++;}
SetWindow(10*i,5*i);
Sleep(100);
}
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}
Late to the party ...
As far as can be devised from MSDN and a few tests, the screen buffer can't be set smaller than the window's extent or the window's extent made bigger than the screen buffer.
One hack is to shrink the window to a minimal before changing the buffer size :
static void
set_console_size(HANDLE screen_buffer, SHORT width, SHORT height)
{
assert(screen_buffer != NULL);
assert(width > 0);
assert(height > 0);
COORD const size = { width, height };
BOOL success;
SMALL_RECT const minimal_window = { 0, 0, 1, 1 };
success = SetConsoleWindowInfo(screen_buffer, TRUE, &minimal_window);
CHECK(success);
success = SetConsoleScreenBufferSize(screen_buffer, size);
CHECK(success);
SMALL_RECT const window = { 0, 0, size.X - 1, size.Y - 1 };
success = SetConsoleWindowInfo(screen_buffer, TRUE, &window);
CHECK(success);
}
try this code:
system("mode 650");
I modified the code provided by 'Software_Designer' on Oct 15' 12 and created a command line utility to set the console size and scroll buffers.
I compiled it using DEV C++ (http://www.bloodshed.net/devcpp.html).
An executable is included in https://sourceforge.net/projects/wa2l-wintools/. I hope this helps.
/*
* consolesize.cpp - set console size and buffer dimensions
*
* [00] 02.07.2016 CWa Initial Version
*
* inspired by: http://stackoverflow.com/questions/12900713/reducing-console-size
*
*/
#include <iostream>
#include <windows.h>
using namespace std;
// SetWindow(Width,Height,WidthBuffer,HeightBuffer) -- set console size and buffer dimensions
//
void SetWindow(int Width, int Height, int WidthBuffer, int HeightBuffer) {
_COORD coord;
coord.X = WidthBuffer;
coord.Y = HeightBuffer;
_SMALL_RECT Rect;
Rect.Top = 0;
Rect.Left = 0;
Rect.Bottom = Height - 1;
Rect.Right = Width - 1;
HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle
SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size
SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size
} // SetWindow
// main(Width,Height,WidthBuffer,HeightBuffer) -- main
//
int main(int argc, char *argv[]) {
int width = 80;
int height = 25;
int wbuffer = width + 200;
int hbuffer = height + 1000;
if ( argc == 5 ){
width = atoi(argv[1]);
height = atoi(argv[2]);
wbuffer = atoi(argv[3]);
hbuffer = atoi(argv[4]);
} else if ( argc > 1 ) {
cout << "Usage: " << argv[0] << " [ width height bufferwidth bufferheight ]" << endl << endl;
cout << " Where" << endl;
cout << " width console width" << endl;
cout << " height console height" << endl;
cout << " bufferwidth scroll buffer width" << endl;
cout << " bufferheight scroll buffer height" << endl;
return 4;
}
SetWindow(width,height,wbuffer,hbuffer);
return 0;
}
/* So, tiny recap --
SetConsoleScreenBufferSize(): fails if buffer width and height
are less than the width and height of the current screen window
rectangle.
SetConsoleWindowInfo(): fails if the specified window rectangle
is larger than the boundaries of the screen buffer.
Setting the correct one 1st (buffer or window rectangle) would
solve much in a screen resize, but that's hard to know because
calls to GetConsoleScreenBufferInfo() fail easily, and so you cannot
reliably discover the current screen rectangle and/or buffer size,
and hence you cannot know if you'll be setting the screen rectangle
larger or smaller than the current size, and in turn you can't
always know which function to call 1st.
WORKAROUND:
Sloppy solution but super-reliable...
- Set the buffer 1st to something uncommonly large -- like 1000x1000.
- Then set the window rectangle to the actual desired size.
- Finally, set the window buffer to the actual desired size
(matching the screen rectangle) */
int set_screen_size(int requested_wdth, int requested_hgt)
{
SMALL_RECT srWindowRect; /* hold the new console size */
COORD coordBuffer;
BOOL bsuccess = FALSE, wsuccess = FALSE;
srWindowRect.Left = 0;
srWindowRect.Right = requested_wdth-1;
srWindowRect.Top = 0;
srWindowRect.Bottom = requested_hgt-1;
/*set buffer oversize -- like 1000x1000 (500x500 and less
could do too)*/
coordBuffer.X = coordBuffer.Y = 1000;
bsuccess = SetConsoleScreenBufferSize(hConOut, coordBuffer);
wsuccess = SetConsoleWindowInfo(hConOut, TRUE, &srWindowRect);
coordBuffer.X = requested_wdth;
coordBuffer.Y = requested_hgt;
bsuccess = SetConsoleScreenBufferSize(hConOut, coordBuffer);
/* ... */
}