ข้ามไปที่เนื้อหาหลัก

Console(DOS) ก็มี Handle ได้นะ

เนื่องจาก function บางฟังก์ชั่นบนวินโดว์ต้องการตัวแปร HWND หรือ Handle ของวินโดว์ไปใช้งาน
เช่น GetDC, หรืออาจจะเป็นฟังก์ชั่นพวก WinProc หรือฟังก์ชั่นจัดการกับ message นั่นเอง
แต่.... เราเขียนโปรแกรมแบบ console จะมี Handle ได้อย่างไร

วิธีการมีง่ายๆ เพียง 4-5 ขั้นตอนเท่านั้น
  1. เรียกใช้ฟังก์ชั่น GetConsoleTitle() เพื่อเก็บค่าของ Title ของวินโดว์ปัจจุบันไว้
  2. เรียกฟังก์ชั่น SetConsoleTitle() เพื่อเปลี่ยน Title ของ console ที่เราต้องการ
  3. ให้มัน delay 40 มิลลิวินาที โดยใช้ฟังก์ชั่น Sleep(40) เพื่อให้แน่ใจว่า title ของ โปรแกรมเรา update แล้ว
  4. เรียกใช้ฟังก์ชั่น FindWindow(NULL, uniquetitle) ฟังก์ชั่นนี้ถือว่าเป็นฟังก์ชั่นหลักเลยทีเดียว เพื่อจะนำค่า HWND ของ โปรแกรมแบบ console หรือแบบ Win32 มา ค่าที่ retune กลัมาจะเป็น HWND หรือ NULL ในกรณีที่มันเกิดการผิดพลาดขึ้น เช่นหาโปรแกรมที่มี Title ตามที่เราต้องการไม่เจอ
  5. เรียกฟังก์ชั่น SetConsoleTitle() เซ็ทค่าที่ได้จากขั้นตอนที่ 1 ให้มันเหมือนเดิม หรือจะไม่เซ็ทกลับก็ได้
ตัวอย่างโค้ดพร้อม Comment


#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

    //This main function is correctly by standard C
    //**returned int not void**
int main(int argc, char *argv[])
{
    //Declare Variable
    char windowTitle[1024], pszOldTitle[1024], pszNewTitle[1024];
    //Step 1
    GetConsoleTitle(pszOldTitle, sizeof(pszOldTitle));
    cout <<
"Old Title: " << pszOldTitle << endl; //To test if it captured or not
    //We use GetTickCount() and GetCurrentProcessId() for new Title
    //it's could be something else you can thinking of such as your name or whatever
    //sprinf() to copy value to pszNewTitle which is a char
    sprintf(pszNewTitle, "%d/%d", GetTickCount(), GetCurrentProcessId());
    //Step 2
        //We use SetConsoleTitle() to specified the new Title name for our console
    //By passing a char variable (pszNewTitle) which is gived a named
    SetConsoleTitle(pszNewTitle);
    //Step 3
    //Make it sleep or delay by 40 milliseconds
    Sleep(40);
    //This is a debug to see if new title is set or not
    //We use pszNewTitle as the same because of it's not use anymore
    //and it could save a little memory but it's not actually good way
    GetConsoleTitle(pszNewTitle, sizeof(pszNewTitle));
cout <<
"New Title: " << pszNewTitle << endl;
    //Step 4
    // This is the time you will get what you want Console window handle
    //To Specified the title value of the window that you want to get a handle
    //in the second parameters
    HWND consoleHwnd = FindWindow(NULL, pszNewTitle);
    //GetWindowText() function to test the handle we got is that we want or not
    //by compare with the pszNewTitle ot by calling GetConsoleTitle() function
    GetWindowText(consoleHwnd, windowTitle, sizeof(windowTitle));
cout <<
"Window Title: " << windowTitle << endl;
    //This is in <stdlib.h> for paused console to see the value
    //if it's not called the console will terminate(Close) immediately
    system("PAUSE");
    //This MACRO or constance set as 0 for success without error
    //or you can return 0
    return EXIT_SUCCESS;
}



โค้ดโปรแกรมหลังจากตัด Comment แล้ว


#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    char windowTitle[1024], pszOldTitle[1024], pszNewTitle[1024];
    GetConsoleTitle(pszOldTitle,
sizeof(pszOldTitle));
    cout <<
"Old Title: " << pszOldTitle << endl;
    sprintf(pszNewTitle,
"%d/%d", GetTickCount(), GetCurrentProcessId());
    SetConsoleTitle(pszNewTitle);
    Sleep(
40);
    GetConsoleTitle(pszNewTitle,
sizeof(pszNewTitle));
    cout <<
"New Title: " << pszNewTitle << endl;
    HWND consoleHwnd = FindWindow(NULL, pszNewTitle);
    GetWindowText(consoleHwnd, windowTitle,
sizeof(windowTitle));
    cout <<
"Window Title: " << windowTitle << endl;
    system(
"PAUSE");
    return     EXIT_SUCCESS;
}




Code ข้างบนนำมาจากการ Export to HTML ของ Dev-C++

แล้วผมไม่ได้ทำ ขั้นตอนที่ 5 นะครับ เพราะ่เห็นว่าได้ผลตามที่ต้องการแล้ว

รูปจากการรันครับ


