Bom galera hoje vou ensinar a vocês uma simples função , mais creio que seja util.
As codes utilizada são do projeto [C#] Client Chooser - Kimoszin
Bom vamos começar primeiramente cértifique que no WinApi e Client(Kernel) tenha essas funções
WinApi.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Util
{
public static class WinApi
{
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern void SetWindowText(IntPtr hWnd, string str);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
public static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool IsZoomed(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool FlashWindow(IntPtr hWnd, bool invert);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, uint hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmd);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, int lpdwProcessId);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder className, int maxCharCount);
public const uint PROCESS_ALL_ACCESS = 0x1F0FFF;
public const uint PROCESS_VM_READ = 0x0010;
public const uint PROCESS_VM_WRITE = 0x0020;
public const uint PROCESS_VM_OPERATION = 0x0008;
public const uint MEM_COMMIT = 0x1000;
public const uint MEM_RESERVE = 0x2000;
public const uint MEM_RELEASE = 0x8000;
public const uint SWP_NOMOVE = 0x2;
public const uint SWP_NOSIZE = 0x1;
public const uint HWND_TOPMOST = 0xFFFFFFFF;
public const uint HWND_NOTOPMOST = 0xFFFFFFFE;
public const int SW_HIDE = 0;
public const int SW_SHOWNORMAL = 1;
public const int SW_SHOWMINIMIZED = 2;
public const int SW_SHOWMAXIMIZED = 3;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_SHOW = 5;
public const int SW_MINIMIZE = 6;
public const int SW_SHOWMINNOACTIVE = 7;
public const int SW_SHOWNA = 8;
public const int SW_RESTORE = 9;
public const int SW_SHOWDEFAULT = 10;
public const uint WM_LBUTTONDOWN = 0x201;
public const uint WM_LBUTTONUP = 0x202;
public const uint CREATE_SUSPENDED = 0x00000004;
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
public struct STARTUPINFO
{
public uint cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
public struct SECURITY_ATTRIBUTES
{
public int length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CreateProcess(String imageName,
String cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool boolInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
String lpszCurrentDir,
ref STARTUPINFO si,
out PROCESS_INFORMATION pi);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern Int32 WaitForSingleObject(IntPtr Handle, UInt32 Wait);
[DllImport("kernel32.dll")]
public static extern uint ResumeThread(IntPtr hThread);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
[DllImport("kernel32.dll")]
public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress,
IntPtr dwSize, MemoryProtection flNewProtect, ref MemoryProtection lpflOldProtect);
[DllImport("kernel32.dll")]
public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
[DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,
uint dwSize, AllocationType dwFreeType);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateRemoteThread(IntPtr hProcess,
IntPtr lpThreadAttributes, uint dwStackSize, IntPtr
lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public static int MakeLParam(int LoWord, int HiWord)
{
return ((HiWord << 16) | (LoWord & 0xffff));
}
//the same function but with another name =D
// just for understand the code better.
public static int MakeWParam(int LoWord, int HiWord)
{
return ((HiWord << 16) | (LoWord & 0xffff));
}
[Flags]
public enum AllocationType
{
Commit = 0x1000,
Reserve = 0x2000,
Decommit = 0x4000,
Release = 0x8000,
Reset = 0x80000,
Physical = 0x400000,
TopDown = 0x100000,
WriteWatch = 0x200000,
LargePages = 0x20000000
}
[Flags]
public enum MemoryProtection
{
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
GuardModifierflag = 0x100,
NoCacheModifierflag = 0x200,
WriteCombineModifierflag = 0x400
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize,
MemoryProtection flNewProtect, out MemoryProtection lpflOldProtect);
}
}
Client.cs (Pasta Kernel)
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace Kernel
{
public class Client
{
public static Process Process;
public static IntPtr Handle;
public static int HWND;
public static int PID;
public static UInt32 BaseAddress;
public Client(Process _process, string name)
{
Process = _process;
BaseAddress = (uint)_process.MainModule.BaseAddress.ToInt32() - 0x400000;
Handle = _process.MainWindowHandle;
PID = _process.Id;
}
}
}
Olhou ai ? esta tudo corretamente ? ok as ferramentas que iremos utilizar sao
ShowWindow , SW_HIDE = 0; , SW_RESTORE = 9;
1° Crie um checkbox em sua Form.
2° Cértifique que nao esteja nada errado.
3° Clique rapidamente 2x no Checkbox oque levara a Form.cs
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
No espaço entre { } coloquei as seguintes informaçoes.
if (CheckBox1.Checked == true)
{
WinApi.ShowWindow((IntPtr)Client.HWND, WinApi.SW_HIDE);
}
else
{
WinApi.ShowWindow((IntPtr)Client.HWND, WinApi.SW_RESTORE);
}
Irar ficar assim:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == true)
{
WinApi.ShowWindow((IntPtr)Client.HWND, WinApi.SW_HIDE);
}
else
{
WinApi.ShowWindow((IntPtr)Client.HWND, WinApi.SW_RESTORE);
}
}
Pronto , salva aperte F5 , clique no checkbox e veja se funciona, caso nao funcione poste o erro ou duvida aqui
Oque essa funçao ira fazer ? : Bom ela ira esconder a janela do Tibia , porem o processo ainda estara aberto , caso queira que volte ao normal o Tibia , desmarque o Checkbox.