How to detect UNICODE characters when trapping WM_CHAR messages? - c

So, my window procedure is currently trapping all WM_CHAR messages, it works fine with ASCII characters but when I try something like:
case 'ф':
MessageBox(NULL, TEXT("Ф detected"), TEXT("key"), MB_OK);
it fails for some reason.
What's wrong with my approach and how do I fix this?
EDIT: source-code:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
switch(wParam)
{
case '3':
MessageBox(NULL, TEXT("'3' was pressed"), TEXT("key detected"), MB_OK);
return 0;
case 'ф':
MessageBox(NULL, TEXT( "ф was pressed"), TEXT("key detected"), MB_OK);
return 0;
default:
return 0;
}
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

Non-Unicode characters need to be replace as Unicode characters in switch-case:
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
switch(wParam)
{
case L'3':
MessageBox(NULL, TEXT("'3' was pressed"), TEXT("key detected"), MB_OK);
return 0;
case L'ф':
MessageBox(NULL, TEXT( "ф was pressed"), TEXT("key detected"), MB_OK);
return 0;
default:
return 0;
}
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
The values of 'ф' and L'ф' are different. The values of 'ф' and other characters not in the ASCII table are often resolved to '?' (63)

Related

How to respond a mouse message about right-button in listbox item?

If i use the win32 api,how to respond WM_RBUTTONUP?
case WM_RBUTTONUP:
{
break;
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId) {
case IDE_FRIEND:
{
switch (wmEvent) {
case WM_RBUTTONUP:
{
POINT pt;
GetCursorPos(&pt);
TrackPopupMenu(g_hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, nullptr);
break;
}
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
break;
}
When I click the right mouse button,it will not enter 'case WM_RBUTTONUP'
'IDE_FRIEND' is associated with listbox.
Right-click on listbox will generate context message for parent window. You should be able to catch it like this:
case WM_CONTEXTMENU:
{
if (hListBox == (HWND)wParam)
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
TrackPopupMenu(g_hMenu, TPM_RIGHTBUTTON, x, y, 0, hWnd, nullptr);
}
break;
}
case WM_RBUTTONUP:
{
//main window's right-click message
break;
}
case WM_COMMAND:
{
//...
}
Edit:
To do the same thing with subclass
#include "CommCtrl.h" //may need to add this
#pragma comment(lib, "comctl32.lib")
LRESULT CALLBACK ListBoxProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
UINT_PTR, DWORD_PTR)
{
switch (msg){
case WM_RBUTTONUP:
{
//add listbox menu here
break;
}
case WM_NCDESTROY://safely remove subclass
{
RemoveWindowSubclass(hwnd, ListBoxProc, 0);
return DefSubclassProc(hwnd, msg, wp, lp);
}
}
return DefSubclassProc(hwnd, msg, wp, lp);
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
//...
case WM_CREATE:
ListBox = CreateWindow(L"listbox", ... )
SetWindowSubclass(ListBox, ListBoxProc, 0, 0);
//...
}

C win32 app there can be compile and link but show nothing to me but process.why?

