C++ Add A Bitmap from resource to Picture Control - c++

i want to display a bitmap on my Picture control,
but actually no matter what Im trying, it doesnt work.
Right now my method looks like this:
SendDlgItemMessage(hWnd ,picture_CTRL, BM_SETIMAGE , IMAGE_BITMAP, (LPARAM) MAKEINTRESOURCE(IDB_BITMAP1));
I have also already tried to use the
STM_SETIMAGE
Message, but it wont work as well.
My Picture Control property "Type" is set to "Bitmap" and my Bitmap has an ID in resource.h
Hope you guys can help :)

First, you can't just mix and match messages in standard Windows controls. BM_SETIMAGE and STM_SETIMAGE have completely different values. If you are using a Static control, use STM_SETIMAGE. If you are using a Button control, use BM_SETIMAGE.
Second, the documentation for STM_SETIMAGE says that the LPARAM must be a handle to the image. You are passing a resource ID. You need to get the handle by calling LoadImage first. For example:
HBITMAP hBitmap = reinterpret_cast<HBITMAP>(LoadImage(
GetModuleHandle(NULL),
MAKEINTRESOURCE(IDB_BITMAP1),
IMAGE_BITMAP,
0,
0,
0));
SendDlgItemMessage(
hWnd,
picture_CTRL,
STM_SETIMAGE,
IMAGE_BITMAP,
reinterpret_cast<LPARAM>(hBitmap));
Note that according to the documentation you are responsible for deleting the bitmap when you are done with it.
EDITS: Note that I had two problems in my original version of the code, both caused by copying and pasting from the question. I failed to pass the handle as the LPARAM myself and I used BM_SETIMAGE instead of STM_SETIMAGE. I will also add a note in the answer that, as Cody Gray pointed out, the MSDN documentation for a function or message will tell you whether you can use MAKEINTRESOURCE.

Related

SendMessage and ncm not changing font

I have looked for answers and I have found some that didn't work, so I would be grateful if anybody can help with this.
From this post I used the NONCLIENTMETRICS typedef and the SendMessage function to find and create a font.
I have ran it before everything in the WM_CREATE switch, though the post said to run it before the window is created, and I'm pretty sure WM_CREATE is run after the window is created but I tried it anyway.
Code of the setfont() function:
void setfont(HWND hWnd) {
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont);
SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
}
Please tell me if I need to show any other code.
I have tried the CreateFont way, but that didn't work as well, so I'm hoping someone can tell me where I went skewer.
Note I'm using Visual Studio 2022
Thanks in advance.
Found the answer via this post
Steps:
Make the font, Via CreateFont or CreateFontIndirect
Add a HWND to the button or static control your trying to make
Last of all, use this line to set the font:
SendMessage([HWND of the control], WM_SETFONT, (WPARAM)[Font HFONT], TRUE);

WS_EX_TOOLWINDOW tool windows doesn't work as expected

In our legacy code Windows extended style WS_EX_TOOLWINDOW is being used.This is basically for showing the title bar narrow.But recently in the later winodws versions the title bar is not drawn as narrow.That is WS_EX_TOOLWINDOW doesnt give a narrow title bar in the newer windows versions.Making the title bar narrow is done on a click event.Let me know if there is another way of achieving this?
I have read that we need to handle WM_NCCALCSIZE.But is there any other way of doing it?.Or if this is the only way,how can I handle it in a button click?
Code Snippet:
HWND hwnd = m_hWnd;
......
DWORD dwStylesEx = ::GetWindowLong( hwnd, GWL_EXSTYLE );
if ( bNarrowTitle == true)
{
dwStylesEx |= WS_EX_TOOLWINDOW;
}
else
{
dwStylesEx &= ~WS_EX_TOOLWINDOW;
}
...
::SetWindowLong( hwnd, GWL_EXSTYLE, dwStylesEx );
MSDN says:
Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.
The default look just doesn't distinguish it in any way. Which suggests that you will just have to live with it.
It's probably been changed due to not being finger friendly if smaller!
Refer : WS_EX_TOOLWINDOW doesn't give look I want
As you said, handle WM_NCCALCSIZE may be the only way to handle the size of non-client areas.
Refer: How to set the size of the Non-client area of a Win32 window (native)

how to get text from a window with specific HWND?

I'm new to win32 programming and haven't worked around with cpp for a long time. What I intend to do is to get a window's HWND via spy++, and get the text of this window. The problem is that I don't know how to create a HWND object, could anyone give me some idea? Thanks a lot!
If you have the numeric value of the HWND, you can cast it to the right type. Start with an integer of the right size, e.g.:
uintptr_t numeric_hwnd = 0x987654;
HWND hwnd = reinterpret_cast<HWND>(numeric_hwnd);

Why SelectObject is called twice?

