Advertentie

Auteur Topic: GUI Functie uitbreiden  (gelezen 416 keer)

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
GUI Functie uitbreiden
« Gepost op: januari 10, 2012, 23:12:43 pm »
Beste lezers,

ik heb mijn GUI klaar en kan nu ook netjes een functie uitvoeren aan de hand van het gekozen aantal.
Ik heb alles in één classe verwerkt en het werkt dus gelukkig ook allemaal goed.

Echter wil ik nu de volgende code toevoegen maar vanaf daar gaat het fout. Kan iemand mij vertellen
hoe ik dit wel correct kan aanpassen?

Mijn main.cpp
Code: C++ (QT)
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QObject>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QSlider>
  7. #include <QSpinBox>
  8. #include <QPushButton>
  9. #include <QMessageBox>
  10. #include <QLabel>
  11. #include <QSound>
  12.  
  13. #include <windows.h>
  14.  
  15. #include "PROCESS.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     QApplication app(argc, argv);
  20.  
  21.     // create a main window widget
  22.     QWidget *main = new QWidget;
  23.     main->setWindowTitle("FFXIV Auto Shouter");
  24.     main->setWindowIcon(QIcon("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png"));
  25.     main->setFixedWidth(325);
  26.  
  27.     // create the widgets which we will use
  28.     QLabel *label1 = new QLabel("Amount: ");
  29.     QLabel *label2 = new QLabel("<strong><font color=\"orange\">Configuring</font></strong>");
  30.     QSlider *slider = new QSlider(Qt::Horizontal);
  31.     QSpinBox *spinbox = new QSpinBox;
  32.     QPushButton *button = new QPushButton;
  33.  
  34.     // connect the slider and the box for synchronisation
  35.     QObject::connect(slider, SIGNAL(valueChanged(int)), spinbox, SLOT(setValue(int)));
  36.     QObject::connect(spinbox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
  37.  
  38.     // connect the start button to the class and start
  39.     // executing the process of repeated shouting, but first
  40.     // we will set some options to be able to do all of this
  41.     int amount = 0;
  42.     initiateProcess execFunction( amount, label2, slider );
  43.  
  44.     QObject::connect(button, SIGNAL(clicked()), &execFunction, SLOT(startProcess()));
  45.  
  46.     // configurate the widgets
  47.     slider->setRange(1, 199);
  48.     spinbox->setRange(1, 199);
  49.     slider->setValue(1);
  50.     button->setText("SHOUT");
  51.  
  52.     // styling templates for the widgets
  53.     slider->setStyleSheet("background:#000; color:#FFFFFF;");
  54.     spinbox->setStyleSheet("background:#000; color:#FFFFFF;");
  55.     button->setStyleSheet("background:#000; color:#FFFFFF;");
  56.  
  57.     // tooltips for the widgets
  58.     slider->setToolTip("Select the amount of shouts you want to repeat");
  59.     spinbox->setToolTip("Select the amount of shouts you want to repeat");
  60.     button->setToolTip("Click here to start shouting");
  61.  
  62.     // create the bounding boxes
  63.     QHBoxLayout *wrapper = new QHBoxLayout;
  64.     QHBoxLayout *leftside = new QHBoxLayout;
  65.     QVBoxLayout *rightside = new QVBoxLayout;
  66.  
  67.     // add the widgets to the boxes
  68.     leftside->addWidget(label1);
  69.     leftside->addWidget(slider);
  70.     leftside->addWidget(spinbox);
  71.  
  72.     rightside->addWidget(button);
  73.     rightside->addWidget(label2);
  74.  
  75.     // add the boxes to each other
  76.     wrapper->addLayout(leftside);
  77.     wrapper->addLayout(rightside);
  78.  
  79.     // set a couple of sounds
  80.     QSound bootSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/greetings.wav");    // start up
  81.     QSound executeSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/execute.wav");   // starting loop
  82.     QSound finishedSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/cheer.wav");    // finished loop
  83.  
  84.     // play at startup
  85.     bootSND.play();
  86.  
  87.     // show the widget
  88.     main->setLayout(wrapper);
  89.     main->show();
  90.  
  91.     return app.exec();
  92. }