there can be compile and link but show nothing to me but process.why?
i have download another Similar mine but this show me the window. i am confused.
#include<windows.h>
#include<stdio.h>
LRESULT CALLBACK WinSunProc(//可以通过MSDN查看回调函数的声明方式
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
){
WNDCLASS wndcls;//设计一个窗口类
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);//黑色的刷子
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);//窗口上面是应用程序的图标
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WinSunProc;/窗口过程函数。回调函数
wndcls.lpszClassName = "First MFC";//类名
wndcls.lpszMenuName = NULL; //没有菜单
wndcls.style = CS_HREDRAW | CS_VREDRAW;//CS:类的样式---水平重绘、垂直重绘
RegisterClass(&wndcls);//注册窗口
HWND hwnd;//创建窗口
hwnd = CreateWindow("MFC", "JLU", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);//显示窗口
UpdateWindow(hwnd);
MSG msg;//消息循环机制
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hDC; //Device Context的句柄
switch (uMsg) //针对不同消息处理过程
{
case WM_PAINT: //窗口重绘时,触发的消息
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps); //BeginPaint/EndPaint只能用在WM_PAINT消息中
TextOut(hDC, 50, 50, "This is first MFC Program!", strlen("This is first MFC Program!"));
EndPaint(hwnd, &ps);
break;
case WM_CHAR: //按下键盘字母键响应消息
char cArry[20];
memset(cArry, 0, 20);
sprintf(cArry, "char is %d", wParam);
MessageBox(hwnd, cArry, "WM_CHAR", MB_OKCANCEL);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "LeftMouse Click", "WM_LBUTTONDOWN", MB_OKCANCEL);
hDC = GetDC(hwnd);
TextOut(hDC, 50, 100, "MFC Program!", strlen("MFC Program!"));
ReleaseDC(hwnd, hDC);
break;
case WM_CLOSE:
if (IDOK == MessageBox(hwnd, "是否真的要退出?", "提示", MB_OKCANCEL))
{
DestroyWindow(hwnd); //销毁窗口,同时发送WM_DESTROY消息
}
break;
case WM_DESTROY:
PostQuitMessage(0); //发送WM_QUIT消息
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam); //对待不关心的消息,采用默认方式处理
}
return 0;
}
i post the WINMAIN and CALLBACK function i cant found any error. please hellp me fix this.
First of all it is not an MFC application. It is just a very simple Win32 application written in C.
As far as I can tell your WindowProc function is WinQinProc(), but for some reason you do register your window class with WinSunProc. That's your first problem. The other problem is the name of your Window class (WNDCLASS). Please note that you should also use the same Window Class name registered in CreateWindow() call.
Here is the fixed version of your application (include "tchar.h" to make app UNICODE aware):
LRESULT CALLBACK WinSunProc(//可以通过MSDN查看回调函数的声明方式
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
WNDCLASS wndcls;//设计一个窗口类
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);//黑色的刷子
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);//窗口上面是应用程序的图标
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WinSunProc;
wndcls.lpszClassName = _T("MyClassName");//类名
wndcls.lpszMenuName = NULL; //没有菜单
wndcls.style = CS_HREDRAW | CS_VREDRAW;//CS:类的样式---水平重绘、垂直重绘
RegisterClass(&wndcls);//注册窗口
HWND hwnd;//创建窗口
hwnd = CreateWindow(_T("MyClassName"), _T("JLU"), WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);//显示窗口
UpdateWindow(hwnd);
MSG msg;//消息循环机制
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinSunProc(HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
HDC hDC; //Device Context的句柄
switch (uMsg) //针对不同消息处理过程
{
case WM_PAINT: //窗口重绘时,触发的消息
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps); //BeginPaint/EndPaint只能用在WM_PAINT消息中
TextOut(hDC, 50, 50, _T("This is first Win32 Program!"), _tcslen(_T("This is first Win32 Program!")));
EndPaint(hwnd, &ps);
break;
case WM_CHAR: //按下键盘字母键响应消息
char cArry[20];
memset(cArry, 0, 20);
// sprintf(cArry, "char is %d", wParam);
// MessageBox(hwnd, cArry, "WM_CHAR", MB_OKCANCEL);
break;
case WM_LBUTTONDOWN:
// MessageBox(hwnd, "LeftMouse Click", "WM_LBUTTONDOWN", MB_OKCANCEL);
hDC = GetDC(hwnd);
TextOut(hDC, 50, 100, _T("Win32 Program!"), _tcslen(_T("Win32 Program!")));
ReleaseDC(hwnd, hDC);
break;
case WM_CLOSE:
if (IDOK == MessageBox(hwnd, _T("是否真的要退出?"), _T("提示"), MB_OKCANCEL))
{
DestroyWindow(hwnd); //销毁窗口,同时发送WM_DESTROY消息
}
break;
case WM_DESTROY:
PostQuitMessage(0); //发送WM_QUIT消息
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam); //对待不关心的消息,采用默认方式处理
}
return 0;
}

My Win32 application won't exit the main loop