I read code in MFC, but was confused with the code below:
void EditView::ResetDefaultFont()
{
HFONT hFont = (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
CDC* pDC = GetDC();
CFont* pFont = pDC->SelectObject(CFont::FromHandle(hFont));
pDC->SelectObject(pFont);
::DeleteObject(hFont);
}
Why CDC Select the default font first(CFont* pFont = pDC->SelectObject(CFont::FromHandle(hFont));), but select pFont again?
The first SelectObject call changes the font selected in the device context.
The second SelectObject call resets the font to whatever it was before the first call.
While that answers the "why" for the second call, which is what you ask about, I do not have any idea what the point of doing the complete call sequence is. I find no documentation results for ResetDefaultFont, neither online in MSDN Library nor in the local Visual Studio 2012 help. Just to be thorough I created a new default MFC project in VS 2012, and used the identifier ResetDefaultFont in the constructor of a class derived from CEditView. It did not compile: no such.
So,
where did you get that ResetDefaultFont function from?
The answer is quite simple.
This code is just for getting the current font of the DC.
If they had placed the code following these statements, it would have been obvious.

Reading from a text field in another application's window

Is there a way for a Windows application to access another applications data, more specifically a text input field in the GUI, and grab the text there for processing in our own application?
If it is possible, is there a way to "shield" your application to prevent it?
EDIT: The three first answers seem to be about getting the another applications window title, not a specific text input field in that window.
I'm no Windows API expert, so could you be more exact how do I find a certain text field in that window, what are the prerequisites for it (seems like knowing a window handle something is required, does it require knowing the text field handle as well? How do I get that? etc...)
Code snippets in C++ really would be really appreciated. MSDN help is hard to browse since Win32-API has such horrible naming conventions.
Completed! See my answer below for a how-to in C++.
For reading text content from another application's text box you will need to get that text box control's window handle somehow. Depending on how your application UI is designed (if it has a UI that is) there are a couple of different ways that you can use to get this handle. You might use "FindWindow"/"FindWindowEx" to locate your control or use "WindowFromPoint" if that makes sense. Either way, once you have the handle to the text control you can send a "WM_GETTEXT" message to it to retrieve its contents (assuming it is a standard text box control). Here's a concocted sample (sans error checks):
HWND hwnd = (HWND)0x00310E3A;
char szBuf[2048];
LONG lResult;
lResult = SendMessage( hwnd, WM_GETTEXT, sizeof( szBuf ) / sizeof( szBuf[0] ), (LPARAM)szBuf );
printf( "Copied %d characters. Contents: %s\n", lResult, szBuf );
I used "Spy++" to get the handle to a text box window that happened to be lying around.
As for protecting your own text boxes from being inspected like this, you could always sub-class your text box (see "SetWindowLong" with "GWL_WNDPROC" for the "nIndex" parameter) and do some special processing of the "WM_GETTEXT" message to ensure that only requests from the same process are serviced.
OK, I have somewhat figured this out.
The starting point is now knowing the window handle exactly, we only know partial window title, so first thing to do is find that main window:
...
EnumWindows((WNDENUMPROC)on_enumwindow_cb, 0);
...
which enumerates through all the windows on desktop. It makes a callback with each of these window handles:
BOOL CALLBACK on_enumwindow_cb(HWND hwndWindow, LPARAM lParam) {
TCHAR wsTitle[2048];
LRESULT result;
result = SendMessage(hwndWindow, WM_GETTEXT, (WPARAM) 2048, (LPARAM) wsTitle);
...
and by using the wsTitle and little regex magic, we can find the window we want.
By using the before mentioned Spy++ I could figure out the text edit field class name and use it to find wanted field in the hwndWindow:
hwndEdit = FindWindowEx(hwndWindow, NULL, L"RichEdit20W", NULL);
and then we can read the text from that field:
result = SendMessage(hwndEdit, WM_GETTEXT, (WPARAM) 4096, (LPARAM) wsText);
I hope this helps anyone fighting with the same problem!
Look at AutoHotkey. If you need an API for your application, look at their sources.
To prevent it, use a custom widget instead of WinForms, MFC or Win32 API. That is not foolproof, but helps.
Yes it is possible in many ways (one way is to use WINAPI GetWindow and GetWindowText).
First, get a handle to the textbox you want to retrieve text from (using FindWindow, EnumChildWindows and other APIs), then:
Old VB6-codeexample, declaration of API:
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Code to extract text:
Dim MyStr As String
MyStr = String(GetWindowTextLength(TextBoxHandle) + 1, Chr$(0))
GetWindowText TextBoxHandle, MyStr, Len(MyStr)
MsgBox MyStr
About how to shield the application to prevent it, you could do many things.
One way would be to have a own control to handle text input that build up the text from lets say a couple of labels placed where the text would be, or that draws the text graphically.
You can also get text from a richedit control with EM_GETTEXTRANGE message, but it works only in the same process in which the control was created.