Related
The goal is to create a program that logs keystrokes and writes it into a text file. Currently, just tapping a key will write that key a hundred times so I'm trying to slow it down a bit.
However, using Sleep() will prevent this whole code from doing anything at all unless I use Sleep(0) (which, as I understand, means "Do not let lower priority threads run").
Code:
// Subconsole is Windows so the running app is not visible to a certain someone
int __stdcall WinMain(_In_ HINSTANCE hinstance, _In_opt_ HINSTANCE hprevinstance, _In_ LPSTR lpcmdline, _In_ int ncmdshow)
{
FILE* write;
char running = 1;
fopen_s(&write, "typelog.txt", "w");
while (running)
{
_Bool keytoggle;
char key;
// Go from A to Z and see if the key for that was pressed
for (int i = 0x41; i < 0x5A; i++)
{
// Is the highest order bit for GetAsyncKeyState a 1 (is the key down)
keytoggle = (GetAsyncKeyState(i) & (1 << 15)) != 0;
if (keytoggle)
{
key = i; // save the key that was pressed
break;
}
}
// If the key was pressed, write it, otherwise write a space
if (keytoggle)
{
if (write)
fprintf(write, "%c", key);
}
else
{
if (write)
fprintf(write, " ");
}
// Sleep for like, just one millisecond please
Sleep(1);
}
return 0;
}
I have heard that using Sleep, even for a 1ms, can be extended to 20ms because of the system timer. Is that the case? Even if it was, why would the code not be executed at all?
I've searched for an hour or so and found nothing. If you can help it'd be great.
With debugging, the problem is your txt handle is not closed when calling Sleep(1). You can use Message-Only Windows and Raw Input to achieve your goal. For example:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
RawInput(hWnd);
return TRUE;
}
and
#define BUFFER 512
HRESULT ShowRawInputInfo(LPARAM lParam)
{
UINT dwSize = 0;
HRESULT hResult;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER));
LPBYTE lpb = new BYTE[dwSize];
if (lpb == NULL)
{
return 0;
}
if (GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)) != dwSize)
OutputDebugString(TEXT("GetRawInputData does not return correct size !\n"));
RAWINPUT* raw = (RAWINPUT*)lpb;
WCHAR szTempOutput[BUFFER];
if (raw->header.dwType == RIM_TYPEKEYBOARD)
{
hResult = StringCchPrintf(szTempOutput, BUFFER,
TEXT(" Kbd: make=%04x Flags:%04x Reserved:%04x ExtraInformation:%08x, msg=%04x VK=%04x \n"),
raw->data.keyboard.MakeCode,
raw->data.keyboard.Flags,
raw->data.keyboard.Reserved,
raw->data.keyboard.ExtraInformation,
raw->data.keyboard.Message,
raw->data.keyboard.VKey);
if (FAILED(hResult))
{
// TODO: write error handler
}
OutputDebugString(szTempOutput);
}
else if (raw->header.dwType == RIM_TYPEMOUSE)
{
hResult = StringCchPrintf(szTempOutput, BUFFER,
TEXT("Mouse: usFlags=%04x ulButtons=%04x usButtonFlags=%04x usButtonData=%04x ulRawButtons=%04x lLastX=%04x lLastY=%04x ulExtraInformation=%04x\r\n"),
raw->data.mouse.usFlags,
raw->data.mouse.ulButtons,
raw->data.mouse.usButtonFlags,
raw->data.mouse.usButtonData,
raw->data.mouse.ulRawButtons,
raw->data.mouse.lLastX,
raw->data.mouse.lLastY,
raw->data.mouse.ulExtraInformation);
if (FAILED(hResult))
{
// TODO: write error handler
}
OutputDebugString(szTempOutput);
}
delete[] lpb;
return 0;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD dwStyleOfStaticText = 0;
HWND hBmp2 = NULL;
WORD reason = 0;
switch (message)
{
case WM_INPUT:
{
ShowRawInputInfo(lParam);
break;
}
case WM_KEYDOWN:
WCHAR szTempOutput[512];
StringCchPrintf(szTempOutput, 512,
TEXT(" KeyValue=%04x\n"), wParam);
OutputDebugString(szTempOutput);
//PostMessage(hWnd, WM_COMMAND, KILLTHEWIN, 0);// hope it serializes message
//PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Edit: Or Hook
I am receiving incompatible pointer type everywhere when I am trying to work with unicode LPCWSTR types.
I am completely stuck no matter what I do, tried to search for a answer lots of times and still no hope!
My code:
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Mouse.h"
#include "Keyboard.h"
//#define APP_WindowClassName "MOUSE_CLICKER"
//#define APP_WindowTitle "Mouse Clicker"
LRESULT CALLBACK app_WindowProcedure (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.
WNDCLASSEXW main_WindowClass = { };
main_WindowClass.cbSize = sizeof(WNDCLASSEXA);
main_WindowClass.cbClsExtra = 0;
main_WindowClass.cbWndExtra = 0;
main_WindowClass.hInstance = hInstance;
main_WindowClass.lpfnWndProc = app_WindowProcedure;
main_WindowClass.lpszClassName = TEXT("MOUSE_CLICKER");
main_WindowClass.lpszMenuName = NULL;
main_WindowClass.hbrBackground = (HBRUSH) (TEXT(COLOR_BACKGROUND));
main_WindowClass.hCursor = LoadCursorW (NULL, TEXT(IDC_ARROW));
main_WindowClass.hIcon = LoadIconW(NULL, TEXT(IDI_APPLICATION));
main_WindowClass.hIconSm = LoadIconW(NULL, TEXT(IDI_APPLICATION));
main_WindowClass.style = CS_DBLCLKS;
//CS_HREDRAW | CS_VREDRAW a
if (RegisterClassExW(&main_WindowClass) == 0) {
printf ("[CRITICAL] main_WindowClass cannot be registered!");
return -1;
}
HWND main_WindowHandle = CreateWindowExW (0, TEXT("MOUSE_CLICKER"), TEXT("MouseClicker"), WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
if (main_WindowHandle == NULL) {
return -1;
}
ShowWindow(main_WindowHandle, nCmdShow);
printf("Unicode: %d", IsWindowUnicode(main_WindowHandle));
MSG ProcessingMessage;
while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
TranslateMessage(&ProcessingMessage);
DispatchMessage(&ProcessingMessage);
}
return ProcessingMessage.wParam;
}
My build log picture link:
PS: I am a beginner in C (still learning) and understandable descriptive information on what I am doing wrong would be nice.
PS2: To avoid confusion this is pure C, NOT C++.
Solution code:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include "Mouse.h"
#include "Keyboard.h"
LRESULT CALLBACK app_WindowProcedure (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.
WNDCLASSEXW main_WindowClass = { };
main_WindowClass.cbSize = sizeof(WNDCLASSEXA);
main_WindowClass.cbClsExtra = 0;
main_WindowClass.cbWndExtra = 0;
main_WindowClass.hInstance = hInstance;
main_WindowClass.lpfnWndProc = app_WindowProcedure;
main_WindowClass.lpszClassName = L"MOUSE_CLICKER";
main_WindowClass.lpszMenuName = NULL;
main_WindowClass.hbrBackground = (HBRUSH) (COLOR_BACKGROUND);
main_WindowClass.hCursor = LoadCursorW (NULL, (LPCWSTR) IDC_ARROW);
main_WindowClass.hIcon = LoadIconW(NULL, (LPCWSTR) IDI_APPLICATION);
main_WindowClass.hIconSm = LoadIconW(NULL, (LPCWSTR) IDI_APPLICATION);
main_WindowClass.style = CS_DBLCLKS;
//CS_HREDRAW | CS_VREDRAW a
if (RegisterClassExW(&main_WindowClass) == 0) {
printf ("[CRITICAL] main_WindowClass cannot be registered!");
return -1;
}
HWND main_WindowHandle = CreateWindowExW (0, L"MOUSE_CLICKER", L"MouseClicker", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
if (main_WindowHandle == NULL) {
return -1;
}
ShowWindow(main_WindowHandle, nCmdShow);
printf("Unicode: %d", IsWindowUnicode(main_WindowHandle));
MSG ProcessingMessage;
while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
TranslateMessage(&ProcessingMessage);
DispatchMessage(&ProcessingMessage);
}
return ProcessingMessage.wParam;
}
Define UNICODE in your project.
Based on whether UNICODE is defined, TEXT expands to either LPSTR or LPWSTR. You are explicitly calling *W versions of WinAPI functions but pass LPSTR instead of LPWSTR. Prefixing string literal with L should work actually. Maybe you used it as L("foo") - it won't work this way. You need to use L"foo".
Overall, if you use TEXT, you should use WinAPI functions without suffixes, so that code will compile both with and without UNICODE defined. If you explicitly use *W functions, use L"" strings.
There are several approaches you can take to get this to work; since you're already going down the path of using Generic-Text Mappings in Tchar.h, let's continue on that route.
Here is the main body of your program, revised to run as Unicode when your project is set to build Unicode.
(A side "benefit", if you would call it that, is that you can run your application as an ASCII application as well just by changing the character set of the project.)
LRESULT CALLBACK app_WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.
WNDCLASSEX main_WindowClass = {};
main_WindowClass.cbSize = sizeof(WNDCLASSEX);
main_WindowClass.cbClsExtra = 0;
main_WindowClass.cbWndExtra = 0;
main_WindowClass.hInstance = hInstance;
main_WindowClass.lpfnWndProc = app_WindowProcedure;
main_WindowClass.lpszClassName = TEXT("MOUSE_CLICKER");
main_WindowClass.lpszMenuName = NULL;
main_WindowClass.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
main_WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
main_WindowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
main_WindowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
main_WindowClass.style = CS_DBLCLKS;
//CS_HREDRAW | CS_VREDRAW a
if (RegisterClassEx(&main_WindowClass) == 0) {
_tprintf(TEXT("[CRITICAL] main_WindowClass cannot be registered!"));
return -1;
}
HWND main_WindowHandle = CreateWindowEx(0, TEXT("MOUSE_CLICKER"), TEXT("MouseClicker"), WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
if (main_WindowHandle == NULL) {
return -1;
}
ShowWindow(main_WindowHandle, nCmdShow);
_tprintf(TEXT("Unicode: %d"), IsWindowUnicode(main_WindowHandle));
MSG ProcessingMessage;
while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
TranslateMessage(&ProcessingMessage);
DispatchMessage(&ProcessingMessage);
}
return ProcessingMessage.wParam;
}
Note that Unicode-specific function calls were replaced with their generic counterparts (e.g., DefWindowProcW() is now DefWindowProc(); RegisterClassExW() is now RegisterClassEx(); printf() is now _tprintf(), and so on). All text is wrapped in the TEXT() macro.
An alternative approach, as you worked in part, is to make all API calls the ...W versions for Unicode functions, and to hard-code text using the L prefix to use Unicode text.
A good rule of thumb is to try to pick one technique or another, and apply it consistently and across the board.
This should work
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "Mouse.h"
#include "Keyboard.h"
//#define APP_WindowClassName "MOUSE_CLICKER"
//#define APP_WindowTitle "Mouse Clicker"
LRESULT CALLBACK app_WindowProcedure(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Arg1 - A handle to the current instance of the application, Arg 2 - ???, Arg 3 - Arguments, Arg 4 - Controls how the window is to be shown.
WNDCLASSEXW main_WindowClass = *((WNDCLASSEXW*)malloc(sizeof(WNDCLASSEXW)));
main_WindowClass.cbSize = sizeof(WNDCLASSEXA);
main_WindowClass.cbClsExtra = 0;
main_WindowClass.cbWndExtra = 0;
main_WindowClass.hInstance = hInstance;
main_WindowClass.lpfnWndProc = app_WindowProcedure;
main_WindowClass.lpszClassName = L"MOUSE_CLICKER";
main_WindowClass.lpszMenuName = NULL;
main_WindowClass.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
main_WindowClass.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
main_WindowClass.hIcon = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
main_WindowClass.hIconSm = LoadIconW(NULL, (LPWSTR)IDI_APPLICATION);
main_WindowClass.style = CS_DBLCLKS;
//CS_HREDRAW | CS_VREDRAW a
if (RegisterClassExW(&main_WindowClass) == 0) {
printf("[CRITICAL] main_WindowClass cannot be registered!");
return -1;
}
HWND main_WindowHandle = CreateWindowExW(0, L"MOUSE_CLICKER", L"MouseClicker", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600, NULL, NULL, hInstance, NULL);
if (main_WindowHandle == NULL) {
return -1;
}
ShowWindow(main_WindowHandle, nCmdShow);
printf("Unicode: %d", IsWindowUnicode(main_WindowHandle));
MSG ProcessingMessage;
while (GetMessage(&ProcessingMessage, NULL, 0, 0)) {
TranslateMessage(&ProcessingMessage);
DispatchMessage(&ProcessingMessage);
}
return ProcessingMessage.wParam;
}
I've sub-classed some button controls, since I'm drawing the whole UI myself (to the dialog's hdc).
This is to avoid flicker, the intention is that all drawing is done via a single memDC - preventing the staggered update of the UI.
So, I draw everything to the dialog's background, then position some buttons over the regions of the UI that should react to mouse events. So far so good. Or so I thought.
I sub-classed the buttons, using the following WndProc, expecting that Windows would do everything as per normal, except the drawing.
LRESULT CALLBACK invisibleBtnProc(HWND hwndBtn, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
long oldProc = GetWindowLong(hwndBtn, GWL_USERDATA);
switch (uMsg)
{
case WM_PAINT:
ValidateRect(hwndBtn, NULL);
return 0;
case WM_ERASEBKGND:
return 1;
}
return CallWindowProc((WNDPROC)oldProc, hwndBtn, uMsg, wParam, lParam);
}
The buttons are created and subclassed with the following code:
for (i=0; i<n; i++)
{
btn = CreateWindow(WC_BUTTON, L"", WS_VISIBLE|WS_CHILD, 0,0,0,0, hwndDlg, (HMENU)(firstBigBtnId+i), hInst, NULL);
long btnProcCur = GetWindowLong(btn, GWL_WNDPROC);
SetWindowLong(btn, GWL_USERDATA, btnProcCur);
SetWindowLong(btn, GWL_WNDPROC, (long) invisibleBtnProc);
}
When I built this code with MinGW & Code::Blocks, it works flawlessly. (both in debug and release builds)
Unfortunately, when built with MSVC & VS2010, I observe different behaviour. The debug-mode build is okay, but the release build is not. When one of the invisible buttons is clicked, the system is drawing it, obscuring the underlying 'button'.
I've a large WMF (emf? I forget) that needs to be drawn - it's quite slow and produces flicker when the window is resized, for those that wonder why the custom-draw-everything approach.
Here's what I'm seeing:
Note, that before I tried to click on the leftmost button it was not visible - just like the one on the right. Only upon clicking it does windows decide to draw it.
Resizing the parent window - (a dialog which triggers a call to InvalidateRect for the dialog) removes the erroneous drawing. Clicking the button once again causes it to be drawn.
Any ideas where I've made a mistake in my thinking?
EDIT: Added code below for a SCCCE (This displays the same unwanted behaviour when built with GCC debug & release, that the original program showed in debug build only)
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
RECT btnRect;
const int btnSize = 150;
const int btnId = 1000;
HINSTANCE hInst;
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Code::Blocks Template Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
LRESULT CALLBACK invisibleBtnProc(HWND hwndBtn, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
long oldProc = GetWindowLong(hwndBtn, GWL_USERDATA);
switch (uMsg)
{
case WM_PAINT:
ValidateRect(hwndBtn, NULL);
return 0;
case WM_ERASEBKGND:
return 1;
}
return CallWindowProc((WNDPROC)oldProc, hwndBtn, uMsg, wParam, lParam);
}
void onSize(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
RECT mRect;
GetClientRect(hwnd, &mRect);
btnRect.left = (mRect.right - btnSize) / 2;
btnRect.top = (mRect.bottom - btnSize) / 2;
btnRect.right = btnRect.left + btnSize;
btnRect.bottom = btnRect.top + btnSize;
HWND btn;
btn = GetDlgItem(hwnd, btnId);
MoveWindow(btn, btnRect.left, btnRect.top, btnSize, btnSize, false);
InvalidateRect(hwnd, NULL, false);
}
void onPaint(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HBRUSH bkBrush, redBrush;
RECT mRect;
GetClientRect(hwnd, &mRect);
hdc = BeginPaint(hwnd, &ps);
bkBrush = CreateSolidBrush(RGB(51,51,51) );
redBrush = CreateSolidBrush(RGB(255,0,0) );
FillRect(hdc, &mRect, bkBrush);
FillRect(hdc, &btnRect, redBrush);
DeleteObject(bkBrush);
DeleteObject(redBrush);
EndPaint(hwnd, &ps);
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
HWND tmp;
tmp = CreateWindow("Button", "Press Me", WS_VISIBLE|WS_CHILD, 0,0,0,0, hwnd, (HMENU)btnId, hInst, NULL);
long oldProc;
oldProc = GetWindowLong(tmp, GWL_WNDPROC);
SetWindowLong(tmp, GWL_USERDATA, oldProc);
SetWindowLong(tmp, GWL_WNDPROC, (long)invisibleBtnProc);
return 0;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_SIZE:
onSize(hwnd, wParam, lParam);
return 0;
case WM_PAINT:
onPaint(hwnd, wParam, lParam);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case btnId:
MessageBeep(MB_ICONEXCLAMATION);
break;
}
return 0;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Setting the BS_OWNERDRAW style tells Windows that it shall not draw the button itself, but that you are responsible for that. That does the trick.
There is not much you need to change. Just create the button with this style.
tmp = CreateWindow("Button", "Press Me", WS_VISIBLE|WS_CHILD|BS_OWNERDRAW, 0,0,0,0, hwnd, (HMENU)btnId, hInst, NULL);
Then in your invisibleBtnProc you can add
case WM_DRAWITEM:
ValidateRect(hwndBtn, NULL);
return TRUE;
I want to catch message of WM_DEVICECHANGE.But, there is a problem which i can not understand.I want to see when usb or cd inserted.Maybe my notification filter is wrong.
I m using radstudio and the language of its c,also its commandline application.I think everything is obvious in code.What am i doing wrong,i created window for only getting messages.Also i did not understand how it message going to WndProc from message loop.
#pragma hdrstop
#pragma argsused
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <dbt.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
switch (uiMsg)
{
case WM_DEVICECHANGE:
{
MessageBox(0,"a","b",1);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BOOL bRet;
HANDLE a;
HWND lua;
HANDLE hInstance;
MSG msg;
WNDCLASSEX wndClass;
HANDLE hVolNotify;
DEV_BROADCAST_DEVICEINTERFACE dbh;
DEV_BROADCAST_VOLUME NotificationFilter;
lua = CreateWindow("lua", NULL, WS_MINIMIZE, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
wndClass.lpfnWndProc = WndProc;
ZeroMemory(&NotificationFilter, sizeof (NotificationFilter));
NotificationFilter.dbcv_size = sizeof (NotificationFilter);
NotificationFilter.dbcv_devicetype = DBT_DEVTYP_VOLUME;
a = RegisterDeviceNotification(lua,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
MessageBox(0,"o","b",1);
if (bRet == -1)
{
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
What am i doing wrong,i created window for only getting messages.
You are asking CreateWindow() to create a window of class "lua" but you have not actually registered the "lua" class via RegisterClass/Ex() before calling CreateWindow(), and you are not checking to see if CreateWindow() returns a NULL window handle on failure.
Also i did not understand how it message going to WndProc from message loop.
That is handled by DispatchMessage(). You need to assign wndClass.lpfnWndProc and register it with RegisterClass() before calling CreateWindow(). Afterwards, when DispatchMessage() sees a message that targets the window created by CreateWindow(), it knows that WndProc() has been associated with that window and will call it directly, passing it the message.
Try this instead:
#pragma hdrstop
#pragma argsused
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <dbt.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
if (uiMsg == WM_DEVICECHANGE)
{
MessageBox(NULL, TEXT("WM_DEVICECHANGE"), TEXT("WndProc"), MB_OK);
return 0;
}
return DefWindowProc(hWnd, uiMsg, wParam, lParam);
}
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hInstance = reinterpret_cast<HINSTANCE>(GetModuleHandle(NULL));
WNDCLASS wndClass = {0};
wndClass.lpfnWndProc = &WndProc;
wndClass.lpszClassName = TEXT("lua");
wndClass.hInstance = hInstance;
if (RegisterClass(&wndClass))
{
HWND lua = CreateWindow(wndClass.lpszClassName, NULL, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (lua != NULL)
{
DEV_BROADCAST_VOLUME NotificationFilter = {0};
NotificationFilter.dbcv_size = sizeof(NotificationFilter);
NotificationFilter.dbcv_devicetype = DBT_DEVTYP_VOLUME;
HDEVNOTIFY hVolNotify = RegisterDeviceNotification(lua, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
if (hVolNotify != NULL)
{
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterDeviceNotification(hVolNotify);
}
DestroyWindow(lua);
}
UnregisterClass(wndClass.lpszClassName, hInstance);
}
return 0;
}
For added measure, you can use CreateWindowEx() instead of CreateWindow() to create a message-only window instead, if desired:
HWND lua = CreateWindowEx(0, wndClass.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
You need to set the dbcv_unitmask field of the DEV_BROADCAST_VOLUME structure to indicate which drive letters you're interested in. If you want to see media changes you also need to set the DBTF_MEDIA flag in the dbcv_flags field.
I've just started using MinGW and I'm having an issue where it isn't outputting an executable or an object file, or anything really. After fixing a few errors everything compiles fine, but no executable is being outputted. When I first installed MinGW I tested it on a simple hello world program and everything worked correctly, but I'm trying to write a basic windows application and it's not working. I've worked with gcc before, but only briefly, I don't really know anything about it.
C:\Users\Cole\Dev\Hello Windows\>gcc win_main.c -o win_main
Here's the win_main.c file:
#include <windows.h>
#include <stdio.h>
/*
* Global Variables
*/
HWND g_hwnd = NULL;
HINSTANCE g_hinst = NULL;
/*
* Forward Declarations
*/
LRESULT CALLBACK win_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param);
HRESULT init_window(HINSTANCE h_instance, int cmd_show);
/*
* Main entry point to the application.
*/
int WINAPI WinMain(HINSTANCE h_instance, HINSTANCE h_previnstance, LPSTR cmd_line, int cmd_show) {
if(FAILED(init_window(h_instance, cmd_show)))
return -1;
MSG msg = {0};
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
/*
* Register window class and create the window.
*/
HRESULT init_window(HINSTANCE h_instance, int cmd_show) {
/* Register window class. */
WNDCLASSEX wcx;
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_VREDRAW | CS_HREDRAW;
wcx.lpfnWndProc = win_proc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = h_instance;
wcx.hIcon = NULL;
wcx.hIconSm = NULL;
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "BasicWindowClass";
if(!RegisterClassEx(&wcx)) {
printf("Failed to register window class.\n");
return E_FAIL;
}
/* Create the window. */
g_hinst = h_instance;
RECT rc = {0, 0, 640, 480};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hwnd = CreateWindow("BasicWindowClass", "Windows Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, g_hinst, NULL);
if(g_hwnd == NULL) {
printf("Failed to create the window.\n");
return E_FAIL;
}
ShowWindow(g_hwnd, cmd_show);
return S_OK;
}
LRESULT CALLBACK win_proc(HWND h_wnd, UINT message, WPARAM w_param, LPARAM l_param) {
PAINTSTRUCT ps;
HDC hdc;
switch(message) {
case WM_PAINT:
hdc = BeginPaint(h_wnd, &ps);
EndPaint(h_wnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(h_wnd, message, w_param, l_param);
}
return 0;
}
You should add -mwindows gcc -mwindows win_main.c -o win_main .I guess your first program was using a 'main' function as entry point...