How to print format string into a window?
A window handle HWND hwnd
has already been available, now I want to print a format string
into a window described by this hwnd
. I am not familiar with Windows API, Thanks very much for your help.
For example:
num = 23;
printFunction(hwnd, "number is %d", num);
Is there a function to solve this problem? Thank you!
UPDATE: I encounter this problem during using IDA Pro SDK.
Create a sample subwindow below:
HWND hwnd = NULL;
TForm *form = create_tform("Sample s开发者_StackOverflow中文版ubwindow", &hwnd);
I use SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)"hello world");
to print hello world
string in the subwindow, but what should I do if I want to print format string into this subwindow?
Its not really that simple.
First you need to setup your window procedure to handle WM_PAINT. This will then give you an HDC (Device Context). With this HDC you can call DrawText to draw your string. You will need to form your string using sprintf most likely.
sprintf(myString, "number is %d", num);
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)myString);
If you already have a subwindow that you can send text to, then you can use something similar to the following:
#include <strsafe.h>
#define MAXSTRING 1024
CHAR string[MAXSTRING];
StringCchPrintf(string, ARRAYSIZE(string), "number is %d", num);
SendMessage(hwnd, WM_SETTEXT, NULL, (LPARAM)string);
Use StringCchPrintf to format the text in an intermediate string, and then use WM_SETTEXT to send that string to a subwindow for display. There's no function that does both in one go, but there's nothing stopping you from writing a helper function that wraps both of these if you find yourself doing this often.
(I've used StringCchPrintf here, which is a Windows API, you can also use the C-flavor sprintf(), which is just like printf(), but it takes an additional string as a parameter. The advantage to using StringCchPrintf is that it takes an additional length parameter that it uses to avoid buffer overruns, which can lead to security issues; if you plan on doing any 'real-world', it's good practice to get to know these 'safe' versions.)
--
Quick note on how this relates to the other answer: there's generally two basic approaches to getting text on the screen in Windows. The simplest way is to create a bunch of label or text controls, have them do the drawing for you, and send them the text. If you're coming from a unix-style console based environment (printf, etc), this is the easiest way to get started. That appears to be the approach you are already using, and the one I'm using above. Another approach is to actually draw the text yourself. This is quite a bit more complicated, as you need to create your own control and handle various requests from Windows so that your control will redraw itself when windows asks it to - using the DrawText or other APIs. This is what those controls are actually doing on your behalf. This gives you total control over text placement, font, color, and so on, but is somewhat overkill if you just want some status or result text displayed for a simple applet. Any of the Programming Windows books out there will have plenty of details on this approach.)
精彩评论