I am an absolute beginner. This is my first C++ code. Taking from the default project in Dev C++, youtube tutorials, and some intuition, I came up with the simple code below. I know the answer is going to be easy. I just can't seem to paste together answers from other posts the fix for mine. Any help you provide is GREATLY appreciated!!!
I get "undefined reference to CreateProcessWithLogonW" error.
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
HWND textfield;
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
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 color as the background of the window */
wincl.hbrBackground = GetSysColorBrush (COLOR_3DFACE);
/* 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 */
"Files open on Mars",/* Title Text */
WS_SYSMENU, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
444, /* The programs width */
175, /* 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, nFunsterStil);
/* 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;
}
/* 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:
CreateWindow(TEXT("button"), TEXT("Open files"),
WS_VISIBLE | WS_CHILD,
10,10,80,25,
hwnd, (HMENU) 1, NULL, NULL
);
textfield = CreateWindow("STATIC",
"Please click the button to retrieve a list of open files on server",
WS_VISIBLE | WS_CHILD,
10,50,400,25, hwnd, NULL, NULL, NULL);
textfield = CreateWindow("STATIC",
"Message at bottom of box",
WS_VISIBLE | WS_CHILD,
10,120,400,25, hwnd, NULL, NULL, NULL);
break;
case WM_COMMAND:
if (LOWORD(wParam) == 1) {
CreateProcessWithLogonW(L"me", L"company", L"xxxxxx", 0, 0, L"c:\\files.cmd", 0, NULL, NULL, NULL, NULL);
MessageBox(hwnd, "A text file with all of the open files has been placed on your desktop", "Open Files", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
According to the documentation, CreateProcessWithLogonW is defined in Advapi32.lib. You need to pass that library to the linker.
Related
I am trying to add a bitmap image to a window in my Win32 GUI program.
I watched several tutorials but I couldn't get the image to appear in the window, I compiled the program and it just isn't there.
This is my code, most of it is just the default Codeblocks Win32 template. The image is in the same directory as the files for the program.
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void loadImages();
HWND textfield, nextbutton, himage;
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("Main");
HBITMAP hportrait;
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 */
_T("Title"), /* 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;
}
/* 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:
loadImages();
himage = CreateWindow("STATIC", NULL, WS_VISIBLE
| WS_CHILD | SS_BITMAP, 10, 50, 300, 70,
hwnd, NULL, NULL, NULL);
SendMessageW(himage, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hportrait);
textfield = CreateWindow("STATIC", "Hello!", WS_VISIBLE
| WS_CHILD | WS_BORDER, 100, 20, 300, 50,
hwnd, NULL, NULL, NULL);
nextbutton = CreateWindow("BUTTON", "Continue", WS_VISIBLE
| WS_CHILD | WS_BORDER, 140, 80,
200, 20, hwnd, (HMENU) 1, NULL, NULL);
break;
case WM_COMMAND:
switch(LOWORD(wParam)){
case 1:
::MessageBeep(MB_ICONERROR);
break;
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW-2));
EndPaint(hwnd, &ps);
}
case WM_ERASEBKGND:
{
Sleep(1000);
return DefWindowProc(hwnd, message, wParam, lParam);
}
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
void loadImages(){
hportrait = (HBITMAP)LoadImageW(NULL, L"parrots.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
The problem was that the image was an invalid file. I opened ms paint and saved the image as a 24bit bitmap and the program worked with no problems.
I am writing a quite simple WinAPI application. Just a window with single button in client area (this is minimum reproducible example). Button creation code is in WM_CREATE main window message handler:
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
hwndButton = CreateWindowW(L"BUTTON", L"Start", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
3, 30, 60, 30, hwnd, NULL, hInst, NULL);
break;
In real application I have to update window content when user moves mouse over window and when user resizes window.
All update here is just filling client area with single color:
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 10));
EndPaint(hwnd, &ps);
return 0
WM_size and WM_MOUSEMOVE message handlers just invalidate all client area:
case WM_SIZE:
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_MOUSEMOVE:
InvalidateRect(hwnd, NULL, TRUE);
break;
The problem is, behaviour of application differs in case of resizing window and in case of moving mouse pointer over it. In the first case, nothing happens except of window resizing - just as I want.
But in second case, when mouse pointer moves over window, window begins to flicker. It's annoying. Moreover, button in client area also disappears - and appears again only after mouse pointer stops. This is absolutely unacceptable.
What can I do to fix this situation?
Full text of program, if it will be useful:
#include <tchar.h>
#include <windows.h>
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
HINSTANCE hInst;
/* Make the class name into a global variable */
WCHAR szClassName[ ] = L"WindowsApp";
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/*Application entry point. Function made from template. */
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd, hWndToolbar; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEXW wincl; /* Data structure for the windowclass */
hInst = hThisInstance;
/* 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 = NULL;
wincl.hIconSm = wincl.hIcon; //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 (!RegisterClassExW (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowExW (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
L"Check button", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
CW_USEDEFAULT, /* The programs width */
CW_USEDEFAULT, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* Class menu used */
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;
}
/* Message handler: */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndButton;
PAINTSTRUCT ps;
HDC hdc;
switch (message) /* handle the messages */
{
case WM_CREATE:
hInst = ((LPCREATESTRUCT)lParam)->hInstance;
hwndButton = CreateWindowW(L"BUTTON", L"Start", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
3, 30, 60, 30, hwnd, NULL, hInst, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 10));
EndPaint(hwnd, &ps);
return 0;
case WM_SIZE:
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_MOUSEMOVE:
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
I am creating a Win32 ComboBox for the first time. And I have a problem here.
When calling CreateWindow for the ComboBox, it calls the WndProc callback function again with the WM_CREATE message, so what happens is the ComboBox makes a child ComboBox, again and again like recursion.
Here is the code:
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <random>
#include <time.h>
#include <string>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInst;
LPCTSTR lpszClass = L"ComboBox";
const WCHAR *items[] = { L"Apple", L"Orange", L"Melon", L"Grape", L"Strawberry" };
HWND hwnd;
enum COMMAND_ID {
COMMAND_ID_CONTROL_COMBO_0
};
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
{
srand(time(NULL));
g_hInst = hInstance;
WNDCLASS wndClass;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WndProc;
wndClass.lpszClassName = lpszClass;
wndClass.lpszMenuName = NULL;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndClass);
hwnd = CreateWindow(
lpszClass,
lpszClass,
WS_CAPTION | WS_SYSMENU | WS_THICKFRAME,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
(HMENU)NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
MSG msg;
while (true)
{
GetMessage(&msg, NULL, 0, 0);
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
static HWND hCombo;
static WCHAR str[128];
switch (msg)
{
case WM_CREATE:
{
hCombo = CreateWindow(
L"combobox",
NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER | CBS_DROPDOWN,
10, 10, 200, 200,
hWnd,
(HMENU)COMMAND_ID_CONTROL_COMBO_0,
g_hInst,
NULL);
for (int i = 0; i < 5; ++i)
{
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)items[i]);
}
SendMessage(hCombo, CB_SETCURSEL, 0, NULL);
}
break;
case WM_COMMAND:
{
switch (LOWORD(wparam))
{
case COMMAND_ID_CONTROL_COMBO_0:
switch (HIWORD(wparam))
{
case CBN_SELCHANGE:
{
int iCurSel = SendMessage(hCombo, CB_GETCURSEL, NULL, NULL);
SendMessage(hCombo, CB_GETLBTEXT, iCurSel, (LPARAM)str);
SetWindowText(hWnd, str);
}
break;
case CBN_EDITCHANGE:
GetWindowText(hCombo, str, 128);
SetWindowText(hWnd, str);
break;
}
break;
default:
break;
}
}
return 0;
}
return DefWindowProc(hWnd, msg, wparam, lparam);
}
And here is the result:
I tried to put some boolean flag to execute WM_CREATE only once, and it works, I mean only creating one ComboBox without any child in it.
But, it just looked like only a white window with a border mark, there's no arrow button or anything to dropdown page that the ComboBox is supposed to have.
This recursive case never happened when I was creating different controls like Buttons, CheckBoxes, ListBoxes, etc.
And the ComboBox created doesn't look like it has the proper shape, too.
Hope I am just missing something simple.
When calling CreateWindow for the ComboBox, it calls the WndProc
callback function again with the WM_CREATE message, so what happens
is the ComboBox makes a child ComboBox, again and again like
recursion.
WM_CREATE message is sent to the window procedure of the new window after the window is created. Your first WM_CREATE message is generated by this line hwnd = CreateWindow(). Then you create another window in first WM_CREATE message, so it will generate second WM_CREATE message. Because you use the same registered class ("ComboBox" / "combobox", it is not case sensitive) to create all these windows, all of them use the same one window procedure. So you receive WM_CREATE message again and again until CreateWindow fail to create a window and return NULL.
But, it just looked like only a white window with a border mark,
there's no arrow button or anything to dropdown page that the ComboBox
is supposed to have.
The root cause is you register a class with the same name as the existing system class: "ComboBox" / "combobox". This new registered class override the existing one. It is just a common window instead of a predefined Combobox control as #RemyLebeau pointed out.
An application can register an application local class having the same
name as a system class. This replaces the system class in the context
of the application but does not prevent other applications from using
the system class.
Refer to "How the System Locates a Window Class".
To make the Combobox display in expected shape, what you need to do is changing the lpszClass to a non-predefined one, for example, like "SimpleComboBoxExample".
It is suggested to use predefined macro of Combobox Class Name: WC_COMBOBOX instead of L"combobox".
More reference: "How to Create a Simple Combo Box".
You are not actually creating a Win32 ComboBox at all. You are registering your own class named "ComboBox", and then creating a window of that class, which creates a window of that class, which creates a window of that class, and so on recursively.
You need to change this line:
LPCTSTR lpszClass = L"ComboBox";
To a different unique name, such as "MyWindowClass".
On a side note, your message loop is structured wrong. It should look like this instead:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
And in your WndProc(), the return 0; statement underneath the WM_COMMAND handler is in the wrong place. It needs to be moved inside of the WM_COMMAND handler instead:
case WM_COMMAND:
{
switch (...)
{
...
}
return 0; // <-- moved here
}
//return 0; // <-- from here
I have put a procedure for capturing screenshots into memoryDC in a Winapi timer.
I can successfully blit the said image to the window, but how would i do this repeatedly, lets say every 1-2 seconds?
The currenct code i have can blit it 1-2 seconds at a time, but it wont blit it to the Window correctly (the image is misplaced).
How shall i go about doing this?
#include <windows.h>
#include <iostream>
#include <windowsx.h>
#define TIMERID 3232
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
RECT rectangle{
50,
50,
690,
409
};
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
HDC handle_WindowDC;
HDC handle_MemoryDC;
HDC handle_ScreenDC;
//BITMAP bitmap;
HBITMAP handle_Bitmap;
int x, y;
HWND hand;
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 */
1600, /* The programs width */
900, /* 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);
SetTimer(hwnd, TIMERID, 1000, (TIMERPROC)NULL);
/* 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;
}
/* 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:{
}
//hand = CreateWindowEx(NULL, "STATIC", "", SS_BITMAP|WS_VISIBLE, 500,300, 640 ,360 , hwnd, HMENU(IDCSTATIC_BITMAP), GetModuleHandle(NULL), NULL); }
break;
case WM_TIMER:
switch(LOWORD(wParam)){
case TIMERID:{
//MessageBox(NULL,NULL,NULL,NULL);
handle_ScreenDC = GetDC(NULL);
handle_MemoryDC = CreateCompatibleDC(handle_ScreenDC);
x = GetDeviceCaps(handle_ScreenDC, HORZRES);
y = GetDeviceCaps(handle_ScreenDC, VERTRES);
handle_Bitmap = CreateCompatibleBitmap(handle_ScreenDC, 640, 360);
SelectObject(handle_MemoryDC, handle_Bitmap);
StretchBlt(handle_MemoryDC, 0, 0, 640, 360, handle_ScreenDC, 0, 0, x, y, SRCCOPY);
UpdateWindow(hwnd);
BitBlt(handle_WindowDC, 50, 50, x, y, handle_MemoryDC, 0, 0, SRCCOPY);
}
break;
}break;
case WM_PAINT:{
PAINTSTRUCT paintstruct;
handle_WindowDC = BeginPaint(hwnd, &paintstruct);
//BitBlt(handle_WindowDC, 50, 50, x, y, handle_MemoryDC, 0, 0, SRCCOPY);
EndPaint(hwnd, &paintstruct);
}
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_LBUTTONDOWN:{
std::cout <<"\nx: " << GET_X_LPARAM(lParam) << "\ny: " << GET_Y_LPARAM(lParam);
tagPOINT point;
point.x = GET_X_LPARAM(lParam);
point.y = GET_Y_LPARAM(lParam);
if( PtInRect(&rectangle, point)){
int x, y;
x = 2.5*(point.x - 50);
y = 2.5*(point.y - 50);
//I juse use setcursorpos for now, but when connecting to server.exe i will do a send() and send x,y coordinates along with
//a click or rightclick, or whatever.
SetCursorPos(x, y);
}
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
You are using the DC from WM_PAINT to paint in the WM_TIMER code, that DC would have been released during EndPaint() -- the MSDN Docs says "EndPaint releases the display device context that BeginPaint retrieved'.
Also make sure you release your DCs with ReleaseDC(handle_ScreenDC) and DeleteObject(handle_MemoryDC).
The code for WM_TIMER should just invalidate the rectangle, do all The painting during WM_PAINT.
See this article for sample code.
I need to hookup alt+alphanumberic keypress inside a child window control.
I have written this program to test it's functionality. But it seems it does not response to keystrokes
like ALT+X , ALT+X which are necessary for my application(keyboard translator).
#include <windows.h>
#include <stdio.h>
#include <wchar.h>
#include <tchar.h>
#include <string.h>
#ifndef NULL
#define NULL 0
#endif
/* glaobal variables */
HWND hwndEdit = NULL;
HWND hwndWindow = NULL;
WCHAR* lpszClassName = L"Vijesekara Keyboard Test Application";
WCHAR* lpszAppName = L"Vijesekara Keyboard Test Application";
int main_window_width = 0;
int main_window_height = 0;
WNDPROC edit_old_wndproc;
/*
GWL_WNDPROC
*/
#ifndef GWL_WNDPROC
#define GWL_WNDPROC (-4)
#endif
/* child window ID's */
#ifndef __CHILD_WINDOW_EDIT_ID__
#define __CHILD_WINDOW_EDIT_ID__
#define ID_EDIT 2000
// :TODO: more comes from here //
#endif
/* constrains on child window positions */
#ifndef __CHILD_WINDOW_POSITION__
#define __CHILD_WINDOW_POSITION__
#define EDIT_CHILD_LEFT_PRECENTAGE 0.05
#define EDIT_CHILD_RIGHT_PRECENTAGE 0.05
#define EDIT_CHILD_TOP_PRECENTAGE 0.10
#define EDIT_CHILD_BOTTOM_PRECENTAGE 0.10
// :TODO: Other controls //
#endif
/* main window procedure */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam);
/* edit box procedure */
LRESULT CALLBACK EditBoxProc(HWND ,UINT , WPARAM , LPARAM );
/* resize child windows */
void ResizeChildWindows();
/* win main entry */
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW |CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = (HICON)LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground =(HBRUSH) GetStockObject( WHITE_BRUSH );
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName ;
/* Register class */
int result ;
result = RegisterClass( &wndclass) ;
/* if failed to register window */
if ( 0 == result )
{
MessageBox(NULL,L"Registering Window Have Been Failed" , lpszAppName, MB_OK);
exit(0);
}
/* Create the window */
hwndWindow = CreateWindow(lpszAppName , lpszAppName ,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,\
CW_USEDEFAULT, CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
if(NULL == hwndWindow )
{
MessageBox(NULL,L"Window Creation have being failed", lpszAppName,MB_OK);
exit(0);
}
ShowWindow(hwndWindow, iCmdShow);
/* Enter message loop */
MSG msg ;
while(GetMessage(&msg,NULL, 0,0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/* window procedure */
LRESULT CALLBACK WndProc (HWND hwnd, UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
if( hwndEdit != NULL)
{
MessageBox(NULL,L"Edit control handle have already initialized",lpszAppName , MB_OK);
exit(0);
}
hwndEdit = CreateWindow( L"edit" ,NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL\
|WS_BORDER|ES_LEFT|ES_MULTILINE|ES_AUTOVSCROLL,0,0,0,0,hwnd,(HMENU)ID_EDIT\
,GetModuleHandle(NULL),NULL);
if( NULL == hwndEdit )
{
MessageBox(hwnd, L"Edit child window control creation failed", lpszAppName, MB_OK);
exit(0);
}
edit_old_wndproc = (WNDPROC)SetWindowLongPtr(hwndEdit,GWL_WNDPROC,(LONG_PTR)EditBoxProc );
break;
case WM_SIZE:
main_window_height = HIWORD(lParam);
main_window_width = LOWORD (lParam);
ResizeChildWindows();
return 0;
case WM_COMMAND:
/*:TODO:process */
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
/* resize window procedure */
void ResizeChildWindows()
{
MoveWindow(hwndEdit,main_window_width*EDIT_CHILD_LEFT_PRECENTAGE , main_window_height* EDIT_CHILD_TOP_PRECENTAGE , \
main_window_width * (1- EDIT_CHILD_LEFT_PRECENTAGE - EDIT_CHILD_RIGHT_PRECENTAGE) ,// height
main_window_height * (1- EDIT_CHILD_TOP_PRECENTAGE - EDIT_CHILD_BOTTOM_PRECENTAGE), TRUE // width
);
}
static int alt=0;
static int ctl=0;
static int sys =0;
WCHAR buffer[1024];
LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT message, WPARAM wParam , LPARAM lParam)
{
switch ( message)
{
case WM_SYSCHAR:
sys =1;
if( lParam && (1<<29) ) { alt= 1;}
else {
alt=0;
}
case WM_KEYPRESS:
wsprintf( buffer,TEXT("KeyCode is :%d"), wParam);
MessageBox( NULL,buffer, TEXT("Vijesekara Keyboard"), MB_OK);
SetFocus(hwnd);
/* keyboard procedure */
alt =0;
if ( sys==1)
{
sys=0;
break;
}
return 0;
default:
break;
}
return CallWindowProc(edit_old_wndproc ,hwndEdit,message,wParam,
lParam);
}
// end of vijesekara_keyboard.cpp
Note: Inside the subclassed textbox , the WM_CHAR message is not working for the alt keystrokes, I it does
not sent a WM_CHAR message with alt key pressed to the for AlT+keystokes.So I log all the messages using spy++
and then I found WM_SYSCHAR have been sent instead of WM_CHAR for those messages inside a editbox child
window.
Any idea how could I do this?
--Thanks in advance--
Okay this is pretty strange,
This is my spy+ output for the Alt+A key.
So then I have hook the WM_SYSKEYUP message, and the Fixup is this,
case WM_SYSKEYUP:
if( lParam && (1<<29) ) {alt=1;}else{break;}
if( wParam == 'A' || wParam=='Z'|| wParam =='X' || wParam== 'W' || wParam == 'Y' ){}else{break;}
case WM_SYSCHAR:
sys =1;
if( lParam && (1<<29) ) { alt=1; }
else {
alt=0;
break;
}
case WM_CHAR:
wsprintf( buffer,TEXT("KeyCode is :%d"), wParam);
MessageBox( NULL,buffer, TEXT("Vijesekara Keyboard"), MB_OK);
SetFocus(hwnd);
/* keyboard procedure */
alt =0;
return 0;
And also when I break (calling the default window procedure) on WM_SYSKEYU it gives a nasty ding-dong sound
inside the Windows.I have return it then that sound disappear.
What arx claim is very strange. And Yes I have mistaken WM_KEYPRESS, there's no something called WM_KEYPRESS.
And It should be corrected to WM_CHAR. According to him without these chances MessageBox appears. That's
pretty strange. #Arx, you could please post your answer, so somebody else who digging it may benefit, and also
I could give you some credits too. And also dig why was that happened and after that we could accept that
and this answer would keep as an alternative answer (does not address the question, but have fixed the
application).