PROCESS.h
Code: C++ (QT)
  1. #ifndef PROCESS_H
  2. #define PROCESS_H
  3.  
  4. #include <QObject>
  5. #include <QLabel>
  6. #include <QSlider>
  7. #include <QMessageBox>
  8.  
  9. #include <windows.h>
  10.  
  11. class initiateProcess : public QObject {
  12.     Q_OBJECT
  13.  
  14.     public:
  15.     int &amount;
  16.     QLabel *label;
  17.     QSlider *slider;
  18.  
  19.     initiateProcess( int &amount, QLabel *label, QSlider *slider )
  20.         : amount( amount ), label( label ), slider( slider ) {}
  21.  
  22.     public slots:
  23.     void startProcess( ){
  24.         // update the label to status "executing"
  25.         label->setText("<strong><font color=\"red\">executing</font></strong>");
  26.  
  27.         // update the amount according to the chosen
  28.         // value with the slider / spinbox
  29.         amount = slider->value();
  30.  
  31.         // loop through the amount of repeats
  32.         if( amount != 0 )
  33.         {
  34.             // bring the window to the front
  35.             HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
  36.             SetForegroundWindow(GameWindow);
  37.  
  38.             // execute the loop
  39.             for( int i = 0; i < amount; i++ ){
  40.  
  41.             }
  42.         }
  43.         else
  44.         {
  45.             // show a message box with an error
  46.             QMessageBox popup;
  47.             popup.setWindowTitle("NO AMOUNT CHOSEN!");
  48.             popup.setText("You have to chose an amount of shouts for me to repeat!\nIf you fail to do so I cannot repeat your shouts! doh, kupo!");
  49.             QPixmap popupIco("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png");
  50.             popup.setIconPixmap(popupIco);
  51.             popup.exec();
  52.         }
  53.  
  54.         // update the label to status "finished"
  55.         label->setText("<strong><font color=\"green\">finished</font></color>");
  56.     }
  57. };
  58.  
  59. #endif // PROCESS_H

De code die ik wil uitvoeren tussen de:
// execute the loop
for( int i = 0; i < amount; i++ ){

}

is het volgende:
Code: C++ (QT)
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. struct extraKeyInfo
  5. {
  6.     unsigned short repeatCount;
  7.     unsigned char scanCode;
  8.     bool extendedKey, prevKeyState, transitionState;
  9.  
  10.     operator unsigned int()
  11.     {
  12.         return repeatCount | (scanCode << 16) | (extendedKey << 24) |
  13.                (prevKeyState << 30) | (transitionState << 31);
  14.     }
  15. };
  16.  
  17. void pressKey( short code )
  18. {
  19.     HWND hNotepad = FindWindow(0, L"FINAL FANTASY XIV");
  20.     if(!hNotepad){ std::cout << "Window not found"; }
  21.  
  22.     SetForegroundWindow(hNotepad);
  23.  
  24.     short vkCode = LOBYTE(VkKeyScan(code));
  25.  
  26.     extraKeyInfo lParam = {};
  27.     lParam.scanCode = MapVirtualKey(vkCode, 29);
  28.  
  29.     PostMessage(hNotepad, WM_KEYDOWN, vkCode, lParam);
  30.  
  31.     lParam.repeatCount = 1;
  32.     lParam.prevKeyState = true;
  33.     lParam.transitionState = true;
  34.  
  35.     PostMessage(hNotepad, WM_KEYUP, vkCode, lParam);
  36.     Sleep(100);
  37. }
  38.  
  39. int main()
  40. {
  41.     pressKey( VK_SPACE );
  42.     pressKey( 'h' );
  43.     pressKey( 'e' );
  44.     pressKey( 'l' );
  45.     pressKey( 'l' );
  46.     pressKey( 'o' );
  47.     pressKey( VK_RETURN );
  48.  
  49.     return 0;
  50. }

echter krijg ik het niet voor elkaar.
Kan iemand mij aub vertellen hoe ik dit wel correct kan doen?

Het is dus de bedoeling dat ik in die loop het volgende kan doen:

pressKey( VK_SPACE );
pressKey( 'h' );
pressKey( 'e' );
pressKey( 'l' );
pressKey( 'l' );
pressKey( 'o' );
pressKey( VK_RETURN );

en dat hij dan x-aantal keer die dingen uitvoert zeg maar :)
« Laatst bewerkt op: januari 13, 2012, 15:13:29 pm door Laloeka »

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #1 Gepost op: januari 12, 2012, 14:19:14 pm »
Iemand enig idee misschien? heeft gewoon met classes en uitbreiding te maken, niks met Qt zelf ;)
Kom er namelijk niet uit heb al zo veel geprobeerd haha :P

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 264
Re: GUI Functie uitbreiden
« Reactie #2 Gepost op: januari 12, 2012, 15:51:23 pm »
Splits process.h in en h-file met enkel de klassedefinitie en een process.c-file met de implementatie.
* Plaats de struct-definitie bovenaan in je implementatiebestand.
* Maak van pressKey een private method van je klasse
* plaats je aanroep in je for-lus, zoals je zelf al aangaf
2 + 2 = 5, within a large range of 2

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #3 Gepost op: januari 12, 2012, 16:15:56 pm »
Dankjewel <3

