登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> 程序员学前班[不再更新,只读] >> 主题: [windows/sdk] sdk 编程的特殊点     [回主站]     [分站链接]
标题
[windows/sdk] sdk 编程的特殊点
clq
浏览(0) + 2007-12-12 15:33:29 发表 编辑

关键字:

[2019-09-05 02:50:36 最后更新]
[windows/sdk] sdk 编程的特殊点

windows 下用 sdk 直接编程和用 mfc 和 delphi 其实是不太一样的,特别是在事件处理等地方,往往和我们想当然的不太一样.

clq
2007-12-12 15:47:55 发表 编辑

listbox 的鼠标点击事件

必须用子类化.即替换 listbox 的窗口函数,注意保存原函数并在其它处理中调用它.例如:

WNDPROC oldProc;
BOOL CALLBACK ListBoxProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
//LRESULT lResult = TRUE;
LRESULT lResult = FALSE;

switch (uMsg)
{

//clq add
case WM_LBUTTONUP:
{
printf("\r\n");
//传递给 BOOL CALLBACK CShowDlg::DlgShowProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
PostMessage(CShowDlg::m_pThis->m_hWnd, uMsg, wParam, lParam);
}
break;

case WM_LBUTTONDOWN:
{
printf("\r\n");
}
break;
//clq add _end;


default:
// lResult = DefWindowProc(hwndDlg, uMsg, wParam, lParam);

break;
}

//一定要保存好替换函数后原来的函数指针,如果不用原来的函数处理那么 listbox 中将不绘制
return CallWindowProc(oldProc, hwndDlg, uMsg, wParam, lParam);
//return lResult;
}


--------------------------------------------------
替换函数的操作放在 case WM_INITDIALOG: 初始化窗口消息之后.

hwndListMarket = GetDlgItem(m_hWnd, IDC_LISTMENU);

//clq add
//要响应 WM_LBUTTONUP 等消息,必须自己写处理函数并替代默认的消息处理函数
oldProc=(WNDPROC)SetWindowLong(hwndListMarket,GWL_WNDPROC,(long)ListBoxProc);



clq
2007-12-12 15:49:08 发表 编辑

有些子窗口事件windows已经封装在 WM_COMMAND 消息中了,例如:

