C++ (press button 1, show bitmap "P", 2sec, hide bitmap "P") - c++

I am using C++ , and I want to do this in a dialog box.
Press button 1, show bitmap "P", wait 2sec, hide bitmap "P", press button 1 again.....
void CPreparationDlg::OnBnClickedButton1()
{
GetDlgItem(IDC_P)->ShowWindow(SW_SHOW);
SetTimer(0, 2000, NULL);
GetDlgItem(IDC_P)->ShowWindow(SW_HIDE);
}
In this dialogbox I have do 4 buttons and 4 different pictures respectively.
The buttons are 1,2,3,4, pictures are IDC_P, IDC_L, IDC_E, IDC_K.
!!!!!
After I tried these code for button 1, the bitmap cannot be shown. I am only able to do is show, but it can't hide.
void CPreparationDlg::OnBnClickedButton1()
{
GetDlgItem(IDC_P)->ShowWindow(SW_SHOW);
}
void CPreparationDlg::OnBnClickedButton2()
{
GetDlgItem(IDC_L)->ShowWindow(SW_SHOW);
}
void CPreparationDlg::OnBnClickedButton3()
{
GetDlgItem(IDC_E)->ShowWindow(SW_SHOW);
}
void CPreparationDlg::OnBnClickedButton4()
{
GetDlgItem(IDC_K)->ShowWindow(SW_SHOW);
}
I have also tried these, but it underline the "IDC_P" in CALLBACK, and said it argument of type "int" is incompatible with parameter of type HWND
void CPreparationDlg::OnBnClickedButton1()
{
GetDlgItem(IDC_P)->ShowWindow(SW_SHOW);
UINT TimerId = SetTimer(0, 2000, &TimerProc);
}
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
GetDlgItem(**IDC_P**)->ShowWindow(SW_HIDE);
}

You could try:
void CPreparationDlg::OnBnClickedButton1()
{
GetDlgItem(IDC_P)->ShowWindow(SW_SHOW);
Sleep(2000);
GetDlgItem(IDC_P)->ShowWindow(SW_HIDE);
}
But this would halt your app until the 2 seconds are up.
You could also try using AfxBeginThread, or CreateThread to create a thread to do
this for you, so it doesn't halt your application.
But I think the most viable option would be to use a Windows timer as you have in your example, and handle WM_TIMER messages in the window procedure, not exactly well versed in how you'd go about that in MFC, but I hope this post was at least of some help to you.

Related

C++ CreateWindow: button position gets offset at window maximize

