|
很有趣的现象:重绘static采取重设控件属性SS_OWNERDRAW实现的时候,特么的发现属性解释出现了差异,具体如下:
- SS_ENHMETAFILE | SS_LEFT == SS_OWNERDRAW | SS_RIGHT
复制代码
这就搞笑了,系统在解释属性的时候与预期不符:
spy去看了一下,系统是按照 SS_ENHMETAFILE | SS_LEFT 进行解释的,但我要的是右对齐呀 SS_RIGHT ???
spy 截图和代码如下:
spy截图
- /*
- win32的static控件 SS_RIGHT(右对齐) 似乎有点问题
- */
- #include <windows.h>
- #include <windowsx.h>
- #include <commctrl.h>
- #include <stdio.h>
- #include <string.h>
- #pragma comment(lib,"user32.lib")
- #pragma comment(lib,"gdi32.lib")
- #pragma comment(lib,"kernel32.lib")
- #pragma comment(lib,"comctl32.lib")
- #pragma comment(lib,"shell32.lib")
- #define WM_STATICCUSTOMDRAW (UINT)(WM_USER+0x1001)
- HFONT setfont(HDC hdc,LPCTSTR font_name);
- int Static_draw(HWND hwnd,HDC hdc,RECT rect,UINT itemState);
- LRESULT CALLBACK StaticProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
- LRESULT CALLBACK ContainerProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
- #define ID_STATIC_01 0x0001
- #define ID_STATIC_02 0x0002
- HINSTANCE GInstance;
- void regist_window(char* class_name,WNDPROC proc);
- void InitialWindow(HWND hwnd);
- LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
- HFONT setfont(HDC hdc,LPCTSTR font_name)
- {
- LOGFONT lf;
- HFONT pre_font = (HFONT)GetCurrentObject(hdc,OBJ_FONT);
- HFONT text_font;
-
- if(pre_font) {
- GetObject(pre_font,sizeof(LOGFONT),&lf);
- strcpy(lf.lfFaceName,font_name);
- text_font = CreateFontIndirect(&lf);
- }
- pre_font = (HFONT)SelectObject(hdc,text_font);
-
- return pre_font;
- }
- int Static_draw(HWND hwnd,HDC hdc,RECT rect,UINT itemState)
- {
- char* text;
- HFONT pre_font;
- HBRUSH brush,pre_brush;
- COLORREF color_text = RGB(255,255,255);
- COLORREF color_bk = RGB(0,0,0);
- int length;
-
- length = GetWindowTextLength(hwnd);
- text = (char*)malloc(sizeof(char)*(length+1));
- memset(text,0x00,length+1);
- GetWindowText(hwnd,text,length+1);
-
- pre_font = setfont(hdc,"微软雅黑");
- brush = CreateSolidBrush(color_bk);
- pre_brush = (HBRUSH)SelectObject(hdc,brush);
-
- Rectangle(hdc,rect.left,rect.top,rect.right,rect.bottom);
- SetTextColor(hdc,color_text);
- SetBkMode(hdc,TRANSPARENT);
-
- //SS_RIGHT
- UINT window_style=GetWindowLong(hwnd,GWL_STYLE);
- if((UINT)(GetWindowLong(hwnd,GWL_STYLE)&(UINT)SS_RIGHT)==(UINT)SS_RIGHT)
- DrawText(hdc,text,-1,&rect,DT_RIGHT);//DT_WORDBREAK|DT_EDITCONTROL|
- else DrawText(hdc,text,-1,&rect,DT_WORDBREAK|DT_EDITCONTROL);
-
- DeleteObject(SelectObject(hdc,pre_font));
- DeleteObject(SelectObject(hdc,pre_brush));
-
- free(text);
- return 0;
- }
- LRESULT CALLBACK StaticProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- WNDPROC Ori_STCProc = (WNDPROC)GetWindowLong(hwnd,GWL_USERDATA);
-
- switch(message){
- case WM_STATICCUSTOMDRAW:
- {
- LPDRAWITEMSTRUCT draw_struct = (LPDRAWITEMSTRUCT)lParam;
- HDC hdc = draw_struct->hDC;
- RECT rect;
-
- CopyRect(&rect,&(draw_struct->rcItem));
- Static_draw(hwnd,hdc,rect,draw_struct->itemState);
- return TRUE;
- }
- break;
- case WM_ERASEBKGND:
- {
- //avoid control flicker
- return 1;
- }
- break;
- }
-
- return CallWindowProc(Ori_STCProc,hwnd,message,wParam,lParam);
- }
- LRESULT CALLBACK ContainerProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- WNDPROC Ori_ParentProc = (WNDPROC)GetWindowLong(hwnd,GWL_USERDATA);
-
- switch(message){
- case WM_DRAWITEM:
- {
- LPDRAWITEMSTRUCT draw_struct = (LPDRAWITEMSTRUCT)lParam;
-
- if(draw_struct->CtlType == ODT_STATIC) {
- return SendMessage(draw_struct->hwndItem,WM_STATICCUSTOMDRAW,wParam,lParam);
- }
- }
- break;
- }
- return CallWindowProc(Ori_ParentProc,hwnd,message,wParam,lParam);
- }
- void regist_window(char* class_name,WNDPROC proc)
- {
- WNDCLASSEX wndclass;
- wndclass.cbSize=sizeof(wndclass);
- wndclass.style = NULL;//CS_HREDRAW|CS_VREDRAW;
- wndclass.lpfnWndProc = proc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = GInstance;
- wndclass.hIcon = NULL;
- wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = class_name;
- wndclass.hIconSm = NULL;
- RegisterClassEx(&wndclass);
- }
- int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
- {
- GInstance = hInstance;
- char szClassName[]="BACK_PANEL";
-
- regist_window(szClassName,MainWndProc);
-
- HWND hwnd = CreateWindowEx(0,
- szClassName,
- szClassName,
- WS_THICKFRAME|WS_CAPTION|WS_OVERLAPPEDWINDOW|WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_CLIPCHILDREN,
- 200,
- 50,
- 800,
- 600,
- NULL,
- NULL,
- hInstance,
- NULL);
- if(hwnd == NULL)
- {
- MessageBox(NULL,"create windows error !","error",MB_OK);
- return -1;
- }
-
- ShowWindow(hwnd,nCmdShow);
- UpdateWindow(hwnd);
- MSG msg;
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int) msg.wParam;
- }
- LRESULT CALLBACK MainWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- switch(message)
- {
- case WM_CREATE:
- {
- InitialWindow(hwnd);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- break;
- }
- return DefWindowProc(hwnd,message,wParam,lParam);
- }
- void InitialWindow(HWND hwnd)
- {
- HWND static_hwnd = CreateWindow(TEXT("Static"),
- "Text.",
- WS_BORDER|WS_CHILD|WS_VISIBLE|SS_OWNERDRAW|SS_RIGHT,
- 50, 50, 250,315,
- hwnd,
- (HMENU)ID_STATIC_01,
- GInstance,
- NULL);
-
- SetWindowLong(static_hwnd,GWL_USERDATA,(LONG)GetWindowLong(static_hwnd,GWL_WNDPROC));
- SetWindowLong(static_hwnd,GWL_WNDPROC,(LONG)StaticProc);
- SetWindowLong(hwnd,GWL_USERDATA,(LONG)GetWindowLong(hwnd,GWL_WNDPROC));
- SetWindowLong(hwnd,GWL_WNDPROC,(LONG)ContainerProc);
-
- {
- //SetWindowLong(static_hwnd,GWL_EXSTYLE,WS_EX_RIGHT|GetWindowLong(static_hwnd,GWL_EXSTYLE));//似乎与 GWL_EXSTYLE 没有关系
-
- char attribution_buffer[256]="";
- UINT static_attri=(UINT)GetWindowLong(static_hwnd,GWL_STYLE);
-
- sprintf(attribution_buffer,"WS_BORDER:0x%08X\n"
- "WS_CHILDWINDOW:0x%08X\n"
- "WS_VISIBLE:0x%08X\n"
- "SS_OWNERDRAW:0x%08X\n"
- "SS_RIGHT:0x%08X\n"
- "Current style:0x%08X\n"
- "SIZEOF(UINT):%d\n"
- "SS_ENHMETAFILE:0x%08X\n"
- "SS_LEFT:0x%08X\n",
- WS_BORDER,WS_CHILDWINDOW,WS_VISIBLE,SS_OWNERDRAW,SS_RIGHT,
- static_attri,
- sizeof(UINT),
- SS_ENHMETAFILE,SS_LEFT);
- HWND static_desc= CreateWindow(TEXT("Static"),
- attribution_buffer,
- WS_BORDER|WS_CHILD|WS_VISIBLE,
- 310, 50, 250,315,
- hwnd,
- (HMENU)ID_STATIC_02,
- GInstance,
- NULL);
- }
- }
复制代码 |
上一篇: VS2017单文档程序窗口分割没有CFormView基类的解决方法下一篇: 谁能够分享个修改内网IP地址的软件?
|