case WM_COMMAND:
{

switch (HIWORD(wParam))
{
case EN_CHANGE:

clq
2007-12-14 20:29:19 发表 编辑

listbox 的 item 高度

delphi 中设置为 lbOwnerDrawVariable 类型后即可改其高度. sdk 中则是比较麻烦的.

--------------------------------------------------
from http://topic.csdn.net/u/20070925/09/0330b1c6-8fb0-49cd-82a1-6492b943af0d.html

用createwindow来创建的listbox怎么重绘? [已结贴]

楼主
hWndList = CreateWindow(L "listbox ",
NULL,
WS_CHILD ¦ WS_VISIBLE ¦ LBS_STANDARD ¦ LBS_OWNERDRAWFIXED,
0,0
100,
100,
hwnd,
(HMENU)ID_LIST,
(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),
NULL);

在父窗口上创建listbox,是在父窗口的WM_COMMAND 还是在 WM_NOTIFY中重绘,请指教!

1楼 得分:8
WM_DRAWITEM

2楼 得分:12
#define XBITMAP 80
#define YBITMAP 20

#define BUFFER MAX_PATH

HBITMAP hbmpPencil, hbmpCrayon, hbmpMarker, hbmpPen, hbmpFork;
HBITMAP hbmpPicture, hbmpOld;

void AddItem(HWND hwnd, LPSTR lpstr, HBITMAP hbmp)
{
int nItem;

nItem = SendMessage(hwndList, LB_ADDSTRING, 0, lpstr);
SendMessage(hwndList, LB_SETITEMDATA, nItem, hbmp);
}

DWORD APIENTRY DlgDrawProc(
HWND hDlg, // window handle to dialog box
UINT message, // type of message
UINT wParam, // message-specific information
LONG lParam)
{
int nItem;
TCHAR tchBuffer[BUFFER];
HBITMAP hbmp;
HWND hListBox;
TEXTMETRIC tm;
int y;
HDC hdcMem;
LPMEASUREITEMSTRUCT lpmis;
LPDRAWITEMSTRUCT lpdis;
RECT rcBitmap;

switch (message)
{

case WM_INITDIALOG:

// Load bitmaps.

hbmpPencil = LoadBitmap(hinst, MAKEINTRESOURCE(700));
hbmpCrayon = LoadBitmap(hinst, MAKEINTRESOURCE(701));
hbmpMarker = LoadBitmap(hinst, MAKEINTRESOURCE(702));
hbmpPen = LoadBitmap(hinst, MAKEINTRESOURCE(703));
hbmpFork = LoadBitmap(hinst, MAKEINTRESOURCE(704));

// Retrieve list box handle.

hListBox = GetDlgItem(hDlg, IDL_STUFF);

// Initialize the list box text and associate a bitmap
// with each list box item.

AddItem(hListBox, "pencil ", hbmpPencil);
AddItem(hListBox, "crayon ", hbmpCrayon);
AddItem(hListBox, "marker ", hbmpMarker);
AddItem(hListBox, "pen ", hbmpPen);
AddItem(hListBox, "fork ", hbmpFork);

SetFocus(hListBox);
SendMessage(hListBox, LB_SETCURSEL, 0, 0);
return TRUE;

case WM_MEASUREITEM:

lpmis = (LPMEASUREITEMSTRUCT) lParam;

// Set the height of the list box items.

lpmis- >itemHeight = 20;
return TRUE;

case WM_DRAWITEM:

lpdis = (LPDRAWITEMSTRUCT) lParam;

// If there are no list box items, skip this message.

if (lpdis- >itemID == -1)
{
break;
}

// Draw the bitmap and text for the list box item. Draw a
// rectangle around the bitmap if it is selected.

switch (lpdis- >itemAction)
{
case ODA_SELECT:
case ODA_DRAWENTIRE:

// Display the bitmap associated with the item.

hbmpPicture =(HBITMAP)SendMessage(lpdis- >hwndItem,
LB_GETITEMDATA, lpdis- >itemID, (LPARAM) 0);

hdcMem = CreateCompatibleDC(lpdis- >hDC);
hbmpOld = SelectObject(hdcMem, hbmpPicture);

BitBlt(lpdis- >hDC,
lpdis- >rcItem.left, lpdis- >rcItem.top,
lpdis- >rcItem.right - lpdis- >rcItem.left,
lpdis- >rcItem.bottom - lpdis- >rcItem.top,
hdcMem, 0, 0, SRCCOPY);

// Display the text associated with the item.

SendMessage(lpdis- >hwndItem, LB_GETTEXT,
lpdis- >itemID, (LPARAM) tchBuffer);

GetTextMetrics(lpdis- >hDC, &tm);

y = (lpdis- >rcItem.bottom + lpdis- >rcItem.top -
tm.tmHeight) / 2;

TextOut(lpdis- >hDC,
XBITMAP + 6,
y,
tchBuffer,
strlen(tchBuffer));

SelectObject(hdcMem, hbmpOld);
DeleteDC(hdcMem);

// Is the item selected?

if (lpdis- >itemState & ODS_SELECTED)
{
// Set RECT coordinates to surround only the
// bitmap.

rcBitmap.left = lpdis- >rcItem.left;
rcBitmap.top = lpdis- >rcItem.top;
rcBitmap.right = lpdis- >rcItem.left + XBITMAP;
rcBitmap.bottom = lpdis- >rcItem.top + YBITMAP;

// Draw a rectangle around bitmap to indicate
// the selection.

DrawFocusRect(lpdis- >hDC, &rcBitmap);
}
break;

case ODA_FOCUS:

// Do not process focus changes. The focus caret
// (outline rectangle) indicates the selection.
// The IDOK button indicates the final
// selection.

break;
}
return TRUE;

case WM_COMMAND:

switch (LOWORD(wParam))
{
case IDOK:
// Get the selected item 's text.

nItem = SendMessage(GetDlgItem(hDlg, IDL_STUFF),
LB_GETCURSEL, 0, (LPARAM) 0);
hbmp = SendMessage(GetDlgItem(hDlg, IDL_STUFF),
LB_GETITEMDATA, nItem, 0);

// If the item is not the correct answer, tell the
// user to try again.
//
// If the item is the correct answer, congratulate
// the user and destroy the dialog box.

if (hbmp != hbmpFork)
{
MessageBox(hDlg, "Try again! ", "Oops ", MB_OK);
return FALSE;
}
else
{
MessageBox(hDlg, "You 're right! ",
"Congratulations. ", MB_OK);

// Fall through.

}

case IDCANCEL:

// Destroy the dialog box.

EndDialog(hDlg, TRUE);
return TRUE;

default:

return FALSE;
}

case WM_DESTROY:

// Free any resources used by the bitmaps.

DeleteObject(hbmpPencil);
DeleteObject(hbmpCrayon);
DeleteObject(hbmpMarker);
DeleteObject(hbmpPen);
DeleteObject(hbmpFork);

return TRUE;

default:
return FALSE;

}
return FALSE;
}

3楼 得分:0
WELL
case WM_MEASUREITEM:
设置ITEM的高度
case WM_DRAWITEM:
实现自绘!

4楼 得分:0
原来就是WM_DRAWITEM


clq
2007-12-18 16:10:14 发表 编辑

listview 的单击事件.

这个是在 WM_NOTIFY 消息中的 NM_CLICK 响应,感觉 WM_NOTIFY 和 差不多,承载了很多消息,这样的处理实在有点奇怪,不知道消息 WM_COMMAND 分类的原则是什么.


case WM_NOTIFY:
{
NMHDR * pnmh = (NMHDR *) lParam;
if (pnmh->code == NM_CLICK)
//if (wParam == NM_CLICK)
{
::PostMessage(hwndDlg, WM_COMMAND, ID_NEW_OK, 0);
}
else
{
return( m_pThis->INotifyHandler(hwndDlg, uMsg, wParam, lParam));
}
}
break;


--------------------------------------------------
另,可以参考 delphi 的
procedure TCustomListView.CNNotify(var Message: TWMNotify);

clq
2007-12-25 17:25:23 发表 编辑

在windowsce中改edit的背景色无论如何都不成功.最后就用了下面的代码,外加将 edit 设置为只读才行,我觉得还是有问题的.不过没时间研究.

case WM_CTLCOLOREDIT:
if( lParam == (LPARAM)::GetDlgItem( hwndDlg,IDC_EDIT_TEXT) )
{
HDC hdc = (HDC )wParam ;
RECT rect;
// GetClientRect(hwndDlg, &rect);

// ::SetBkColor( hdc,BLACK_PEN);
// HBRUSH brush = CreateSolidBrush(BKCOLOR );
// FillRect(hdc,&rect,m_pThis->brush);
// DeleteObject(brush);
//::SetTextColor( hdc,RGB(255,0,0));
SetBkMode( hdc, TRANSPARENT );
SetBkColor( hdc,BKCOLOR);
SetWindowLong (hwndDlg, DWL_MSGRESULT, (long)m_pThis->brush);

//return ( INT_PTR )BLACK_PEN;

//return ( INT_PTR )GetStockObject(NULL_BRUSH);
return TRUE;
}

break;


msdn 是这样说的:
--------------------------------------------------
Microsoft Windows CE 3.0
WM_CTLCOLOREDIT

This message is sent to the parent window of an edit control when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the edit control.
WM_CTLCOLOREDIT hdcEdit = (HDC) wParam;
hwndEdit = (HWND) lParam;

Parameters
hdcEdit
Handle to the device context for the edit control window.
hwndEdit
Handle to the edit control.

Return Values

If an application processes this message, it must return the handle of a brush. The system uses the brush to paint the background of the edit control.
Default Action
The DefWindowProc function selects the default system colors for the edit control.

Remarks

Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message. However, for compatibility purposes, the system sends the WM_CTLCOLOREDIT message for read-only and disabled edit controls if the application was designed for Windows 3.1 or earlier.

The system does not automatically destroy the returned brush. It is the application's responsibility to destroy the brush when it is no longer needed.

The WM_CTLCOLOREDIT message is never sent between threads, it is only sent within the same thread.

If a dialog box procedure handles this message, it should cast the desired return value to a BOOL and return the value directly. If the dialog box procedure returns FALSE, then default message handling is performed. The DWL_MSGRESULT value set by the SetWindowLong function is ignored.
--------------------------------------------------
上面这个还是 ce 的说明. 下面这个是标准 windows api 的.
--------------------------------------------------
WM_CTLCOLOREDIT Notification
Language Filter : All
WM_CTLCOLOREDIT Notification

An edit control that is not read-only or disabled sends the WM_CTLCOLOREDIT message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text and background colors of the edit control.

Syntax
WM_CTLCOLOREDIT

WPARAM wParam
LPARAM lParam;

Parameters
wParam
A handle to the device context for the edit control window.
lParam
A handle to the edit control.

Return Value
If an application processes this message, it must return the handle of a brush. The system uses the brush to paint the background of the edit control.

Remarks

By default, the DefWindowProc function selects the default system colors for the edit control.

Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message.

The WM_CTLCOLOREDIT message is never sent between threads, it is only sent within the same thread.

If a dialog box procedure handles this message, it should cast the desired return value to a BOOL and return the value directly. If the dialog box procedure returns FALSE, then default message handling is performed. The DWL_MSGRESULT value set by the SetWindowLong function is ignored.

Rich Edit: This message is not supported. To set the background color for a rich edit control, use the EM_SETBKGNDCOLOR message.

Notification RequirementsMinimum DLL Version None
Header Declared in Winuser.h, include Windows.h
Minimum operating systems Windows 95, Windows NT 3.1


See Also
Edit Controls Overview, DefWindowProc, EM_SETBKGNDCOLOR, RealizePalette, SelectPalette, WM_CTLCOLORSTATIC

clq
2008-4-17 20:08:45 发表 编辑

刚看了那本很厚的 mfc 书,提到修改 edit 等和背景色时 mfc 简化了它的消息映射,其实在 win32 中已经不用那个消息了的。

总数:6 页次:1/1 首页 尾页  
总数:6 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1