Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last month.
Improve this question
I am new to the Win32 API, and I tried making my own calculator, but when I run the code, the edit control starts to glitch, and I can't see what has been inputted.
I tried adding a button which stores the value 1 into the edit control (where the text of the calculator will be displayed), but whenever I use the button, or try to type directly, the text starts to glitch.
#include <windows.h>
#include <stdlib.h>
#define ONE_BUTTON 1
/*#define TWO_BUTTON 2
#define THREE_BUTTON 3
#define FOUR_BUTTON 4
#define FIVE_BUTTON 5
#define SIX_BUTTON 6
#define SEVEN_BUTTON 7
#define EIGHT_BUTTON 8
#define NINE_BUTTON 9
#define TEN_BUTTON 0*/
void AddControls(HWND);
HWND hOut;
HWND hOne;
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst , LPSTR args, int ncmdshow)
{
WNDCLASSW wc = {0};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"mywindowclass";
wc.lpfnWndProc = WindowProcedure;
if(!RegisterClassW(&wc))
return -1;
CreateWindowW(L"mywindowclass",L"calculatorGUi",WS_OVERLAPPEDWINDOW|WS_VISIBLE,100,100,500,350,NULL,NULL,NULL,NULL);
MSG msg = {0};
while(GetMessage(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WindowProcedure(HWND hWnd,UINT msg,WPARAM wp,LPARAM lp)
{
switch(msg)
{
case WM_COMMAND:
switch (wp)
{
case ONE_BUTTON:
char one[50];
char out[15];
GetWindowText(hOne,one,50);
strcpy(out,one);
SetWindowText(hOut,out);
}
case WM_CREATE:
AddControls(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
default:
return DefWindowProcW(hWnd,msg,wp,lp);
}
}
void AddControls(HWND hWnd)
{
hOut = CreateWindowW(L"Edit",(int)NULL,WS_VISIBLE|WS_CHILD|WS_BORDER,2,2,480,106,hWnd,NULL,NULL,NULL);
hOne= CreateWindowW(L"Button",L"1",WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON|BS_DEFPUSHBUTTON 2,110,98,50,hWnd,(HMENU)ONE_BUTTON,NULL,NULL);
/*CreateWindowW(L"Button",L"2",WS_VISIBLE|WS_CHILD,102,110,99,50,hWnd,(HMENU)TWO_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"3",WS_VISIBLE|WS_CHILD,202,110,99,50,hWnd,(HMENU)THREE_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"4",WS_VISIBLE|WS_CHILD,302,110,99,50,hWnd,(HMENU)FOUR_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"5",WS_VISIBLE|WS_CHILD,402,110,80,50,hWnd,(HMENU)FIVE_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"6",WS_VISIBLE|WS_CHILD,2,165,99,50,hWnd,(HMENU)SIX_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"7",WS_VISIBLE|WS_CHILD,102,165,99,50,hWnd,(HMENU)SEVEN_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"8",WS_VISIBLE|WS_CHILD,202,165,99,50,hWnd,(HMENU)EIGHT_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"+",WS_VISIBLE|WS_CHILD,302,165,99,50,hWnd,(HMENU)NINE_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"-",WS_VISIBLE|WS_CHILD,402,165,99,50,hWnd,(HMENU)TEN_BUTTON,NULL,NULL);
CreateWindowW(L"Button",L"9",WS_VISIBLE|WS_CHILD,2,220,99,50,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Button",L"0",WS_VISIBLE|WS_CHILD,102,220,99,50,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Button",L"*",WS_VISIBLE|WS_CHILD,202,220,99,50,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Button",L"/",WS_VISIBLE|WS_CHILD,302,220,99,50,hWnd,NULL,NULL,NULL);
CreateWindowW(L"Button",L"=",WS_VISIBLE|WS_CHILD,402,220,99,50,hWnd,NULL,NULL,NULL);*/
}
Your WM_COMMAND and ONE_BUTTON cases are both missing break statements, so whenever the Edit sends update notifications to your main window, or whenever the button is clicked, your code falls through to call AddControls(), creating more and more child controls on top of the previous child controls, and reusing the same child IDs.
Also, your ONE_BUTTON handler has a buffer overflow if the user enters text with more than 14 characters.
Try this instead:
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_COMMAND:
switch (wp)
{
case ONE_BUTTON: {
char one[50] = {};
GetWindowText(hOne, one, 50);
SetWindowText(hOut, one);
break; // <-- add this!
}
break; // <-- add this!
case WM_CREATE:
AddControls(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break; // <-- add this!
default:
return DefWindowProcW(hwnd, msg, wp, lp);
}
return 0; // <-- add this!
}
Related
I want to break from the windows message loop. Just like C++ how to break message loop in windows hook . I came up with one solution, which works fine for Window Desktop Application, but fails for Console Application. How can this happen?
EDIT: I upload my codes to https://github.com/hellohawaii3/Experiment , clone it and then you can reproduce my problem quickly. Thanks!
1.Why I want to do so?
I am writing a console application. First, users are asked to set some options which determine app's behaviors. Then, the app start the message loop and monitor the input of the keyboard/mouse. Users may want to change the setting, so I want to enable users to press some hotkey, quit from the message loop and go back to the beginning of the application.
If you know any way to implement this function without worrying about breaking from the message loop, please tell me! However, I also want to know why my solution fails for console app and works well for desktop app. Thanks for your help!
2.What have I tried.
I use a bool variable 'recieve_quit'. When certain key is pressed on the keyboard, the variable is setting to True by the hook callback function. For every loop getting message, check the variable 'recieve_quit' first and quit when the variable is False.
3.Result of my experiment
For Console APP, The variable 'recieve_quit' is set correctly when certain key is pressed, however, the message loop continues.
For Windows Desktop APP with GUI, I can quit from the message loop as expected.
4.Experiment settings
I am using VS2019, C++, windows 10 home 1909.
I use dll inject to set hook for my console app.
5.My code
I provide toy example codes here, most of which is generated by Visual Studio automatically, do not bother if you think my codes are too loog.
(a)My console app
Console:
// Console.cpp
#include <iostream>
#include "dll_func.h"
#include <windows.h>
int main()
{
MSG msg;
HHOOK hhook_tmp2 = SetWindowsHookEx(WH_KEYBOARD_LL, HandleKeyboardEvent, hDllModule, 0);
while (recieve_quit == false)
{
if (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
MessageBox(NULL, TEXT("APP QUIT"), TEXT(" "), MB_OK);
}
(b)my dll containing hook function
My dll_func.h file, following the doc https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160
#pragma once
#ifdef DLL1_EXPORTS
#define DLLFUNC_API __declspec(dllexport)
#else
#define DLLFUNC_API __declspec(dllimport)
#endif
#include "framework.h"
extern "C" DLLFUNC_API bool recieve_quit;
extern "C" DLLFUNC_API LRESULT CALLBACK HandleKeyboardEvent(int nCode, WPARAM wParam, LPARAM lParam);// refer to https://stackoverflow.com/a/60913531/9758790
extern "C" DLLFUNC_API HMODULE hDllModule;//or whatever name you like
My dll_func.cpp file, containing the definition of hook function.
#include "pch.h"
#include <windows.h>
#include "dll_func.h"
bool recieve_quit = false;
LRESULT CALLBACK HandleKeyboardEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == VK_F8) {
recieve_quit = true;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
The dllmain.cpp is also modified a little after it is created by Visual Studio as suggested by https://stackoverflow.com/a/60913531/9758790 to get the hinstance of the dll for dll injection.
// dllmain.cpp
#include "pch.h"
#include "dll_func.h"
// refer to https://stackoverflow.com/a/60913531/9758790
HMODULE hDllModule;//or whatever name you like
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hDllModule = hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
(c) My Desktop application.
I select 'Creating Windows Desktop Application' when created the solution with Visual Studio. Most of the codes is generated automatically by VS. The codes I added and modifed is surrounded by ***** in the codes.
// GUI.cpp
//
#include "framework.h"
#include "GUI.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
WCHAR szTitle[MAX_LOADSTRING];
WCHAR szWindowClass[MAX_LOADSTRING];
// *********************** ADDED BY ME! ***********************
// same as dll_func.cpp
bool recieve_quit = false;
LRESULT CALLBACK HandleKeyboardEvent(int nCode, WPARAM wParam, LPARAM lParam)
{
//FILE* file;
//fopen_s(&file, "./temp0210222.txt", "a+");
//fprintf(file, "Function keyboard_hook called.n");
//fclose(file);
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == VK_F8) {
recieve_quit = true;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// *********************** END OF MY CODES ***********************
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// *********************** ADDED BY ME! ***********************
HHOOK hhook_tmp2 = SetWindowsHookEx(WH_KEYBOARD_LL, HandleKeyboardEvent, hInst, 0);
// *********************** END OF MY CODES ***********************
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_GUI, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GUI));
MSG msg;
// main message loop:
// *********************** MODIFIED BY ME! ***********************
//while (GetMessage(&msg, nullptr, 0, 0))
//{
// if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
// {
// TranslateMessage(&msg);
// DispatchMessage(&msg);
// }
//}
while (recieve_quit == false)
{
if (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
MessageBox(NULL, TEXT("APP QUIT"), TEXT(" "), MB_OK);
// *********************** END OF MODIFICATION ***********************
return (int) msg.wParam;
}
//
// Function: MyRegisterClass()
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GUI));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_GUI);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// Function: InitInstance(HINSTANCE, int)
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// Function: WndProc(HWND, UINT, WPARAM, LPARAM)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// "About"
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
(d)Tips for reproducing my results
For Console APP: Build a new console application solution named Console, and copy my codes to replace Console.cpp. Add a new project to this solution, select Create new project -> DLL, name it as Dll1. Create dll_func.h, dll_func.c, and copy my codes in. Do not forget to modify dllmain.cpp, too. Set the path of AdditionalIncludeDirectories, add dll1.lib to AdditionalDependencies. Set the path to dll and lib files too.
For Windows Desktop APP: Build a now Windows Desktop App solution, name it as GUI. Copy my codes in GUI.cpp and simply run it. Press key F8, the app will quit and pop up a message box as expected.
This post explains why console apps dont receive keyboard messages and how they can handle them:
does not go inside the Windows GetMessage loop on console application
In your console case execution enters GetMessage and never exits it. The hook receives a notification and correctly sets recieve_quit. Since the execution never exits GetMessage, recieve_quit is not checked.
This answer is about handling getting keypresses in a console app:
https://stackoverflow.com/a/6479673/4240951
In general - add a hidden window to your console app or check GetAsyncState of the needed key.
There is no need to set hooks when you have a message loop. You may check keypresses as follows:
while (recieve_quit == false)
{
if (GetMessage(&msg, nullptr, 0, 0)) {
if (msg.message == WM_KEYDOWN && msg.wParam == VK_F8)
recieve_quit = true;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
I'm having trouble utilizing Win API's DwmDefWindowProc. It always returning false, even when DWM is enabled.
I'm sending a WM_NCHITTEST message to the window procedure to detect the action of the mouse position but it's not giving me accurate results (due to DWM being enabled and me needing to call DwmDefWindowProc beforehand).
I'm using a Windows 10 (Ver. 2004) machine and I have tested the same behavior on my Windows 7 virtual machine. If I change the combability of the executable to launch as Windows XP (Disabling DWM, I'm getting the expected behavior).
Otherwise, the information I'm getting back from WM_NCHITTEST is incorrect as the right side of the maximize button returns the expected HTMAXBUTTON while the left side of it returns HTREDUCE.
Reproducing the issue with the Windows Desktop Application template (found below). If you have DWM enabled (Most Windows 10 users would), the maximize button no longer works properly. If run the program in Visual Studio and select the Output tab, you'll see that when you hover to the left (green border in the picture below) of the maximize button you will get a HTMINBUTTON message while if you hover to the right (red border in the picture below) you will get the expected HTMAXBUTTON message.
Here's the sample code
// SimpleWindow.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "SimpleWindow.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
/* New Stuff */
#include <dwmapi.h>
#pragma comment(lib, "Dwmapi.lib")
void DWMExample(HWND hWnd, unsigned int msg, unsigned int wParam, int lParam);
LRESULT ProcessNCHitTest(HWND hWnd, int lParam);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_SIMPLEWINDOW, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow)) {
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SIMPLEWINDOW));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SIMPLEWINDOW));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_SIMPLEWINDOW);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd) {
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 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)
{
switch (message) {
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId) {
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
/* New Stuff */
if (message == WM_NCHITTEST) {
return ProcessNCHitTest(hWnd, lParam);
}
DWMExample(hWnd, message, wParam, lParam);
return DefWindowProc(hWnd, message, wParam, lParam);
}
void DWMExample(const HWND hWnd, const unsigned int msg, const unsigned int wParam, const int lParam)
{
switch (msg) {
case WM_NCMOUSEMOVE:
case WM_MOUSEMOVE:
{
const auto ht = SendMessageA(hWnd, WM_NCHITTEST, wParam, lParam);
switch (ht) {
case HTBORDER:
OutputDebugStringA("HTBORDER\n");
break;
case HTBOTTOM:
OutputDebugStringA("HTBOTTOM\n");
break;
case HTBOTTOMLEFT:
OutputDebugStringA("HTBOTTOMLEFT\n");
break;
case HTBOTTOMRIGHT:
OutputDebugStringA("HTBOTTOMRIGHT\n");
break;
case HTCAPTION:
OutputDebugStringA("HTCAPTION\n");
break;
case HTCLIENT:
OutputDebugStringA("HTCLIENT\n");
break;
case HTCLOSE:
OutputDebugStringA("HTCLOSE\n");
break;
case HTERROR:
OutputDebugStringA("HTERROR\n");
break;
case HTGROWBOX:
OutputDebugStringA("HTGROWBOX\n");
break;
case HTHELP:
OutputDebugStringA("HTHELP\n");
break;
case HTHSCROLL:
OutputDebugStringA("HTHSCROLL\n");
break;
case HTLEFT:
OutputDebugStringA("HTLEFT\n");
break;
case HTMENU:
OutputDebugStringA("HTMENU\n");
break;
case HTMAXBUTTON:
OutputDebugStringA("HTMAXBUTTON\n");
break;
case HTMINBUTTON:
OutputDebugStringA("HTMINBUTTON\n");
break;
case HTNOWHERE:
OutputDebugStringA("HTNOWHERE\n");
break;
// case HTREDUCE:
// OutputDebugStringA("HTREDUCE\n");
// break;
case HTRIGHT:
OutputDebugStringA("HTRIGHT\n");
break;
// case HTSIZE:
// OutputDebugStringA("HTSIZE\n");
// break;
case HTSYSMENU:
OutputDebugStringA("HTSYSMENU\n");
break;
case HTTOP:
OutputDebugStringA("HTTOP\n");
break;
case HTTOPLEFT:
OutputDebugStringA("HTTOPLEFT\n");
break;
case HTTOPRIGHT:
OutputDebugStringA("HTTOPRIGHT\n");
break;
case HTTRANSPARENT:
OutputDebugStringA("HTTRANSPARENT\n");
break;
case HTVSCROLL:
OutputDebugStringA("HTVSCROLL\n");
break;
// case HTZOOM:
// OutputDebugStringA("HTZOOM\n");
// break;
}
}
break;
}
}
LRESULT ProcessNCHitTest(const HWND hWnd, const int lParam)
{
BOOL isDWMEnabled = false;
if (FAILED(DwmIsCompositionEnabled(&isDWMEnabled))) {
return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
}
if (!isDWMEnabled) {
return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
}
LRESULT result = 0;
if (!DwmDefWindowProc(hWnd, WM_NCHITTEST, 0, lParam, &result)) {
return DefWindowProcA(hWnd, WM_NCHITTEST, 0, lParam);
}
return result;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message) {
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Any idea where I'm going wrong? Any help would be appreciated!
I am creating a program of painting a text in multiline edit using windows api in c++. It consists of WM_PAINT message which paints the text in client area, but the caret shows unexpected behaviour in edit. It sometimes does not print characters, sometimes printed characters hides.
The code is
#define UNICODE
/*
**
**ALL HEADER FILES
**
*/
#include<wchar.h>
#include<windows.h>
#include <errno.h>
#include<conio.h>
#include<iostream>
#include<stdio.h>
#include<resource.h>
/*
**
**ALL DEFINES CONSTANT
**
*/
#define WINVER 0x0A00
#define _WIN32_WINNT 0x0A00
LRESULT CALLBACK WindowProcedure(HWND,UINT,WPARAM,LPARAM);
using namespace std;
HWND hwnd_handle;
HWND hw;
HINSTANCE his;
int WINAPI WinMain(HINSTANCE hvalue,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
his=hvalue;
/////com=nCmdShow;
LPWSTR myname=L"MY WINDOWS CLASS";
MSG msg={0};
WNDCLASSW wc;
wc.hbrBackground=(HBRUSH)COLOR_BTNFACE;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.lpszMenuName=NULL;
wc.lpszClassName=myname;
wc.hCursor=NULL;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.style=0;
wc.hInstance=hvalue;
wc.lpfnWndProc=WindowProcedure;
if(!RegisterClassW(&wc))
{
return 0;
}
///////main window
hwnd_handle=CreateWindowW(myname,L"CODE EDITOR",WS_OVERLAPPEDWINDOW,240,80,600,600,NULL, NULL,hvalue, NULL);
ShowWindow(hwnd_handle,nCmdShow);
UpdateWindow(hwnd_handle);
////////HWND hwnd=CreateWindowW(L"STATIC",L"CODE EDITOR",WS_OVERLAPPEDWINDOW,24,8,60,6,hwnd_handle, NULL,hInstance, NULL);
while(GetMessageW(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
/////main_structure(hwnd);
hw=CreateWindowW(TEXT("edit"),TEXT(""), WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE|ES_AUTOHSCROLL|ES_AUTOVSCROLL ,3,0,600,600,hwnd,NULL,his,NULL);
}
break;
case WM_COMMAND:
{
/////menu_function(hwnd,wParam);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT info;
HBRUSH hbrush;
HDC hdc = BeginPaint(hw,&ps);
wcout<<"vishal";
TextOut(hdc,20,20,L"vishak",6);
EndPaint(hw,&ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
break;
}
}
///////all men
Since you are using an edit control as the client area of your window don't handle WM_PAINT yourself; instead add the text to the edit control (using SendMessage with EM_SETTEXT or possibly EM_REPLACESEL) and let it do the drawing.
Also, you should handle WM_SIZE and set the edit control to the actual client area size.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Code:
#include "CST CAL.h"
int SUSU()//Start Up
{
WSADATA WSAData;
if (WSAStartup(MAKEWORD(2,2), &WSAData) != 0)
{
printf("WSASTATUP Falure: ", WSAGetLastError);
Sleep(1200);
return 1;
}
return 0;
}
int SR()//Send and Recive Data
{
return 0;
}
int DirX_Screen()//Render and Buffrer stuff and put on screen
{
return 0;
}
int SHUT()//ShutDown Program
{
return 0;
}
int GDI()//Get User Data Imput
{
return 0;
}
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//HERE IS CODE
if (SUSU() != 0)
{
return 1;
}int C = 0;
do{while(GetForegroundWindow() == GetForegroundWindow()){
int GeDI = GDI();
int SeR = SR();
int DirX = DirX_Screen();
if( GeDI != 0 || SeR != 0 || DirX != 0){C = 1;}else{printf("FR Suc\n");}
Sleep(0);
}}while (C == 0);
SHUT();
//HERE IS END OF CODE SPACE
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 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)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
This is originally a console application but then i use this template and just placed my code... my loop is supposed to print out the text but its not... it doesn't eve make the window. Please Help!
This is originally a console application but then i use this template
and just placed my code...
I wish things were that easy :)
You can't switch from console programming to the event driven one that easily. Maybe you should go through this tutorial.
my loop is supposed to print out the text but its not...
In winapi we usually use MessageBox to display text, or you could change the text of the static control or even pop a tooltip. This is done usually in response to a certain event, like button press for an example.
it doesn't eve make the window.
This happens because you have an infinite while loop before your call to InitInstance( ... ).
Your condition while(GetForegroundWindow() == GetForegroundWindow()) is always true so you never leave the while loop, thus never reaching the code that creates the window ( InitInstance( ... ) ).
Please Help!
After you go through the tutorial I recommended above, you will be able to recode properly your application.
I do not know enough about what you are trying to do, but I will try to offer you some advice:
Put your initialization code in WM_CREATE handler:
case WM_CREATE:
{
if( SUSU() != 0 )
return (LRESULT)-1;
else
return (LRESULT)0;
}
In your WM_DESTROY you should put your cleanup code ( note that WM_CLOSE sometimes does not get called! ):
case WM_DESTROY:
{
SHUT();
}
Your GDI() should be a part of an event, maybe a button press-I don't know it is entirely up to you. The same goes for SR(). They will be processed in WM_COMMAND handler.
I do not know DirectX so I do not know where to put your DirX_Screen(), but you could leave fancy graphics last and concentrate on functionality first.
Hopefully this helps a little.
I strongly advise you to go through that tutorial and to learn to read MSDN documentation.
Best regards and good luck!
i am trying to write a program for multiple windows. In this program when user left click on one window it should a message which window was clicked. Here is my code:
#include<Windows.h>
// Store handles to the main window and application instance globally.
HWND ghFirstWnd =0;
HWND ghSecondWnd=0;
HWND ghThirdWnd=0;
HINSTANCE ghAppInst=0;
//========================================================================================
// WINDOW 1
// Step 1: Define and implement the window procedure.
LRESULT CALLBACK
WndProc1(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
// Handle left mouse button click message.
case WM_LBUTTONDOWN:
MessageBox(0,L"first window ",L"MSG",MB_OK);
return 0;
// Handle key down message.
case WM_KEYDOWN:
if(wParam==VK_ESCAPE)
if(MessageBox(hWnd,L"sure ??",L"confirmation",MB_YESNO)==IDYES)
DestroyWindow(ghFirstWnd);
return 0;
// Handle destroy window message.
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
//========================================================================================
// WINDOW 2
//========================================================================================
LRESULT CALLBACK
WndProc2(HWND hWnd,UINT msg, WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
MessageBox(0,L"second window",L"msg",MB_OK);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
//========================================================================================
// WINDOW 3
//========================================================================================
LRESULT CALLBACK
WndProc3(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
MessageBox(0,L"third window",L"msg",MB_OK);
return 0;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
// WinMain: Entry point for windows application.
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine,int showCmd)
{
// Save handle to application instance.
ghAppInst=hInstance;
// Step 2: Fill out a WNDCLASS instance.
WNDCLASS wc1;
wc1.style =CS_HREDRAW|CS_VREDRAW;
wc1.lpfnWndProc =WndProc1;
wc1.cbClsExtra=0;
wc1.cbWndExtra=0;
wc1.hInstance=ghAppInst;
wc1.hIcon=::LoadIcon(0,IDI_APPLICATION);
wc1.hCursor=::LoadCursor(0,IDC_ARROW);
wc1.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wc1.lpszMenuName=0;
wc1.lpszClassName=L"first class";
// Window 2
WNDCLASS wc2;
wc2.style=CS_HREDRAW|CS_VREDRAW;
wc2.lpfnWndProc=WndProc2;
wc2.cbClsExtra=0;
wc2.cbWndExtra=0;
wc2.hInstance=ghAppInst;
wc2.hIcon=::LoadIcon(0,IDI_APPLICATION);
wc2.hCursor=::LoadCursor(0,IDC_ARROW);
wc2.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wc2.lpszMenuName=0;
wc2.lpszClassName=L"second class";
// Window 3
WNDCLASS wc3;
wc3.style=CS_HREDRAW|CS_VREDRAW;
wc3.lpfnWndProc=WndProc3;
wc3.cbClsExtra=0;
wc3.cbWndExtra=0;
wc3.hInstance=ghAppInst;
wc3.hIcon=::LoadIcon(0,IDI_APPLICATION);
wc3.hCursor=::LoadCursor(0,IDC_ARROW);
wc3.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);
wc3.lpszMenuName=0;
wc3.lpszClassName=L"third class";
// Step 3: Register with WNDCLASS instance with windows.
RegisterClass(&wc1);
RegisterClass(&wc2);
RegisterClass(&wc3);
// Step 4: Create the window, and save the handle in global window handle variable ghMainWnd.
ghFirstWnd=::CreateWindow(L"MyWndClassName",L"MyWindow1",WS_OVERLAPPEDWINDOW,0,0,50,50,0,0,ghAppInst,0);
ghSecondWnd=::CreateWindow(L"MyWndClassName",L"MyWindow2",WS_OVERLAPPEDWINDOW,50,0,50,50,0,0,ghAppInst,0);
ghThirdWnd=::CreateWindow(L"MyWndClassName",L"MyWindow3",WS_OVERLAPPEDWINDOW,100,0,50,50,0,0,ghAppInst,0);
if(ghFirstWnd==0)
{
::MessageBox(0,L"create window1-failed",0,0);
return false;
}
if(ghSecondWnd==0)
{
::MessageBox(0,L"create window2 failed",0,0);
return false;
}
if(ghThirdWnd==0)
{
::MessageBox(0,L"create window3 failed",0,0);
return false;
}
// Step 5: Show and update the window.
ShowWindow(ghFirstWnd,showCmd);
UpdateWindow(ghFirstWnd);
ShowWindow(ghSecondWnd,showCmd);
UpdateWindow(ghSecondWnd);
ShowWindow(ghThirdWnd,showCmd);
UpdateWindow(ghThirdWnd);
// Step 6: Enter the message loop and don't quit until a WM_QUIT message is received.
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Return exit code back to operating system.
return(int)msg.wParam;
}
problem is when i try to execute the code it just say create window1- failed!!
Cutting the program down, you can end up with something similar to this (I've made a couple changes to the code itself, but it should still work in C++03):
#include <Windows.h>
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE, PSTR,int showCmd)
{
WNDCLASS wc1 = {};
wc1.style = CS_HREDRAW|CS_VREDRAW;
wc1.lpfnWndProc = DefWindowProc;
wc1.hInstance = hInstance;
wc1.hIcon = ::LoadIcon(0,IDI_APPLICATION);
wc1.hCursor = ::LoadCursor(0,IDC_ARROW);
wc1.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc1.lpszClassName = L"first class";
RegisterClass(&wc1);
HWND ghFirstWnd = ::CreateWindow(L"MyWndClassName",L"MyWindow1",WS_OVERLAPPEDWINDOW,0,0,50,50,0,0,hInstance,0);
if(!ghFirstWnd)
{
::MessageBox(0,L"create window1-failed",0,0);
return 1;
}
return 0;
}
With this little code, it's much easier to spot your mistake. Your window class is named "first class", but in your CreateWindow call, you use a class named "MyWndClassName". The class cannot be found, so no window can be created using it.
On a side note, you have little error checking. One thing that would really beef it up is using GetLastError appropriately.
CreateWindow function returns handle to the windows or when fails then returns NULL.
So replace 'if(ghFirstWnd==0)" with "if(ghFirstWnd==NULL)" and check what happens. I'm not sure because I don't use Win32.
If only "create window1-failed" message is displaying maybe try to replace "if(ghSecondWnd==0)" with "if(ghSecondWnd==1)" and then two message boxes should appear.