我对这个问题的解决方法仅在几个程序中才是正确的。为什么它不是通用的?
适用于:
不幸的是,在某些情况下什么也没发生(即使我在执行程序之前单击了文本框区域):
即使使用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的回答后的更新 …
以下程序应该打开/创建一个文件,并且每次都将当前日期写入其中.
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)
我不知道为什么它只创建一个空文件.
在这个例子的启发下,我决定扩展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) 我认为以下代码应该是不言自明的.
#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) #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) #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++版本.
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) 我想要的是:
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等)
#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 …
服务器:
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)