Gaan we vannacht onmiddellijk even proberen! :D

wat bedoel je precies met:

Citaat
Splits process.h in en h-file met enkel de klassedefinitie en een process.c-file met de implementatie.

want een process.c file? wat is dat lol ?

Ik heb het nu denk ik allemaal wel in elkaar, alleen voert hij nu nog niks uit.
Hij focust wel mijn notepad scherm nu maar de keystrokes worden niet verstuurd.

Mijn code nu:

process.h
Code: C++
  1. #ifndef PROCESS_H
  2. #define PROCESS_H
  3.  
  4. #include <QObject>
  5. #include <QLabel>
  6. #include <QSlider>
  7. #include <QMessageBox>
  8.  
  9. #include <windows.h>
  10.  
  11. struct extraKeyInfo
  12. {
  13.     unsigned short repeatCount;
  14.     unsigned char scanCode;
  15.     bool extendedKey, prevKeyState, transitionState;
  16.  
  17.     operator unsigned int()
  18.     {
  19.         return repeatCount | (scanCode << 16) | (extendedKey << 24) |
  20.                (prevKeyState << 30) | (transitionState << 31);
  21.     }
  22. };
  23.  
  24. class initiateProcess : public QObject, public extraKeyInfo {
  25.     Q_OBJECT
  26.  
  27.     public:
  28.     int &amount;
  29.     QLabel *label;
  30.     QSlider *slider;
  31.  
  32.     initiateProcess( int &amount, QLabel *label, QSlider *slider )
  33.         : amount( amount ), label( label ), slider( slider ) {}
  34.  
  35.     public:
  36.     void pressKey( short code )
  37.     {
  38.         HWND hNotepad = FindWindow(0, L"Naamloos - Kladblok");
  39.         if(!hNotepad){
  40.             QMessageBox iWindow;
  41.             iWindow.setWindowTitle("Window not found");
  42.             iWindow.setText("The window was not found");
  43.             iWindow.exec();
  44.         }
  45.  
  46.         SetForegroundWindow(hNotepad);
  47.  
  48.         short vkCode = LOBYTE(VkKeyScan(code));
  49.  
  50.         extraKeyInfo lParam = {};
  51.         lParam.scanCode = MapVirtualKey(vkCode, 29);
  52.  
  53.         PostMessage(hNotepad, WM_KEYDOWN, vkCode, lParam);
  54.  
  55.         lParam.repeatCount = 1;
  56.         lParam.prevKeyState = true;
  57.         lParam.transitionState = true;
  58.  
  59.         PostMessage(hNotepad, WM_KEYUP, vkCode, lParam);
  60.         Sleep(100);
  61.     }
  62.  
  63.     public slots:
  64.     void startProcess( ){
  65.         // update the label to status "executing"
  66.         label->setText("<strong><font color=\"red\">executing</font></strong>");
  67.  
  68.         // update the amount according to the chosen
  69.         // value with the slider / spinbox
  70.         amount = slider->value();
  71.  
  72.         // loop through the amount of repeats
  73.         if( amount != 0 )
  74.         {
  75.             // bring the window to the front
  76.             HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
  77.             SetForegroundWindow(GameWindow);
  78.  
  79.             // execute the loop
  80.             for( int i = 0; i < amount; i++ ){
  81.                 // not the last loop so add a pause at the end
  82.                 if( i < (amount-1))
  83.                 {
  84.                     pressKey( VK_SPACE );
  85.                     pressKey( 't' );
  86.                     Sleep(2000);
  87.                 }
  88.                 // last loop so dont add a pause at the end
  89.                 else
  90.                 {
  91.                     pressKey( VK_SPACE );
  92.                     pressKey( 't' );
  93.                 }
  94.             }
  95.         }
  96.         else
  97.         {
  98.             // show a message box with an error
  99.             QMessageBox popup;
  100.             popup.setWindowTitle("NO AMOUNT CHOSEN!");
  101.             popup.setText("You have to chose an amount of shouts for me to repeat!\nIf you fail to do so I cannot repeat your shouts! doh, kupo!");
  102.             QPixmap popupIco("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png");
  103.             popup.setIconPixmap(popupIco);
  104.             popup.exec();
  105.         }
  106.  
  107.         // update the label to status "finished"
  108.         label->setText("<strong><font color=\"green\">finished</font></color>");
  109.     }
  110. };
  111.  
  112. #endif // PROCESS_H

