-
HWND problem
I want to be able in my applications to tell if the app's window is resized, moved around, or just minimized and stuff, and I googled to figure out that most of the functions that could achieve his used a variable of type "HWND". But the problem is that I found nothing about how to set this variable to my application's console window process Id or whatever. I know that it can be complicated since an app can have multiple windows, but in my case I only have one.
But what is the function to set that kind of variable to the window? I know that we can also set it to another window that has no real connection to our app, but I'd like to find something that just get the Id from where the function was called, so in my application.
I have found something, but that's not helpful as I need to find the position of the window first! "WindowFromPoint()"... But BTW I use Windows XP, and my complier is MSVC++6...
-
I'm not sure what it is you're trying to do. The app with the window is your app? If so, you just handle the message WM_SIZE to find out when its sized. Many actions have an associated WM_ message.
but if you are doing a windows app, you would probably know about WM_SIZE already. I'm not sure where you're going with this
-
I think I would need you to just be more specific as to what your app is, and is trying to do
-
Well, I just want to know how I can manage to work around with window handles, but it's currently to try on a console window (just learning...) and then later I will try to use these in a game, but that will be in some time. First I need to know how to catch the window's events and get/set them. I just need to know what to do after this:
Code:
HWND hwnd;
hwnd = some_function_that_return_what_is_the_application_id();
//then try stuff here with the handle!
But that's a function that I'm not aware about now... I know I will(maybe) be able to deal with my window because I found some functions, just I'm not able to set a value or address to an HWND...
Code:
SetWindowPos(/*hwnd params...*/);
SetConsoleTitle(/*hwnd params...*/);
GetWindowPlacement(/*hwnd params...*/);
I would really like to know, because I couldn't find any thing related to this, even on MSDN...
-
ok I think there's a misunderstanding somewhere in here. the HWND is a handle to a window. You get it from CreateWindow. Which means your app created the window. If you're trying to manipulate another application that you did not right, its a whole different ball game.
-
Well, the console window is created by itself when I run my application... I just can't do a thing to it since it's the "main()" that does it (I think...)
-
I think I understand what you're trying. Well first off, the console window is not your app. Your app is running inside the console window. If you're trying to learn how to do windows stuff, this is not the right approach. If you really want to screw with the console window itself though, I don't think I can help you without researching it.
maybe someone else?
-
I have researched the topic, and I'll just say that it's very hard and very unreliable. The console window seems to change its window class in every Windows version, never has a predictable title, and is thus impossible to find directly.
Because the console doesn't fully belong to your program (that is, it might have been created by your program, but that's unlikely - more likely it was created by explorer.exe, cmd.exe, or vstudio.exe) you can't even enumerate all windows of your app and just grab the most likely one.
If you want to play around with windows, do yourself a favour and use regular windows.
-
I see. I'll have to try with "real" windows app then... But it looks really complicated, though...
-
yes, it is fairly complicated. But have you tried .NET? Much less complicated
-
Well, I don't really know what's .NET... I mean I'm all mixed up with all the compilers versions and frameworks, I just can't seem to tell what is what... :) But I know that I might be working with it, because of the "about MSVC" tab in the comp...
-
figure out which version of MSVC you have. But I have to say, you don't sound like you're ready for windows programming yet.
-
Well, I know I'm maybe not ready for this, but I'm making efforts to try myself at it. Since yesterday I gathered pieces of snippets about creating windows and I found out a kind of library that handles windows and I want to be able to do that to be able to handle also the pixels in a window, because I'm able to do it all by myself, using SDL... So I checked in a 20000 lines of code header file and found out some things that might help me build a window.
I will look later to how to change the screen's pixels, but right now I'm only trying to "keep" a window. Though I fail... This is what I came up with, it sure is messed up, but when I run it I get a window that opens and closes in a flash, even if my program enters an infinite loop...
Code:
#include "windows.h"
static DWORD WINAPI _events_thread(void* arg) {
CLIENTCREATESTRUCT lpParam;
RECT rect;
rect.left = rect.top = 0;
rect.right = 480;
rect.bottom = 432;
AdjustWindowRect(&rect, WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, false);
CreateWindowA("MDICLIENT", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
480, 432, 0, 0, 0, &lpParam);
return 0;
}
void main() {
HANDLE thread;
HANDLE created;
HANDLE mutex;
void *arg = 0;
unsigned long ThreadID;
mutex = CreateMutex(0, 0, 0);
created = CreateEvent(0, 0, 0, 0);
thread = CreateThread(0, 0, _events_thread, arg, 0, &ThreadID);
WaitForSingleObject(thread, INFINITE);
for (;;) {}
}
So I came up with this, but I got to mention, my project type is still in console, so I think I'd have to use WinMain() and change to a Window project, right?
Edit: I tried also "__stdcall WinMain(void) {" to change from "void main()", but I get an error telling me that I can't just overload that function... As if I was trying to redefine a previously defined function... What??
-
There are *so* many things wrong in that code. Either start learning how to properly program with the Win32 API (www.winprog.org) or learn how to use C++ itself correctly but don't try to mix that many things you don't fully understand.
-
Yeah I know BTW how to use C++ correctly but its just that I never tried on with this kind of programming (meaning building a window...) but it's just that I like to rush myself and get all along the rough paths...
-
I went over to Win32 API when I had messed with console for about 5 days.
I really don't understand, why people mess with console anyway...
-
many applications do not need a gui. Most of the valuable stuff on unix/linux is command line utilities/servers. Additionally, console is a pretty good place to stay while learning C++ because you don't get bogged down in learning something as complex as the Win32 API while you should be concentrating on the language.
-
Win32 API is simple... extremely simple...
-
On the original question -- you can get a handle to the console window using the API function GetConsoleWindow(). However, you can't (easily) receive window messages so it's of only limited use if you want to learn event-driven programming.
-
You can get resize events with ReadConsoleInput (the mode, set with SetConsoleMode, has to be correct). Some other interesting events can be obtained with SetConsoleCtrlHandler.
However, the console programs should typically be limited to programs that take a command line and output text. Anything more complicated typically requires a custom GUI.