How to make WM_PAINT run from outside of wndproc - c++

I apologize if this is a dumb question, im just starting to learn winapi. here is the code in concern:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WCHAR greeting[] = _T("line1");
WCHAR greeting1[] = _T("another line");
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:
{
repaint = false;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
switch (pageID) {
case 1: {//display first string
TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
break;
}
case 2: {//display the other string
TextOut(hdc, 5, 100, greeting1, _tcslen(greeting1));
break;
}
}
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;
pageID = 1;
repaint = true;
}
if (LOWORD(wParam) == IDC_BUTTON1) {
pageID = 2;
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
I apologize for the seemingly random variables that don't do anything, I was just trying to make this work any way possible, but no dice.
What I am trying to do, is to have one of the textouts run after a button is pressed in a dialog box. Unfortunately, the text isn't displayed until wndproc runs next time. UpdateWindow is useless, because I need to pass hWnd to it, but I can't do that when Im not in wndproc. Need a quick tip on how this should be done. Im sure everyone does it every day, just not obvious for me... Thanks!

Basically use InvalidateRect, but this comment
UpdateWindow is useless, because I need to pass hWnd to it, but I
can't do that when Im not in wndproc.
exhibits kind of a fundamental misunderstanding of how Win32 programming works. You always need a window handle to a window you are working with. You get window handles when you create windows; you need to store those somewhere.
Anyway below is a minimal version of using buttons on one window to drive repainting of another window.
#include <windows.h>
HINSTANCE g_hInstance = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK SomeOtherWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
const int kBtn1ID = 101;
const int kBtn2ID = 102;
int pageID = 1;
HWND g_other_hwnd = NULL;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg = { 0 };
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"foobar";
if (!RegisterClass(&wc))
return 1;
wc.lpszClassName = L"some_other_window";
wc.lpfnWndProc = SomeOtherWndProc;
if (!RegisterClass(&wc))
return 1;
g_hInstance = hInstance;
if (!CreateWindow(L"foobar",
L"foobar",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, 0, 0, hInstance, NULL))
return 2;
if (!(g_other_hwnd = CreateWindow(L"some_other_window",
L"quux",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 0, 640, 480, 0, 0, hInstance, NULL)))
return 2;
while (GetMessage(&msg, NULL, 0, 0) > 0)
DispatchMessage(&msg);
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
CreateWindow(L"button", L"some button", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 10, 10, 150, 35, hWnd, (HMENU)kBtn1ID, g_hInstance, 0);
CreateWindow(L"button", L"some other button", BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, 10, 55, 150, 35, hWnd, (HMENU)kBtn2ID, g_hInstance, 0);
break;
case WM_COMMAND: {
int btn_id = LOWORD(wParam);
switch (btn_id) {
case kBtn1ID:
pageID = 1;
InvalidateRect(g_other_hwnd, NULL, TRUE);
break;
case kBtn2ID:
pageID = 2;
InvalidateRect(g_other_hwnd, NULL, TRUE);
break;
}
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK SomeOtherWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
static auto greeting = L"hello there.";
static auto greeting1 = L"goodbye.";
switch (pageID) {
case 1: {//display first string
TextOut(hdc, 5, 5, greeting, lstrlenW(greeting));
break;
}
case 2: {//display the other string
TextOut(hdc, 5, 100, greeting1, lstrlenW(greeting1));
break;
}
}
EndPaint(hWnd, &ps);
}
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Related

WIN32 Dialogue Box Throws exception [duplicate]

This question already has answers here:
Read access violation using m_pRenderTarget with winapi due to non-NULL pointer
(2 answers)
Closed 2 years ago.
I am trying to learn the win32 api using the Microsoft documentation. I have gotten to chapter 4, and I seem to have run into an issue that I am struggling to debug. The dialogue box triggered by the about button throws an exception:
Exception thrown at 0x773BDCFF (ntdll.dll) in practice.exe: 0xC0000005: Access violation reading location 0x00905A4C.
Here is the declaration of the WndProc and About callbacks:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
Here is the code for the implementation of the WndProc and About callbacks:
LRESULT CALLBACK App::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
App* pApp;
if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
pApp = (App*)pcs->lpCreateParams;
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, PtrToUlong(pApp));
return TRUE;
}
else
{
pApp = reinterpret_cast<App*>(static_cast<LONG_PTR>(::GetWindowLongPtrW(hWnd, GWLP_USERDATA)));
if (!pApp)
return DefWindowProc(hWnd, message, wParam, lParam);
}
int wmld, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(pApp->getInstance(), MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, pApp->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;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK App::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;
I hope the error is within these snippets. If not, I would be happy to provide more code. I have done research, and I truly have very little idea what the problem is. I know that the callback for the about function must be static, which I believe it is. Apart from that, I don't know what would cause it to throw an exception. Thank you for any help you can give me.
Fail to reproduce this issue using your presented code. The following is an example based on your code piece and it works for me. You can refer to.
App.h
class App
{
public:
App(HINSTANCE hInstance, CONST WCHAR* clsName);
private:
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
ATOM MyRegisterClass(HINSTANCE hInstance, CONST WCHAR* clsName);
};
App.cpp
#include "App.h"
App::App(HINSTANCE hInstance, CONST WCHAR* clsName)
{
App::MyRegisterClass(hInstance, clsName);
}
ATOM App::MyRegisterClass(HINSTANCE hInstance, CONST WCHAR* clsName)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = App::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DIALOGBOXEXCEPTION));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_DIALOGBOXEXCEPTION);
wcex.lpszClassName = clsName;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
LRESULT CALLBACK App::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
App* pApp;
if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
pApp = (App*)pcs->lpCreateParams;
::SetWindowLongPtrW(hWnd, GWLP_USERDATA, (LONG_PTR)pApp);
return TRUE;
}
else
{
pApp = reinterpret_cast<App*>(static_cast<LONG_PTR>(::GetWindowLongPtrW(hWnd, GWLP_USERDATA)));
if (!pApp)
return DefWindowProc(hWnd, message, wParam, lParam);
}
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(NULL, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, pApp->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;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK App::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;
}
Create the main window:
App pApp = App(hInst, szWindowClass);
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, &pApp);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

