dialog box creation in windows - c++

I am running the following to create a dialog box in windows. when I run it I get the follwoing error:
Error 1 error C2065: 'IDD_DLGFIRST' : undeclared identifier
Here is the code:
HWND hWnd;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
I do know that there are resource files but I haven't understood that very well.
could someone help me out to resolve this error please.

You need to define the symbol both in the resource file, as well as the file that calls MAKEINTRESOURCE. usually it's done via a common header file that you #include in both places (for instance #include resource.h in resource.rc and main.cpp).
And in the resource.h you put #define IDD_DLGFIRST 1 for example. Just make sure that the number is unique across resources.
EDIT:
As an example:
resource.h
#define IDD_DLGFIRST 1001
#define IDC_STATIC 1002
yourapp.rc
#include <windows.h>
#include "resource.h"
IDD_DLGFIRST DIALOGEX 0, 0, 170, 62
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "My dialog"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "My first dialog box, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
DEFPUSHBUTTON "OK",IDOK,113,41,50,14,WS_GROUP
END
yourapp.cpp
#include <windows.h>
#include "resource.h"
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//Open dialog box
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST), HWND_DESKTOP, DlgProc);
return 0;
}
INT_PTR CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
return TRUE;
break; //Don't forget about the break;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
}

Related

What is the best way to write another text line starting with LPCTSTR?

I'm trying to write another second line, but I don't know which code to use.
I've tried using \r\n, \n, \r, and etc,
but none of them worked.
Thanks to everyone who helps! :)
Here is a part of my code.
(I included the header as well.)
HINSTANCE g_hInst;
LPCTSTR lpszClass = L"HelloAPI";
LPCTSTR ChildClassName = L"ChildWin";
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdParam,
int nCmdShow)
hWnd=CreateWindow(lpszClass,
L"Visual C++",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
200, 200,
600, 600,
(HWND)NULL,
(HMENU)NULL,
NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage,
WPARAM wParam, LPARAM lParam)
{
LPCTSTR text = L"Visual C++201934-243369";
switch(iMessage) {
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
TextOut(hdc,100, 100, text, lstrlen(text));
EndPaint(hWnd,&ps);
return 0;
}
TextOut doesn't process linebreak characters in the input string. Use DrawText instead, specifying the DT_WORDBREAK flag.
Meta-comment: now you see why we needed you to post your code.

I wrote some code in c to make simple win32 window but failed [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm getting this errors:
LNK1120: 1 unresolved externals on line 1
error LNK2019: unresolved external symbol _winproc#20 referenced in function _WinMain#16 C:\Users\giorgi\Documents\Visual Studio 2013\Projects\Hello\Hello\Source.obj Hello
I'm new to WinApi please help.
#include <windows.h>
LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR LpCmdLine, int nCmdShow)
{
WNDCLASSEX class;
ZeroMemory(&class, sizeof(WNDCLASSEX));
class.cbSize = sizeof(WNDCLASSEX);
class.style = CS_HREDRAW | CS_VREDRAW;
class.lpfnWndProc = (WNDPROC)winproc;
class.cbClsExtra = 0;
class.cbWndExtra = 0;
class.hInstance = hInstance;
class.hIcon = NULL;
class.hCursor = LoadCursor(NULL, IDC_ARROW);
class.hbrBackground = (HBRUSH)COLOR_WINDOW;
class.lpszClassName = "window class";
class.lpszMenuName = NULL;
class.hIconSm = NULL;
RegisterClassEx(&class);
HWND hwnd = CreateWindowEx
(
WS_EX_ACCEPTFILES,
"window class",
"window",
WS_OVERLAPPED,
200,
200,
800,
600,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nCmdShow);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hwnd, msg, wp, lp);
}
As others have said, C is case-sensitive, so winproc and WinProc would be two different functions. You also need to insure that the signature of your windows procedure matches what Windows expect, so make the following changes:
change LRESULT CALLBACK winproc(WNDPROC lpPrevWndFunc, HWND hwnd, UINT msg, WPARAM wp, LPARAM lp); to LRESULT CALLBACK winProc(HWND, UINT, WPARAM, LPARAM);
change class.lpfnWndProc = (WNDPROC)winproc; to class.lpfnWndProc = (WNDPROC)winProc;
change LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) to LRESULT CALLBACK winProc(HWND hwnd, UINT mgs, WPARAM wp, LPARAM lp)
Finally, its been awhile since I programmed at the win32-API level, but I believe that you windows procedure should look like:
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
}
In other words, you only want to return the default window proceedure (DefWindowProc) if you do not handle the message yourself.

Creating a window in OO

Here is my header
#pragma once
#ifndef BASE_H
#define BASE_H
#include <Windows.h>
#include <windowsx.h>
class Base
{
HWND hWnd;
WNDCLASSEX WndCls;
HRESULT Hr;
public:
Base();
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void RegisterWnd(HINSTANCE hInstance);
void ShowWnd(int nCmdShow);
~Base();
};
#endif
Here is my base.cpp
#include "Base.h"
Base::Base()
{
}
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
}
// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}
void Base::RegisterWnd(HINSTANCE hInstance)
{
ZeroMemory(&WndCls, sizeof(WNDCLASSEX));
WndCls.cbSize = sizeof(WNDCLASSEX);
WndCls.hbrBackground = (HBRUSH)COLOR_WINDOW;
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hIcon = LoadIcon(hInstance, NULL);
WndCls.hIconSm = LoadIcon(hInstance, NULL);
WndCls.hInstance = hInstance;
WndCls.lpfnWndProc = WndProc;
WndCls.lpszClassName = "ClsName";
WndCls.style = CS_HREDRAW | CS_VREDRAW;
Hr = RegisterClassEx(&WndCls);
if (FAILED(Hr))
MessageBox(NULL, "Window Class failed to register.", "ERROR", MB_OK);
hWnd = CreateWindowEx(
NULL,
"WndClassName",
"WndName",
WS_OVERLAPPEDWINDOW,
100, 100,
480, 640,
NULL,
NULL,
hInstance,
NULL);
if (FAILED(hWnd))
MessageBox(NULL, "Window Class failed to create", "ERROR", MB_OK);
}
void Base::ShowWnd(int nCmdShow)
{
Hr = ShowWindow(hWnd, nCmdShow);
if (FAILED(Hr))
MessageBox(NULL, "Failed to display Window", "ERROR", MB_OK);
}
Base::~Base()
{
}
And here is my main.cpp
#include "Base.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
Base CreateWnd;
CreateWnd.RegisterWnd(hInstance);
CreateWnd.ShowWnd(nCmdShow);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&Msg);
// send the message to the WindowProc function
DispatchMessage(&Msg);
}
// return this part of the WM_QUIT message to Windows
return Msg.wParam;
}
The problem is, I keep getting this error message that I dont understand of. Sorry for the bad explanation..Still a student in programming...
UPDATED :
The error above has been corrected by replacing
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
with
LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Thanks to ravi and IInspectable for the quick help.
Now I am having another error D: When i clicked on debug, everything run perfectly but nothing shows up. No window is showing. Visual studio is running perfectly as "Ready". (Sorry i do not want to make another new question because it's still related to creating window in oo
SECOND UPDATE :
My class name in CreateWindowEx is different from the RegisterWnd..My bad. Thanks to IInspectable again for the help.
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
You have to define this with class scope OR how compiler know if its global static OR static member of class. So it should be
LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

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;

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).