mijn cpp file:
Code: C++
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QObject>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QSlider>
  7. #include <QSpinBox>
  8. #include <QPushButton>
  9. #include <QMessageBox>
  10. #include <QLabel>
  11. #include <QSound>
  12.  
  13. #include <windows.h>
  14.  
  15. #include "PROCESS.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     QApplication app(argc, argv);
  20.  
  21.     // create a main window widget
  22.     QWidget *main = new QWidget;
  23.     main->setWindowTitle("FFXIV Auto Shouter");
  24.     main->setWindowIcon(QIcon("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png"));
  25.     main->setFixedWidth(325);
  26.  
  27.     // create the widgets which we will use
  28.     QLabel *label1 = new QLabel("Amount: ");
  29.     QLabel *label2 = new QLabel("<strong><font color=\"orange\">Configuring</font></strong>");
  30.     QSlider *slider = new QSlider(Qt::Horizontal);
  31.     QSpinBox *spinbox = new QSpinBox;
  32.     QPushButton *button = new QPushButton;
  33.  
  34.     // connect the slider and the box for synchronisation
  35.     QObject::connect(slider, SIGNAL(valueChanged(int)), spinbox, SLOT(setValue(int)));
  36.     QObject::connect(spinbox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
  37.  
  38.     // connect the start button to the class and start
  39.     // executing the process of repeated shouting, but first
  40.     // we will set some options to be able to do all of this
  41.     int amount = 0;
  42.     initiateProcess execFunction( amount, label2, slider );
  43.  
  44.     QObject::connect(button, SIGNAL(clicked()), &execFunction, SLOT(startProcess()));
  45.  
  46.     // configurate the widgets
  47.     slider->setRange(1, 199);
  48.     spinbox->setRange(1, 199);
  49.     slider->setValue(1);
  50.     button->setText("SHOUT");
  51.  
  52.     // styling templates for the widgets
  53.     slider->setStyleSheet("background:#000; color:#FFFFFF;");
  54.     spinbox->setStyleSheet("background:#000; color:#FFFFFF;");
  55.     button->setStyleSheet("background:#000; color:#FFFFFF;");
  56.  
  57.     // tooltips for the widgets
  58.     slider->setToolTip("Select the amount of shouts you want to repeat");
  59.     spinbox->setToolTip("Select the amount of shouts you want to repeat");
  60.     button->setToolTip("Click here to start shouting");
  61.  
  62.     // create the bounding boxes
  63.     QHBoxLayout *wrapper = new QHBoxLayout;
  64.     QHBoxLayout *leftside = new QHBoxLayout;
  65.     QVBoxLayout *rightside = new QVBoxLayout;
  66.  
  67.     // add the widgets to the boxes
  68.     leftside->addWidget(label1);
  69.     leftside->addWidget(slider);
  70.     leftside->addWidget(spinbox);
  71.  
  72.     rightside->addWidget(button);
  73.     rightside->addWidget(label2);
  74.  
  75.     // add the boxes to each other
  76.     wrapper->addLayout(leftside);
  77.     wrapper->addLayout(rightside);
  78.  
  79.     // set a couple of sounds
  80.     QSound bootSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/greetings.wav");    // start up
  81.     QSound executeSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/execute.wav");   // starting loop
  82.     QSound finishedSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/cheer.wav");    // finished loop
  83.  
  84.     // play at startup
  85.     bootSND.play();
  86.  
  87.     // show the widget
  88.     main->setLayout(wrapper);
  89.     main->show();
  90.  
  91.     return app.exec();
  92. }

Dit zou toch moeten werken? ik heb immers de struct als public vermeld in mijn classe.
« Laatst bewerkt op: januari 13, 2012, 15:49:34 pm door Divendo »

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #4 Gepost op: januari 14, 2012, 01:34:52 am »
Heb nog het e.e.a. geprobeerd maar kom er niet uit haha :P
Iemand enig idee?

Ga dadelijk de struct verwijderen en die inhoud + functie in mijn eigen class
plaatsen, dan hoef ik geen afgeleide classe te maken en misschien werkt het dan wel.

Offline Divendo

  • Administrator
  • Zeer gerespecteerd lid
  • ********
  • Berichten: 2540
  • Voer in op google maps: 37.971458,23.726706
    • Divendo Webs
