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

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 ยุคใหม่) จริงๆ แล้ว คิดจะเขียนมาตั้งนานแล้วแต่ไม่รู้จะเขียนอะไร พอดีวันนี้อ่านเจอโพสต์ของเพื่อนในเฟสบุ๊คที่แชร์มา หัวข้อประมาณว่า ประเทศไทยมีความพยายามทำให้คนโง่ลง คือผมก็ติดตามข่าวมาเรื่อยๆ ช่วงไปอยู่จีนก็ยังติดตามอยู่ แต่มีความรู้สึกว่าข่าวของประเทศเรากับประเทศจีนนี่คือคนละขั้วเลย ที่จีนข่าวส่วนใหญ่จะเป็นข่าวที่ค่อนข้างมีสาระ และไม่มีข่าวงมงาย น่าจะเนื่องมาจากกฎหมายของประเทศไทยที่ค่อนข้างคุมเข้มกับสื่อ ย้อนกลับมาที่ไทย ประเทศเราไม่มีกฎหมายคุมเข้มสื่อเหมือนประเทศจีนขนาดนั้น สื่อประเทศเราค่อนข้างมีอิสระในการนำเสนอ แต่หลังๆ มานี่ผมเห็นแต่ข่าวอาชญากรรม หรือข่าวให้หวยซะส่วนใหญ่ ก็เข้าใจว่าต้องการยอดผู้ชม แต่การที่สื่อนำเสนอแบบนั้นนี่ไม่แน่ใจว่าจะดีต่ออนาคตประเทศชาติหรือไม่นะครับ คนดูข่าวเพื่อจะรอเลขเอาไปซื้อหวย ไม่แน่ใจว่าสนใจอะไรมากกว่าระหว่างหวยกับข่าว ตอนจะมีกฎหมายหวยออนไลน์ก็เห็นออกมาต่อต้านบอกมอมเมาประชาชน แต่สิ่งที่สื่อกำลังนำเสนออยู่นั้นมันย้อนแย้งในตัวเองชอบกล และ

Fluent NHibernate Configuration

I have a C# project which I have to develop for MS SQL Server, customer requirment, but since I do not have a license for it. Then I decide to go with NHibernate but NHibernate is relied on XML based configuration, which is not my preference, I research a little bit more, definitely I found out Fluent NHibernate is a good replacement for XML based configuration. I can map the entities with C# code that can reduce the errors prone in my code. I use Firebird database in the development, though it can be used in production, I define the mapping as normal NHibernate (also Hibernate in Java). But when I try to build the configure for Fluent NHibernate, it's not the same as SQLite example, I try to fine the solution one by one from StackOverflow and the steps below can solved my problem. Add the reference for NHibernate and its dependecies (NHibernate.dll, Iesi.Collections.dll) Add a reference for Fluent NHibernate (FluentNHibernate.dll), you can get it from nuget packacge manager

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