This is my main loop:
while(TRUE)
{
PeekMessage(&msg,hWnd,0,0,PM_REMOVE);
if (msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
and this is my callback procedure:
LRESULT CALLBACK WinProc(HWND hWnd,UINT msg1,WPARAM wParam,LPARAM lParam)
{
switch(msg1)
{
case WM_DESTROY :
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd,msg1,wParam,lParam);
}
I found out that when I press Close button WM_NCLBUTTONDOWN will be returned by the PeekMessage function in the next loop, and no WM_QUIT!
The correct way to do a message loop is
BOOL bRet;
MSG msg;
while ((bRet = GetMessage(&msg, hWnd, 0, 0)) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
You can use PeekMessage if you really need to... but why are you ignoring the return value?
Also, note that this is specific to a window. I believe PostQuitMessage is for a thread... I don't remember it off the top of my head, but you might need to pass NULL instead of hWnd.
If you have any other windows, that may hijack their message loop as well -- I don't think it's usually an issue, but it might potentially be one; keep that in mind.
Here's some code I found. It should give you something to work with.
// Main message loop:
do
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
// Run game code here.
gTradeApp->ExecuteNextAction();
}
while (msg.message != WM_QUIT);
and the WndProc
LRESULT CALLBACK WndProc(HWND aHWnd, UINT aMessage, WPARAM aWParam, LPARAM aLParam)
{
switch (aMessage)
{
case WM_COMMAND:
return HandleCommand(aHWnd, aMessage, aWParam, aLParam);
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(aHWnd, aMessage, aWParam, aLParam);
}
return 0;
}
I recommend sticking with this, to ensure errors (-1) returned by GetMessage can be handled properly:
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
Also, another error is not handling WM_CLOSE properly. Try this instead to make your program actually listen to WM_CLOSE (the close button):
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CLOSE: {
DestroyWindow(hwnd); // this
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}

Returning HTTRANSPARENT for WM_NCHITTEST in a plugin

How can I get a plugin to make the program have my clicks go though the window? I heard I would have to return HTTRANSPARENT for WM_NCHITTEST. Is it possible to do that with hooking? This is as far as I have got in my hook:
LRESULT CALLBACK myHook(int nCode, WPARAM wParam, LPARAM lParam)
{
CWPSTRUCT* pwp = (CWPSTRUCT*)lParam;
if (nCode >= 0)
{
if (pwp->message==WM_NCHITTEST)
MessageBox(NULL, TEXT("NCHITTEST has been receieved."), TEXT("NCHITTEST"), MB_ICONINFORMATION);
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}

how to handle click event in win32 API?

I have created a simple win 32 application..in which it has a textbox and a button in a dialog window..first when I created this..it didnt display the dialog window and then what I did is added the code below to handle the close(WM_CLOSE) of the dialog window...but I want to know, how to handle the button click event..
void ValidatePassword(CString encryptedPassword)
{
//create password dialog window
CreateEvent(NULL,true,false,L"TestEvent");
MSG msg;
HWND hwnd = CreateWindowEx(0,WC_DIALOG,L"Security Alert",WS_OVERLAPPEDWINDOW|WS_VISIBLE,
600,300,300,200,NULL,NULL,NULL,NULL);
//create label
CreateWindowEx(NULL,L"Static",L"Requires Password to Run the File:", WS_CHILD|WS_VISIBLE,
10,25,300,20,hwnd,(HMENU)label_id,NULL,NULL);
//create textboxcontrol within the dialog
CreateWindowEx(WS_EX_CLIENTEDGE,L"EDIT",L"",WS_CHILD|WS_VISIBLE | ES_PASSWORD,
10,50,125,25,hwnd,(HMENU)textbox_id,NULL,NULL);
//create button
HWND button = CreateWindowEx(WS_EX_CLIENTEDGE,L"Button",L"OK",WS_CHILD|WS_VISIBLE,
10,100,100,25,hwnd,(HMENU)button_id,NULL,NULL);
ShowWindow (hwnd, SW_SHOW);
UpdateWindow(hwnd);
//SetWindowLong(button,DWL_DLGPROC, (long)myProc);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hwndButton;
switch (message)
{
/* Handles all Windows Messages */
case WM_COMMAND:
{
if(((HWND)lParam) && (HIWORD(wParam) == BN_CLICKED))
{
int iMID;
iMID = LOWORD(wParam);
switch(iMID)
{
case button_id:
{
MessageBox(hwnd, (LPCTSTR)"You just pushed me!", (LPCTSTR) "My Program!", MB_OK|MB_ICONEXCLAMATION);
break;
}
default:
break;
}
}
break;
}
case WM_DESTROY:
{
PostQuitMessage (0); /* send a WM_QUIT to Message Queue, to shut off program */
break;
}
}
return 0;
}
Yikes.
It should not be necessary to call SetWindowLong to set the dialog proc for a dialog. Your "simple" program should look something like
#include <windows.h>
#include "resource.h"
BOOL CALLBACK myProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if( LOWORD(wParam) == IDCLOSE) // close button click
EndDialog(hwnd,0);
return TRUE;
}
return FALSE;
}
int CALLBACK WinMain(HINSTANCE hExe,HINSTANCE,LPCSTR,INT)
{
return DialogBox(hExe,MAKEINTRESOURCE(IDD_DIALOG),NULL,myProc);
}
Check for WM_COMMAND. LOWORD(wParam) will be your control ID and lParam will be your hWnd for the button.

Resources