小编0x6*_*C74的帖子

使用PostMessage模拟按键仅在某些应用程序中有效吗?

我对这个问题的解决方法仅在几个程序中才是正确的。为什么它不是通用的?

适用于:

  • 火狐浏览器

  • Visual Studio文字编辑器
  • 不幸的是,在某些情况下什么也没发生(即使我在执行程序之前单击了文本框区域):

  • 谷歌浏览器
  • 记事本
  • 即使使用SendMessage而不是PostMessage,GetLastError始终返回0.您能指出我的错误吗?

    #include <Windows.h>
    #include <iostream>
    
    int main()
    {
        HWND hCurrentWindow;
    
        Sleep(5000);
    
        hCurrentWindow = GetForegroundWindow();
    
        std::cout<<"GO!!!\n";
    
        for(int i=0; i<500; i++) //simulate 500 keystrokes of 'E'.
            {
                PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
                PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL);
            }
    
        std::cout<<GetLastError()<<std::endl;
    
        system("Pause");
        return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    Maximus建议后的更新

    #include <Windows.h>
    #include <iostream>
    
    int main()
    {
        HWND hCurrentWindow;
    
        Sleep(5000);
    
        hCurrentWindow = GetForegroundWindow();
    
        if(!hCurrentWindow)
            std::cout<<"Failed get set the window handle\n";
    
        std::cout<<"GO!!!\n";
    
        for(int i=0; i<500; i++)
            {
                PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
                PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45);
            }
    
        std::cout<<GetLastError()<<std::endl;
    
        system("Pause");
        return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    效果没有差别。

    Rob Kennedy的评论和Hans Passant的回答后的更新 …

    c++ winapi keypress

    3
    推荐指数
    1
    解决办法
    2万
    查看次数

    使用StreamWriter附加文本

    以下程序应该打开/创建一个文件,并且每次都将当前日期写入其中.

    using System;
    using System.IO;
    using System.Text;
    
    
    namespace roughDraft
    {
        class Program
        {
            public static void Main()
            {
                StreamWriter oFile = File.AppendText("baza.txt");
                string output = "Current date and time: " + DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");
    
    
                oFile.WriteLine(output);
    
                Console.WriteLine(output);
    
                Console.ReadKey();
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我不知道为什么它只创建一个空文件.

    .net c# file visual-studio-2010

    2
    推荐指数
    1
    解决办法
    1万
    查看次数

    通过控制台界面访问编译器时包含库

    这个例子的启发下,我决定扩展Factorial类.

    using System;
    using System.Numerics;
    
    namespace Functions
    {
        public class Factorial
        {
            public static BigInteger CalcRecursively(int number)
            {
                if (number > 1)
                    return (BigInteger)number * CalcRecursively(number - 1);
                if (number <= 1)
                    return 1;
    
                return 0;
            }
    
            public static BigInteger Calc(int number)
            {
                BigInteger rValue=1;
    
                for (int i = 0; i < number; i++)
                {
                    rValue = rValue * (BigInteger)(number - i);                
                }
    
                return rValue;
    
            }      
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我使用过System.Numerics,默认情况下不包含它.因此,命令
    csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs输出:

    Microsoft (R) Visual …
    Run Code Online (Sandbox Code Playgroud)

    c# windows console visual-studio

    2
    推荐指数
    1
    解决办法
    950
    查看次数

    如何将文本追加到TextBox?

    我认为以下代码应该是不言自明的.

    #include <Windows.h>
    
    static HWND textBoxInput;
    static HWND button;
    static HWND textBoxOutput;
    
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdLine,int nCmdShow)
    {
        HWND hMainWindow;
        WNDCLASS wc = {};
        wc.lpfnWndProc = WindowProc;
        wc.lpszClassName = "Main's window class";
        wc.hInstance = hInstance;
        RegisterClass(&wc);
    
    
        hMainWindow = CreateWindow(wc.lpszClassName,"Append text main window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,500,400,NULL,NULL,hInstance,NULL);
    
        error=GetLastError();
    
        if(hMainWindow == NULL) return 1;
    
        textBoxInput = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", NULL,WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 10, 10, 300, 21, hMainWindow, NULL, NULL, NULL);
    
        button = CreateWindowEx(WS_EX_CLIENTEDGE,"Button","Append",WS_CHILD …
    Run Code Online (Sandbox Code Playgroud)

    c c++ winapi textbox

    2
    推荐指数
    1
    解决办法
    1万
    查看次数

    获取包含 windows 系统目录的驱动器的盘符

    #ifndef UNICODE
    #define UNICODE
    #endif
    
    #include <Windows.h>
    #include <cstdio>
    
    TCHAR* getSystemVolumine()
    {
        TCHAR volumine[2];
        TCHAR buffer[30];
    
        GetSystemWindowsDirectory(buffer,30);
    
        for(int i=0 ; i < 2 ; i++)
            volumine[i]=buffer[i];
    
        return volumine;
    }
    
    
    int main()
    {
        wprintf(L"Your system volumine letter%s\n",getSystemVolumine());
        system("pause");
    }
    
    Run Code Online (Sandbox Code Playgroud)

    我不知道这段代码有什么问题。调试未显示错误或警告,但输出为:

    Your system volumine letter
    Press any key to continue...
    
    Run Code Online (Sandbox Code Playgroud)

    c++ windows winapi

    1
    推荐指数
    1
    解决办法
    2675
    查看次数

    从cstring中删除空字符

    #ifndef UNICODE
    #define UNICODE
    #endif
    
    #include <Windows.h>
    #include <cstring>
    #include <cstdio>
    
    
    
    int main()
    {
        TCHAR* greeting = L"HELL\0O W\0ORLD!";
    
        wprintf(L"%s\n",greeting);
    
        _wsystem(L"pause");
        return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    如何删除"问候语"中的所有空字符(尾随除外)?我想避免从头开始创建函数.我的意思是PHP的str_replace的C++版本.

    c

    1
    推荐指数
    1
    解决办法
    1895
    查看次数

    在单独的头文件中定义的结构

    LMlib.h

    #ifndef LMlib_H
    #define LMlib_H
    #endif
    
    #define MAX_IP_LENGTH 15         
    #define MAX_TABLE_ROWS 255
    
    struct ForwardingTableRow
    {
            char address[MAX_IP_LENGTH];
            int subnetMask;
            int interface;
    };
    
    typedef struct ForwardingTableRow ForwardingTableRow;
    
    Run Code Online (Sandbox Code Playgroud)

    LMlib.c

    #include <stdio.h>
    #include <math.h>
    #include "LMlib.h"
    
    void ReadForwardingTable(FILE* f,ForwardingTableRow * table)
    {
            int i;
            for(i=0;i<MAX_TABLE_ROWS;i++)
            {
                    fscanf(f,"%s %d %d",&table.address[i],&table.subnetMask[i],&table.interface[i]);
            }
    
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

    编译器命令:

    cc LMlib.c LMlib.h main.c -lm

    错误:

    LMlib.c: In function ‘ReadForwardingTable’:
    LMlib.c:11:27: error: request for member ‘address’ in something not a structure or union
    LMlib.c:11:45: error: request for member ‘subnetMask’ in something …
    Run Code Online (Sandbox Code Playgroud)

    c struct header

    1
    推荐指数
    1
    解决办法
    363
    查看次数

    如何从标准FILE结构中获取文件名?

    我想要的是:

     void printFname(FILE * f)
        {    
            char buf[255];
            MagicFunction(f,buf);
            printf("File name: %s",buf);
        }
    
    Run Code Online (Sandbox Code Playgroud)

    所以,我需要的只是"MagicFunction",但不幸的是我没有找到这样的 ......

    有没有办法使用OS库实现?(windows.h,cocoa.h,posix.h等)

    c

    1
    推荐指数
    1
    解决办法
    230
    查看次数

    奇怪的值添加到列表中

    #include <stdlib.h>
    #include <stdio.h>
    
    
    struct a
    {
        a * next;
        double v;
    };
    
    
    void add(struct a* list,double d)
    {
        if(!list->next) goto exception; //I know that a lot of programmers have a low opinion about "goto"
    
        list=list->next;
    
        list->v=d;
    
        return;
    
    exception:
        printf("Cannot add a new element to the list\n");
    }
    
    int main()
    {
        struct a l;
        double j;
        int i;
    
        for(j=1.0; j<10.0; j+=1.0)
        {   
            l.next= (a*)malloc(sizeof(a));
            add(&l,j);
            printf("%lf ",l.v);
        }
        return 0;
    }
    
    Run Code Online (Sandbox Code Playgroud)

    这个程序编译,但输出中有一个混乱:

    -92559631349317831000000000000000000000000000000000000000000000.000000 -92559631 349317831000000000000000000000000000000000000000000000.000000 -92559631349317831 000000000000000000000000000000000000000000000.000000 -92559631349317831000000000 000000000000000000000000000000000000.000000 -92559631349317831000000000000000000 …

    c list

    1
    推荐指数
    1
    解决办法
    159
    查看次数

    "java.net.BindException:无法在客户端分配请求的地址"

    服务器:

    import java.net.*;
    import java.io.*;
    
    import javax.swing.text.html.HTMLEditorKit;
    
    public class TCPReceiver implements Runnable
    {
        private Settings s;
        private int portTCP;
        private BlackAndWhite2GUI gui;
        private ServerSocket ss = null;
        private InputStream in = null;
    
        public TCPReceiver(Settings s,BlackAndWhite2GUI gui)
        {
            this.s = s;
            this.gui = gui;
        }        
    
        @Override 
        public void run()
        {
            startTCP();
        }
    
        public void startTCP()
        {
            final int BACKLOG=100;
            final int BUFSIZE=32;
            int recVMsgSize;
    
            Socket client = null;        
    
            byte[] receiveBuf = new byte[BUFSIZE];
            String content = new String();
    
            try
            {
               ss=new …
    Run Code Online (Sandbox Code Playgroud)

    java sockets nat

    1
    推荐指数
    1
    解决办法
    1万
    查看次数

    标签 统计

    c ×5

    c++ ×3

    winapi ×3

    c# ×2

    windows ×2

    .net ×1

    console ×1

    file ×1

    header ×1

    java ×1

    keypress ×1

    list ×1

    nat ×1

    sockets ×1

    struct ×1

    textbox ×1

    visual-studio ×1

    visual-studio-2010 ×1