Re: GUI Functie uitbreiden
« Reactie #5 Gepost op: januari 14, 2012, 10:16:54 am »
Je hoeft ook helemaal niet van die struct af te leiden.
Werkt de code normaal (dus los) wel?
Admin - http://divendo-webs.com
[C++][C#][PHP][JavaScript][HTML][XML][CSS][SQL][Bash][GML][Nederlands][Engels][Latijn]

"Leren is proberen."

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 264
Re: GUI Functie uitbreiden
« Reactie #6 Gepost op: januari 14, 2012, 11:35:50 am »
wat bedoel je precies met:
Citaat
Splits process.h in en h-file met enkel de klassedefinitie en een process.c-file met de implementatie.
want een process.c file? wat is dat lol ?
In OOP is het de gewoonte om de definitie en implementatie van een klasse in twee aparte bestanden te plaatsen, waarbij de definitie in de header komt en de implementatie in een code-file (een .c / .cpp / .cxx). Het is namelijk zo dat wanneer iemand jouw code wil gebruiken of samenwerkt aan hetzelfde project, die meestal enkel de definitie van jouw klasses hoeft te kennen:
bvb voor een klasse Auto is het voor iemand anders belangrijk te weten dat een Auto wielen heeft en dat ie kan rijden (een methode rij() ), maar hoe die Auto dan precies rijdt, maakt voor die andere persoon helemaal niet uit.
2 + 2 = 5, within a large range of 2

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #7 Gepost op: januari 15, 2012, 17:48:38 pm »
Oke duidelijk, dankjewel voor je uitleg :)
Echter weet ik nu nog niet waarom mijn code niks doet. Jij enig idee?
Ik ga er nu weer verder aan prutsen haha :P

Nogmaals dank voor je uitleg!

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #8 Gepost op: januari 17, 2012, 21:55:34 pm »
Tot op heden nog niet werkend gekregen. Weet iemand wat ik verkeerd doe? geen foutmelding of niks, het scherm voert de FOCUS goed uit, notepad komt naar voren maar hij toetst de knoppen niet in. Ik weet dat ik alles eigenlijk buiten de header moet definieren maar ik ben de enige die het gebruikt en het gaat mij er puur om dat ik begrijp hoe ik alles moet linken en bouwen haha, echter zit ik nu nog steeds met dit probleem en enkele dagen later nog niet opgelost gekregen helaas. Hopelijk weet iemand een oplossing.


mijn header file:
Code: C++ (QT)
  1. #ifndef PROCESS_H
  2. #define PROCESS_H
  3.  
  4. #include <QObject>
  5. #include <QLabel>
  6. #include <QSlider>
  7. #include <QMessageBox>
  8.  
  9. #include <windows.h>
  10.  
  11. struct extraKeyInfo
  12. {
  13.     unsigned short repeatCount;
  14.     unsigned char scanCode;
  15.     bool extendedKey, prevKeyState, transitionState;
  16.  
  17.     operator unsigned int()
  18.     {
  19.         return repeatCount | (scanCode << 16) | (extendedKey << 24) |
  20.                (prevKeyState << 30) | (transitionState << 31);
  21.     }
  22. };
  23.  
  24. class initiateProcess : public QObject, public extraKeyInfo {
  25.     Q_OBJECT
  26.  
  27.     public:
  28.     int &amount;
  29.     QLabel *label;
  30.     QSlider *slider;
  31.  
  32.     initiateProcess( int &amount, QLabel *label, QSlider *slider )
  33.         : amount( amount ), label( label ), slider( slider ) {}
  34.  
  35.     private:
  36.     void pressKey( short code )
  37.     {
  38.         HWND hNotepad = FindWindow(0, L"Naamloos - Kladblok");
  39.         if(!hNotepad){
  40.             QMessageBox iWindow;
  41.             iWindow.setWindowTitle("Window not found");
  42.             iWindow.setText("The window was not found");
  43.             iWindow.exec();
  44.         }
  45.  
  46.         SetForegroundWindow(hNotepad);
  47.  
  48.         short vkCode = LOBYTE(VkKeyScan(code));
  49.  
  50.         extraKeyInfo lParam = {};
  51.         lParam.scanCode = MapVirtualKey(vkCode, 29);
  52.  
  53.         PostMessage(hNotepad, WM_KEYDOWN, vkCode, lParam);
  54.  
  55.         lParam.repeatCount = 1;
  56.         lParam.prevKeyState = true;
  57.         lParam.transitionState = true;
  58.  
  59.         PostMessage(hNotepad, WM_KEYUP, vkCode, lParam);
  60.         Sleep(100);
  61.     }
  62.  
  63.     public slots:
  64.     void startProcess( ){
  65.         // update the label to status "executing"
  66.         label->setText("<strong><font color=\"red\">executing</font></strong>");
  67.  
  68.         // update the amount according to the chosen
  69.         // value with the slider / spinbox
  70.         amount = slider->value();
  71.  
  72.         // loop through the amount of repeats
  73.         if( amount != 0 )
  74.         {
  75.             // bring the window to the front
  76.             HWND GameWindow = FindWindow(0, L"Naamloos - Kladblok");
  77.             SetForegroundWindow(GameWindow);
  78.  
  79.             // execute the loop
  80.             for( int i = 0; i < amount; i++ ){
  81.                 // not the last loop so add a pause at the end
  82.                 if( i < (amount-1))
  83.                 {
  84.                     pressKey( VK_SPACE );
  85.                     pressKey( 't' );
  86.                     Sleep(2000);
  87.                 }
  88.                 // last loop so dont add a pause at the end
  89.                 else
  90.                 {
  91.                     pressKey( VK_SPACE );
  92.                     pressKey( 't' );
  93.                 }
  94.             }
  95.         }
  96.         else
  97.         {
  98.             // show a message box with an error
  99.             QMessageBox popup;
  100.             popup.setWindowTitle("NO AMOUNT CHOSEN!");
  101.             popup.setText("You have to chose an amount of shouts for me to repeat!\nIf you fail to do so I cannot repeat your shouts! doh, kupo!");
  102.             QPixmap popupIco("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png");
  103.             popup.setIconPixmap(popupIco);
  104.             popup.exec();
  105.         }
  106.  
  107.         // update the label to status "finished"
  108.         label->setText("<strong><font color=\"green\">finished</font></color>");
  109.     }
  110. };
  111.  
  112. #endif // PROCESS_H