DwmDefWindowProc always returns false

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!

Dialog boxes are not created WinAPI

If I create a child window (In this case window "About") to the main window, the dialog box for some reason is not called. If you do not the child window is a dialog box called normal and works fine. GetLastError returns the error number 1812 (The specified image file did not contain a resource section.). But from a resource file everything is fine. And as I said, if you do not create a child window then everything works fine. What's the problem?
#include <windows.h>
#include "resource.h"
LONG WINAPI WndProc(HWND, UINT, WPARAM,LPARAM);
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK PointsProc(HWND hPoints, UINT message, WPARAM wParam,LPARAM lParam);
HINSTANCE hInst;
HWND hPoints;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInst = hInstance;
HWND hwnd;
MSG msg;
WNDCLASS w;
memset(&w,0,sizeof(WNDCLASS));
w.style = CS_HREDRAW | CS_VREDRAW;
w.lpfnWndProc = WndProc;
w.hInstance = hInstance;
w.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
w.lpszClassName = L"My Class";
RegisterClass(&w);
hwnd = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW,
300, 200, 200, 180, NULL, NULL, hInstance, NULL);
HMENU main_menu = CreateMenu();
AppendMenu(main_menu, MF_STRING, 1111, L"Box");
WNDCLASS w2;
memset(&w2, 0, sizeof(WNDCLASS));
w2.lpfnWndProc = (WNDPROC)PointsProc;
w2.hInstance = hInst;
w2.lpszClassName = L"About";
w2.hCursor = LoadCursor(NULL, IDC_ARROW);
w2.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(111, 111, 111));
RegisterClass(&w2);
hPoints = CreateWindowEx(0, L"About", (LPCTSTR) NULL,
WS_CHILD | WS_BORDER | WS_VISIBLE | WS_DISABLED, 10, 10,
100, 100, hwnd, (HMENU)1112, hInst, NULL);
ShowWindow(hPoints,SW_NORMAL);
UpdateWindow(hPoints);
SetMenu(hwnd, main_menu);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch (Message)
{
case WM_COMMAND:
switch(LOWORD(wparam))
{
case 1111:
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, About);
return 0;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wparam, lparam);
}
return 0;
}
LRESULT CALLBACK PointsProc(HWND hPoints, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
break;
default:
return DefWindowProc(hPoints, message, wParam, lParam);
}
return 0;
}
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) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
The dialog box is called, only it takes a long time because PointsProc() is looping...
The problem was the break; while handling WM_PAINT in PointsProc() -- it then skips calling DefWindowProc(), so the window keeps getting WM_PAINT messages because the window remains invalid.
// case WM_PAINT:
// break;

Creating a Window in WinAPI after pressing a button