I have a weird problem over here. I created a DLL proxy for Spotify so I can "overlay" a button onto it. Basicially, thats how it works:
DllMain
-> Creates CMain class
-> Creates CToggleButton class
-> Hooks the button onto the Spotify window
It has two methods, one static one which I use for the thread since threads can't call member functions, and one non-static function which gets called by the member function.
With this, I create the thread and pass an instance of the CToggleButton class via lpParam:
CreateThread(0, NULL, WindowThreadStatic, (void*)this, NULL, NULL);
Then, the WindowThreadStatic function:
DWORD WINAPI CToggleButton::WindowThreadStatic(void* lpParam)
{
return ((CToggleButton*)lpParam)->WindowThread();
}
And the main window thread function inside the class:
DWORD CToggleButton::WindowThread()
{
MSG msg;
hButton = CreateWindowA("BUTTON", "Test", (WS_VISIBLE | WS_CHILD), 0, 0, 100, 20, parenthWnd, NULL, hInst, NULL);
bool bQueueRunning = true;
while (bQueueRunning)
{
if (PeekMessage(&msg, parenthWnd, 0, 0, PM_REMOVE))
{
switch (msg.message)
{
case WM_QUIT:
bQueueRunning = false;
break;
case WM_LBUTTONDOWN:
if (msg.hwnd == hButton)
{
MessageBoxA(parenthWnd, "Button!", "Button", MB_OK);
continue;
}
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(10);
}
return 0;
}
As you can see, this also contains the message loop for the button (I didn't use GetMessage() here because it was very unresponsive so I decided to use PeekMessage() together with a 10ms delay, which works fine.)
Little picture to show how it looks like:
All great, but if I maximize the window, the button disappears. When I minimize and maximize the window a few times, the button can be seen again, but with very weird coordinates (not the original 0,0 I gave him).
So what is my problem here? Why do the coordinates get offset?
Thanks for reading my long post :)

C++ Animating a button with MoveWindow()

I am new to C++ and seem to be stuck. I basically have a Window and a Button inside it(also created with CreateWindow()). I want to have the button moving on the X axis, I tried doing with MoveWindow() but I cant seem to do the animation effect. I tried writing it in a for loop but I have not found what to use to delay the animation. I would appreciate any help.
for (int i = 0; i < 50; i++) {
MoveWindow(g_MovingDot, i, ButtonTop, ButtonWidth, ButtonHeight, true);
//Delay it somehow
}
Would this be the right way to do it? I just want the button to move slowly to the right.
Use the SetTimer function to be notified when the time-out value is elapse.
Implement a function with the signature
VOID (CALLBACK* TIMERPROC)(HWND, UINT, UINT_PTR, DWORD);
and pass a function pointer to SetTimer, to register a callback procedure .
Use a global control variable, which controls the animation of the window. In the following code snippet the control variable is named i_g.
When the timer is elapsed, then the callback procedure is called. Increment the control variable, slightly move the dialog element and restart the timer, until the final position is reached.
The animation time is controlled by the number of animation steps and the time interval of a single step.
int g_i=0;
void CALLBACK BtnTimer( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime )
{
KillTimer( hwnd, idEvent );
if ( g_i < 50 )
{
MoveWindow(g_MovingDot, i, ButtonTop, ButtonWidth, ButtonHeight, true);
g_i ++;
SetTimer( hwnd, idEvent, 100 /* time milliseconds */, &BtnTimer );
}
}
void AnimateButton( HWND hDialogWnd //* HWND from Dialog */)
{
g_i = 0;
SetTimer( hDialogWnd, 0 /* idEvent */, 100 /* time milliseconds */, &BtnTimer );
}

Browse For Folder dialog window handle C++

How to get the handle HWND of the dialog which user open when clicking on a button.
I'm using Spy++ to find the window class and tittle, but it says that no such window is found. And how then to get the handle of that dialog in C++ using Win API ?
I hope that I will be able to do that using simple functions as FindWindow, GetParent, any WIN APi function. I do not like to inject something or load DLL. Thanks
UPDATE:
the folder browser dialog is opened by other program. I want to get it's handle from different program , my program. Thanks.
The closest to want i need is for now the function WindowFromPoint
Accessibility will let you capture window creation events from other processes without DLL injection. You can modify the example to accommodate for the browsing window specifically. Here's an example I made previously to test that is based on the one from the article. Modify it however you wish:
#include <iostream>
#include <windows.h>
void CALLBACK proc(HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG obj, LONG child, DWORD thr, DWORD time) {
if (hwnd && obj == OBJID_WINDOW && child == CHILDID_SELF) {
switch (event) {
case EVENT_OBJECT_CREATE: {
std::cout << "Window created!\n";
break;
}
case EVENT_OBJECT_DESTROY: {
std::cout << "Window destroyed!\n";
break;
}
}
}
}
int main() {
HWINEVENTHOOK hook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, nullptr, proc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hook) {
UnhookWinEvent(hook);
}
}

SetTimer function in a dll file

I'm writing a plugin for the musicplayer named MusicBee.
The plugin is for the Logitech G keyboards LCD.
Now I will look at buttons activity every 30ms so everyting is fast when pressing on it.
I will use the setTimer function of windows.h but I can't get it to work in my dll file.
Can someone help me with this little problem??
The code I have is (TimerProc function is a static function):
Logitech * Logitech::LogitechObject;
Logitech::Logitech(): stopthread(false), firstTime(true), position(0), duration(0)
{
LogitechObject = this;
SetTimer(NULL, 1, 30, &Logitech::TimerProc);
}
Logitech::~Logitech()
{
stopthread = true;
this->state = StatePlay::Undefined;
timerThread.detach();
}
VOID CALLBACK Logitech::TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
LogitechObject->time = 0;
LogitechObject->m_lcd.SetProgressBarPosition(LogitechObject->progressbar, static_cast<FLOAT>(100));
LogitechObject->m_lcd.Update();
SetTimer(NULL, 1, 30, &Logitech::TimerProc);
}
In order SetTimer to work the application should run "message pump" loop ( GetMessage()/DispatchMessage() ). Without it WM_TIMER message will not be delivered and so your TimerProc will not be invoked.
Use CreateTimerQueueTimer() instead.

How to check if a window button is pressed C++

How can I test if a button is being pressed ?
I am using EnumChildWindows() to enumerate the child windows of a given window, and one of the child window is a button, I want to test if that specific button is being pressed.
My code until know is:
BOOL CALLBACK MyEnumProc(HWND hwnd, LPARAM lParam)
{
char buffer[256];
GetWindowText(hwnd, buffer, sizeof(buffer));
cout << buffer << endl;
return true;
}
int main()
{
HWND hwnd = FindWindow(0, "Window to find");
EnumChildWindows(hwnd, MyEnumProc, 0);
return 0;
}
You can send the BM_GETSTATE message to the button control, if it is pressed the result will be
BST_PUSHED.
You need to inject a DLL into the process space, hook the window message loop (like you used to hand code a subclassed window in native Win32 API C code, Window Proc) (google-able) and listen to the actual messages.
All of this is ancient stuff for me, and I'm afraid that recent Windows versions (hopefully) made this a little bit more difficult to do.
That said, if you can get the application trusted with the right level of permissions, you should still be able to do this