mijn c++ file:
Code: C++ (QT)
  1. #include <QApplication>
  2. #include <QWidget>
  3. #include <QObject>
  4. #include <QHBoxLayout>
  5. #include <QVBoxLayout>
  6. #include <QSlider>
  7. #include <QSpinBox>
  8. #include <QPushButton>
  9. #include <QMessageBox>
  10. #include <QLabel>
  11. #include <QSound>
  12.  
  13. #include <windows.h>
  14.  
  15. #include "PROCESS.h"
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     QApplication app(argc, argv);
  20.  
  21.     // create a main window widget
  22.     QWidget *main = new QWidget;
  23.     main->setWindowTitle("FFXIV Auto Shouter");
  24.     main->setWindowIcon(QIcon("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/favicon.png"));
  25.     main->setFixedWidth(325);
  26.  
  27.     // create the widgets which we will use
  28.     QLabel *label1 = new QLabel("Amount: ");
  29.     QLabel *label2 = new QLabel("<strong><font color=\"orange\">Configuring</font></strong>");
  30.     QSlider *slider = new QSlider(Qt::Horizontal);
  31.     QSpinBox *spinbox = new QSpinBox;
  32.     QPushButton *button = new QPushButton;
  33.  
  34.     // connect the slider and the box for synchronisation
  35.     QObject::connect(slider, SIGNAL(valueChanged(int)), spinbox, SLOT(setValue(int)));
  36.     QObject::connect(spinbox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
  37.  
  38.     // connect the start button to the class and start
  39.     // executing the process of repeated shouting, but first
  40.     // we will set some options to be able to do all of this
  41.     int amount = 0;
  42.     initiateProcess execFunction( amount, label2, slider );
  43.  
  44.     QObject::connect(button, SIGNAL(clicked()), &execFunction, SLOT(startProcess()));
  45.  
  46.     // configurate the widgets
  47.     slider->setRange(1, 199);
  48.     spinbox->setRange(1, 199);
  49.     slider->setValue(1);
  50.     button->setText("SHOUT");
  51.  
  52.     // styling templates for the widgets
  53.     slider->setStyleSheet("background:#000; color:#FFFFFF;");
  54.     spinbox->setStyleSheet("background:#000; color:#FFFFFF;");
  55.     button->setStyleSheet("background:#000; color:#FFFFFF;");
  56.  
  57.     // tooltips for the widgets
  58.     slider->setToolTip("Select the amount of shouts you want to repeat");
  59.     spinbox->setToolTip("Select the amount of shouts you want to repeat");
  60.     button->setToolTip("Click here to start shouting");
  61.  
  62.     // create the bounding boxes
  63.     QHBoxLayout *wrapper = new QHBoxLayout;
  64.     QHBoxLayout *leftside = new QHBoxLayout;
  65.     QVBoxLayout *rightside = new QVBoxLayout;
  66.  
  67.     // add the widgets to the boxes
  68.     leftside->addWidget(label1);
  69.     leftside->addWidget(slider);
  70.     leftside->addWidget(spinbox);
  71.  
  72.     rightside->addWidget(button);
  73.     rightside->addWidget(label2);
  74.  
  75.     // add the boxes to each other
  76.     wrapper->addLayout(leftside);
  77.     wrapper->addLayout(rightside);
  78.  
  79.     // set a couple of sounds
  80.     QSound bootSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/greetings.wav");    // start up
  81.     QSound executeSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/execute.wav");   // starting loop
  82.     QSound finishedSND("C:/Users/Ground Zero/Desktop/FFXIV Auto Shouter/cheer.wav");    // finished loop
  83.  
  84.     // play at startup
  85.     bootSND.play();
  86.  
  87.     // show the widget
  88.     main->setLayout(wrapper);
  89.     main->show();
  90.  
  91.     return app.exec();
  92. }

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 264
Re: GUI Functie uitbreiden
« Reactie #9 Gepost op: januari 18, 2012, 08:09:57 am »
Wat is de bedoeling hiervan (in je header)?
Code: C++ (QT)
  1. class initiateProcess : public QObject, public extraKeyInfo