I'm making an auto clicker for a game in WinAPI and I have 4 simple buttons on the main window. When the user presses the 'start' button I want another window to open asking them for settings such as number of times to click and time between clicks. When I try to create a new window, nothing is happening, but everything else works perfectly.
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_COMMAND:
{
switch (wParam)
{
case ID_START:
{
HINSTANCE hInstance = GetModuleHandle(CLASS_NAME);
HWND settings = CreateWindowEx(
0,
L"Settings",
L"Settings",
WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CHILD,
100, 100, 600, 200,
NULL,
(HMENU) ID_SETTINGS,
hInstance,
NULL
);
MSG msg = { };
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
case ID_QUIT:
{
PostQuitMessage(0);
return 0;
}
case ID_CALIB:
{
if (MessageBox(hwnd, L"You pressed Calibrate", L"Calibrate", MB_OK))
{
return 0;
}
}
case ID_INFO:
{
if (MessageBox(hwnd, L"You pressed about", L"About", MB_OK))
{
return 0;
}
}
}
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW+1));
EndPaint(hwnd, &ps);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
I just started WinAPI today, so I am extremely new. Thanks for any help in advance!
The second parameter to CreateWindowEx must be a class name that you registered earlier by calling RegisterClass.
You are specifying WS_CHILD. But a child must have a parent. Pass the parent HWND into the hwndParent parameter.

WinAPI nothing happens on button click

My Question is: In the below C++ code, why does clicking on the button do nothing while it is supposed to call MessageBox from WndProc1?
P.S: After compiling, I got some errors like the following:
"C:\Windows\SysWOW64\ntdll.dll", Can't find or open PDB file.
Code:
#include <Windows.h>
LRESULT CALLBACK WndProc(
_In_ HWND hwnd,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
LONG WINAPI WndProc1(
_In_ HWND hwnd_button,
_In_ UINT uMsg,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
//Точка входа в программу
int WINAPI WinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
//Создаем класс окна
WNDCLASS WindowClass;
//Заполняем структуру
WindowClass.style = 0;
WindowClass.lpfnWndProc = (WNDPROC)WndProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = hInstance;
WindowClass.hIcon = LoadIcon(hInstance,
(LPCTSTR)IDI_APPLICATION);
WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WindowClass.lpszMenuName = 0;
WindowClass.lpszClassName = TEXT("Class");
//Зарегистируем класс окна
RegisterClass(&WindowClass);
//Создаем переменную, в которой поместим идентификатор окна
HWND hWnd;
hWnd = CreateWindow(TEXT("Class"), TEXT("ClickTest"),
WS_OVERLAPPEDWINDOW, 0, 0, 500, 300, NULL, NULL, hInstance, NULL);
//Создаем кнопку
HWND hWnd_button;
hWnd_button = CreateWindow(TEXT("button"), TEXT("Click me"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10, 10, 80, 30, hWnd, (HMENU)10000, hInstance, NULL);
//показать окно
ShowWindow(hWnd, nCmdShow);
//обновить содержимое окна
UpdateWindow(hWnd);
//Создадим переменную для храненния сообщений
MSG msg;
//Создадим цикл обработки сообщений
while(GetMessage(&msg, NULL,0 ,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LONG WINAPI WndProc1(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch (Message){
case WM_COMMAND:
if(LOWORD(wparam)==10000)
{
MessageBox(hwnd, TEXT("Button Pressed"), TEXT(""), 0);
}
return 0;}}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
BeginPaint(hWnd, &PS);
EndPaint(hWnd, &PS);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Now working, just added button function as one of the cases to WndProc (WndProc1 deleted)
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wparam, LPARAM lParam)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_CREATE:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
BeginPaint(hWnd, &PS);
EndPaint(hWnd, &PS);
break;
case WM_COMMAND:
if(LOWORD(wparam)==10000)
{
MessageBox(hWnd, TEXT("Button Pressed"), TEXT(""), 0);
}
default:
return DefWindowProc(hWnd, message, wparam, lParam);
}
return 0;
}
One final newbie question: what's the difference between LRESULT CALLBACK and LONG WINAPI then?
Do this modification on WndProc:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT PS;
switch(message)
{
case WM_COMMAND:
if(LOWORD(wParam)==10000)
{
MessageBox(hWnd, TEXT("Button Pressed"), TEXT(""), 0);
}
break;
case WM_CREATE:
break;
// ...
I added WM_COMMAND in the switch/case of WndProc.
How do you expect WndProc1 to be called? It isn't associated to any window class... You have to handle the WM_COMMAND inside WndProc (buttons, as well as other common controls, notify their parent of their events via WM_COMMAND).