หากมีข้อผิดพลาดประการใด แนะนำและติชมได้เลยครับ

ที่มา: Microsoft

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

หมดหวังกับรายการทีวีในประเทศไทย

ไม่ได้เขียน Blog มานานสกิลด้านเขียนไม่รู้จะยังดีเหมือนเดิมมั้ยนะครับ (หัวข้อก็จะ clickbait  ตามแนว Content ยุคใหม่) จริงๆ แล้ว คิดจะเขียนมาตั้งนานแล้วแต่ไม่รู้จะเขียนอะไร พอดีวันนี้อ่านเจอโพสต์ของเพื่อนในเฟสบุ๊คที่แชร์มา หัวข้อประมาณว่า ประเทศไทยมีความพยายามทำให้คนโง่ลง คือผมก็ติดตามข่าวมาเรื่อยๆ ช่วงไปอยู่จีนก็ยังติดตามอยู่ แต่มีความรู้สึกว่าข่าวของประเทศเรากับประเทศจีนนี่คือคนละขั้วเลย ที่จีนข่าวส่วนใหญ่จะเป็นข่าวที่ค่อนข้างมีสาระ และไม่มีข่าวงมงาย น่าจะเนื่องมาจากกฎหมายของประเทศไทยที่ค่อนข้างคุมเข้มกับสื่อ ย้อนกลับมาที่ไทย ประเทศเราไม่มีกฎหมายคุมเข้มสื่อเหมือนประเทศจีนขนาดนั้น สื่อประเทศเราค่อนข้างมีอิสระในการนำเสนอ แต่หลังๆ มานี่ผมเห็นแต่ข่าวอาชญากรรม หรือข่าวให้หวยซะส่วนใหญ่ ก็เข้าใจว่าต้องการยอดผู้ชม แต่การที่สื่อนำเสนอแบบนั้นนี่ไม่แน่ใจว่าจะดีต่ออนาคตประเทศชาติหรือไม่นะครับ คนดูข่าวเพื่อจะรอเลขเอาไปซื้อหวย ไม่แน่ใจว่าสนใจอะไรมากกว่าระหว่างหวยกับข่าว ตอนจะมีกฎหมายหวยออนไลน์ก็เห็นออกมาต่อต้านบอกมอมเมาประชาชน แต่สิ่งที่สื่อกำลังนำเสนออยู่นั้นมันย้อนแย้งในตัวเองชอบกล...

เปลี่ยนเว็บเดิมให้ Pluggable ด้วย OSGi

เป็นบทความภาษาอังกฤษครับลองอ่านดู เปลี่ยน Web application เดิมให้ยืดหยุ่นด้วย OSGi เพิ่มเติ่มให้ครับ OSGi คืออะไร เป็น Specification ครับที่เจ้าใหญ่ของ Java เค้าทำมาเพื่อให้เกิดความเป็น modularized ครับ ซึ่งถ้าจะให้เห็นภาพคือการทำงานของ plugin น่ะครับ ที่เราสามารถเปิดปิดการทำงานของมันได้ตลอดเวลาเมื่อเอามาใส่ web application ก็ทำให้เราสามารถถอดหรือเสียบ plugin ใหม่ๆ ได้ตลอดเวลา โดยที่ plugin นี้ใน OSGi จะเรียกว่า module ครับ หรือถ้าเป็นศัพท์เทคนิคเค้าจะเรียกว่า Bundle ครับ ลองไปอ่านบทความกันดูครับ ซึ่ง OSGi นั้นจริงๆ แล้วมันเป็น Specification เหมือนกับ EJB หรือ Servlet นี่แหละครับ แต่ตัว implement นั้นแล้วแต่ว่าใครจะนำไป implement นะครับ ที่ดังๆ อยู่ในตอนนี้ก็เห็นมีอยู่สองสามเจ้า Equinox อันนี้หลายๆ คนคงจะคุ้นเคยกันดีมันคือเจ้า eclipse ของเรานั่นเองใช้ตัวนี้อยู่ครับ ส่วนอีกตัวเป็นของ Apache Felix ครับ ลักษณะการใช้งานจะแตกต่างกันบาง และตัว module อาจจะเรียกไม่เหมือนกันบางอันเป็น Bundle บางอันอาจไม่ใช่ ผมก็จำไม่ได้แล้วเหมือนกันครับ ลองไปศึกษากันดูครับ น่าสนใจทีเดียว อีกตัวนั้นจะเป็นของ Sp...

Standalone MySQL setup

Currently I am learning Spring Boot. I follow some tutorials but there is a part requires to use DBMS. So I decide to use MySQL as a database. But I don't want to install it as as service neither install with Windows installer. I decide to download an archive version to run it as standalone application. I expect it will be ready to use as the installer version but it is not as easy as I think. The first thing you have to do after extract the archive file is to copy my-default.ini to my.ini Open my.ini with notepad or text editor that you like, edit these lines as show below basedir = X:/<pat to your mysql folderh>/mysql-5.7.9-winx64 datadir = X:/<pat to your mysql folderh>/mysql-5.7.9-winx64/data Important!  Don't forget to create the folder data to the path they you refer in datadir  Second you have to mysql bin directory and then input the following command in command prompt mysqld --initialize --console --console is optional to see the verbose in...