2 + 2 = 5, within a large range of 2

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #10 Gepost op: januari 18, 2012, 13:36:13 pm »
hij heeft de struct

Code: C++ (QT)
  1. struct extraKeyInfo
  2. {
  3.     unsigned short repeatCount;
  4.     unsigned char scanCode;
  5.     bool extendedKey, prevKeyState, transitionState;
  6.  
  7.     operator unsigned int()
  8.     {
  9.         return repeatCount | (scanCode << 16) | (extendedKey << 24) |
  10.                (prevKeyState << 30) | (transitionState << 31);
  11.     }
  12. };

nodig om de lparam te maken. Zonder deze werkt de keypress niet.
Ik neem deze struct / class nu toch mee met mijn eigen functie of zie ik dat verkeerd?

of neemt hij de hele lparam nu niet mee?



Kijk, ik heb deze struct:

Code: C++ (QT)
  1. struct extraKeyInfo
  2. {
  3.     unsigned short repeatCount;
  4.     unsigned char scanCode;
  5.     bool extendedKey, prevKeyState, transitionState;
  6.  
  7.     operator unsigned int();
  8. };

In mijn functie pressKey(short code) roep ik aan:
Code: C++ (QT)
  1. extraKeyInfo lParam = {};

Dus ik maak een nieuwe lparam variable aan de hand van die classe.

Deze lparam is noodzakelijk om een keystroke te kunnen sturen in de volgende code:

Code: C++ (QT)
  1. PostMessage(hNotepad, WM_KEYDOWN, vkCode, lParam);

Zonder die lparam gebeurd er niks (wat nu het geval is lijkt me).

Echter wanneer ik mijn classe maak en de lparam struct als public mee geef, dan zou ik
deze toch over moeten nemen, en dus ook kunnen gebruiken?
« Laatst bewerkt op: januari 18, 2012, 13:39:53 pm door GroundZero »

Offline jeetee

  • Zeer actief lid
  • *****
  • Berichten: 264
Re: GUI Functie uitbreiden
« Reactie #11 Gepost op: januari 18, 2012, 16:16:53 pm »
Je wil een object van die struct gebruiken in jouw klasse; niet dat jouw klasse die struct wordt!

Als een Auto koffer heeft, dan zal de klasse Auto een protected/private member van het type Koffer hebben. De klasse Auto zal echter niet overerven van de klasse Koffer: je Auto is namelijk geen Koffer!

Je moet dus helemaal niet overerven van extraKeyInfo, maar gewoon een member-variabele of lokale variabele van dat type aanmaken. En dat laatste doe je al.


Ik denk dat we dus een stap dichter bij de oplossing komen door die foutieve overerving alvast weg te halen en eens te zien wat dat geeft.
2 + 2 = 5, within a large range of 2

Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #12 Gepost op: januari 18, 2012, 16:50:53 pm »
Oke duidelijk. Ik heb

Code: C++ (QT)
  1. public extraKeyInfo

verwijderd, maar ook nu geen foutmelding (had dit al eens geprobeerd).
Ik snap er weinig tot niks van lol.

Ik zal mijn bron bestanden eens online uploaden ;)

EDIT: http://www.devbizz.nl/bronbestanden.rar
« Laatst bewerkt op: januari 18, 2012, 16:54:09 pm door GroundZero »

Offline TopTools

  • Nieuw lid
  • **
  • Berichten: 28
Re: GUI Functie uitbreiden
« Reactie #13 Gepost op: januari 21, 2012, 18:13:52 pm »
Hallo GZ,

Ik denk dat jouw probleem niet een C++ maar een Win32 probleem is.

Vroeger, in de Windows 95 dagen (ik word oud..) kon elk proces messages versturen naar elk ander proces. Toen malware schrijvers hier op grote schaal misbruik van gingen maken heeft Microsoft dit moeilijker gemaakt.

Om te doen wat jij wilt kun je het beste een "keyboard event" simuleren, je moet dan zorgen dat de applicatie waar je de events wilt laten plaatsvinden (NotePad) keyboard-focus heeft, iets wat jij al doet in:

SetForegroundWindow(hNotepad);

Vervolgens kun je mbv. keybd_event() de input in NotePad laten verschijnen.

NB. Microsoft noemt deze functie 'superseded'; dwz. is vervangen door SendInput().
Mijn ervaring tot dusver is dat keybd_event() prima werkt in alle versies van Windows en eenvoudiger in het gebruik is dan SendInput().


Offline GroundZero

  • Zeer actief lid
  • *****
  • Berichten: 300
    • Devbizz - webdevelopment
Re: GUI Functie uitbreiden
« Reactie #14 Gepost op: januari 22, 2012, 21:10:33 pm »
Beste TopTools,

dat klopt maar ik wil juist dat er ook keys gestuurd worden als er GEEN focus is op het scherm.
Daarom ook mijn methode. Deze methode werkt vlekkenloos, totdat ik hem in mijn GUI wilde implementeren.

Daar ga ik ergens de mist in, want wanneer ik het niet in mijn GUI implementeer en gewoon de originele code
aanhoud dan werkt het perfect.

De originele code:

Code: C++ (QT)
  1. #include <windows.h>
  2. #include <iostream>
  3.  
  4. struct extraKeyInfo
  5. {
  6.     unsigned short repeatCount;
  7.     unsigned char scanCode;
  8.     bool extendedKey, prevKeyState, transitionState;
  9.  
  10.     operator unsigned int()
  11.     {
  12.         return repeatCount | (scanCode << 16) | (extendedKey << 24) |
  13.                (prevKeyState << 30) | (transitionState << 31);
  14.     }
  15. };
  16.  
  17. int main()
  18. {
  19.     HWND hNotepad = FindWindow(TEXT("Notepad"), NULL);
  20.  
  21.     if (!hNotepad)
  22.         std::cout << "Notepad was not found!\n";
  23.     else
  24.     {
  25.         HWND hEdit = FindWindowEx(hNotepad, NULL, TEXT("Edit"), NULL);
  26.  
  27.         if (!hEdit)
  28.             std::cout << "Notepads edit control was not found!\n";
  29.         else
  30.         {
  31.             SetForegroundWindow(hNotepad);
  32.  
  33.             for (char ch = 'a'; ch <= 'z'; ch++) // Output a-z in Notepad
  34.             {
  35.                 short vkCode = LOBYTE(VkKeyScan(ch));
  36.  
  37.                 extraKeyInfo lParam = {};
  38.                 lParam.scanCode = MapVirtualKey(vkCode, 29);
  39.  
  40.                 PostMessage(hEdit, WM_KEYDOWN, vkCode, lParam);
  41.  
  42.                 lParam.repeatCount = 1;
  43.                 lParam.prevKeyState = true;
  44.                 lParam.transitionState = true;
  45.  
  46.                 PostMessage(hEdit, WM_KEYUP, vkCode, lParam);
  47.                 Sleep(100);
  48.             }
  49.         }
  50.     }
  51.     std::cin.get();
  52.     return 0;
  53. }

Echter wil ik het dus in mijn GUI hebben, wanneer ik op de knop "start" druk of hoe ik hem ook noem, dat hij het
gaat uitvoeren zeg maar voor het gekozen aantal.

Echter word er vanaf dit moment niks meer verstuurd, echter werkt de focus nog wel dus de functies worden wel
uitgevoerd. Er zit denk ik, dus iets fout met de lparam, die hij uit de struct zou moeten ophalen.

Advertentie